Search in sources :

Example 11 with RegistryContext

use of org.kie.internal.command.RegistryContext in project drools by kiegroup.

the class NewContextCommand method execute.

@Override
public Void execute(Context context) {
    Context returned = ((RegistryContext) context).getContextManager().createContext(name);
    ((RequestContextImpl) context).setApplicationContext(returned);
    return null;
}
Also used : Context(org.kie.api.runtime.Context) RegistryContext(org.kie.internal.command.RegistryContext) RequestContextImpl(org.drools.core.command.RequestContextImpl)

Example 12 with RegistryContext

use of org.kie.internal.command.RegistryContext 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) {
            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) 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.kie.internal.command.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.kie.api.command.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.kie.api.command.ExecutableCommand)

Example 13 with RegistryContext

use of org.kie.internal.command.RegistryContext in project drools by kiegroup.

the class AddCoverageListenerCommand method execute.

@Override
public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ksession.addEventListener(coverageAgendaListener);
    return null;
}
Also used : KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.kie.internal.command.RegistryContext)

Example 14 with RegistryContext

use of org.kie.internal.command.RegistryContext in project drools by kiegroup.

the class RuleStatefulScenarioExecutableBuilder method run.

@Override
public Map<String, Object> run() {
    Objects.requireNonNull(executableBuilder, "Executable builder is null, please invoke create(KieContainer, )");
    CoverageAgendaListener coverageAgendaListener = new CoverageAgendaListener();
    kieSessionFluent.addCommand(new AddCoverageListenerCommand(coverageAgendaListener));
    kieSessionFluent.addCommand(context -> {
        KieSession kieSession = ((RegistryContext) context).lookup(KieSession.class);
        return getAvailableRules(kieSession.getKieBase(), agendaGroupName);
    }).out(RULES_AVAILABLE);
    kieSessionFluent.fireAllRules();
    internalConditions.values().forEach(factToCheck -> kieSessionFluent.addCommand(new ValidateFactCommand(factToCheck)));
    kieSessionFluent.dispose().end();
    RequestContext execute = createExecutableRunner().execute(executableBuilder.getExecutable());
    Map<String, Object> toReturn = new HashMap<>(execute.getOutputs());
    toReturn.put(COVERAGE_LISTENER, coverageAgendaListener);
    return toReturn;
}
Also used : KieSessionFluent(org.kie.internal.builder.fluent.KieSessionFluent) BiFunction(java.util.function.BiFunction) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) KieContainer(org.kie.api.runtime.KieContainer) RegistryContext(org.kie.internal.command.RegistryContext) HashMap(java.util.HashMap) Function(java.util.function.Function) ScenarioException(org.drools.scenariosimulation.backend.runner.ScenarioException) ArrayList(java.util.ArrayList) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Objects(java.util.Objects) List(java.util.List) ExecutableBuilder(org.kie.internal.builder.fluent.ExecutableBuilder) RequestContext(org.kie.api.runtime.RequestContext) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) Map(java.util.Map) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) KieSession(org.kie.api.runtime.KieSession) HashMap(java.util.HashMap) KieSession(org.kie.api.runtime.KieSession) RequestContext(org.kie.api.runtime.RequestContext)

Example 15 with RegistryContext

use of org.kie.internal.command.RegistryContext in project drools by kiegroup.

the class ModifyCommand method execute.

public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint wmep = ksession.getEntryPoint(factHandle.getEntryPointName());
    Object object = wmep.getObject(this.factHandle);
    CoreComponentsBuilder.get().getMVELExecutor().eval(getMvelExpr(), object);
    wmep.update(factHandle, object);
    return object;
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.kie.internal.command.RegistryContext)

Aggregations

RegistryContext (org.kie.internal.command.RegistryContext)126 KieSession (org.kie.api.runtime.KieSession)106 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)37 Context (org.kie.api.runtime.Context)27 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)27 ArrayList (java.util.ArrayList)17 FactHandle (org.kie.api.runtime.rule.FactHandle)16 Test (org.junit.Test)14 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)13 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)12 EntryPoint (org.kie.api.runtime.rule.EntryPoint)12 List (java.util.List)11 Map (java.util.Map)11 HashMap (java.util.HashMap)10 ProcessInstanceNotFoundException (org.jbpm.services.api.ProcessInstanceNotFoundException)9 ExecutableCommand (org.kie.api.command.ExecutableCommand)9 WorkflowProcessInstance (org.jbpm.workflow.instance.WorkflowProcessInstance)8 NodeInstance (org.kie.api.runtime.process.NodeInstance)8 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)8 Collection (java.util.Collection)7