Search in sources :

Example 26 with ExecutionResults

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

the class SimpleBatchExecutionTest method testInsertElementsCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertElementsCommand() throws Exception {
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    Object[] expectedArr = { expected_1, expected_2 };
    Collection<Object> factCollection = Arrays.asList(expectedArr);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsertElements(factCollection, "out_list", true, null));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    Collection<? extends Object> outList = (Collection<? extends Object>) result.getValue("out_list");
    assertNotNull(outList);
    ksession.fireAllRules();
    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    Collection<? extends Object> factList = ksession.getObjects();
    assertTrue("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size());
    for (Object fact : factList) {
        if (expectedList.contains(fact)) {
            expectedList.remove(fact);
        }
    }
    assertTrue("Retrieved object list did not contain expected objects.", expectedList.isEmpty());
}
Also used : Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Test(org.junit.Test)

Example 27 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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 28 with ExecutionResults

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

the class StatelessSessionTest method testQuery.

@Test
public void testQuery() throws Exception {
    String str = "";
    str += "package org.kie.test  \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "query cheeses \n";
    str += "    stilton : Cheese(type == 'stilton') \n";
    str += "    cheddar : Cheese(type == 'cheddar', price == stilton.price) \n";
    str += "end\n";
    final 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());
    kbase = SerializationHelper.serializeObject(kbase);
    final StatelessKieSession ksession = kbase.newStatelessKieSession();
    final Cheese stilton1 = new Cheese("stilton", 1);
    final Cheese cheddar1 = new Cheese("cheddar", 1);
    final Cheese stilton2 = new Cheese("stilton", 2);
    final Cheese cheddar2 = new Cheese("cheddar", 2);
    final Cheese stilton3 = new Cheese("stilton", 3);
    final Cheese cheddar3 = new Cheese("cheddar", 3);
    final Set set = new HashSet();
    List list = new ArrayList();
    list.add(stilton1);
    list.add(cheddar1);
    set.add(list);
    list = new ArrayList();
    list.add(stilton2);
    list.add(cheddar2);
    set.add(list);
    list = new ArrayList();
    list.add(stilton3);
    list.add(cheddar3);
    set.add(list);
    final List<Command> cmds = new ArrayList<Command>();
    cmds.add(CommandFactory.newInsert(stilton1));
    cmds.add(CommandFactory.newInsert(stilton2));
    cmds.add(CommandFactory.newInsert(stilton3));
    cmds.add(CommandFactory.newInsert(cheddar1));
    cmds.add(CommandFactory.newInsert(cheddar2));
    cmds.add(CommandFactory.newInsert(cheddar3));
    cmds.add(CommandFactory.newQuery("cheeses", "cheeses"));
    final ExecutionResults batchResult = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
    final org.kie.api.runtime.rule.QueryResults results = (org.kie.api.runtime.rule.QueryResults) batchResult.getValue("cheeses");
    assertEquals(3, results.size());
    assertEquals(2, results.getIdentifiers().length);
    final Set newSet = new HashSet();
    for (final org.kie.api.runtime.rule.QueryResultsRow result : results) {
        list = new ArrayList();
        list.add(result.get("stilton"));
        list.add(result.get("cheddar"));
        newSet.add(list);
    }
    assertEquals(set, newSet);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project jbpm by kiegroup.

the class BusinessRuleTaskHandler method handleStateless.

protected void handleStateless(WorkItem workItem, String kieSessionName, Map<String, Object> parameters, Map<String, Object> results) {
    logger.debug("Evalating rules in stateless session with name {}", kieSessionName);
    StatelessKieSession kieSession = kieContainer.newStatelessKieSession(kieSessionName);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    for (Entry<String, Object> entry : parameters.entrySet()) {
        String inputKey = workItem.getId() + "_" + entry.getKey();
        commands.add(commandsFactory.newInsert(entry.getValue(), inputKey, true, null));
    }
    commands.add(commandsFactory.newFireAllRules("Fired"));
    BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands);
    ExecutionResults executionResults = kieSession.execute(executionCommand);
    logger.debug("{} rules fired", executionResults.getValue("Fired"));
    for (Entry<String, Object> entry : parameters.entrySet()) {
        String inputKey = workItem.getId() + "_" + entry.getKey();
        String key = entry.getKey().replaceAll(workItem.getId() + "_", "");
        results.put(key, executionResults.getValue(inputKey));
    }
}
Also used : Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) StatelessKieSession(org.kie.api.runtime.StatelessKieSession)

Example 30 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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)

Aggregations

ExecutionResults (org.kie.api.runtime.ExecutionResults)41 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)37 Command (org.kie.api.command.Command)35 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)17 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)11 Cheese (org.drools.compiler.Cheese)10 Cheese (org.drools.mvel.compiler.Cheese)10 List (java.util.List)9 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 FactHandle (org.kie.api.runtime.rule.FactHandle)5 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 QueryResults (org.kie.api.runtime.rule.QueryResults)4 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)3 KieServices (org.kie.api.KieServices)3 ExecutableCommand (org.kie.api.command.ExecutableCommand)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3