use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class EventNodeInstance method signalEvent.
public void signalEvent(String type, Object event) {
if ("timerTriggered".equals(type)) {
TimerInstance timerInstance = (TimerInstance) event;
if (timerInstance.getId().equals(slaTimerId)) {
handleSLAViolation();
}
} else if (("slaViolation:" + getId()).equals(type)) {
handleSLAViolation();
} else {
String variableName = getEventNode().getVariableName();
if (!getEventNode().getOutAssociations().isEmpty()) {
for (DataAssociation association : getEventNode().getOutAssociations()) {
if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null) {
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") && event instanceof String) {
event = dataType.readValue((String) event);
} else {
variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), event);
}
variableScopeInstance.setVariable(this, association.getTarget(), event);
} else {
String output = association.getSources().get(0);
String target = association.getTarget();
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(target);
if (matcher.find()) {
String paramName = matcher.group(1);
String expression = VariableUtil.transformDotNotation(paramName, output);
NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
resolver.addExtraParameters(Collections.singletonMap(association.getSources().get(0), event));
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, resolver);
String varName = VariableUtil.nameFromDotNotation(paramName);
variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("when trying to complete start node {}", getEventNode().getName());
logger.warn("Continuing without setting variable.");
}
}
} else {
Object data = event;
association.getAssignments().stream().forEach(assignment -> handleAssignment(assignment, data));
}
}
} else if (variableName != null) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, variableName);
if (variableScopeInstance != null) {
EventTransformer transformer = getEventNode().getEventTransformer();
if (transformer != null) {
event = transformer.transformEvent(event);
}
variableScopeInstance.setVariable(this, variableName, event);
} else {
String output = "event";
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(variableName);
if (matcher.find()) {
String paramName = matcher.group(1);
String expression = VariableUtil.transformDotNotation(paramName, output);
NodeInstanceResolverFactory resolver = new NodeInstanceResolverFactory(this);
resolver.addExtraParameters(Collections.singletonMap("event", event));
Serializable compiled = MVEL.compileExpression(expression);
MVEL.executeExpression(compiled, resolver);
String varName = VariableUtil.nameFromDotNotation(paramName);
variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VARIABLE_SCOPE, varName);
variableScopeInstance.setVariable(this, varName, variableScopeInstance.getVariable(varName));
} else {
logger.warn("Could not find variable scope for variable {}", variableName);
logger.warn("when trying to complete start node {}", getEventNode().getName());
logger.warn("Continuing without setting variable.");
}
}
}
triggerCompleted();
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class EventNodeInstance method getEventDescriptions.
@Override
public Set<EventDescription<?>> getEventDescriptions() {
NamedDataType dataType = null;
if (getEventNode().getVariableName() != null) {
Map<String, Object> dataOutputs = (Map<String, Object>) getEventNode().getMetaData().get("DataOutputs");
if (dataOutputs != null) {
for (Entry<String, Object> dOut : dataOutputs.entrySet()) {
dataType = new NamedDataType(dOut.getKey(), dOut.getValue());
}
} else {
VariableScope variableScope = (VariableScope) getEventNode().getContext(VariableScope.VARIABLE_SCOPE);
Variable variable = variableScope.findVariable(getEventNode().getVariableName());
dataType = new NamedDataType(variable.getName(), variable.getType());
}
}
return Collections.singleton(new BaseEventDescription(getEventType(), getNodeDefinitionId(), getNodeName(), "signal", getId(), getProcessInstance().getId(), dataType));
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class WorkItemTest method getWorkItemProcess.
private ExecutableProcess getWorkItemProcess(String processId, String workName) {
ExecutableProcess process = new ExecutableProcess();
process.setId(processId);
List<Variable> variables = new ArrayList<Variable>();
Variable variable = new Variable();
variable.setName("UserName");
variable.setType(new StringDataType());
variables.add(variable);
variable = new Variable();
variable.setName("Person");
variable.setType(new ObjectDataType(Person.class));
variables.add(variable);
variable = new Variable();
variable.setName("MyObject");
variable.setType(new ObjectDataType());
variables.add(variable);
variable = new Variable();
variable.setName("Number");
variable.setType(new IntegerDataType());
variables.add(variable);
process.getVariableScope().setVariables(variables);
StartNode startNode = new StartNode();
startNode.setName("Start");
startNode.setId(1);
WorkItemNode workItemNode = new WorkItemNode();
workItemNode.setName("workItemNode");
workItemNode.setId(2);
workItemNode.addInMapping("Comment", "Person.name");
workItemNode.addInMapping("Attachment", "MyObject");
workItemNode.addOutMapping("Result", "MyObject");
workItemNode.addOutMapping("Result.length()", "Number");
Work work = new WorkImpl();
work.setName(workName);
Set<ParameterDefinition> parameterDefinitions = new HashSet<ParameterDefinition>();
ParameterDefinition parameterDefinition = new ParameterDefinitionImpl("ActorId", new StringDataType());
parameterDefinitions.add(parameterDefinition);
parameterDefinition = new ParameterDefinitionImpl("Content", new StringDataType());
parameterDefinitions.add(parameterDefinition);
parameterDefinition = new ParameterDefinitionImpl("Comment", new StringDataType());
parameterDefinitions.add(parameterDefinition);
work.setParameterDefinitions(parameterDefinitions);
work.setParameter("ActorId", "#{UserName}");
work.setParameter("Content", "#{Person.name}");
workItemNode.setWork(work);
EndNode endNode = new EndNode();
endNode.setName("End");
endNode.setId(3);
connect(startNode, workItemNode);
connect(workItemNode, endNode);
process.addNode(startNode);
process.addNode(workItemNode);
process.addNode(endNode);
return process;
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class CompensationTest method createCompensationBoundaryEventProcess.
private ExecutableProcess createCompensationBoundaryEventProcess(String processId, String[] workItemNames, final List<String> eventList) throws Exception {
ExecutableProcess process = new ExecutableProcess();
process.setAutoComplete(true);
process.setId(processId);
process.setName("CESP Process");
process.setMetaData("Compensation", true);
List<Variable> variables = new ArrayList<Variable>();
Variable variable = new Variable();
variable.setName("event");
ObjectDataType personDataType = new ObjectDataType(java.lang.String.class);
variable.setType(personDataType);
variables.add(variable);
process.getVariableScope().setVariables(variables);
NodeCreator<StartNode> startNodeCreator = new NodeCreator<StartNode>(process, StartNode.class);
NodeCreator<EndNode> endNodeCreator = new NodeCreator<EndNode>(process, EndNode.class);
NodeCreator<WorkItemNode> workItemNodeCreator = new NodeCreator<WorkItemNode>(process, WorkItemNode.class);
NodeCreator<BoundaryEventNode> boundaryNodeCreator = new NodeCreator<BoundaryEventNode>(process, BoundaryEventNode.class);
NodeCreator<ActionNode> actionNodeCreator = new NodeCreator<ActionNode>(process, ActionNode.class);
// Create process
StartNode startNode = startNodeCreator.createNode("start");
Node lastNode = startNode;
WorkItemNode[] workItemNodes = new WorkItemNode[3];
for (int i = 0; i < 3; ++i) {
workItemNodes[i] = workItemNodeCreator.createNode("work" + (i + 1));
workItemNodes[i].getWork().setName(workItemNames[i]);
connect(lastNode, workItemNodes[i]);
lastNode = workItemNodes[i];
}
EndNode endNode = endNodeCreator.createNode("end");
connect(workItemNodes[2], endNode);
// Compensation (boundary event) handlers
for (int i = 0; i < 3; ++i) {
createBoundaryEventCompensationHandler(process, workItemNodes[i], eventList, "" + i + 1);
}
return process;
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class AbstractNodeHandler method findVariable.
/**
* Finds the right variable by its name to make sure that when given as id it
* will be also matched
*
* @param variableName name or id of the variable
* @param parser parser instance
* @return returns found variable name or given 'variableName' otherwise
*/
protected String findVariable(String variableName, final ExtensibleXmlParser parser) {
if (variableName == null) {
return null;
}
List<?> parents = parser.getParents();
for (Object parent : parents) {
if (parent instanceof ContextContainer) {
ContextContainer contextContainer = (ContextContainer) parent;
VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
return variableScope.getVariables().stream().filter(v -> v.matchByIdOrName(variableName)).map(v -> v.getName()).findFirst().orElse(variableName);
}
}
return variableName;
}
Aggregations