Search in sources :

Example 31 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class SimpleBatchExecutionTest method testGetObjectCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectCommand() throws Exception {
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    FactHandle handle_1 = ksession.insert(expected_1);
    FactHandle handle_2 = ksession.insert(expected_2);
    ksession.fireAllRules();
    Object fact = ksession.getObject(handle_1);
    assertNotNull(fact);
    assertEquals(expected_1, fact);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetObject(handle_1, "out_1"));
    commands.add(CommandFactory.newGetObject(handle_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("GetObjectCommand result is null!", result);
    assertEquals(expected_1, result.getValue("out_1"));
    assertEquals(expected_2, result.getValue("out_2"));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 32 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class SimpleBatchExecutionTest method testInsertObjectCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInsertObjectCommand() throws Exception {
    String expected_1 = "expected_1";
    String expected_2 = "expected_2";
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newInsert(expected_1, "out_1"));
    commands.add(CommandFactory.newInsert(expected_2, "out_2"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    Object fact_1 = result.getValue("out_1");
    assertNotNull(fact_1);
    Object fact_2 = result.getValue("out_2");
    assertNotNull(fact_2);
    ksession.fireAllRules();
    Object[] expectedArr = { expected_1, expected_2 };
    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) Test(org.junit.Test)

Example 33 with Command

use of org.kie.api.command.Command 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 34 with Command

use of org.kie.api.command.Command 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 35 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class TraitTest method traitsInBatchExecution.

// BZ #748752
@Test(timeout = 10000)
public void traitsInBatchExecution() {
    String str = "package org.jboss.qa.brms.traits\n" + "import org.drools.compiler.Person;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "global java.util.List list;" + "" + "declare Person \n" + "  @Traitable \n" + "end \n" + "" + "declare trait Student\n" + "  school : String\n" + "end\n" + "\n" + "rule \"create student\" \n" + "  when\n" + "    $student : Person( age < 26 )\n" + "  then\n" + "    Student s = don( $student, Student.class );\n" + "    s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + "  when\n" + "    student : Person( this isA Student )\n" + "  then" + "    list.add( 1 );\n" + "    System.out.println(\"Person is a student: \" + student);\n" + "end\n" + "\n" + "rule \"print school\"\n" + "  when\n" + "    Student( $school : school )\n" + "  then\n" + "    list.add( 2 );\n" + "    System.out.println(\"Student is attending \" + $school);\n" + "end";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    List list = new ArrayList();
    KieBase kbase = kbuilder.newKieBase();
    TraitFactory.setMode(mode, kbase);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person student = new Person("student", 18);
    commands.add(CommandFactory.newInsert(student));
    System.out.println("Starting execution...");
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    System.out.println("Finished...");
    assertEquals(2, list.size());
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) List(java.util.List) ArrayList(java.util.ArrayList) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Command (org.kie.api.command.Command)38 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)34 ExecutionResults (org.kie.api.runtime.ExecutionResults)18 KieSession (org.kie.api.runtime.KieSession)14 Cheese (org.drools.compiler.Cheese)10 KieBase (org.kie.api.KieBase)9 KieServices (org.kie.api.KieServices)9 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)7 Resource (org.kie.api.io.Resource)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 List (java.util.List)5 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)5 KieSessionTest (org.drools.testcoverage.common.KieSessionTest)4 FactType (org.kie.api.definition.type.FactType)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 QueryResults (org.kie.api.runtime.rule.QueryResults)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2