Best way to achieve forward chaining in NRULES - nrules

I want to run a rule based on the result of a previous rule. How can I achieve this functionality using forward chaining? I don't want to create a different class object for each rule to achieve forward chaining.
Here in this example an InstantDiscount object is created just for this one rule to achieve forward chaining.
public class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Customer customer = null;
IEnumerable<Order> orders = null;
Double total = Double.NaN;
When()
.Match<Customer>(() => customer, c => c.IsPreferred)
.Query(() => orders, x => x
.Match<Order>(
o => o.Customer == customer,
o => o.IsOpen)
.Collect())
.Let(() => total, () => orders.Sum(x => x.Amount))
.Having(() => total > 1000);
Then()
.Yield(_ => new InstantDiscount(customer, total * 0.05));
}
}
public class PrintInstantDiscountRule : Rule
{
public override void Define()
{
InstantDiscount discount = null;
When()
.Match(() => discount);
Then()
.Do(_ => Console.WriteLine("Customer {0} has instant discount of {1}",
discount.Customer.Name, discount.Amount));
}
}

Forward chaining is the process where one rule changes the working memory of the rules engine in such a way as to activate some other rules. This can be achieved by inserting new facts into the rules engine (using Yield or IContext.Insert in NRules), or by changing some of the existing facts (using IContext.Update).
Here is the original example, reformulated to attach the discount to the Customer fact, and then update that fact to achieve forward chaining.
public class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Customer customer = null;
IEnumerable<Order> orders = null;
Double total = Double.NaN;
When()
.Match<Customer>(() => customer, c => c.IsPreferred, c => !c.DiscountPercent.HasValue)
.Query(() => orders, x => x
.Match<Order>(
o => o.Customer == customer,
o => o.IsOpen)
.Collect())
.Let(() => total, () => orders.Sum(x => x.Amount))
.Having(() => total > 1000);
Then()
.Do(ctx => ApplyDiscount(customer, 0.05))
.Do(ctx => ctx.Update(customer));
}
private static void ApplyDiscount(Customer customer, double discount)
{
customer.DiscountPercent = discount;
}
}
public class DicsountNotificationRule : Rule
{
public override void Define()
{
Customer customer = null;
When()
.Match(() => customer, c => c.DiscountPercent.HasValue);
Then()
.Do(_ => Console.WriteLine("Customer {0} has instant discount of {1}%",
customer.Name, customer.DiscountPercent));
}
}
When forward chaining by updating existing facts, care must be taken to not re-activate the rule that updated the fact, to avoid undesirable recursion. There are several mechanisms to control recursion in NRules:
Write conditions in such a way that the update invalidates conditions of the rule (this is what we did in the above example; once the discount is set, the rule will no longer match)
Use Repeatability attribute on the rule to prevent re-firing
Use agenda filters to only activate the rule when certain changes occur in the matching facts.
The later two options are described in NRules documentation.

Related

NRules - Organizing Rules

When organizing rules for NRules, is it necessary to only have 1 When/Then group in a file?
I have one rule that looks at 3 conditions (fact matches), is flagA = false, inputA = one of a list of values, and inputB = a single value. When all true, set flagA to true. I got that one working,then wanted to add the second rule.
The second rule is when flagA is false and flagB is true then set flagA to true.
Do these two rules need to be in separate .cs files or can they be together in one.
I looked at the ".Or" option, but I am not good enough with fluent to figure out what it is doing.
Thanks for your help,
Toom
In NRules an instance of a class that inherits from NRules.Fluent.Dsl.Rule is one rule. You can put multiple rule classes in one .cs file or different files, it does not matter - each class is still a separate rule.
In that rule class, you normally would specify When and Then sections only once. If you specify them multiple times, all conditions would still be combined into a single set using and group. Actions would also be merged into a single set.
So:
public class Rule1: Rule
{
public override void Define()
{
When()
.Match<A>();
Then()
.Do(_ => X());
When()
.Match<B>();
Then()
.Do(_ => Y());
}
}
is exactly the same as
public class Rule1: Rule
{
public override void Define()
{
When()
.Match<A>()
.Match<B>();
Then()
.Do(_ => X())
.Do(_ => Y());
}
}
In other words, both examples above create just a single rule that matches both A and B, and if both those facts match, then both X and Y methods are executed.
If you want that to be two independent rules, then put those different When/Then sections into different rule classes.
public class Rule1: Rule
{
public override void Define()
{
When()
.Match<A>();
Then()
.Do(_ => X());
}
}
public class Rule2: Rule
{
public override void Define()
{
When()
.Match<B>();
Then()
.Do(_ => Y());
}
}
UPDATE:
If you wanted to connect conditions A and B with an OR, you could do it like below. Here the rule will fire if ((A OR B) AND C):
public class Rule1: Rule
{
public override void Define()
{
When()
.Or(x => x
.Match<A>()
.Match<B>())
.Match<C>();
Then()
.Do(_ => Z());
}
}

Nest - how to write a span near query with multiple clauses?

I want to use NEST to write a query as described in:
https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-span-near-query.html
I have a collection of values to use as a SpanTerm clause. The collection size varies by query.
If the collection size was limited I could do:
var sn = q.SpanNear(snr => snr.Clauses(c => c.SpanTerm(), c => c.SpanTerm(), ...))
How do I do it dynamically (unknown clauses count)?
You can use this extension method:
public static class SpanNearQueryDescriptorExtensions
{
public static void SpanTermClauses<T>(this SpanNearQueryDescriptor<T> descriptor, Expression<Func<T, object>> field, string[] terms)
where T : class
{
descriptor.Clauses(terms
.Select(t => new Func<SpanQuery<T>, SpanQuery<T>>(query => query.SpanTerm(field, t)))
.ToArray());
}
}
Usage:
var terms = new[] {"term1", "term2"};
client.Search<Docuemnt>(s => s
.Query(q => q
.SpanNear(sp => sp
.Slop(12)
.SpanTermClauses(f => f.Title, terms))));
Hope it helps.

Multiple Subscriptions on one observable

I have a read-write property on my ViewModel and need two separate actions to occur when it changes :
public decimal Paid {
get { return paid; }
set { this.RaiseAndSetIfChanged(ref paid, value); }
}
...
in the ctor:
this.WhenAnyValue(pb => pb.Paid)
.Select(amount => NumberToEnglish.ToSentence(amount))
.ToProperty(this, x => x.AmountInWords, out amountInWords);
this.WhenAnyValue(pb => pb.Paid)
.Subscribe(amount => SelectedPaymentBatch.Paid = amount );
Is there a way to do this in one statement or is this the correct way to do this?
It's very much feasible to do both in one stream, e.g using Do operator (see below), but I would recommend to keep your current approach, as it correctly separates both concerns, which are unrelated but the fact they trigger on the same property (but that could change).
this.WhenAnyValue(pb => pb.Paid)
.Do(amount => SelectedPaymentBatch.Paid = amount)
.Select(amount => NumberToEnglish.ToSentence(amount))
.ToProperty(this, x => x.AmountInWords, out amountInWords);

How to use Func with IQueryable that returns IOrderedQueryable

I'm doing some research about EF and came across a function that accepts
Func<IQueryable<Student>, IOrderedQueryable<Student>>
and just wondering how to call that function that accepts that kind of parameter?
imagine function is something like that, and you've got a property Id in Student class.
public static class Helper {
public static void Test(Func<IQueryable<Student>, IOrderedQueryable<Student>> param)
{
var test = 0;
}
}
then you could use it this way
var student = new List<Student>().AsQueryable();//non sense, just for example
Helper.Test(m => student.OrderBy(x => x.Id));
m => student.OrderBy(x => x.Id) is a
Func<IQueryable<Student>, IOrderedQueryable<Student>>
(IQueryable<student> as parameter, returning a IOrderedQueryable<Student>)
or just
Helper.Test(m => m.OrderBy(x => x.Id));
In fact this doesn't make much sense without a "real" function...
define a method.
public IOrderedQueryable<Student> OrderingMethod(IQueryable<Student> query)
{
return query.OrderBy(student => student.Name);
}
Now this assignment is legal:
Func<IQueryable<Student>, IOrderedQueryable<Student>> orderingFunc = this.OrderingMethod;
And now that you have it in a variable, it's easy to pass it to the method.
You could also do it all inline:
Func<IQueryable<Student>, IOrderedQueryable<Student>> orderingFunc =
query => query.OrderBy(student => student.Name);

Entity framework linq query Include() multiple children entities

This may be a really elementry question but whats a nice way to include multiple children entities when writing a query that spans THREE levels (or more)?
i.e. I have 4 tables: Company, Employee, Employee_Car and Employee_Country
Company has a 1:m relationship with Employee.
Employee has a 1:m relationship with both Employee_Car and Employee_Country.
If i want to write a query that returns the data from all 4 the tables, I am currently writing:
Company company = context.Companies
.Include("Employee.Employee_Car")
.Include("Employee.Employee_Country")
.FirstOrDefault(c => c.Id == companyID);
There has to be a more elegant way! This is long winded and generates horrendous SQL
I am using EF4 with VS 2010
Use extension methods.
Replace NameOfContext with the name of your object context.
public static class Extensions{
public static IQueryable<Company> CompleteCompanies(this NameOfContext context){
return context.Companies
.Include("Employee.Employee_Car")
.Include("Employee.Employee_Country") ;
}
public static Company CompanyById(this NameOfContext context, int companyID){
return context.Companies
.Include("Employee.Employee_Car")
.Include("Employee.Employee_Country")
.FirstOrDefault(c => c.Id == companyID) ;
}
}
Then your code becomes
Company company =
context.CompleteCompanies().FirstOrDefault(c => c.Id == companyID);
//or if you want even more
Company company =
context.CompanyById(companyID);
EF Core
For eager loading relationships more than one navigation away (e.g. grand child or grand parent relations), where the intermediate relation is a collection (i.e. 1 to many with the original 'subject'), EF Core has a new extension method, .ThenInclude(), and the syntax is slightly different to the older EF 4-6 syntax:
using Microsoft.EntityFrameworkCore;
...
var company = context.Companies
.Include(co => co.Employees)
.ThenInclude(emp => emp.Employee_Car)
.Include(co => co.Employees)
.ThenInclude(emp => emp.Employee_Country)
With some notes
As per above (Employees.Employee_Car and Employees.Employee_Country), if you need to include 2 or more child properties of an intermediate child collection, you'll need to repeat the .Include navigation for the collection for each child of the collection.
Personally, I would keep the extra 'indent' in the .ThenInclude to preserve your sanity.
For serialization of intermediaries which are 1:1 (or N:1) with the original subject, the dot syntax is also supported, e.g.
var company = context.Companies
.Include(co => co.City.Country);
This is functionally equivalent to:
var company = context.Companies
.Include(co => co.City)
.ThenInclude(ci => ci.Country);
However, in EFCore, the old EF4 / 6 syntax of using 'Select' to chain through an intermediary which is 1:N with the subject is not supported, i.e.
var company = context.Companies
.Include(co => co.Employee.Select(emp => emp.Address));
Will typically result in obscure errors like
Serialization and deserialization of 'System.IntPtr' instances are not supported
EF 4.1 to EF 6
There is a strongly typed .Include which allows the required depth of eager loading to be specified by providing Select expressions to the appropriate depth:
using System.Data.Entity; // NB!
var company = context.Companies
.Include(co => co.Employees.Select(emp => emp.Employee_Car))
.Include(co => co.Employees.Select(emp => emp.Employee_Country))
.FirstOrDefault(co => co.companyID == companyID);
The Sql generated is by no means intuitive, but seems performant enough. I've put a small example on GitHub here
You might find this article of interest which is available at codeplex.com.
Improving Entity Framework Query Performance Using Graph-Based Querying.
The article presents a new way of expressing queries that span multiple tables in the form of declarative graph shapes.
Moreover, the article contains a thorough performance comparison of this new approach with EF queries. This analysis shows that GBQ quickly outperforms EF queries.
How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
There is no other way - except implementing lazy loading.
Or manual loading....
myobj = context.MyObjects.First();
myobj.ChildA.Load();
myobj.ChildB.Load();
...
Might be it will help someone, 4 level and 2 child's on each level
Library.Include(a => a.Library.Select(b => b.Library.Select(c => c.Library)))
.Include(d=>d.Book.)
.Include(g => g.Library.Select(h=>g.Book))
.Include(j => j.Library.Select(k => k.Library.Select(l=>l.Book)))
To doing this:
namespace Application.Test
{
using Utils.Extensions;
public class Test
{
public DbSet<User> Users { get; set; }
public DbSet<Room> Rooms { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public void Foo()
{
DB.Users.Include(x => x.Posts, x => x.Rooms, x => x.Members);
//OR
DB.Users.Include(x => x.Posts, x => x.Rooms, x => x.Members)
.ThenInclude(x => x.Posts, y => y.Owner, y => y.Comments);
}
}
}
this extension might be helpful:
namespace Utils.Extensions
{
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
public static partial class LinqExtension
{
public static IQueryable<TEntity> Include<TEntity>(
this IQueryable<TEntity> sources,
params Expression<Func<TEntity, object>>[] properties)
where TEntity : class
{
System.Text.RegularExpressions.Regex regex = new(#"^\w+[.]");
IQueryable<TEntity> _sources = sources;
foreach (var property in properties)
_sources = _sources.Include($"{regex.Replace(property.Body.ToString(), "")}");
return _sources;
}
public static IQueryable<TEntity> ThenInclude<TEntity, TProperty>(
this IQueryable<TEntity> sources,
Expression<Func<TEntity, IEnumerable<TProperty>>> predicate,
params Expression<Func<TProperty, object>>[] properties)
where TEntity : class
{
System.Text.RegularExpressions.Regex regex = new(#"^\w+[.]");
IQueryable<TEntity> _sources = sources;
foreach (var property in properties)
_sources = _sources.Include($"{regex.Replace(predicate.Body.ToString(), "")}.{regex.Replace(property.Body.ToString(), "")}");
return _sources;
}
}
}

Resources