Search in sources :

Example 86 with QueryResults

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

the class QueryCepTest method noResultTest.

@Test
public void noResultTest() {
    QueryResults results = ksession.getQueryResults("EventsFromStream");
    assertEquals(0, results.size());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 87 with QueryResults

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

the class QueryCepFireUntilHaltTest method noResultTest.

@Test(timeout = 10000L)
public void noResultTest() {
    QueryResults results = ksession.getQueryResults("EventsFromStream");
    assertEquals(0, results.size());
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 88 with QueryResults

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

the class QueryCepFireUntilHaltTest method withResultTest.

@Test(timeout = 10000L)
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 89 with QueryResults

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

the class PatternBuilderForQueryTest method testQuery.

@Test
public void testQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$type");
    packageDescr.addRule(queryDescr);
    final AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == $type"));
    // Another query, no parameters
    QueryDescr queryDescr2 = new QueryDescr("query2");
    packageDescr.addRule(queryDescr2);
    AndDescr lhs2 = new AndDescr();
    queryDescr2.setLhs(lhs2);
    PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs2.addDescr(pattern2);
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
    final KieSession session = kbase.newKieSession();
    session.insert(new Cheese("stilton", 15));
    QueryResults results = session.getQueryResults("query1", "stilton");
    assertEquals(1, results.size());
    Object object = results.iterator().next().get("stilton");
    assertEquals(new Cheese("stilton", 15), object);
    results = session.getQueryResults("query1", new Object[] { "cheddar" });
    assertEquals(0, results.size());
    session.insert(new Cheese("dolcelatte", 20));
    results = session.getQueryResults("query2", new Object[] {});
    assertEquals(2, results.size());
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 90 with QueryResults

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

the class BackwardChainingTest method testQueryWithOr.

@Test(timeout = 10000)
public void testQueryWithOr() {
    final String drl = "" + "package org.drools.compiler.test  \n" + "import java.util.List\n" + "import java.util.ArrayList\n" + "import " + BackwardChainingTest.class.getName() + ".Q\n" + "import " + BackwardChainingTest.class.getName() + ".R\n" + "import " + BackwardChainingTest.class.getName() + ".S\n" + "global List list\n" + "dialect \"mvel\"\n" + "\n" + "query q(int x)\n" + "    Q( x := value )\n" + "end\n" + "\n" + "query r(int x)\n" + "    R( x := value )\n" + "end\n" + "\n" + "query s(int x)\n" + "    S( x := value )    \n" + "end\n" + "\n" + "query p(int x)\n" + "    (?q(x;) and ?r(x;) ) \n" + "    or\n" + "    ?s(x;)\n" + "end\n" + "rule init when\n" + "then\n" + " insert( new Q(1) );\n " + " insert( new Q(5) );\n " + " insert( new Q(6) );\n " + " insert( new R(1) );\n " + " insert( new R(4) );\n " + " insert( new R(6) );\n " + " insert( new R(2) );\n " + " insert( new S(2) );\n " + " insert( new S(3) );\n " + " insert( new S(6) );\n " + "end\n" + "";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("backward-chaining-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List<Integer> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        ksession.fireAllRules();
        QueryResults results;
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 0 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(0, list.size());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 1 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(1, list.size());
        assertEquals(1, list.get(0).intValue());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 2 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(1, list.size());
        assertEquals(2, list.get(0).intValue());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 3 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(1, list.size());
        assertEquals(3, list.get(0).intValue());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 4 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(0, list.size());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 5 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(0, list.size());
        list.clear();
        results = ksession.getQueryResults("p", new Integer[] { 6 });
        for (final QueryResultsRow result : results) {
            list.add((Integer) result.get("x"));
        }
        assertEquals(2, list.size());
        assertEquals(6, list.get(0).intValue());
        assertEquals(6, list.get(1).intValue());
    } finally {
        ksession.dispose();
    }
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) 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