Search in sources :

Example 1 with Thing

use of org.drools.mvel.compiler.oopath.model.Thing in project drools by kiegroup.

the class OOPathQueriesTest method testQueryFromCode.

@Test
public void testQueryFromCode() {
    final String drl = "import org.drools.mvel.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));
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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 : Arrays(java.util.Arrays) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Variable(org.kie.api.runtime.rule.Variable) Test(org.junit.Test) Thing(org.drools.mvel.compiler.oopath.model.Thing) Collectors(java.util.stream.Collectors) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) SensorEvent(org.drools.mvel.compiler.oopath.model.SensorEvent) List(java.util.List) Room(org.drools.mvel.compiler.oopath.model.Room) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) StreamSupport(java.util.stream.StreamSupport) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.mvel.compiler.oopath.model.Thing) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 2 with Thing

use of org.drools.mvel.compiler.oopath.model.Thing in project drools by kiegroup.

the class QueryTest method testQueryWithFrom.

@Test
public void testQueryWithFrom() {
    final String drl = "import org.drools.mvel.compiler.oopath.model.Thing;\n" + "query isContainedIn( Thing $x, Thing $y )\n" + "    $y := Thing() from $x.children\n" + "or\n" + "    ( $z := Thing() from $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));
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession();
    ReteDumper.dumpRete(ksession);
    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 : Arrays(java.util.Arrays) InternalFactHandle(org.drools.core.common.InternalFactHandle) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Variable(org.kie.api.runtime.rule.Variable) DroolsQuery(org.drools.core.base.DroolsQuery) DomainObject(org.drools.mvel.compiler.DomainObject) Worker(org.drools.mvel.compiler.Worker) ByteArrayInputStream(java.io.ByteArrayInputStream) InsertedObject(org.drools.mvel.compiler.InsertedObject) QueryResults(org.kie.api.runtime.rule.QueryResults) Map(java.util.Map) Address(org.drools.mvel.compiler.Address) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) QueryListenerOption(org.kie.api.runtime.conf.QueryListenerOption) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Collection(java.util.Collection) ReteDumper(org.drools.core.reteoo.ReteDumper) Set(java.util.Set) Row(org.kie.api.runtime.rule.Row) Thing(org.drools.mvel.compiler.oopath.model.Thing) Collectors(java.util.stream.Collectors) QueryResultsImpl(org.drools.core.QueryResultsImpl) List(java.util.List) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) Cheese(org.drools.mvel.compiler.Cheese) Assert.assertFalse(org.junit.Assert.assertFalse) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) Person(org.drools.mvel.compiler.Person) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) RunWith(org.junit.runner.RunWith) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) Charset(java.nio.charset.Charset) TestName(org.junit.rules.TestName) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Message(org.kie.api.builder.Message) StreamSupport(java.util.stream.StreamSupport) EntryPointNode(org.drools.core.reteoo.EntryPointNode) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) JAXBContext(javax.xml.bind.JAXBContext) Before(org.junit.Before) ViewChangedEventListener(org.kie.api.runtime.rule.ViewChangedEventListener) KieBuilder(org.kie.api.builder.KieBuilder) Iterator(java.util.Iterator) Assert.assertNotNull(org.junit.Assert.assertNotNull) StringWriter(java.io.StringWriter) LiveQuery(org.kie.api.runtime.rule.LiveQuery) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Interval(org.drools.mvel.compiler.Interval) FactHandle(org.kie.api.runtime.rule.FactHandle) KieUtil(org.drools.testcoverage.common.util.KieUtil) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Assert.assertEquals(org.junit.Assert.assertEquals) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.mvel.compiler.oopath.model.Thing) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 3 with Thing

use of org.drools.mvel.compiler.oopath.model.Thing in project drools by kiegroup.

the class OOPathQueriesTest method testRecursiveOOPathQuery.

@Test
public void testRecursiveOOPathQuery() {
    final String drl = "import org.drools.mvel.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);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.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 : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Thing(org.drools.mvel.compiler.oopath.model.Thing) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 Thing (org.drools.mvel.compiler.oopath.model.Thing)3 Test (org.junit.Test)3 KieBase (org.kie.api.KieBase)3 KieSession (org.kie.api.runtime.KieSession)3 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)2 KieBaseUtil (org.drools.testcoverage.common.util.KieBaseUtil)2 TestParametersUtil (org.drools.testcoverage.common.util.TestParametersUtil)2 RunWith (org.junit.runner.RunWith)2 Parameterized (org.junit.runners.Parameterized)2 QueryResults (org.kie.api.runtime.rule.QueryResults)2 Variable (org.kie.api.runtime.rule.Variable)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1