Search in sources :

Example 66 with QueryResults

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

the class QueryTest method testQueriesWithVariableUnificationOnNestedFields.

@Test
public void testQueriesWithVariableUnificationOnNestedFields() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test  \n";
    str += "import org.drools.compiler.Person \n";
    str += "query peeps( String $name, String $likes, String $street) \n";
    str += "    $p : Person( $name := name, $likes := likes, $street := address.street ) \n";
    str += "end\n";
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(str));
    KieSession ksession = createKieSession(kbase);
    Person p1 = new Person("darth", "stilton", 100);
    p1.setAddress(new Address("s1"));
    Person p2 = new Person("yoda", "stilton", 300);
    p2.setAddress(new Address("s2"));
    ksession.insert(p1);
    ksession.insert(p2);
    QueryResults results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, Variable.v });
    assertEquals(2, results.size());
    List names = new ArrayList();
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        names.add(((Person) row.get("$p")).getName());
    }
    assertTrue(names.contains("yoda"));
    assertTrue(names.contains("darth"));
    results = getQueryResults(ksession, "peeps", new Object[] { Variable.v, Variable.v, "s1" });
    assertEquals(1, results.size());
    names = new ArrayList();
    for (org.kie.api.runtime.rule.QueryResultsRow row : results) {
        names.add(((Person) row.get("$p")).getName());
    }
    assertTrue(names.contains("darth"));
}
Also used : Address(org.drools.compiler.Address) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) DomainObject(org.drools.compiler.DomainObject) InsertedObject(org.drools.compiler.InsertedObject) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 67 with QueryResults

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

the class QueryTest method testTwoQuerries.

@Test
public void testTwoQuerries() throws Exception {
    // @see JBRULES-410 More than one Query definition causes an incorrect
    // Rete network to be built.
    KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_TwoQuerries.drl"));
    KieSession session = createKieSession(kbase);
    final Cheese stilton = new Cheese("stinky", 5);
    session.insert(stilton);
    final Person per1 = new Person("stinker", "smelly feet", 70);
    final Person per2 = new Person("skunky", "smelly armpits", 40);
    session.insert(per1);
    session.insert(per2);
    QueryResults results = getQueryResults(session, "find stinky cheeses");
    assertEquals(1, results.size());
    results = getQueryResults(session, "find pensioners");
    assertEquals(1, results.size());
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) 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 68 with QueryResults

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

the class AbductionTest method testQueryAPIs.

@Test
public void testQueryAPIs() {
    String droolsSource = "package org.drools.abductive.test; \n" + "import " + Abducible.class.getName() + "; \n" + "" + "declare Foo " + "   @Abducible " + "   id : String " + "end " + "query foo( String $s ) " + "   @Abductive( target=Foo.class ) \n" + "end \n " + "query bar( String $s, Foo $foo ) " + "   $foo := Foo() " + "end \n " + "rule MoveAround " + "when " + "   $s : String() " + "   $f : foo( $s ; ) " + "   bar( $s, $f ; ) " + "then " + "   delete( $s ); " + "   System.out.println( 'Foo ' + $f ); " + "end " + "";
    // ///////////////////////////////////
    KieSession session = getSessionFromString(droolsSource);
    session.insert("faa");
    session.fireAllRules();
    for (Object o : session.getObjects()) {
        System.out.println(">>> " + o);
    }
    assertEquals(1, session.getObjects().size());
    Query q1 = session.getKieBase().getQuery("org.drools.abductive.test", "foo");
    Query q2 = session.getKieBase().getQuery("org.drools.abductive.test", "bar");
    assertNotNull(q1);
    assertNotNull(q2);
    QueryResults q10res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo"));
    QueryResults q11res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("foo", "foo", Variable.v));
    QueryResults q20res = new FlatQueryResults((QueryResultsImpl) session.getQueryResults("bar", "foo", Variable.v));
    assertEquals(1, q10res.size());
    assertEquals(1, q11res.size());
    assertEquals(1, q20res.size());
    QueryResultsRow row10 = q10res.iterator().next();
    QueryResultsRow row11 = q11res.iterator().next();
    QueryResultsRow row20 = q20res.iterator().next();
    assertEquals("foo", row10.get("$s"));
    assertEquals("foo", row11.get("$s"));
    assertEquals("foo", row20.get("$s"));
    Object foo = row20.get("$foo");
    assertSame(foo, session.getObjects().iterator().next());
    // the implicit return argument, the abduced/retrieved fact, is hidden
    assertNull(row11.get(""));
}
Also used : Query(org.kie.api.definition.rule.Query) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieSession(org.kie.api.runtime.KieSession) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Abducible(org.drools.core.beliefsystem.abductive.Abducible) QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Test(org.junit.Test)

Example 69 with QueryResults

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

the class MoreBatchExecutionTest method testQuery.

@Test
public void testQuery() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);
    ksession.insert(new Cheese("stinky", 5));
    ksession.insert(new Cheese("smelly", 7));
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("Batch execution result is null!", result);
    Object queryResultsObject = result.getValue("numStinkyCheeses");
    assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
    assertEquals(1, ((QueryResults) queryResultsObject).size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 70 with QueryResults

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

the class QueryCommand method execute.

public QueryResults execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    if (this.arguments == null || this.arguments.isEmpty()) {
        this.arguments = Collections.emptyList();
    }
    for (int j = 0; j < arguments.size(); j++) {
        if (arguments.get(j) instanceof Variable) {
            arguments.set(j, Variable.v);
        }
    }
    QueryResults results = ksession.getQueryResults(name, this.arguments.toArray());
    if (this.outIdentifier != null) {
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, new FlatQueryResults((QueryResultsImpl) results));
    }
    return results;
}
Also used : QueryResultsImpl(org.drools.core.QueryResultsImpl) Variable(org.kie.api.runtime.rule.Variable) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) QueryResults(org.kie.api.runtime.rule.QueryResults)

Aggregations

QueryResults (org.kie.api.runtime.rule.QueryResults)79 Test (org.junit.Test)67 KieSession (org.kie.api.runtime.KieSession)58 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 Assertions (org.assertj.core.api.Assertions)9 Relationship (org.drools.modelcompiler.domain.Relationship)9 Collection (java.util.Collection)8 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