Awesome Open Source
Awesome Open Source

Rule# - Language for NRules

Rule# (Rule Sharp) is a business rules language for NRules rules engine.

⚠️ This project is currently in early development

Build status NuGet NRules on Stack Overflow Join the chat

Installing Rule#

First, install NuGet. Then, from the Package Manager Console:

To compile Rule# rules to the canonical form, install NRules.RuleSharp:

PM> 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:

PM> Install-Package NRules.Runtime

Getting Started

In NRules, rules are expressed against a domain model. Given the following domain model (located in the Domain assembly):

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 { get { return PercentDiscount > 0; } }

        public double Price
            get { return UnitPrice*Quantity*(1.0 - PercentDiscount/100.0); }

And given the following rule Discount.rul:

using Domain;

rule "Order Discount"
    var customer = Customer(x => x.IsPreferred);
    var order = Order(x => !x.IsDiscounted, x => x.Quantity > 10);
    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.

var repository = new RuleRepository();


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.InsertAll(new[] {order1, order2});


Copyright © 2012-2020 Sergiy Nikolayev under the MIT license.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
c-sharp (10,523
language (372
rules (29
rules-engine (15

Find Open Source By Browsing 7,000 Topics Across 59 Categories