I have got below structure in Java:
public class Request {
List<Product> product;
List<Account> accounts;
}
public class Product {
String productIdOne;
String productIdTwo;
String productTax;
}
public class Account {
List<ProductRelationship> productsRelationship;
}
public class ProductRelationship {
String productIdOne;
String productIdTwo;
}
And the request is the fact object send to drools. I am wondering how I can check if there is at least one product that productTax is set to 'true' and there is a relationship between one account and one product. In other words, if there is a product with tax set to true and at least one account contains a relationship with this product (by productIdOne and productIdTwo) then the rule result should pass;
The main issue is that the list of the product relationship is inside the account list.
Thanks for any advice
You have a rather straight-forward set of conditions, so it is possible to write a relatively simple rule to check them. I will consider each condition separately and then combine them into a final rule.
As you wrote:
there is at least one product that productTax is set to 'true'
Now, as you mentioned, your rule inputs are the Request instance which contains two lists (products, accounts.) We'll start by declaring that:
rule "Account exists with taxed product"
when
Request( $products: product != null,
$accounts: accounts != null )
Next, we want to find the taxed product. If we only wanted to prove the existence of the product, we could use an exists( ... ) condition, which is extremely fast. However since we want to do further comparisons, we'll want to actuall find the product with this condition and save a reference to it.
$taxedProduct: Product( productTax == "true" ) from $products
I've assumed here that any value other than exactly "true" is indicative of an untaxed product. You should adjust as needed (and possibly consider changing this type to a boolean.)
The next condition is to find the account:
there is a relationship between one account and [the taxed] product
First, we'll need to update our $taxedProduct declaration and get references to its ids:
$taxedProduct: Product( productTax == "true",
$id1: productIdOne,
$id2: productIdTwo ) from $products
Now we need to find an account with a matching relationship.
$account: Account( $relationships: productsRelationship != null ) from $accounts
exists( ProductRelationship( productIdOne == $id1,
productIdTwo == $id2 ) from $relationships )
Here, I used an exists condition for the relationship because we don't need to refer to the relationship itself ever again, just verify that the relationship exists. I did declare a variable $account to point to the account that has the product relationship.
Putting it all together, we have:
rule "Account exists with taxed product"
when
Request( $products: product != null,
$accounts: accounts != null )
$taxedProduct: Product( productTax == "true",
$id1: productIdOne,
$id2: productIdTwo ) from $products
$account: Account( $relationships: productsRelationship != null ) from $accounts
exists( ProductRelationship( productIdOne == $id1,
productIdTwo == $id2 ) from $relationships )
then
// We have a taxed product $taxedProduct
// and an associated account $account
end
When I first started with Drools I found it difficult to wrap my head around the way it treated objects in lists, which is why the ProductRelationship sub-list seems like a tricky issue on its face. What Drools is going to do is iterate through the $products list and find those Product instances that meet our criteria (namely, have productTax == "true".) Once it has found these taxed products, it then similarly goes through the $accounts list and finds all Accounts that meet the criteria (which have a productsRelationship list.) Then for each of those accounts, it is going to test that there exists a relationship as we've defined.
This is a simplified explanation, of course, but it helps to form a mental model of roughly what Drools is doing here. In reality Drools is much more efficient than then roughly O(n^3) workflow I've just described.
An interesting thing you should keep in mind is that this rule is not going to "stop" as soon as it finds a match. If you have two taxed products that have a relationship to a single account, this rule will fire twice -- once for each taxed product. Or, alternatively, if you have one taxed product and two accounts that have a relationship to it, the rule will fire twice (once for each account.) Basically, the rule will fire once for each "match" it finds in the given request.
can you try this following
rule "sample"
no-loop
when
request:Request(accountList: accounts)
request1:Request(productList: product)
Account(productsRelationshipList:ProductRelationship) from accountList
Product(productId contains productsRelationshipList, productTax = true ) from productList
then
System.out.println("Rule fired satisfied");
end
Related
I am solving a problem similar to employee rostering. I have an additional constraint. The employees have a "type" value assigned to them. It's a hard constraint that atleast 1 employee of each "type" be there everyday. I have modelled it as follows:
rule "All employee types must be covered"
when
$type: Constants.EmployeeType() from Constants.EmployeeType.values()
not Shift(employeeId != null, $employee: getEmployee(), $employee.getType() == $type.getValue())
then
scoreHolder.addHardConstraintMatch(kcontext, -100);
end
This rule however, does not consider that the constraint be satisfied on each day. I have a list of date strings. How can I iterate over them in the drools file in the same manner that I am on the EmployeeType enum?
Edit: I figured out a way but it feels like a hack. When initialising the list of date strings, I also assign it to a static variable. Then I am able to use the static variable similar to the enum.
rule "All employee types must be covered"
when
$type: Constants.EmployeeType() from Constants.EmployeeType.values()
$date: String() from Constants.dateStringList;
not Shift(employeeId != null, $date == getDate(), $employee: getEmployee(), $employee.getType() == $type.getValue())
then
scoreHolder.addHardConstraintMatch(kcontext, -100);
end
Don't think this is the correct approach though.
Your approach works, but having to define dynamic configurations in a static property of a class doesn't sound right (like you pointed out).
One solution would be to either use a global in the session, or to have a fact class that specify this configuration.
Using a global
If you decide to take this approach, then you need to define a global of type List<String> in your DRL and then use it in your rules in combination with the memberOf operator:
global List<String> dates;
rule "All employee types must be covered"
when
$type: Constants.EmployeeType() from Constants.EmployeeType.values()
not Shift(
employeeId != null,
date memberOf dates,
$employee: getEmployee(),
$employee.getType() == $type.getValue()
)
then
scoreHolder.addHardConstraintMatch(kcontext, -100);
end
It is recommended to set the value for global before you insert any fact Shift into you session:
List<String> dates = //get the List from somewhere
ksession.setGlobal("dates", dates);
Using a Fact Class
Other than a global, you can model your configuration as a class. This makes things easier if you want for example to modify the configuration inside the rules themselves.
for this approach you will need to have a class containing the List<String> first. You could in theory insert the List<String> without wrapping it in any class, but this will make things hard to read and maintain.
public class DatesConfiguration {
private List<String> dates;
//... getters + setters
}
Then, you need to instantiate an object of this class and to insert it into your session:
DatesConfiguration dc = new DatesConfiguration();
dc.setDates(...);
ksession.insert(dc);
At this point, the object you have created is just another fact for Drools and can be used in your rules:
rule "All employee types must be covered"
when
$type: Constants.EmployeeType() from Constants.EmployeeType.values()
DatesConfiguration($dates: dates)
not Shift(
employeeId != null,
date memberOf $dates,
$employee: getEmployee(),
$employee.getType() == $type.getValue()
)
then
scoreHolder.addHardConstraintMatch(kcontext, -100);
end
Hope it helps,
I have list of objects Person. Object Person contain list of objects Car. How i can select from list only thoose Person, who contain Car selected type. For exmplain: Car with brand "BMW". I don't know do it without for loop.
person[0].addCar(new Car("BMW"));
person[0].addCar(new Car("Ford"));
person[1].addCar(new Car("Ford"));
person[1].addCar(new Car("Ford"));
person[1].addCar(new Car("Ford"));
How i can return person[0] in drools-regulations.
My code doesn't work.
rule "HardDrool"
salience 100
when
$world : World();
$persons: Human(
(name == "Yura"),
(!cars.isEmpty()),
(Car(name == "BMW") from getCars())
) from $world.getPersons()
then
System.out.println($persons);
end
rule "HardDrool"
when
$world : World();
$person: Human( name == "Yura", !cars.isEmpty() )
from $world.getPersons()
exists Car( name == "BMW" ) from $person.getCars())
then
System.out.println( $person );
end
This should fire once for each Human owning at least one BMW. If you want to see each BMW, omit the exists.
I want to know how to modify a object in a list . I tried following, but it gives a error.
when
Category( $bookList : books )
UserProfile( profile == UserProfile.STUDENT )
$book : Book( student == true )
$category : Category( books contains $group )
then
modify( $category.books[$book] ) { setEligible(true) }
end
Book.setEligible is the method i need to call. But i need to call this for selected object in Cagegory.books list. What am i doing wrong ? can anyone help ?
Thanks !
First thing: Are you sure you want to include 2 different Category patterns in your rule? If you have 2 different categories, you may end up with 4 executions of that rule.
Second thing, if you want to modify the book, why don't you just do:
modify($book) {
setEligible(true)
}
Hope it helps,
You also need to make sure that Book is a fact.. that means that you are inserting that Fact into the Ksession..
What is the error that you are getting? which version of drools are you using?
I am modifying an item in a nested collection like this...
[using "from"]
rule "4G complete"
salience -1
when
$tr: TopTowerResult()
$ptncascade: PtnCascade() from $tr.cascadeList
Timestamp() from $ptncascade.cascadeFact.actual4g
then
$ptncascade.getCascadeFact().setComplete4g(true);
$ptncascade.getCascadeFact().setEstimate4g("Completed");
end
Explanation -
This line matches every instance of PtnCascade in $tr.cascadeList
You could add an additional pattern here to limit the selected items from the list.
$ptncascade: PtnCascade() from $tr.cascadeList
The next line operates on every instance of PtnCascade() that was matched.
Timestamp() from $ptncascade.cascadeFact.actual4g
The consequence also operates on the specific PtnCascade that was matched in the list -
$ptncascade.getCascadeFact().setComplete4g(true);
I have an object graph that I am trying to generate Fulfillment object from in Drools. Specifically, Fulfillment objects represent a rule that is either satisfied, or unsatisfied. My object graph looks like the following:
Users ---> many Requirements --> Event
`--> many Records ----^
Records can fulfill Requirements if they both point at the same Event. This produces a Fulfillment object in Drools.
A reduce down rule to produce Fulfillments is the following:
rule "fulfils"
when
$u : User()
$rec : Record() from $u.records
$r : Requirement(event contains $rec.event) from $u.requirements
then
insertLogical( new Fulfillment($u, $rec, $r, true));
System.out.println("Inserting logical");
end
rule "unfulfils"
when
$u : User()
$rec : Record() from $u.records
$r : Requirement(event not contains $rec.event) from $u.requirements
then
insertLogical( new Fulfillment($u, $rec, $r, false));
System.out.println("Inserting logical");
end
query "fulfillment"
$fulfillment : Fulfillment()
end
The problem I run into here is if the user has no records, there is no Fulfillment inserted for the requirement. I believe this is because there is no Record() to search on to satisfy my graph.
Is there a way to use the records without requiring more than zero to exist?
Also, do I need two rules here to insert both true and false Fulfillments or is there a better way to do this?
Edit
Another problem I am facing with these rules is the Requirement(event contains $rec.event) does not accomplish the task of finding if any records satisfy the given collection of events. Is there a better way to find if there exists an overlap between the many record's single events, and the single requirements multiple events?
Another Edit
Here's another approach I thought up. Instead of inserting Fulfillments if a requirement/record pair is not found, why not just insertLogical Fullfillments for all Requirements that have no matching positive Fullfillment:
rule "unfulfils"
when
$u : User()
$r : Requirement() from $u.requirements
not(Fulfillment(user == $u, requirement == $r, fulfilled == true))
then
insertLogical( new Fulfillment($u, null, $r, false));
System.out.println("Inserting logical");
end
query "fulfillment"
$fulfillment : Fulfillment()
end
This takes care of the issue of comparing the overlap of two collections, and the case where a user has no records. (Would appreciate some validation on this).
Using 2 different rules for your situation is a common pattern. It makes your rule base easier to read (and in a way to maintain too).
Regarding your question about no Record(), I think you could write something like this (If I understood your question correctly):
rule "unfulfils because of no Record"
when
$u : User(records == null || records.empty == true) //A user without records
$r : Requirement() from $u.requirements // but with Requirements
then
//You don't have a record to set in your Fulfillment object
insertLogical( new Fulfillment($u, $rec, null, false));
System.out.println("Inserting logical");
end
My model contains an Order (parent object) and Shipments (child object). The database table for these already have a surrogate key as an auto-increment primary key.
I have the business rule is that for each shipment in the order, we need to have an auto generated "counter" field -- e.g. Shipment 1, Shipment 2, Shipment 3, etc. Shipment model has properties: "ShipmentId", "OrderId", "ShipmentNumber". My attempted implemention is to have ShipmentNumber an int and in code(as opposed to database), query the Shipment collection and do max() + 1.
Here's a code snipet of what I'm doing.
Shipment newShipmentObj = // blah;
int? currentMaxId = myOrderObj.Shipments
.Select(x => (int?) x.ShipmentNumber)
.Max();
if (currentMaxId.HasValue)
newShipmentObj.ShipmentNumber = currentMaxId.Value + 1;
else
newShipmentObj.ShipmentNumber = 1; // 1st one
myOrderObj.Shipments.Add(newShipmentObj);
// etc.. rest of EF4 code
Is there a better way?
I don't really like this as I have the following problems because of potential transaction/concurrency issues.
My Order object also has a autoincrement "counter" -- e.g. Order 1, Order 2, Order 3, ... My Order model has properties: "OrderId", "CustomerId", "OrderNumber".
My design is that I have an OrderRepository but not a ShipmentRepository. The ShipmentRepository could query off the Order.Shipment collection... but with Orders, I have to query directly off the dbcontext, e.g.
int? currentMaxId = (_myDbContext)).Orders
.Where(x => x.CustomerId == 123456)
.Select(x => (int?)x.OrderNumber)
.Max();
However, the above part doesn't work well if I attempt to add multiple objects to the DbContext without committing/saving changes to the database. (i.e. the .Where() returns null... and only works if I use DbContext ".Local", which is not what I want.)
Help! Not sure what the best solution would be. Thanks!
you seem to already have shipmentid that is incremental. you can use it for you shipment number and maybe combined with current date as described here: How to implement gapless, user-friendly IDs in NHibernate? what you are trying to do with Max() is evil. Stay away from it as it can cause problems with getting the same shipment numbers for multiple shipments when the load is high