use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.
the class WorkItemNodeInstance method createWorkItem.
protected WorkItem createWorkItem(WorkItemNode workItemNode) {
Work work = workItemNode.getWork();
workItem = new WorkItemImpl();
((WorkItem) workItem).setName(work.getName());
((WorkItem) workItem).setProcessInstanceId(getProcessInstance().getId());
((WorkItem) workItem).setParameters(new HashMap<String, Object>(work.getParameters()));
// if there are any dynamic parameters add them
if (dynamicParameters != null) {
((WorkItem) workItem).getParameters().putAll(dynamicParameters);
}
for (Iterator<DataAssociation> iterator = workItemNode.getInAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
if (parameterValue != null) {
((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
} else {
try {
parameterValue = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
logger.error("when trying to execute Work Item {}", work.getName());
logger.error("Continuing without setting parameter.");
}
}
if (parameterValue != null) {
((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
}
} else {
for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
handleAssignment(it.next());
}
}
}
for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
if (entry.getValue() instanceof String) {
String s = (String) entry.getValue();
Map<String, String> replacements = new HashMap<String, String>();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
while (matcher.find()) {
String paramName = matcher.group(1);
if (replacements.get(paramName) == null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
if (variableScopeInstance != null) {
Object variableValue = variableScopeInstance.getVariable(paramName);
String variableValueString = variableValue == null ? "" : variableValue.toString();
replacements.put(paramName, variableValueString);
} else {
try {
Object variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new NodeInstanceResolverFactory(this));
String variableValueString = variableValue == null ? "" : variableValue.toString();
replacements.put(paramName, variableValueString);
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", paramName);
logger.error("when trying to replace variable in string for Work Item {}", work.getName());
logger.error("Continuing without setting parameter.");
}
}
}
}
for (Map.Entry<String, String> replacement : replacements.entrySet()) {
s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
}
((WorkItem) workItem).setParameter(entry.getKey(), s);
}
}
return workItem;
}
use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.
the class WorkItemNodeInstance method triggerCompleted.
public void triggerCompleted(WorkItem workItem) {
this.workItem = workItem;
WorkItemNode workItemNode = getWorkItemNode();
if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
for (Iterator<DataAssociation> iterator = getWorkItemNode().getOutAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), workItem.getResults());
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
variableScopeInstance.setVariable(association.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete Work Item {}", workItem.getName());
logger.warn("Continuing without setting variable.");
}
if (parameterValue != null) {
((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
Object value = workItem.getResult(association.getSources().get(0));
if (value == null) {
try {
value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new WorkItemResolverFactory(workItem));
} catch (Throwable t) {
// do nothing
}
}
Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
DataType dataType = varDef.getType();
// exclude java.lang.Object as it is considered unknown type
if (!dataType.getStringType().endsWith("java.lang.Object") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
value = dataType.readValue((String) value);
} else {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), value);
}
variableScopeInstance.setVariable(association.getTarget(), value);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete Work Item {}", workItem.getName());
logger.warn("Continuing without setting variable.");
}
} else {
try {
for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
handleAssignment(it.next());
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
// handle dynamic nodes
if (getNode() == null) {
setMetaData("NodeType", workItem.getName());
mapDynamicOutputData(workItem.getResults());
}
if (isInversionOfControl()) {
KieRuntime kruntime = ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime();
kruntime.update(kruntime.getFactHandle(this), this);
} else {
triggerCompleted();
}
}
use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.
the class RuleSetNodeInstance method processOutputs.
private void processOutputs(Map<String, Object> objects) {
RuleSetNode ruleSetNode = getRuleSetNode();
if (ruleSetNode != null) {
for (Iterator<DataAssociation> iterator = ruleSetNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), objects);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(association.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("Continuing without setting variable.");
}
if (parameterValue != null) {
variableScopeInstance.setVariable(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
Object value = objects.get(association.getSources().get(0));
if (value == null) {
try {
value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new MapVariableResolverFactory(objects));
} catch (Throwable t) {
// do nothing
}
}
Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
DataType dataType = varDef.getType();
// exclude java.lang.Object as it is considered unknown type
if (!dataType.getStringType().endsWith("java.lang.Object") && value instanceof String) {
value = dataType.readValue((String) value);
}
variableScopeInstance.setVariable(association.getTarget(), value);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
}
}
}
}
}
use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.
the class RuleSetNodeInstance method evaluateParameters.
protected Map<String, Object> evaluateParameters(RuleSetNode ruleSetNode) {
Map<String, Object> replacements = new HashMap<String, Object>();
for (Iterator<DataAssociation> iterator = ruleSetNode.getInAssociations().iterator(); iterator.hasNext(); ) {
DataAssociation association = iterator.next();
if (association.getTransformation() != null) {
Transformation transformation = association.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
if (parameterValue != null) {
replacements.put(association.getTarget(), parameterValue);
}
}
} else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
Object parameterValue = null;
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
if (variableScopeInstance != null) {
parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
} else {
try {
parameterValue = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new NodeInstanceResolverFactory(this));
} catch (Throwable t) {
logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
logger.error("when trying to execute RuleSetNode {}", ruleSetNode.getName());
logger.error("Continuing without setting parameter.");
}
}
if (parameterValue != null) {
replacements.put(association.getTarget(), parameterValue);
}
}
}
for (Map.Entry<String, Object> entry : ruleSetNode.getParameters().entrySet()) {
if (entry.getValue() instanceof String) {
Object value = resolveVariable(entry.getValue());
if (value != null) {
replacements.put(entry.getKey(), value);
}
}
}
return replacements;
}
use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.
the class SubProcessNodeInstance method handleOutMappings.
private void handleOutMappings(ProcessInstance processInstance) {
VariableScopeInstance subProcessVariableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
SubProcessNode subProcessNode = getSubProcessNode();
if (subProcessNode != null) {
for (Iterator<org.jbpm.workflow.core.node.DataAssociation> iterator = subProcessNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
org.jbpm.workflow.core.node.DataAssociation mapping = iterator.next();
if (mapping.getTransformation() != null) {
Transformation transformation = mapping.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), subProcessVariableScopeInstance.getVariables());
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(mapping.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", mapping.getTarget());
logger.warn("Continuing without setting variable.");
}
}
} else {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
if (variableScopeInstance != null) {
Object value = subProcessVariableScopeInstance.getVariable(mapping.getSources().get(0));
if (value == null) {
try {
value = MVELSafeHelper.getEvaluator().eval(mapping.getSources().get(0), new VariableScopeResolverFactory(subProcessVariableScopeInstance));
} catch (Throwable t) {
// do nothing
}
}
variableScopeInstance.setVariable(mapping.getTarget(), value);
} else {
logger.error("Could not find variable scope for variable {}", mapping.getTarget());
logger.error("when trying to complete SubProcess node {}", getSubProcessNode().getName());
logger.error("Continuing without setting variable.");
}
}
}
} else {
// handle dynamic sub processes without data output mapping
mapDynamicOutputData(subProcessVariableScopeInstance.getVariables());
}
}
Aggregations