NRules Issue: Rule Matching on super class dont work

242 Views Asked by At

i have a class C, which inherits from A. Now i want to write a Rules, that matches on type A. So I tried the following code:

 public class tmp : Rule
{
    public override void Define()
    {
        A t = null;
        When().Match<A>(() => t);
        Then().Do(ctx => Console.WriteLine("test"));
    }
}

But this don't work. Can anyone explain, how I can fix this issue? This doesnt work.

Best regards

1

There are 1 best solutions below

0
On

Matching on the parent class definitely does work in NRules. Here is a full working example.

public class A
{ }

public class C : A
{ }

public class TestRule : Rule
{
    public override void Define()
    {
        A fact = null;

        When()
            .Match(() => fact);
        Then()
            .Do(x => Console.WriteLine("Test"));
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var repo = new RuleRepository();
        repo.Load(x => x.From(xx => xx.AssemblyOf<Program>()));

        var factory = repo.Compile();
        var session = factory.CreateSession();

        session.Insert(new C());
        session.Fire();
    }
}

If you post the rest of your code, I can update this answer and show where the issue is. Maybe you didn't insert the fact into the session, or maybe you didn't call the Fire method, or maybe you didn't include the rule when compiling the session factory.