Search in sources :

Example 31 with RegistryContext

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

the class DeleteCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    ksession.getEntryPoint(handle.getEntryPointId()).delete(handle, fhState);
    return null;
}
Also used : KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 32 with RegistryContext

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

the class DeleteObjectCommand method execute.

public Void execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    EntryPoint ep = ksession.getEntryPoint(entryPoint);
    if (ep != null) {
        FactHandle handle = ksession.getEntryPoint(entryPoint).getFactHandle(object);
        ksession.delete(handle);
    }
    return null;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 33 with RegistryContext

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

the class GetObjectCommand method execute.

public Object execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    FactHandle factHandle = this.factHandle;
    if (factHandle == null) {
        factHandle = this.disconnectedFactHandle;
    }
    Object object = ksession.getObject(factHandle);
    if (this.outIdentifier != null) {
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, object);
    }
    return object;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) DisconnectedFactHandle(org.drools.core.common.DisconnectedFactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 34 with RegistryContext

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

the class InsertElementsCommand method execute.

public Collection<FactHandle> execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    List<FactHandle> handles = new ArrayList<FactHandle>();
    EntryPoint wmep;
    if (StringUtils.isEmpty(this.entryPoint)) {
        wmep = ksession;
    } else {
        wmep = ksession.getEntryPoint(this.entryPoint);
    }
    for (Object object : objects) {
        handles.add(wmep.insert(object));
    }
    if (outIdentifier != null) {
        if (this.returnObject) {
            ((RegistryContext) context).lookup(ExecutionResultImpl.class).setResult(this.outIdentifier, objects);
        }
        ((RegistryContext) context).lookup(ExecutionResultImpl.class).getFactHandles().put(this.outIdentifier, handles);
    }
    return handles;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ExecutionResultImpl(org.drools.core.runtime.impl.ExecutionResultImpl) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext)

Example 35 with RegistryContext

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

the class TransactionTestCommand method execute.

public Void execute(Context context) {
    em.joinTransaction();
    em.persist(mainObject);
    if (subObject != null) {
        KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
        // THe following 3 lines are the important ones! (See below for an explanation)
        InternalKnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase();
        cleanKBase.addPackages(ksession.getKieBase().getKiePackages());
        StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession(cleanKBase, null, initializeEnvironment());
        /**
         *  Here's what's going on:
         *  If the PersistableRunner (SSCS) & JtaTransactionManager (JTM) were _not_ aware of transactions,
         *  -> then inserting the mainObject _before_ having inserted the subObject
         *     would cause the operation/persist to fail and the transaction to fail.
         *     - This is because the mainObject contains a foreign key referring to the subObject.
         *
         *  However, the SSCS & JTM check whether or not they're owners of the transaction
         *  when starting and when committing the transaction they use.
         *  -> So that when we insert the mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession),
         *     it does _not_ mess with the transaction state and the operation succeeds.
         */
        TransactionTestCommand transactionTestSubCommand = new TransactionTestCommand(this.subObject, getPersistenceEnvironment());
        commandKSession.execute(transactionTestSubCommand);
    }
    return null;
}
Also used : StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) KieSession(org.kie.api.runtime.KieSession) RegistryContext(org.drools.core.command.impl.RegistryContext) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

RegistryContext (org.drools.core.command.impl.RegistryContext)58 KieSession (org.kie.api.runtime.KieSession)52 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)20 EntryPoint (org.kie.api.runtime.rule.EntryPoint)12 FactHandle (org.kie.api.runtime.rule.FactHandle)10 ArrayList (java.util.ArrayList)8 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)7 InternalFactHandle (org.drools.core.common.InternalFactHandle)6 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)4 HashMap (java.util.HashMap)3 RequestContextImpl (org.drools.core.command.RequestContextImpl)3 Context (org.kie.api.runtime.Context)3 KieContainer (org.kie.api.runtime.KieContainer)3 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 EntryPointCreator (org.drools.core.command.EntryPointCreator)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 KieServices (org.kie.api.KieServices)2 CorrelationAwareProcessRuntime (org.kie.internal.process.CorrelationAwareProcessRuntime)2