0

I want to run all rules asyncronously to make it thread safe

When i m performing load test then why RuleEngine taking so much time to execute all rules.

        NRuleRepository repository = null;
        foreach (var rule in rules)
        {                
            repository = new NRuleRepository();
            repository.LoadRules(rule.Rule);
            var factory = repository.Compile();
            var session = factory.CreateSession();
            NRuleBody data = null;
            foreach (var fact in rule.RuleDataList)
            {
                data = new NRuleBody();
                data.Rule = rule.Rule;
                data.RuleData = fact;
                session.Insert(data);
            }

            result += session.Fire();
        }

Can i make call as below:

session.FireAsync();

or there is any other option to fire Multiple rules but in async ? and NRuleRepository class should be reinitialize on every request ?

1 Answers1

1

At the very least, you could probably utilise Task.Run() in order to create a thread for each instance of the repository, but what you're doing seems very inefficient.

Why are you inserting the rule with the data in the session? You've already added the rule to the repository.

If you are only ever having a singular rule in the repository, NRules is almost certainly overkill, and you would be better placed doing almost anything else.

  • Well according to specs data is always inserted in session isn't it ? – Ghanshyam Singh Aug 30 '19 at 10:43
  • Data is always inserted into session, I'm not debating that - what I'm highlighting is that you are inserting the rule along with it, which is totally unnecessary. You are meant to insert the rules into the rule repository, which compiles the rules, and then you insert the data that you want to run through the rule engine into the session. – Nicholas Tyrrell Aug 30 '19 at 10:55
  • Yes thats what i m doing, let me put my complete code at https://stackoverflow.com/questions/57722215 – Ghanshyam Singh Aug 30 '19 at 13:50