use of org.drools.compiler.oopath.model.Thing in project drools by kiegroup.
the class OOPathTest method testRecursiveOOPathQuery.
@Test
public void testRecursiveOOPathQuery() {
final String drl = "import org.drools.compiler.oopath.model.Thing;\n" + "global java.util.List list\n" + "\n" + "rule \"Print all things contained in the Office\" when\n" + " $office : Thing( name == \"office\" )\n" + " isContainedIn( $office, thing; )\n" + "then\n" + " list.add( thing.getName() );\n" + "end\n" + "\n" + "query isContainedIn( Thing $x, Thing $y )\n" + " $y := /$x/children\n" + "or\n" + " ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" + "end\n";
final Thing house = new Thing("house");
final Thing office = new Thing("office");
house.addChild(office);
final Thing kitchen = new Thing("kitchen");
house.addChild(kitchen);
final Thing knife = new Thing("knife");
kitchen.addChild(knife);
final Thing cheese = new Thing("cheese");
kitchen.addChild(cheese);
final Thing desk = new Thing("desk");
office.addChild(desk);
final Thing chair = new Thing("chair");
office.addChild(chair);
final Thing computer = new Thing("computer");
desk.addChild(computer);
final Thing draw = new Thing("draw");
desk.addChild(draw);
final Thing key = new Thing("key");
draw.addChild(key);
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
final List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(house);
ksession.insert(office);
ksession.insert(kitchen);
ksession.insert(knife);
ksession.insert(cheese);
ksession.insert(desk);
ksession.insert(chair);
ksession.insert(computer);
ksession.insert(draw);
ksession.insert(key);
ksession.fireAllRules();
Assertions.assertThat(list).containsExactlyInAnyOrder("desk", "chair", "key", "draw", "computer");
}
use of org.drools.compiler.oopath.model.Thing in project drools by kiegroup.
the class OOPathQueriesTest method testQueryFromCode.
@Test
public void testQueryFromCode() {
final String drl = "import org.drools.compiler.oopath.model.Thing;\n" + "query isContainedIn( Thing $x, Thing $y )\n" + " $y := /$x/children\n" + "or\n" + " ( $z := /$x/children and isContainedIn( $z, $y; ) )\n" + "end\n";
final Thing smartphone = new Thing("smartphone");
final List<String> itemList = Arrays.asList(new String[] { "display", "keyboard", "processor" });
itemList.stream().map(item -> new Thing(item)).forEach((thing) -> smartphone.addChild(thing));
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.insert(smartphone);
final QueryResults queryResults = ksession.getQueryResults("isContainedIn", new Object[] { smartphone, Variable.v });
final List<String> resultList = StreamSupport.stream(queryResults.spliterator(), false).map(row -> ((Thing) row.get("$y")).getName()).collect(Collectors.toList());
assertThat(resultList).as("Query does not contain all items").containsAll(itemList);
ksession.dispose();
}
Aggregations