use of org.jbpm.process.core.Context in project jbpm by kiegroup.
the class ProcessInstanceImpl method getContextInstance.
public ContextInstance getContextInstance(String contextId) {
ContextInstance contextInstance = this.contextInstances.get(contextId);
if (contextInstance != null) {
return contextInstance;
}
Context context = ((ContextContainer) getProcess()).getDefaultContext(contextId);
if (context != null) {
contextInstance = getContextInstance(context);
return contextInstance;
}
return null;
}
use of org.jbpm.process.core.Context in project jbpm by kiegroup.
the class NodeInstanceImpl method resolveContextInstance.
public ContextInstance resolveContextInstance(String contextId, Object param) {
Context context = resolveContext(contextId, param);
if (context == null) {
return null;
}
ContextInstanceContainer contextInstanceContainer = getContextInstanceContainer(context.getContextContainer());
if (contextInstanceContainer == null) {
throw new IllegalArgumentException("Could not find context instance container for context");
}
return contextInstanceContainer.getContextInstance(context);
}
use of org.jbpm.process.core.Context in project jbpm by kiegroup.
the class AbstractProtobufProcessInstanceMarshaller method readNodeInstance.
public NodeInstance readNodeInstance(MarshallerReaderContext context, NodeInstanceContainer nodeInstanceContainer, WorkflowProcessInstance processInstance) throws IOException {
JBPMMessages.ProcessInstance.NodeInstance _node = (JBPMMessages.ProcessInstance.NodeInstance) context.parameterObject;
NodeInstanceImpl nodeInstance = readNodeInstanceContent(_node, context, processInstance);
nodeInstance.setNodeId(_node.getNodeId());
nodeInstance.setId(_node.getId());
nodeInstance.setNodeInstanceContainer(nodeInstanceContainer);
nodeInstance.setProcessInstance((org.jbpm.workflow.instance.WorkflowProcessInstance) processInstance);
nodeInstance.setLevel(_node.getLevel() == 0 ? 1 : _node.getLevel());
nodeInstance.internalSetSlaCompliance(_node.getSlaCompliance());
if (_node.getSlaDueDate() > 0) {
nodeInstance.internalSetSlaDueDate(new Date(_node.getSlaDueDate()));
}
nodeInstance.internalSetSlaTimerId(_node.getSlaTimerId());
switch(_node.getContent().getType()) {
case COMPOSITE_CONTEXT_NODE:
case DYNAMIC_NODE:
if (_node.getContent().getComposite().getVariableCount() > 0) {
Context variableScope = ((org.jbpm.process.core.Process) ((org.jbpm.process.instance.ProcessInstance) processInstance).getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) nodeInstance).getContextInstance(variableScope);
for (JBPMMessages.Variable _variable : _node.getContent().getComposite().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
}
if (_node.getContent().getComposite().getIterationLevelsCount() > 0) {
for (JBPMMessages.IterationLevel _level : _node.getContent().getComposite().getIterationLevelsList()) {
((CompositeContextNodeInstance) nodeInstance).getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
for (JBPMMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getComposite().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (CompositeContextNodeInstance) nodeInstance, processInstance);
}
for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl : _node.getContent().getComposite().getExclusiveGroupList()) {
ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
((CompositeContextNodeInstance) nodeInstance).addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
NodeInstance groupNodeInstance = ((org.jbpm.workflow.instance.NodeInstanceContainer) processInstance).getNodeInstance(nodeInstanceId, true);
if (groupNodeInstance == null) {
throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
}
exclusiveGroupInstance.addNodeInstance(groupNodeInstance);
}
}
break;
case FOR_EACH_NODE:
for (JBPMMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getForEach().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (ForEachNodeInstance) nodeInstance, processInstance);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((ForEachNodeInstance) nodeInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
for (JBPMMessages.Variable _variable : _node.getContent().getForEach().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
if (_node.getContent().getForEach().getIterationLevelsCount() > 0) {
for (JBPMMessages.IterationLevel _level : _node.getContent().getForEach().getIterationLevelsList()) {
((ForEachNodeInstance) nodeInstance).getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
}
break;
case EVENT_SUBPROCESS_NODE:
for (JBPMMessages.ProcessInstance.NodeInstance _instance : _node.getContent().getComposite().getNodeInstanceList()) {
context.parameterObject = _instance;
readNodeInstance(context, (EventSubProcessNodeInstance) nodeInstance, processInstance);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((EventSubProcessNodeInstance) nodeInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
for (JBPMMessages.Variable _variable : _node.getContent().getComposite().getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
}
break;
default:
}
return nodeInstance;
}
use of org.jbpm.process.core.Context in project jbpm by kiegroup.
the class AbstractProtobufProcessInstanceMarshaller method readProcessInstance.
// Input methods
public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
InternalKnowledgeBase ruleBase = context.kBase;
InternalWorkingMemory wm = context.wm;
JBPMMessages.ProcessInstance _instance = (org.jbpm.marshalling.impl.JBPMMessages.ProcessInstance) context.parameterObject;
if (_instance == null) {
// try to parse from the stream
ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
Header _header;
try {
_header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
} catch (ClassNotFoundException e) {
// Java 5 does not accept [new IOException(String, Throwable)]
IOException ioe = new IOException("Error deserializing process instance.");
ioe.initCause(e);
throw ioe;
}
_instance = JBPMMessages.ProcessInstance.parseFrom(_header.getPayload(), registry);
}
WorkflowProcessInstanceImpl processInstance = createProcessInstance();
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());
long nodeInstanceCounter = _instance.getNodeInstanceCounter();
processInstance.setKnowledgeRuntime(wm.getKnowledgeRuntime());
processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
for (String completedNodeId : _instance.getCompletedNodeIdsList()) {
processInstance.addCompletedNodeId(completedNodeId);
}
if (_instance.getSwimlaneContextCount() > 0) {
Context swimlaneContext = ((org.jbpm.process.core.Process) process).getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance(swimlaneContext);
for (JBPMMessages.ProcessInstance.SwimlaneContextInstance _swimlane : _instance.getSwimlaneContextList()) {
swimlaneContextInstance.setActorId(_swimlane.getSwimlane(), _swimlane.getActorId());
}
}
for (JBPMMessages.ProcessInstance.NodeInstance _node : _instance.getNodeInstanceList()) {
context.parameterObject = _node;
readNodeInstance(context, processInstance, processInstance);
}
for (JBPMMessages.ProcessInstance.ExclusiveGroupInstance _excl : _instance.getExclusiveGroupList()) {
ExclusiveGroupInstance exclusiveGroupInstance = new ExclusiveGroupInstance();
processInstance.addContextInstance(ExclusiveGroup.EXCLUSIVE_GROUP, exclusiveGroupInstance);
for (Long nodeInstanceId : _excl.getGroupNodeInstanceIdList()) {
NodeInstance nodeInstance = ((org.jbpm.workflow.instance.NodeInstanceContainer) processInstance).getNodeInstance(nodeInstanceId, true);
if (nodeInstance == null) {
throw new IllegalArgumentException("Could not find node instance when deserializing exclusive group instance: " + nodeInstanceId);
}
exclusiveGroupInstance.addNodeInstance(nodeInstance);
}
}
if (_instance.getVariableCount() > 0) {
Context variableScope = ((org.jbpm.process.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(variableScope);
for (JBPMMessages.Variable _variable : _instance.getVariableList()) {
try {
Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
variableScopeInstance.internalSetVariable(_variable.getName(), _value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
}
}
}
if (_instance.getIterationLevelsCount() > 0) {
for (JBPMMessages.IterationLevel _level : _instance.getIterationLevelsList()) {
processInstance.getIterationLevels().put(_level.getId(), _level.getLevel());
}
}
processInstance.reconnect();
return processInstance;
}
use of org.jbpm.process.core.Context in project jbpm by kiegroup.
the class RuleSetNodeInstance method internalTrigger.
public void internalTrigger(final NodeInstance from, String type) {
try {
super.internalTrigger(from, type);
// if node instance was cancelled, abort
if (getNodeInstanceContainer().getNodeInstance(getId()) == null) {
return;
}
if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
throw new IllegalArgumentException("A RuleSetNode only accepts default incoming connections!");
}
RuleSetNode ruleSetNode = getRuleSetNode();
KieRuntime kruntime = getProcessInstance().getKnowledgeRuntime();
Map<String, Object> inputs = evaluateParameters(ruleSetNode);
if (ruleSetNode.isDMN()) {
String namespace = resolveVariable(ruleSetNode.getNamespace());
String model = resolveVariable(ruleSetNode.getModel());
String decision = resolveVariable(ruleSetNode.getDecision());
DMNRuntime runtime = ((KieSession) kruntime).getKieRuntime(DMNRuntime.class);
DMNModel dmnModel = runtime.getModel(namespace, model);
if (dmnModel == null) {
throw new IllegalArgumentException("DMN model '" + model + "' not found with namespace '" + namespace + "'");
}
DMNResult dmnResult = null;
DMNContext context = runtime.newContext();
for (Entry<String, Object> entry : inputs.entrySet()) {
context.set(entry.getKey(), entry.getValue());
}
if (decision != null && !decision.isEmpty()) {
dmnResult = runtime.evaluateByName(dmnModel, context, decision);
} else {
dmnResult = runtime.evaluateAll(dmnModel, context);
}
if (dmnResult.hasErrors()) {
String errors = dmnResult.getMessages(Severity.ERROR).stream().map(message -> message.toString()).collect(Collectors.joining(", "));
throw new RuntimeException("DMN result errors:: " + errors);
}
processOutputs(dmnResult.getContext().getAll());
triggerCompleted();
} else {
// first set rule flow group
setRuleFlowGroup(resolveRuleFlowGroup(ruleSetNode.getRuleFlowGroup()));
// proceed
for (Entry<String, Object> entry : inputs.entrySet()) {
if (FIRE_RULE_LIMIT_PARAMETER.equals(entry.getKey())) {
// don't put control parameter for fire limit into working memory
continue;
}
String inputKey = getRuleFlowGroup() + "_" + getProcessInstance().getId() + "_" + entry.getKey();
factHandles.put(inputKey, kruntime.insert(entry.getValue()));
}
if (actAsWaitState()) {
addRuleSetListener();
((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda()).activateRuleFlowGroup(getRuleFlowGroup(), getProcessInstance().getId(), getUniqueId());
} else {
int fireLimit = DEFAULT_FIRE_RULE_LIMIT;
if (inputs.containsKey(FIRE_RULE_LIMIT_PARAMETER)) {
fireLimit = Integer.parseInt(inputs.get(FIRE_RULE_LIMIT_PARAMETER).toString());
}
((InternalAgenda) getProcessInstance().getKnowledgeRuntime().getAgenda()).activateRuleFlowGroup(getRuleFlowGroup(), getProcessInstance().getId(), getUniqueId());
int fired = ((KieSession) getProcessInstance().getKnowledgeRuntime()).fireAllRules(fireLimit);
if (fired == fireLimit) {
throw new RuntimeException("Fire rule limit reached " + fireLimit + ", limit can be set via system property " + FIRE_RULE_LIMIT_PROPERTY + " or via data input of business rule task named " + FIRE_RULE_LIMIT_PARAMETER);
}
removeEventListeners();
retractFacts();
triggerCompleted();
}
}
} catch (Exception e) {
handleException(e);
}
}
Aggregations