Search in sources :

Example 6 with InternalKnowledgeRuntime

use of org.drools.core.common.InternalKnowledgeRuntime in project jbpm by kiegroup.

the class NodeInstanceImpl method triggerCompleted.

protected void triggerCompleted(String type, boolean remove) {
    getExecutionErrorHandler().processed(this);
    Node node = getNode();
    if (node != null) {
        String uniqueId = (String) node.getMetaData().get("UniqueId");
        if (uniqueId == null) {
            uniqueId = ((NodeImpl) node).getUniqueId();
        }
        ((WorkflowProcessInstanceImpl) processInstance).addCompletedNodeId(uniqueId);
        ((WorkflowProcessInstanceImpl) processInstance).getIterationLevels().remove(uniqueId);
    }
    // if node instance was cancelled, or containing container instance was cancelled
    if ((getNodeInstanceContainer().getNodeInstance(getId()) == null) || (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE)) {
        return;
    }
    if (remove) {
        ((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).removeNodeInstance(this);
    }
    List<Connection> connections = null;
    if (node != null) {
        if ("true".equals(System.getProperty("jbpm.enable.multi.con")) && ((NodeImpl) node).getConstraints().size() > 0) {
            int priority = Integer.MAX_VALUE;
            connections = ((NodeImpl) node).getDefaultOutgoingConnections();
            boolean found = false;
            List<NodeInstanceTrigger> nodeInstances = new ArrayList<NodeInstanceTrigger>();
            List<Connection> outgoingCopy = new ArrayList<Connection>(connections);
            while (!outgoingCopy.isEmpty()) {
                priority = Integer.MAX_VALUE;
                Connection selectedConnection = null;
                ConstraintEvaluator selectedConstraint = null;
                for (final Iterator<Connection> iterator = outgoingCopy.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    ConstraintEvaluator constraint = (ConstraintEvaluator) ((NodeImpl) node).getConstraint(connection);
                    if (constraint != null && constraint.getPriority() < priority && !constraint.isDefault()) {
                        priority = constraint.getPriority();
                        selectedConnection = connection;
                        selectedConstraint = constraint;
                    }
                }
                if (selectedConstraint == null) {
                    break;
                }
                if (selectedConstraint.evaluate(this, selectedConnection, selectedConstraint)) {
                    nodeInstances.add(new NodeInstanceTrigger(followConnection(selectedConnection), selectedConnection.getToType()));
                    found = true;
                }
                outgoingCopy.remove(selectedConnection);
            }
            for (NodeInstanceTrigger nodeInstance : nodeInstances) {
                // stop if this process instance has been aborted / completed
                if (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE) {
                    return;
                }
                triggerNodeInstance(nodeInstance.getNodeInstance(), nodeInstance.getToType());
            }
            if (!found) {
                for (final Iterator<Connection> iterator = connections.iterator(); iterator.hasNext(); ) {
                    final Connection connection = (Connection) iterator.next();
                    ConstraintEvaluator constraint = (ConstraintEvaluator) ((NodeImpl) node).getConstraint(connection);
                    if (constraint.isDefault()) {
                        triggerConnection(connection);
                        found = true;
                        break;
                    }
                }
            }
            if (!found) {
                throw new IllegalArgumentException("Uncontrolled flow node could not find at least one valid outgoing connection " + getNode().getName());
            }
            return;
        } else {
            connections = node.getOutgoingConnections(type);
        }
    }
    if (connections == null || connections.isEmpty()) {
        boolean hidden = false;
        Node currentNode = getNode();
        if (currentNode != null && currentNode.getMetaData().get("hidden") != null) {
            hidden = true;
        }
        InternalKnowledgeRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
        if (!hidden) {
            ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireBeforeNodeLeft(this, kruntime);
        }
        // notify container
        ((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).nodeInstanceCompleted(this, type);
        if (!hidden) {
            ((InternalProcessRuntime) kruntime.getProcessRuntime()).getProcessEventSupport().fireAfterNodeLeft(this, kruntime);
        }
    } else {
        Map<org.jbpm.workflow.instance.NodeInstance, String> nodeInstances = new HashMap<org.jbpm.workflow.instance.NodeInstance, String>();
        for (Connection connection : connections) {
            nodeInstances.put(followConnection(connection), connection.getToType());
        }
        for (Map.Entry<org.jbpm.workflow.instance.NodeInstance, String> nodeInstance : nodeInstances.entrySet()) {
            // stop if this process instance has been aborted / completed
            if (((org.jbpm.workflow.instance.NodeInstanceContainer) getNodeInstanceContainer()).getState() != ProcessInstance.STATE_ACTIVE) {
                return;
            }
            triggerNodeInstance(nodeInstance.getKey(), nodeInstance.getValue());
        }
    }
}
Also used : NodeInstanceContainer(org.kie.api.runtime.process.NodeInstanceContainer) HashMap(java.util.HashMap) Node(org.kie.api.definition.process.Node) Connection(org.kie.api.definition.process.Connection) ArrayList(java.util.ArrayList) ConstraintEvaluator(org.jbpm.process.instance.impl.ConstraintEvaluator) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) NodeInstance(org.kie.api.runtime.process.NodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) ActionNodeInstance(org.jbpm.workflow.instance.node.ActionNodeInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with InternalKnowledgeRuntime

use of org.drools.core.common.InternalKnowledgeRuntime in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method retrieveKnowledgeRuntime.

/**
 * Retrieve the {@link ProcessInstanceManager} object from the ObjectOutput- or ObjectInputStream.
 * The stream object will secretly also either be a {@link MarshallerReaderContext} or a
 * {@link MarshallerWriteContext}.
 * </p>
 * The knowledge runtime object is useful in order to reconnect the process instance to the
 * process and the knowledge runtime object.
 * @param streamContext The marshaller stream/context.
 * @return A {@link InternalKnowledgeRuntime} object.
 */
public static InternalKnowledgeRuntime retrieveKnowledgeRuntime(Object streamContext) {
    InternalKnowledgeRuntime kruntime = null;
    if (streamContext instanceof MarshallerWriteContext) {
        MarshallerWriteContext context = (MarshallerWriteContext) streamContext;
        kruntime = ((InternalWorkingMemory) context.wm).getKnowledgeRuntime();
    } else if (streamContext instanceof MarshallerReaderContext) {
        MarshallerReaderContext context = (MarshallerReaderContext) streamContext;
        kruntime = ((InternalWorkingMemory) context.wm).getKnowledgeRuntime();
    } else {
        throw new UnsupportedOperationException("Unable to retrieve " + ProcessInstanceManager.class.getSimpleName() + " from " + streamContext.getClass().getName());
    }
    return kruntime;
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext)

Example 8 with InternalKnowledgeRuntime

use of org.drools.core.common.InternalKnowledgeRuntime in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method connectProcessInstanceToRuntimeAndProcess.

/**
 * Fill the process instance .kruntime and .process fields with the appropriate values.
 * @param processInstance
 * @param streamContext
 */
private void connectProcessInstanceToRuntimeAndProcess(ProcessInstance processInstance, Object streamContext) {
    ProcessInstanceImpl processInstanceImpl = (ProcessInstanceImpl) processInstance;
    InternalKnowledgeRuntime kruntime = processInstanceImpl.getKnowledgeRuntime();
    // Attach the kruntime if not present
    if (kruntime == null) {
        kruntime = retrieveKnowledgeRuntime(streamContext);
        processInstanceImpl.setKnowledgeRuntime(kruntime);
    }
    // Attach the process if not present
    if (processInstance.getProcess() == null) {
        String processId = processInstance.getProcessId();
        if (processId != null) {
            Process process = kruntime.getKieBase().getProcess(processId);
            if (process != null) {
                processInstanceImpl.setProcess(process);
            }
        }
    }
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) Process(org.kie.api.definition.process.Process)

Example 9 with InternalKnowledgeRuntime

use of org.drools.core.common.InternalKnowledgeRuntime in project drools by kiegroup.

the class PersistableRunner method initExistingKnowledgeSession.

protected void initExistingKnowledgeSession(Long sessionId, KieBase kbase, KieSessionConfiguration conf, PersistenceContext persistenceContext) {
    if (!doRollback && this.ksession != null) {
        return;
    // nothing to initialise
    }
    this.doRollback = false;
    try {
        // if locking is active, this will also lock the (found) SessionInfo instance
        this.sessionInfo = (SessionInfo) persistenceContext.findSession(sessionId);
    } catch (Exception e) {
        throw new SessionNotFoundException("Could not find session data for id " + sessionId, e);
    }
    if (sessionInfo == null) {
        throw new SessionNotFoundException("Could not find session data for id " + sessionId);
    }
    if (this.marshallingHelper == null) {
        // this should only happen when this class is first constructed
        this.marshallingHelper = new SessionMarshallingHelper(kbase, conf, env);
        MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration();
        config.setMarshallProcessInstances(false);
        config.setMarshallWorkItems(false);
    }
    this.sessionInfo.setJPASessionMashallingHelper(this.marshallingHelper);
    // if this.ksession is null, it'll create a new one, else it'll use the existing one
    this.ksession = this.marshallingHelper.loadSnapshot(this.sessionInfo.getData(), this.ksession, new JpaSessionInitializer(this));
    // update the session id to be the same as the session info id
    InternalKnowledgeRuntime kruntime = ((InternalKnowledgeRuntime) ksession);
    kruntime.setIdentifier(this.sessionInfo.getId());
    kruntime.setEndOperationListener(new EndOperationListenerImpl(this.txm, this.sessionInfo));
    this.runner = new TransactionInterceptor();
    // apply interceptors
    Iterator<ChainableRunner> iterator = this.interceptors.descendingIterator();
    while (iterator.hasNext()) {
        addInterceptor(iterator.next(), false);
    }
    initKieSessionMBeans(this.ksession);
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) MarshallingConfigurationImpl(org.drools.serialization.protobuf.marshalling.MarshallingConfigurationImpl) ChainableRunner(org.drools.core.runtime.ChainableRunner) SessionNotFoundException(org.drools.persistence.api.SessionNotFoundException) SessionNotFoundException(org.drools.persistence.api.SessionNotFoundException)

Example 10 with InternalKnowledgeRuntime

use of org.drools.core.common.InternalKnowledgeRuntime in project drools by kiegroup.

the class PersistableRunner method initNewKnowledgeSession.

protected void initNewKnowledgeSession(KieBase kbase, KieSessionConfiguration conf) {
    this.sessionInfo = new SessionInfo();
    // create session but bypass command service
    this.ksession = kbase.newKieSession(conf, this.env);
    initKieSessionMBeans(this.ksession);
    this.marshallingHelper = new SessionMarshallingHelper(this.ksession, conf);
    MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration();
    config.setMarshallProcessInstances(false);
    config.setMarshallWorkItems(false);
    this.sessionInfo.setJPASessionMashallingHelper(this.marshallingHelper);
    ((InternalKnowledgeRuntime) this.ksession).setEndOperationListener(new EndOperationListenerImpl(this.txm, this.sessionInfo));
    this.runner = new TransactionInterceptor();
    TimerJobFactoryManager timerJobFactoryManager = ((InternalKnowledgeRuntime) ksession).getTimerService().getTimerJobFactoryManager();
    if (timerJobFactoryManager instanceof CommandServiceTimerJobFactoryManager) {
        ((CommandServiceTimerJobFactoryManager) timerJobFactoryManager).setRunner(this);
    }
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) MarshallingConfigurationImpl(org.drools.serialization.protobuf.marshalling.MarshallingConfigurationImpl) CommandServiceTimerJobFactoryManager(org.drools.core.time.impl.CommandServiceTimerJobFactoryManager) TimerJobFactoryManager(org.drools.core.time.impl.TimerJobFactoryManager) CommandServiceTimerJobFactoryManager(org.drools.core.time.impl.CommandServiceTimerJobFactoryManager) SessionInfo(org.drools.persistence.info.SessionInfo)

Aggregations

InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)18 ArrayList (java.util.ArrayList)4 Connection (org.kie.api.definition.process.Connection)4 NodeInstance (org.kie.api.runtime.process.NodeInstance)4 HashMap (java.util.HashMap)3 CompositeNodeInstance (org.jbpm.workflow.instance.node.CompositeNodeInstance)3 Node (org.kie.api.definition.process.Node)3 Map (java.util.Map)2 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)2 RegistryContext (org.drools.core.command.impl.RegistryContext)2 MarshallingConfigurationImpl (org.drools.serialization.protobuf.marshalling.MarshallingConfigurationImpl)2 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2 ConstraintEvaluator (org.jbpm.process.instance.impl.ConstraintEvaluator)2 NodeInstanceContainer (org.jbpm.workflow.instance.NodeInstanceContainer)2 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)2 ActionNodeInstance (org.jbpm.workflow.instance.node.ActionNodeInstance)2 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 KieRuntime (org.kie.api.runtime.KieRuntime)2