Search in sources :

Example 11 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testQueryWithCollect.

@Test
public void testQueryWithCollect() throws Exception {
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_Query.drl"));
    KieSession session = createKieSession(kbase);
    session.fireAllRules();
    QueryResults results = getQueryResults(session, "collect objects");
    assertEquals(1, results.size());
    final QueryResultsRow row = results.iterator().next();
    final List list = (List) row.get("$list");
    assertEquals(2, list.size());
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 12 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method runQueryListenerTest.

public void runQueryListenerTest(QueryListenerOption option) throws IOException, ClassNotFoundException {
    String str = "";
    str += "package org.drools.compiler.integrationtests\n";
    str += "import " + Cheese.class.getCanonicalName() + " \n";
    str += "query cheeses(String $type) \n";
    str += "    $cheese : Cheese(type == $type) \n";
    str += "end\n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKieSession(kbase, option);
    // insert some data into the session
    for (int i = 0; i < 10000; i++) {
        ksession.insert(new Cheese(i % 2 == 0 ? "stilton" : "brie"));
    }
    // query the session
    List<Cheese> cheeses;
    for (int i = 0; i < 100; i++) {
        org.kie.api.runtime.rule.QueryResults queryResults = ksession.getQueryResults("cheeses", new Object[] { "stilton" });
        cheeses = new ArrayList<Cheese>();
        for (QueryResultsRow row : queryResults) {
            cheeses.add((Cheese) row.get("$cheese"));
        }
        assertEquals(5000, cheeses.size());
    }
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession)

Example 13 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest method testDoubleQueryWithExists.

@Test
public void testDoubleQueryWithExists() throws Exception {
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_DoubleQueryWithExists.drl"));
    KieSession session = createKieSession(kbase);
    final Person p1 = new Person("p1", "stilton", 20);
    p1.setStatus("europe");
    final FactHandle c1FactHandle = session.insert(p1);
    final Person p2 = new Person("p2", "stilton", 30);
    p2.setStatus("europe");
    final FactHandle c2FactHandle = session.insert(p2);
    final Person p3 = new Person("p3", "stilton", 40);
    p3.setStatus("europe");
    final FactHandle c3FactHandle = session.insert(p3);
    session.fireAllRules();
    QueryResults results = session.getQueryResults("2 persons with the same status");
    assertEquals(2, results.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p3.setStatus("america");
    session.update(c3FactHandle, p3);
    session.fireAllRules();
    results = session.getQueryResults("2 persons with the same status");
    assertEquals(1, results.size());
    // europe=[ 1 ], america=[ 2, 3 ]
    p2.setStatus("america");
    session.update(c2FactHandle, p2);
    session.fireAllRules();
    results = session.getQueryResults("2 persons with the same status");
    assertEquals(1, results.size());
    // europe=[ ], america=[ 1, 2, 3 ]
    p1.setStatus("america");
    session.update(c1FactHandle, p1);
    session.fireAllRules();
    results = getQueryResults(session, "2 persons with the same status");
    assertEquals(2, results.size());
    // europe=[ 2 ], america=[ 1, 3 ]
    p2.setStatus("europe");
    session.update(c2FactHandle, p2);
    session.fireAllRules();
    results = getQueryResults(session, "2 persons with the same status");
    assertEquals(1, results.size());
    // europe=[ 1, 2 ], america=[ 3 ]
    p1.setStatus("europe");
    session.update(c1FactHandle, p1);
    session.fireAllRules();
    results = session.getQueryResults("2 persons with the same status");
    assertEquals(1, results.size());
    // europe=[ 1, 2, 3 ], america=[ ]
    p3.setStatus("europe");
    session.update(c3FactHandle, p3);
    session.fireAllRules();
    results = session.getQueryResults("2 persons with the same status");
    assertEquals(2, results.size());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 14 with QueryResults

use of org.kie.api.runtime.rule.QueryResults in project drools by kiegroup.

the class QueryTest3 method doIt.

private void doIt(Object o1, Object o2, String query, int expected, boolean doUpdate, boolean doRetract) {
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    try {
        knowledgeSession.insert(o1);
        FactHandle handle2 = knowledgeSession.insert(o2);
        if (doUpdate) {
            knowledgeSession.update(handle2, o2);
        } else if (doRetract) {
            knowledgeSession.retract(handle2);
            handle2 = knowledgeSession.insert(o2);
        }
        QueryResults queryResults = knowledgeSession.getQueryResults(query);
        assertEquals(expected, queryResults.size());
    } finally {
        knowledgeSession.dispose();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults)

Example 15 with QueryResults

use of org.kie.api.runtime.rule.QueryResults 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

QueryResults (org.kie.api.runtime.rule.QueryResults)80 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)59 ArrayList (java.util.ArrayList)30 KieBase (org.kie.api.KieBase)25 List (java.util.List)24 Person (org.drools.modelcompiler.domain.Person)19 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 FactHandle (org.kie.api.runtime.rule.FactHandle)15 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)10 Collection (java.util.Collection)9 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Map (java.util.Map)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 InternationalAddress (org.drools.modelcompiler.oopathdtables.InternationalAddress)6 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)6 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)6 Global (org.drools.model.Global)5