Search in sources :

Example 1 with ExecutableCommand

use of org.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.

the class PseudoClockRunner method executeQueue.

private void executeQueue(RequestContext ctx) {
    while (!queue.isEmpty()) {
        Batch batch = queue.remove();
        long timeNow = startTime + batch.getDistance();
        for (KieSession ksession : ksessions) {
            // make sure all sessions are set to timeNow
            updateKieSessionTime(timeNow, batch.getDistance(), ksession);
        }
        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(timeNow, batch.getDistance(), 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 2 with ExecutableCommand

use of org.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.

the class StatelessKnowledgeSessionImpl method execute.

public <T> T execute(Command<T> command) {
    StatefulKnowledgeSession ksession = newWorkingMemory();
    RegistryContext context = new ContextImpl().register(KieSession.class, ksession);
    try {
        if (command instanceof BatchExecutionCommand) {
            ((RegistryContext) context).register(ExecutionResultImpl.class, new ExecutionResultImpl());
        }
        ((StatefulKnowledgeSessionImpl) ksession).startBatchExecution();
        Object o = ((ExecutableCommand) command).execute(context);
        // did the user take control of fireAllRules, if not we will auto execute
        boolean autoFireAllRules = true;
        if (command instanceof FireAllRulesCommand) {
            autoFireAllRules = false;
        } else if (command instanceof BatchExecutionCommandImpl) {
            for (Command nestedCmd : ((BatchExecutionCommandImpl) command).getCommands()) {
                if (nestedCmd instanceof FireAllRulesCommand) {
                    autoFireAllRules = false;
                    break;
                }
            }
        }
        if (autoFireAllRules) {
            ksession.fireAllRules();
        }
        if (command instanceof BatchExecutionCommand) {
            return (T) ((RegistryContext) context).lookup(ExecutionResultImpl.class);
        } else {
            return (T) o;
        }
    } finally {
        ((StatefulKnowledgeSessionImpl) ksession).endBatchExecution();
        dispose(ksession);
    }
}
Also used : FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) RegistryContext(org.drools.core.command.impl.RegistryContext) ContextImpl(org.drools.core.command.impl.ContextImpl) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand)

Example 3 with ExecutableCommand

use of org.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.

the class StatelessSessionTest method testInsertObject.

@Test
public void testInsertObject() throws Exception {
    String str = "";
    str += "package org.kie \n";
    str += "import org.drools.compiler.Cheese \n";
    str += "rule rule1 \n";
    str += "  when \n";
    str += "    $c : Cheese() \n";
    str += " \n";
    str += "  then \n";
    str += "    $c.setPrice( 30 ); \n";
    str += "end\n";
    Cheese stilton = new Cheese("stilton", 5);
    final StatelessKieSession ksession = getSession2(ResourceFactory.newByteArrayResource(str.getBytes()));
    final ExecutableCommand cmd = (ExecutableCommand) CommandFactory.newInsert(stilton, "outStilton");
    final BatchExecutionCommandImpl batch = new BatchExecutionCommandImpl(Arrays.asList(new ExecutableCommand<?>[] { cmd }));
    final ExecutionResults result = (ExecutionResults) ksession.execute(batch);
    stilton = (Cheese) result.getValue("outStilton");
    assertEquals(30, stilton.getPrice());
}
Also used : BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) ExecutionResults(org.kie.api.runtime.ExecutionResults) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Cheese(org.drools.compiler.Cheese) Test(org.junit.Test)

Example 4 with ExecutableCommand

use of org.drools.core.command.impl.ExecutableCommand in project drools by kiegroup.

the class AssertEquals method execute.

public Void execute(Context context) {
    Object actualObject = ((ExecutableCommand) command).execute(context);
    if (this.mvelString != null) {
        actualObject = MVELSafeHelper.getEvaluator().eval(this.mvelString, actualObject);
    }
    if (this.expectedIdentifier != null) {
        this.expectedObject = context.get(this.expectedIdentifier);
    }
    Map vars = new HashMap();
    vars.put("expected", expectedObject);
    vars.put("actual", actualObject);
    if (((Boolean) MVELSafeHelper.getEvaluator().eval("expected != actual", vars))) {
        throw new AssertionError(format(this.message, expectedObject, actualObject));
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ExecutableCommand

use of org.drools.core.command.impl.ExecutableCommand 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)

Aggregations

ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)5 Command (org.kie.api.command.Command)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2 KieSession (org.kie.api.runtime.KieSession)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Cheese (org.drools.compiler.Cheese)1 ContextImpl (org.drools.core.command.impl.ContextImpl)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)1 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)1 Test (org.junit.Test)1 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)1 ExecutionResults (org.kie.api.runtime.ExecutionResults)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1