Search in sources :

Example 1 with Thing

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");
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.compiler.oopath.model.Thing) Test(org.junit.Test)

Example 2 with Thing

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();
}
Also used : SensorEvent(org.drools.compiler.oopath.model.SensorEvent) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Variable(org.kie.api.runtime.rule.Variable) Test(org.junit.Test) Room(org.drools.compiler.oopath.model.Room) Thing(org.drools.compiler.oopath.model.Thing) ResourceType(org.kie.api.io.ResourceType) Collectors(java.util.stream.Collectors) List(java.util.List) QueryResults(org.kie.api.runtime.rule.QueryResults) StreamSupport(java.util.stream.StreamSupport) KieSession(org.kie.api.runtime.KieSession) KieHelper(org.kie.internal.utils.KieHelper) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.compiler.oopath.model.Thing) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

Thing (org.drools.compiler.oopath.model.Thing)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 KieHelper (org.kie.internal.utils.KieHelper)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Room (org.drools.compiler.oopath.model.Room)1 SensorEvent (org.drools.compiler.oopath.model.SensorEvent)1 ResourceType (org.kie.api.io.ResourceType)1 QueryResults (org.kie.api.runtime.rule.QueryResults)1 Variable (org.kie.api.runtime.rule.Variable)1