Search in sources :

Example 51 with QueryResults

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

the class QueryTest method testRecursiveQueryWithBatchCommand.

@Test
public void testRecursiveQueryWithBatchCommand() throws Exception {
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "query isContainedIn(String x, String y)\n" + "    Location (x, y;)\n" + "    or\n" + "    ( Location (z, y;) and ?isContainedIn(x, z;))\n" + "end\n" + "declare Location\n" + "    thing : String\n" + "    location : String\n" + "end";
    KieServices kieServices = KieServices.Factory.get();
    KieSession ksession = getKieSession(str);
    FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
    // a pear is in the kitchen
    final Object pear = locationType.newInstance();
    locationType.set(pear, "thing", "pear");
    locationType.set(pear, "location", "kitchen");
    // a desk is in the office
    final Object desk = locationType.newInstance();
    locationType.set(desk, "thing", "desk");
    locationType.set(desk, "location", "office");
    // a flashlight is on the desk
    final Object flashlight = locationType.newInstance();
    locationType.set(flashlight, "thing", "flashlight");
    locationType.set(flashlight, "location", "desk");
    // an envelope is on the desk
    final Object envelope = locationType.newInstance();
    locationType.set(envelope, "thing", "envelope");
    locationType.set(envelope, "location", "desk");
    // a key is in the envelope
    final Object key = locationType.newInstance();
    locationType.set(key, "thing", "key");
    locationType.set(key, "location", "envelope");
    // create working memory objects
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    // Location instances
    commands.add(kieServices.getCommands().newInsert(pear));
    commands.add(kieServices.getCommands().newInsert(desk));
    commands.add(kieServices.getCommands().newInsert(flashlight));
    commands.add(kieServices.getCommands().newInsert(envelope));
    commands.add(kieServices.getCommands().newInsert(key));
    // fire all rules
    final String queryAlias = "myQuery";
    commands.add(kieServices.getCommands().newQuery(queryAlias, "isContainedIn", new Object[] { Variable.v, "office" }));
    final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
    final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
    final List<String> l = new ArrayList<String>();
    for (QueryResultsRow r : qResults) {
        l.add((String) r.get("x"));
    }
    // items in the office should be the following
    Assertions.assertThat(l.size()).isEqualTo(4);
    Assertions.assertThat(l.contains("desk")).isTrue();
    Assertions.assertThat(l.contains("flashlight")).isTrue();
    Assertions.assertThat(l.contains("envelope")).isTrue();
    Assertions.assertThat(l.contains("key")).isTrue();
}
Also used : ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) QueryResults(org.kie.api.runtime.rule.QueryResults) FactType(org.kie.api.definition.type.FactType) Command(org.kie.api.command.Command) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 52 with QueryResults

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

the class QueryTest method testUnificationParameterInPattern.

@Test
public void testUnificationParameterInPattern() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "query personsAges(int ages)\n" + "$p : Person(ages := age)\n" + "end\n";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    QueryResults results = ksession.getQueryResults("personsAges", 41);
    assertEquals(1, results.size());
    Person p = (Person) results.iterator().next().get("$p");
    assertEquals("Mario", p.getName());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 53 with QueryResults

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

the class QueryTest method testQueryOneArgumentWithoutType.

@Test
public void testQueryOneArgumentWithoutType() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "query olderThan( $age )\n" + "    $p : Person(age > (Integer)$age)\n" + "end ";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Person("Mark", 39));
    ksession.insert(new Person("Mario", 41));
    QueryResults results = ksession.getQueryResults("olderThan", 40);
    assertEquals(1, results.size());
    Person p = (Person) results.iterator().next().get("$p");
    assertEquals("Mario", p.getName());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 54 with QueryResults

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

the class JpaPersistenceTraitTest method testTraitWithJPAOnFreshKieBase.

@Test
public void testTraitWithJPAOnFreshKieBase() {
    // DROOLS-904
    String str = "package org.drools.trait.test; " + "global java.util.List list; " + "declare TBean2  " + "  @propertyReactive  " + "  @Traitable  " + "end   " + "declare trait Mask " + "  @propertyReactive  " + "end  " + "query getTraits( Mask $m ) " + "  $m := Mask() " + "end " + "rule Init when then don( new TBean2(), Mask.class ); end " + "rule Trig when String() then don( new TBean2(), Mask.class ); end " + "rule Main when $m : Mask() then list.add( $m ); end ";
    List list = new ArrayList();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    long id = ksession.getIdentifier();
    InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
    kbase2.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase2, null, env);
    ksession.setGlobal("list", list);
    ksession2.insert("go");
    ksession2.fireAllRules();
    assertEquals(2, list.size());
    Class<?> oldProxyClass = list.get(0).getClass();
    Class<?> newProxyClass = list.get(1).getClass();
    assertNotSame(oldProxyClass, newProxyClass);
    QueryResults qry = ksession2.getQueryResults("getTraits", Variable.v);
    assertEquals(2, qry.size());
    java.util.Iterator<QueryResultsRow> iter = qry.iterator();
    int j = 0;
    while (iter.hasNext()) {
        QueryResultsRow row = iter.next();
        Object entry = row.get("$m");
        assertNotNull(entry);
        assertSame(newProxyClass, entry.getClass());
        j++;
    }
    assertEquals(2, j);
    for (Object o : ksession2.getObjects()) {
        if (o.getClass().getName().contains("Mask")) {
            assertSame(newProxyClass, o.getClass());
        }
    }
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) QueryResults(org.kie.api.runtime.rule.QueryResults) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 55 with QueryResults

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

the class PMMLUsageDemoTest method invokePmmlWithTrait.

@Test
public void invokePmmlWithTrait() {
    String extraDrl = "package org.drools.pmml.pmml_4_2.test;" + "" + "import org.drools.core.factmodel.traits.Entity;" + "" + "rule \"Init\" " + "when " + "   $s : String( this == \"trigger\" ) " + "then " + "   System.out.println( \"Trig\" ); " + "   Entity o = new Entity(); " + "   insert( o ); \n" + "" + // both soft and hard fields will be used to feed data into the model
    "" + "   MockColdTrait input = don( o, MockColdTrait.class ); " + "   modify( input ) { " + "       setTemp( 22.0 );" + "   } " + "end " + "" + "" + "rule Log when $x : MockColdTrait() then System.out.println( \"IN \" + $x ); end " + "rule Log2 when $x : Cold() then System.out.println( \"OUT \" + $x ); end ";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ResourceFactory.newClassPathResource(pmmlSource).setResourceType(ResourceType.PMML));
    kfs.write(ResourceFactory.newByteArrayResource(extraDrl.getBytes()).setSourcePath("rules.drl").setResourceType(ResourceType.DRL));
    Results res = ks.newKieBuilder(kfs).buildAll().getResults();
    if (res.hasMessages(Message.Level.ERROR)) {
        System.out.println(res.getMessages(Message.Level.ERROR));
    }
    assertEquals(0, res.getMessages(Message.Level.ERROR).size());
    KieSession kSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    kSession.insert("trigger");
    kSession.fireAllRules();
    System.out.println(reportWMObjects(kSession));
    QueryResults qrs = kSession.getQueryResults("Cold", "MockCold", Variable.v);
    assertTrue(qrs.iterator().hasNext());
    Object val = qrs.iterator().next().get("$result");
    Assert.assertEquals(0.56, val);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) QueryResults(org.kie.api.runtime.rule.QueryResults) KieServices(org.kie.api.KieServices) 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)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