https://github.com/nrules/nrules.language
Business rules language for NRules rules engine.
https://github.com/nrules/nrules.language
c-sharp language nrules rules rules-engine
Last synced: 9 months ago
JSON representation
Business rules language for NRules rules engine.
- Host: GitHub
- URL: https://github.com/nrules/nrules.language
- Owner: NRules
- License: mit
- Created: 2017-12-10T03:20:21.000Z (about 8 years ago)
- Default Branch: develop
- Last Pushed: 2025-03-05T00:31:05.000Z (10 months ago)
- Last Synced: 2025-03-31T06:06:49.665Z (9 months ago)
- Topics: c-sharp, language, nrules, rules, rules-engine
- Language: PowerShell
- Homepage: https://nrules.net
- Size: 213 KB
- Stars: 79
- Watchers: 8
- Forks: 19
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Rule# - Language for NRules
Rule# (Rule Sharp) is a business rules language for the [NRules](https://github.com/NRules/NRules) rules engine.
[](https://ci.appveyor.com/project/NRules/NRules-Language) [](https://nuget.org/packages/NRules.RuleSharp) [](http://stackoverflow.com/questions/tagged/nrules) [](https://github.com/NRules/NRules/discussions)
## Installing Rule#
To compile Rule# rules to the canonical form, get [NRules.RuleSharp](https://www.nuget.org/packages/NRules.RuleSharp) from nuget via the package manager
```console
> Install-Package NRules.RuleSharp
```
To compile rules in the canonical form to the runtime model and to be able to execute rules, install [NRules.Runtime](https://www.nuget.org/packages/NRules.Runtime)
```console
> Install-Package NRules.Runtime
```
## Getting Started
In NRules, rules are expressed against a domain model.
Given the following domain model (located in the ```Domain``` namespace):
```c#
namespace Domain;
public class Customer
{
public string Name { get; set; }
public bool IsPreferred { get; set; }
}
public class Order
{
public int Quantity { get; set; }
public double UnitPrice { get; set; }
public double PercentDiscount { get; set; }
public bool IsDiscounted => PercentDiscount > 0;
public double Price => UnitPrice*Quantity*(1.0 - PercentDiscount/100.0);
}
```
And given the following rule ```Discount.rul```:
```
using Domain;
rule "Order Discount"
when
var customer = Customer(x => x.IsPreferred);
var order = Order(x => !x.IsDiscounted, x => x.Quantity > 10);
then
order.PercentDiscount = 5.0;
Console.WriteLine("Applied discount. Customer={0}, Discount={1}", customer.Name, order.PercentDiscount);
```
The following code will compile the ```Discount.rul```, insert facts into the rules session and fire the activated rules.
```c#
var repository = new RuleRepository();
repository.AddNamespace("System");
repository.AddReference(typeof(Console).Assembly);
repository.AddReference(typeof(Order).Assembly);
repository.Load(@"Discount.rul");
var factory = repository.Compile();
var session = factory.CreateSession();
var customer = new Customer {Name = "John Doe", IsPreferred = true};
var order1 = new Order {Quantity = 12, UnitPrice = 10.0};
var order2 = new Order {Quantity = 5, UnitPrice = 15.0};
session.Insert(customer);
session.InsertAll(new[] {order1, order2});
session.Fire();
```
This prints
```console
Applied discount. Customer=John Doe, Discount=5
```
## Getting Help
Use the following discussion and Q&A platforms to get help with NRules Rule#
- [Discussions](https://github.com/NRules/NRules/discussions)
- [Stack Overflow](https://stackoverflow.com/questions/tagged/nrules)
## Contributing
See [Contributor Guide](https://github.com/NRules/NRules/blob/main/CONTRIBUTING.md) for the guidelines on how to contribute to the project.
---
Copyright © 2012-2025 [Sergiy Nikolayev](https://github.com/snikolayev) under the [MIT license](LICENSE.txt).