If I address the facts (Shift) in a rule as interfaces (IShift) it works for one rule. As soon as I duplicate the rule the compilation throws an error and the inner exception claims:
If I toggle the comment between line 66&67 it will start working again.
using NRules;
using NRules.Fluent;
using NRules.Fluent.Dsl;
using System;
using System.Reflection;
namespace NRuleSample
{
class Program
{
static void Main(string[] args)
{
try
{
var repository = new RuleRepository();
repository.Load(x => x.From(Assembly.GetExecutingAssembly()));
var session = repository.Compile().CreateSession();
Shift shift = new Shift { StartTime = new DateTime(2020, 9, 24, 6, 0, 0), EndTime = new DateTime(2020, 9, 24, 17, 0, 0) };
session.Insert(shift);
session.Fire();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Hello World!");
}
}
public interface IShift
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}
public class Shift : IShift
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
}
public class LongShiftRule1 : Rule
{
public override void Define()
{
IShift shift = null;
When()
.Match<IShift>(() => shift, shf => (shf.EndTime - shf.StartTime).TotalHours > 10);
Then()
.Do(ctx =>
Console.WriteLine($"[LongShiftRule1] IShift is a long shift {shift}"));
}
}
public class LongShiftRule2 : Rule
{
public override void Define()
{
IShift shift = null; // This line makes it throw NotImplemented
//Shift shift = null; // This line makes it work
When()
.Match<IShift>(() => shift, shf => (shf.EndTime - shf.StartTime).TotalHours > 10);
Then()
.Do(ctx =>
Console.WriteLine($"[LongShiftRule2] Shift is a long shift {shift}"));
}
}
}