I am new to rule engines, currently learning the basics.
I want to offer my users the possibility to define some rules to modify their data. So I'm playing with the idea of implementing a rule engine for that. The data is created by the system and it looks more or less like follows:
Destination, Dispatcher, NumberOfEvents, Price
Germany, D1, 15, 150.0
Germany, D5, 20, 200.0
Germany, DWrong, 20, 200.0
Spain, D1, 5, 50.0
Spain, D4, 15, 150.0
So for example for giving 10% discount to all Dispatcher when Destination is "Spain", the user could add following rule:
Rule1:
When
(country == Spain)
Then
Price * 0.9
Now if there is an entry with Dispatcher equals DWrong, the user would also like to create another entry with Dispatcher equals "Error" (and the other values same as "DWrong"). E.g.:
Germany, Error, 20, 200.0
But the entry should be created only when the NumberOfEvents in "DWrong" is bigger than 1% of the total NumberOfEvents for that same Destination.
The pseudo code would be something like this:
loop (Destination) // Germany, Spain
{
loop () {
TotalNumberOfEvents += NumberOfEvents;
}
loop () {
if (Dispatcher == "DWrong") {
if (NumberOfEvents > (TotalNumberOfEvents * 0.01) ) {
createRow(Destinaction, "Error", NumberOfEvents, Price);
}
}
}
}
What I came up with so far is:
Rule1:
When
(Destination == "Germany")
Then
global_GermanyTotalNumberOfEvents += NumberOfEvents
Rule2:
When
(Destination == "Germany" && Dispatcher == "DWrong" && NumberOfEvents > (global_GermanyTotalNumberOfEvents * 0.01))
Then
createRow(Destination, "Error", NumberOfEvents, Price)
// Repeat for spain
This doesn't look very friendly to the user but that is ok. But I am not sure how to run rule2 after rule1 has finished the loop ? I think I am not getting the concept right.
Note: There is no particular rule engine in place, at the moment I am just reasearching and checking whether a rule engine is the right way to go for such situations for my users..