cjboyden / NRules.Language

Business rules language for NRules rules engine.

Home Page:http://nrules.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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"
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.

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();

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

About

Business rules language for NRules rules engine.

http://nrules.net

License:MIT License


Languages

Language:C# 42.4%Language:ANTLR 30.9%Language:PowerShell 26.6%Language:Smalltalk 0.1%Language:Batchfile 0.0%