Tracking dependent complex objects

32 views
Skip to first unread message

Alexander Saevsky

unread,
Feb 14, 2020, 12:35:30 PM2/14/20
to NRules Users
Sergiy, colleges,
I am planning usage of NRules for automated building complex object in complex editor.
The main pattern would be:
 if (some-conditions)
 then (insert a new object).
However, the system is dymanic, so once the original objects are retracted, the derived also comes illegal and should be removed.
I have created a simple demo, attached to my post. It works. 
It shows the "marriage" and creates a couple when meet to guys of opposite sex and same first char in the name.
The kludge was needed to force re-marry a women, when his husband is retracted.

However, I think, that I can engage your Rete engine to serve this functional for me. You have the Synthetic facts being dependent on other.
I wish to expose the functional to the public interface of ISession.
Say, add to ISession the method:
InsertDependent(object fact, IEnumerable<object> dependsOn)
Have you any ideas on it? Have you more profound ideas for developing your very attractive project, which will cover my needs?
Would you accept my branch if I will implement it?

Thanks in advance,
Alexander
Program.cs

Sergiy Nikolayev

unread,
Feb 16, 2020, 10:51:27 PM2/16/20
to NRules Users
It's better to think about dependencies not as facts depending of other facts, but as facts depending on rule matches (aka activations of the rule). NRules already has a feature to insert facts into the session that are "linked" to the activation that generated them, which will automatically retract those facts if the match that produced them is no longer true.
Instead of calling Do(ctx => Insert(...)) use Yield(ctx => ...).
Also take a look at the corresponding wiki page: https://github.com/NRules/NRules/wiki/Forward-Chaining

Alexander Saevsky

unread,
Feb 17, 2020, 3:45:12 AM2/17/20
to NRules Users
Thanks for the responce.
I have seen the Link and have already tried to use it. I have got infinite loop.
Now I have changed my code by your suggestion: substituted Insert to Yield.
The result was same as of my own: the engine goes into infinite loop.

        public override void Define()
        {
            Human ivan = null;
            Human mary = null;
            When()
                .Match(() => ivan, g => g.Sex == Sex.Male)
                .Match(() => mary, m => m.Sex == Sex.Female && ivan.Name[0] == m.Name[0])
                .Not<Marry>(m => m.He == ivan)
                .Not<Marry>(m => m.She == mary);
            Then()
                .Yield(ctx => new Marry { He = ivan, She = mary });
        }

Alexander Saevsky

unread,
Feb 17, 2020, 12:10:31 PM2/17/20
to NRules Users
Ben Jacobs explained me my error. The proper code reads:

public class FindPair : Rule
    {
        public override void Define()
        {
            Human ivan = null;
            Human mary = null;
            When()
                .Match(() => ivan, g => g.Sex == Sex.Male)
                .Match(() => mary, m => m.Sex == Sex.Female && ivan.Name[0] == m.Name[0])
                .Not<Marry>(m => m.He == ivan && m.She != mary)
                .Not<Marry>(m => m.She == mary && m.He != ivan);
Reply all
Reply to author
Forward
0 new messages