Search in sources :

Example 11 with Command

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

the class DisposeCommandPublicAPITest method testDisposeCommand.

@Test
public void testDisposeCommand() {
    InternalKnowledgeBase kBase;
    RuleImpl rule;
    InternalKnowledgePackage pkg;
    kBase = KnowledgeBaseFactory.newKnowledgeBase();
    pkg = new KnowledgePackageImpl("org.droos.test");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    rule = new RuleImpl("Test");
    rule.setDialect("java");
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
        }

        public String getName() {
            return "default";
        }
    });
    pkg.addRule(rule);
    kBase.addPackage(pkg);
    KieSession session = kBase.newKieSession();
    Command dispose = KieServices.Factory.get().getCommands().newDispose();
    session.insert("whatever");
    session.fireAllRules();
    session.execute(dispose);
    try {
        session.insert("whatever");
    } catch (Exception e) {
        Assert.assertEquals(e.getMessage(), "Illegal method call. This session was previously disposed.");
    }
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Command(org.kie.api.command.Command) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 12 with Command

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

the class SimpleBatchExecutionTest method testSetGlobalCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testSetGlobalCommand() throws Exception {
    ksession.insert(new Integer(5));
    ksession.insert(new Integer(7));
    ksession.fireAllRules();
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newSetGlobal("globalCheeseCountry", "France", true));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull(result);
    Object global = result.getValue("globalCheeseCountry");
    assertNotNull(global);
    assertEquals("France", global);
}
Also used : Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with Command

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

the class SimpleBatchExecutionTest method testGetObjectsCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetObjectsCommand() 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 object = ksession.getObject(handle_1);
    assertNotNull(object);
    assertEquals(expected_1, object);
    object = ksession.getObject(handle_2);
    assertNotNull(object);
    assertEquals(expected_2, object);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetObjects("out_list"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("GetObjectsCommand result is null!", result);
    List<Object> objectList = (List) result.getValue("out_list");
    assertTrue("Retrieved object list is null or empty!", objectList != null && !objectList.isEmpty());
    Collection<? extends Object> factList = ksession.getObjects();
    Object[] expectedArr = { expected_1, expected_2 };
    List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr));
    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 : FactHandle(org.kie.api.runtime.rule.FactHandle) Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with Command

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

the class SimpleBatchExecutionTest method testGetGlobalCommand.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testGetGlobalCommand() throws Exception {
    ksession.insert(new Integer(5));
    ksession.insert(new Integer(7));
    ksession.fireAllRules();
    ksession.setGlobal("globalCheeseCountry", "France");
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newGetGlobal("globalCheeseCountry", "cheeseCountry"));
    Command cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("GetGlobalCommand result is null!", result);
    Object global = result.getValue("cheeseCountry");
    assertNotNull("Retrieved global fact is null!", global);
    assertEquals("Retrieved global is not equal to 'France'.", "France", global);
}
Also used : Command(org.kie.api.command.Command) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with Command

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

the class MoreBatchExecutionTest method testFireAllRules.

@Test
public void testFireAllRules() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/drl/test_ImportFunctions.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);
    final Cheese cheese = new Cheese("stilton", 15);
    ksession.insert(cheese);
    List<?> list = new ArrayList();
    ksession.setGlobal("list", list);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newFireAllRules("fired"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("Batch execution result is null!", result);
    Object firedObject = result.getValue("fired");
    assertTrue("Retrieved object is null or incorrect!", firedObject != null && firedObject instanceof Integer);
    assertEquals(4, firedObject);
    list = (List<?>) ksession.getGlobal("list");
    assertEquals(4, list.size());
    assertEquals("rule1", list.get(0));
    assertEquals("rule2", list.get(1));
    assertEquals("rule3", list.get(2));
    assertEquals("rule4", list.get(3));
}
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) 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