Return data after execution dynamically

61 views
Skip to first unread message

Karthi Kn

unread,
Nov 11, 2020, 12:05:03 PM11/11/20
to NRules Users
Hi Sir,

Below is my code. I want to retrieve the records after rule execution. Also the yellow highlighted values will dynamically change(Based on user inputs). I'm feeling hard to find a solution for this since I'm completely new to this. Please guide me on this.

public List<SecurityReport> ExecuteRules()
{
var securityReport = new List<SecurityReport>();
//Load rules
var repository = new RuleRepository();

if (_RuleId == (int)RuleInfo.CartWithOver50partsPendingOver48Hrs)
{
repository.Load(x => x.From(typeof(PendingShoppingCartTemp2).Assembly));
}
//Compile rules
var factory = repository.Compile();

//Create a working session
var session = factory.CreateSession();

//Load domain model
//Insert facts into rules engine's memory
session.InsertAll(_PendingOrderList);
//Start match/resolve/act cycle
session.Fire();
securityReport = session.Query<SecurityReport>().ToList();
return securityReport;
}


public override void Define()
{
List<ShoppingCartItem> currentCartItem = null;
When()
.Query(() => currentCartItem, p => p
.Match<ShoppingCartItem>(
c => (c.Status != null && (c.Status.ToUpper() == "OPEN" || c.Status.ToUpper() == "P" || c.Status.ToUpper() == "NULL"))
&& c.NumberOfUniqueParts > 25
&& c.CreatedUTCDate.DateDiff("hour", DateTime.UtcNow) >= 24)
.Collect()
.Where(c => c.Any()));

Then()
//.Do(ctx => SendEmailNotification(currentCartItem));
}

Sergiy Nikolayev

unread,
Nov 11, 2020, 11:02:22 PM11/11/20
to NRules Users
As far as parameterizing the rules, this has been answered on the forum. Check this thread: https://groups.google.com/g/nrules-users/c/3Hwiru4gz7A/m/wZMvTj3jBQAJ

Sergiy Nikolayev

unread,
Nov 11, 2020, 11:03:45 PM11/11/20
to NRules Users
And I don't understand the part of the question about "retrieving the records after rule execution". What records? Can you please clarify?

Karthi Kn

unread,
Nov 12, 2020, 12:02:42 AM11/12/20
to NRules Users
Sir, I want the matched facts to be returned to the calling method. Is it possible.

List<ShoppingCartItem> cartItem = new List<ShoppingCartItem>();
cartItem .AddRange(ExecuteRules());

public List<ShoppingCartItem> ExecuteRules()
{
.
.
 session.Fire();
 return session.Query<ShoppingCartItem>().ToList(); //Here its returning all the facts.
}

Karthi Kn

unread,
Nov 12, 2020, 9:19:59 AM11/12/20
to NRules Users
Finally I corrected my code and its working fine. Thank you @jon and  @sergiy Sir. Here is my code.

List<SecurityReport> currentCartItem = null;
RuleQueryData ruleQueryData = null;
  When()
.Match<RuleQueryData>(() => ruleQueryData, r => r.RuleId == (int)RuleInfo.CartWithOver50partsPendingOver48Hrs)
.Query(() => currentCartItem, p => p
.Match<SecurityReport>(
c => (c.STS != null && (c.STS.ToUpper() == "OPEN" || c.STS.ToUpper() == "P" || c.STS.ToUpper() == "NULL"))
&& ((ruleQueryData.RuleOperator == ">" && c.NoP > Convert.ToInt32(ruleQueryData.RuleValue))
|| (ruleQueryData.RuleOperator == "<" && c.NoP < Convert.ToInt32(ruleQueryData.RuleValue)))
&& c.LOD.DateDiff("hour", DateTime.UtcNow) >= Utility.NullToInt(CommonServices.GetEnvironmentVariable("HoursDiff"))
)
.Collect()
.Where(c => c.Any()));

Then()
.Do(ctx => ctx.Insert(new Result(currentCartItem)));


Reply all
Reply to author
Forward
0 new messages