this.caseId = ((WorkflowProcessInstanceImpl)source).getCorrelationKey(); this.date = new Date(); this.id = source.getId(); this.parentId = source.getParentProcessInstanceId(); this.caseDefinitionId = source.getProcessId(); this.caseDescription = ((WorkflowProcessInstanceImpl)source).getDescription(); this.caseDefinitionName = source.getProcessName(); this.caseStatus = source.getState(); this.variables = ((WorkflowProcessInstanceImpl)source).getVariables(); this.milestones = new ArrayList<>(); this.stages = new ArrayList<>(); this.participants = new LinkedHashSet<>(); Collection<NodeInstance> instances = ((WorkflowProcessInstanceImpl)source).getNodeInstances(true); for (NodeInstance instance : instances) { if (instance instanceof MilestoneNodeInstance) { this.milestones.add(instance.getNodeName()); } else if (instance instanceof DynamicNodeInstance) { this.stages.add(instance.getNodeName()); CaseData caseFile = internalGetCaseFile(((WorkflowProcessInstanceImpl)source).getKnowledgeRuntime()); if (caseFile != null) {
.setId( workFlow.getId() ) .setProcessId( workFlow.getProcessId() ) .setState( workFlow.getState() ) .setNodeInstanceCounter( workFlow.getNodeInstanceCounter() ) .setProcessType( workFlow.getProcess().getType() ) .setParentProcessInstanceId(workFlow.getParentProcessInstanceId()) .setSignalCompletion(workFlow.isSignalCompletion()) .setSlaCompliance(workFlow.getSlaCompliance()); if (workFlow.getProcessXml() != null) { _instance.setProcessXml( workFlow.getProcessXml()); if (workFlow.getDescription() != null) { _instance.setDescription(workFlow.getDescription()); if (workFlow.getDeploymentId() != null) { _instance.setDeploymentId(workFlow.getDeploymentId()); _instance.addAllCompletedNodeIds(workFlow.getCompletedNodeIds()); if (workFlow.getCorrelationKey() != null) { _instance.setCorrelationKey(workFlow.getCorrelationKey()); if (workFlow.getSlaDueDate() != null) { _instance.setSlaDueDate(workFlow.getSlaDueDate().getTime()); if (workFlow.getSlaTimerId() != null) { _instance.setSlaTimerId(workFlow.getSlaTimerId()); SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) workFlow.getContextInstance( SwimlaneContext.SWIMLANE_SCOPE ); if ( swimlaneContextInstance != null ) {
throw new IllegalArgumentException("Could not find process " + processId); if (processInstance.getProcessId().equals(processId)) { return; org.kie.api.definition.process.Process oldProcess = processInstance.getProcess(); processInstance.disconnect(); processInstance.setProcess(oldProcess); updateNodeInstances(processInstance, nodeMapping); processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) kruntime); processInstance.setProcess(process); processInstance.reconnect();
public String toString() { final StringBuilder sb = new StringBuilder("WorkflowProcessInstance"); sb.append(getId()); sb.append(" [processId="); sb.append(getProcessId()); sb.append(",state="); sb.append(getState()); sb.append("]"); return sb.toString(); }
private void validate() { InternalRuntimeManager manager = (InternalRuntimeManager) getKnowledgeRuntime().getEnvironment().get("RuntimeManager"); if (manager != null) { // check if process instance is owned by the same manager as the one owning ksession if (hasDeploymentId() && !manager.getIdentifier().equals(getDeploymentId())) { throw new IllegalStateException("Process instance " + getId() + " is owned by another deployment " + getDeploymentId() + " != " + manager.getIdentifier()); } } }
private void handleSLAViolation() { if (slaCompliance == SLA_PENDING) { InternalKnowledgeRuntime kruntime = getKnowledgeRuntime(); InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime(); processRuntime.getProcessEventSupport().fireBeforeSLAViolated(this, kruntime); logger.debug("SLA violated on process instance {}", getId()); this.slaCompliance = SLA_VIOLATED; this.slaTimerId = -1; processRuntime.getProcessEventSupport().fireAfterSLAViolated(this, kruntime); } }
@SuppressWarnings("unchecked") public void signalEvent(String type, Object event) { logger.debug("Signal {} received with data {} in process instance {}", type, event, getId()); synchronized (this) { if (getState() != ProcessInstance.STATE_ACTIVE) { return; TimerInstance timer = (TimerInstance) event; if (timer.getId() == slaTimerId) { handleSLAViolation(); handleSLAViolation(); for (Node node : getWorkflowProcess().getNodes()) { if (node instanceof EventNodeInterface) { if (((EventNodeInterface) node).acceptsEvent(type, event, getResolver(node, type, currentView))) { if (node instanceof EventNode && ((EventNode) node).getFrom() == null) { EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node); eventNodeInstance.signalEvent(type, event); } else { if (node instanceof EventSubProcessNode && ((resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type)))) { EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node); eventNodeInstance.signalEvent(type, event); } if (node instanceof DynamicNode && type.equals(((DynamicNode) node).getActivationEventName())) { DynamicNodeInstance dynamicNodeInstance = (DynamicNodeInstance) getNodeInstance(node); dynamicNodeInstance.signalEvent(type, event); } else { List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView); if (nodeInstances != null && !nodeInstances.isEmpty()) { for (NodeInstance nodeInstance : nodeInstances) {
public void assertNumOfIncommingConnections(ProcessInstance process, String nodeName, int num) { assertNodeExists(process, nodeName); WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process; for (Node node : instance.getNodeContainer().getNodes()) { if (node.getName().equals(nodeName)) { if (node.getIncomingConnections().size() != num) { fail("Expected incomming connections: " + num + " - found " + node.getIncomingConnections().size()); } else { break; } } } }
public void assertNumOfOutgoingConnections(ProcessInstance process, String nodeName, int num) { assertNodeExists(process, nodeName); WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process; for (Node node : instance.getNodeContainer().getNodes()) { if (node.getName().equals(nodeName)) { if (node.getOutgoingConnections().size() != num) { fail("Expected outgoing connections: " + num + " - found " + node.getOutgoingConnections().size()); } else { break; } } } }
when(process.getVersion()).thenReturn("1.0"); when(processInstance.getProcess()).thenReturn(process); when(processInstance.getId()).thenReturn(99L); when(processInstance.getDeploymentId()).thenReturn("test"); when(processInstance.getCorrelationKey()).thenReturn("key"); when(processInstance.getParentProcessInstanceId()).thenReturn(-1L); when(processInstance.getProcessId()).thenReturn("myprocess"); when(processInstance.getDescription()).thenReturn(""); when(processInstance.getProcessName()).thenReturn("MyProcess"); when(processInstance.getState()).thenReturn(1); when(processInstance.getVariables()).thenReturn(variables);
public void assertNodeExists(ProcessInstance process, String... nodeNames) { WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process; List<String> names = new ArrayList<String>(); for (String nodeName : nodeNames) { names.add(nodeName); } for (Node node : instance.getNodeContainer().getNodes()) { if (names.contains(node.getName())) { names.remove(node.getName()); } } if (!names.isEmpty()) { String s = names.get(0); for (int i = 1; i < names.size(); i++) { s += ", " + names.get(i); } fail("Node(s) do not exist: " + s); } }
InternalKnowledgeRuntime kruntime = getKnowledgeRuntime(); InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime(); processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, kruntime); NodeInstance nodeInstance = nodeInstances.get(0); ((org.jbpm.workflow.instance.NodeInstance) nodeInstance) .cancel(); processRuntime.getTimerManager().cancelTimer(this.slaTimerId); logger.debug("SLA Timer {} has been canceled", this.slaTimerId); removeEventListeners(); processRuntime.getProcessInstanceManager().removeProcessInstance(this); processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, kruntime); if (isSignalCompletion()) { RuntimeManager manager = (RuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER); if (getParentProcessInstanceId() > 0 && manager != null) { try { org.kie.api.runtime.manager.Context<?> context = ProcessInstanceIdContext.get(getParentProcessInstanceId()); managedkruntime.signalEvent("processInstanceCompleted:" + getId(), this); } catch (SessionNotFoundException e) { processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
private Node findNode(String nodeId) { Node found = null; Queue<Node> allProcessNodes = new LinkedList<Node>(); allProcessNodes.addAll(Arrays.asList( instance.getNodeContainer().getNodes() )); while( ! allProcessNodes.isEmpty() ) { Node node = allProcessNodes.poll(); if( nodeId.equals(node.getMetaData().get("UniqueId")) ) { found = node; break; } if( node instanceof NodeContainer ) { allProcessNodes.addAll(Arrays.asList( ((NodeContainer) node).getNodes())); } } return found; }
processInstance.setId( _instance.getId() ); String processId = _instance.getProcessId(); processInstance.setProcessId( processId ); String processXml = _instance.getProcessXml(); Process process = null; if (processXml != null && processXml.trim().length() > 0) { processInstance.setProcessXml( processXml ); process = processInstance.getProcess(); } else { process = ruleBase.getProcess( processId ); if (process == null) { throw new RuntimeException("Could not find process " + processId + " when restoring process instance " + processInstance.getId()); processInstance.setProcess( process ); processInstance.setDescription(_instance.getDescription()); processInstance.setState( _instance.getState() ); processInstance.setParentProcessInstanceId(_instance.getParentProcessInstanceId()); processInstance.setSignalCompletion(_instance.getSignalCompletion()); processInstance.setDeploymentId(_instance.getDeploymentId()); processInstance.setCorrelationKey(_instance.getCorrelationKey()); processInstance.internalSetSlaCompliance(_instance.getSlaCompliance()); if (_instance.getSlaDueDate() > 0) { processInstance.internalSetSlaDueDate(new Date(_instance.getSlaDueDate())); processInstance.internalSetSlaTimerId(_instance.getSlaTimerId()); processInstance.setKnowledgeRuntime( wm.getKnowledgeRuntime() ); processInstance.internalSetNodeInstanceCounter( nodeInstanceCounter );
protected boolean useAsync(final Node node) { if (!(node instanceof EventSubProcessNode) && (node instanceof ActionNode || node instanceof StateBasedNode || node instanceof EndNode)) { boolean asyncMode = Boolean.parseBoolean((String)node.getMetaData().get("customAsync")); if (asyncMode) { return asyncMode; } return Boolean.parseBoolean((String)getKnowledgeRuntime().getEnvironment().get("AsyncMode")); } return false; }
public TimerInstance configureSLATimer(String slaDueDateExpression) { // setup SLA if provided slaDueDateExpression = resolveVariable(slaDueDateExpression); if (slaDueDateExpression == null || slaDueDateExpression.trim().isEmpty()) { logger.debug("Sla due date expression resolved to no value '{}'", slaDueDateExpression); return null; } logger.debug("SLA due date is set to {}", slaDueDateExpression); InternalKnowledgeRuntime kruntime = getKnowledgeRuntime(); long duration = -1; if (kruntime != null && kruntime.getEnvironment().get("jbpm.business.calendar") != null){ BusinessCalendar businessCalendar = (BusinessCalendar) kruntime.getEnvironment().get("jbpm.business.calendar"); duration = businessCalendar.calculateBusinessTimeAsDuration(slaDueDateExpression); } else { duration = DateTimeUtils.parseDuration(slaDueDateExpression); } TimerInstance timerInstance = new TimerInstance(); timerInstance.setId(-1); timerInstance.setDelay(duration); timerInstance.setPeriod(0); if (useTimerSLATracking()) { ((InternalProcessRuntime)kruntime.getProcessRuntime()).getTimerManager().registerTimer(timerInstance, this); } return timerInstance; }
public void nodeInstanceCompleted(NodeInstance nodeInstance, String outType) { Node nodeInstanceNode = nodeInstance.getNode(); if( nodeInstanceNode != null ) { Object compensationBoolObj = nodeInstanceNode.getMetaData().get("isForCompensation"); boolean isForCompensation = compensationBoolObj == null ? false : ((Boolean) compensationBoolObj); if( isForCompensation ) { return; } } if (nodeInstance instanceof FaultNodeInstance || nodeInstance instanceof EndNodeInstance || ((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic() || nodeInstance instanceof CompositeNodeInstance) { if (((org.jbpm.workflow.core.WorkflowProcess) getProcess()).isAutoComplete()) { if (canComplete()) { setState(ProcessInstance.STATE_COMPLETED); } } } else { throw new IllegalArgumentException( "Completing a node instance that has no outgoing connection is not supported."); } }
public void start(String trigger) { synchronized (this) { registerExternalEventNodeListeners(); // activate timer event sub processes Node[] nodes = getNodeContainer().getNodes(); for (Node node : nodes) { if (node instanceof EventSubProcessNode) { Map<Timer, DroolsAction> timers = ((EventSubProcessNode) node).getTimers(); if (timers != null && !timers.isEmpty()) { EventSubProcessNodeInstance eventSubprocess = (EventSubProcessNodeInstance) getNodeInstance(node); eventSubprocess.trigger(null, org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE); } } } super.start(trigger); } }
@Override public void copyFromSource() { if (this.id != null) { return; } this.compositeId = System.getProperty("org.kie.server.id", "") + "_" + source.getId(); this.containerId = ((WorkflowProcessInstance)source).getDeploymentId(); this.correlationKey = ((WorkflowProcessInstanceImpl)source).getCorrelationKey(); this.date = new Date(); this.id = source.getId(); this.initiator = (String) ((WorkflowProcessInstanceImpl)source).getVariable("initiator"); this.parentId = source.getParentProcessInstanceId(); this.processId = source.getProcessId(); this.processInstanceDescription = ((WorkflowProcessInstanceImpl)source).getDescription(); this.processName = source.getProcessName(); this.processVersion = source.getProcess().getVersion(); this.state = source.getState(); this.variables = ((WorkflowProcessInstanceImpl)source).getVariables(); }
public void assertProcessVarExists(ProcessInstance process, String... processVarNames) { WorkflowProcessInstanceImpl instance = (WorkflowProcessInstanceImpl) process; List<String> names = new ArrayList<String>(); for (String nodeName : processVarNames) { names.add(nodeName); } for (String pvar : instance.getVariables().keySet()) { if (names.contains(pvar)) { names.remove(pvar); } } if (!names.isEmpty()) { String s = names.get(0); for (int i = 1; i < names.size(); i++) { s += ", " + names.get(i); } fail("Process Variable(s) do not exist: " + s); } }