Search in sources :

Example 36 with Command

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

the class PseudoClockRunner method executeBatch.

private void executeBatch(Batch batch, RequestContext ctx) {
    // everything else must be handled by a priority queue and timer afterwards.
    for (Command cmd : batch.getCommands()) {
        Object returned = ((ExecutableCommand) cmd).execute(ctx);
        if (returned != null) {
            ctx.setResult(returned);
            if (returned instanceof KieSession) {
                KieSession ksession = (KieSession) returned;
                // make sure all sessions are set to timeNow
                updateKieSessionTime(startTime, 0, ksession);
                ksessions.add((KieSession) returned);
            }
        }
    }
}
Also used : ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) KieSession(org.kie.api.runtime.KieSession)

Example 37 with Command

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

the class UnicodeInCSVTest method testUnicodeCSVDecisionTable.

@Test
public void testUnicodeCSVDecisionTable() throws FileNotFoundException {
    DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dtconf.setInputType(DecisionTableInputType.CSV);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("unicode.csv", getClass()), ResourceType.DTABLE, dtconf);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors().toString());
        System.out.println(DecisionTableFactory.loadFromInputStream(getClass().getResourceAsStream("unicode.xls"), dtconf));
        fail("Cannot build CSV decision table containing utf-8 characters\n" + kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    List<Command<?>> commands = new ArrayList<Command<?>>();
    List<Člověk> dospělí = new ArrayList<Člověk>();
    commands.add(CommandFactory.newSetGlobal("dospělí", dospělí));
    Člověk Řehoř = new Člověk();
    Řehoř.setVěk(30);
    Řehoř.setJméno("Řehoř");
    commands.add(CommandFactory.newInsert(Řehoř));
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    // people with age greater than 18 should be added to list of adults
    assertNotNull(kbase.getRule("org.drools.decisiontable", "přidej k dospělým"));
    assertEquals(dospělí.size(), 5);
    assertEquals(dospělí.iterator().next().getJméno(), "Řehoř");
    assertNotNull(kbase.getRule("org.drools.decisiontable", "привет мир"));
    assertNotNull(kbase.getRule("org.drools.decisiontable", "你好世界"));
    assertNotNull(kbase.getRule("org.drools.decisiontable", "hallå världen"));
    assertNotNull(kbase.getRule("org.drools.decisiontable", "مرحبا العالم"));
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Command(org.kie.api.command.Command) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 38 with Command

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

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