Are there any restrictions on the conditions for Match()? #339
-
I wanted to set up a simple notifications system inside my app using NRules. The backbone of the whole thing were supposed to be rules checking if user has a given type of notifications enabled. Due to multiple notifications channels this information is stored in fact inside a dictionary. In my rules I hade something like this When I was testing I added a simple bool value "directly" to the fact and changed Match to something like I feel like I'm missing some fundamental concept about the rules, matching and firing? Or maybe there's some limitation on how you should write the matchers due to how the rules are stored internally? Did I missed docs about this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
@trobinpl no, there are no restrictions. I created this simple scenario that demonstrates. It also prints diagnostics when rule conditions are evaluated. You can play with it to see what the problem with your rule or your user fact is. using NRules;
using NRules.Fluent;
using NRules.Fluent.Dsl;
var repository = new RuleRepository();
repository.Load(x => x.From(typeof(TestRule).Assembly));
var factory = repository.Compile();
var session = factory.CreateSession();
session.Events.LhsExpressionEvaluatedEvent += (sender, args) =>
{
Console.WriteLine($"LHS expression evaluated: {args.Expression}, result: {args.Result}");
};
var user = new User{Name = "John Do"};
user.NotificationsSettings.Add("EventType", new EventInfo{SomeChannel = true});
session.Insert(user);
session.Fire();
public class EventInfo
{
public bool SomeChannel { get; set; }
}
public class User
{
public string Name { get; set; }
public Dictionary<string, EventInfo> NotificationsSettings { get; set; } = new Dictionary<string, EventInfo>();
}
public class TestRule : Rule
{
public override void Define()
{
var user = default(User);
When()
.Match(() => user, u => u.NotificationsSettings["EventType"].SomeChannel);
Then()
.Do(ctx => Console.WriteLine($"Matched user {user.Name}"));
}
} Output:
|
Beta Was this translation helpful? Give feedback.
-
I played around with the example you've provided and it looks like the issue lays not in the matching condition, but rather in having multiple rules. I've got correct result when I registered only one rule, however having two almost identical rules loaded
I get
even when What's also weird (at least with my limited understaning) is the diagnostics info
Shouldn't there be also info about evaluating expressions for the I've confirmed that both my rules are loaded into the repository. Both of them are |
Beta Was this translation helpful? Give feedback.
@trobinpl thanks for that repro case - you found a bug! See #340
While I'm fixing it, you can work around, by wrapping those properties into methods and use those in the rules.