Search in sources :

Example 11 with QueryResultsRow

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

the class QueryTest method testPositionalRecursiveQueryWithUnification.

@Test
public void testPositionalRecursiveQueryWithUnification() {
    String str = "import " + Relationship.class.getCanonicalName() + ";" + "query isRelatedTo(String x, String y)\n" + "    Relationship (x, y;)\n" + "    or\n" + "    ( Relationship (z, y;) and ?isRelatedTo(x, z;))\n" + "end";
    KieSession ksession = getKieSession(str);
    ksession.insert(new Relationship("A", "B"));
    ksession.insert(new Relationship("B", "C"));
    QueryResults results = ksession.getQueryResults("isRelatedTo", "A", "C");
    assertEquals(1, results.size());
    final QueryResultsRow firstResult = results.iterator().next();
    Object resultDrlx = firstResult.get("z");
    assertTrue("B".equals(resultDrlx));
}
Also used : QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) Relationship(org.drools.modelcompiler.domain.Relationship) KieSession(org.kie.api.runtime.KieSession) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 12 with QueryResultsRow

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

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

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

the class UnicodeTest method testQueryCallFromJava.

// test queries in Czech language
@Test
public void testQueryCallFromJava() throws InstantiationException, IllegalAccessException {
    final KieServices kieServices = KieServices.Factory.get();
    final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession ksession = kbase.newKieSession();
    final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Umístění");
    // a pear is in the kitchen
    final Object hruška = locationType.newInstance();
    locationType.set(hruška, "věc", "hruška");
    locationType.set(hruška, "místo", "kuchyně");
    // a desk is in the office
    final Object stůl = locationType.newInstance();
    locationType.set(stůl, "věc", "stůl");
    locationType.set(stůl, "místo", "kancelář");
    // a flashlight is on the desk
    final Object svítilna = locationType.newInstance();
    locationType.set(svítilna, "věc", "svítilna");
    locationType.set(svítilna, "místo", "stůl");
    // an envelope is on the desk
    final Object obálka = locationType.newInstance();
    locationType.set(obálka, "věc", "obálka");
    locationType.set(obálka, "místo", "stůl");
    // a key is in the envelope
    final Object klíč = locationType.newInstance();
    locationType.set(klíč, "věc", "klíč");
    locationType.set(klíč, "místo", "obálka");
    // create working memory objects
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    // Location instances
    commands.add(kieServices.getCommands().newInsert(hruška));
    commands.add(kieServices.getCommands().newInsert(stůl));
    commands.add(kieServices.getCommands().newInsert(svítilna));
    commands.add(kieServices.getCommands().newInsert(obálka));
    commands.add(kieServices.getCommands().newInsert(klíč));
    // fire all rules
    final String queryAlias = "obsaženo";
    commands.add(kieServices.getCommands().newQuery(queryAlias, "jeObsažen", new Object[] { Variable.v, "kancelář" }));
    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("stůl")).isTrue();
    Assertions.assertThat(l.contains("svítilna")).isTrue();
    Assertions.assertThat(l.contains("obálka")).isTrue();
    Assertions.assertThat(l.contains("klíč")).isTrue();
}
Also used : ExecutionResults(org.kie.api.runtime.ExecutionResults) Resource(org.kie.api.io.Resource) 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) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 15 with QueryResultsRow

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

the class ConcurrentBasesParallelTest method testQueries.

@Test(timeout = 20000)
public void testQueries() throws InterruptedException {
    final int numberOfObjects = 100;
    final TestExecutor exec = counter -> {
        final String query = "import " + BeanA.class.getCanonicalName() + ";\n" + "query Query " + "    bean : BeanA( seed == " + counter + " ) " + "end";
        final KieBase base = getKieBase(query);
        final KieSession session = base.newKieSession();
        try {
            final BeanA bean = new BeanA(counter);
            session.insert(bean);
            for (int i = 0; i < numberOfObjects; i++) {
                if (i != counter) {
                    session.insert(new BeanA(i));
                }
            }
            final QueryResults results = session.getQueryResults("Query");
            Assertions.assertThat(results).hasSize(1);
            for (final QueryResultsRow row : results) {
                Assertions.assertThat(row.get("bean")).isEqualTo(bean);
            }
            return true;
        } finally {
            session.dispose();
        }
    };
    parallelTest(NUMBER_OF_THREADS, exec);
}
Also used : Arrays(java.util.Arrays) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) BeanA(org.drools.compiler.integrationtests.facts.BeanA) BeanB(org.drools.compiler.integrationtests.facts.BeanB) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Parameterized(org.junit.runners.Parameterized) BeanA(org.drools.compiler.integrationtests.facts.BeanA) 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) Test(org.junit.Test)

Aggregations

QueryResultsRow (org.kie.api.runtime.rule.QueryResultsRow)18 QueryResults (org.kie.api.runtime.rule.QueryResults)16 KieSession (org.kie.api.runtime.KieSession)13 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)12 List (java.util.List)8 KieBase (org.kie.api.KieBase)7 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)3 Cheese (org.drools.compiler.Cheese)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 Person (org.drools.modelcompiler.domain.Person)2 KieServices (org.kie.api.KieServices)2 Command (org.kie.api.command.Command)2 FactType (org.kie.api.definition.type.FactType)2 ExecutionResults (org.kie.api.runtime.ExecutionResults)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1