Search in sources :

Example 81 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 = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_Query.drl");
    KieSession session = kbase.newKieSession();
    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 82 with QueryResults

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

the class QueryTest method testQueryWithOptionalOr.

@Test
public void testQueryWithOptionalOr() {
    // DROOLS-1386
    String str = "package org.test\n" + "import " + Question.class.getCanonicalName() + "\n" + "import " + QuestionVisible.class.getCanonicalName() + "\n" + "query QuestionsKnowledge\n" + "    $question: Question()\n" + "    $visible: QuestionVisible(question == $question) or not QuestionVisible(question == $question)\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    Question question = new Question();
    ksession.insert(question);
    QueryResults results = ksession.getQueryResults("QuestionsKnowledge");
    assertEquals(1, results.size());
    QueryResultsRow row = results.iterator().next();
    assertSame(question, row.get("$question"));
    QuestionVisible questionVisible = new QuestionVisible(question);
    ksession.insert(questionVisible);
    results = ksession.getQueryResults("QuestionsKnowledge");
    assertEquals(1, results.size());
    row = results.iterator().next();
    assertSame(question, row.get("$question"));
    assertSame(questionVisible, row.get("$visible"));
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 83 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 = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_DoubleQueryWithExists.drl");
    KieSession session = kbase.newKieSession();
    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.mvel.compiler.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 84 with QueryResults

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

the class QueryCepTest method withResultTest.

@Test
public void withResultTest() {
    secondEntryPoint.insert(new TestEvent("minusOne"));
    clock.advanceTime(5, TimeUnit.SECONDS);
    firstEntryPoint.insert(new TestEvent("zero"));
    secondEntryPoint.insert(new TestEvent("one"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    secondEntryPoint.insert(new TestEvent("two"));
    clock.advanceTime(5, TimeUnit.SECONDS);
    secondEntryPoint.insert(new TestEvent("three"));
    QueryResults results = ksession.getQueryResults("ZeroToNineteenSeconds");
    assertEquals(1, results.size());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 85 with QueryResults

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

the class QueryCepTest method withNoResultTest.

@Test
public void withNoResultTest() {
    secondEntryPoint.insert(new TestEvent("minusOne"));
    clock.advanceTime(5, TimeUnit.SECONDS);
    firstEntryPoint.insert(new TestEvent("zero"));
    secondEntryPoint.insert(new TestEvent("one"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    secondEntryPoint.insert(new TestEvent("two"));
    // the following expires event "zero" and "one", causing the query to no longer match
    clock.advanceTime(10, TimeUnit.SECONDS);
    secondEntryPoint.insert(new TestEvent("three"));
    QueryResults results = ksession.getQueryResults("ZeroToNineteenSeconds");
    assertEquals(0, results.size());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)120 Test (org.junit.Test)103 KieSession (org.kie.api.runtime.KieSession)87 ArrayList (java.util.ArrayList)45 KieBase (org.kie.api.KieBase)43 List (java.util.List)35 QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)34 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)22 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Person (org.drools.modelcompiler.domain.Person)21 Arrays (java.util.Arrays)12 Collection (java.util.Collection)12 Assertions (org.assertj.core.api.Assertions)11 Map (java.util.Map)10 Relationship (org.drools.modelcompiler.domain.Relationship)9 Assert.assertEquals (org.junit.Assert.assertEquals)9 IOException (java.io.IOException)8 TimeUnit (java.util.concurrent.TimeUnit)7 ClockType (org.drools.core.ClockType)7 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)7