use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class ActionNodeInstance method setOutputVariable.
public void setOutputVariable(Object variable) {
List<DataAssociation> outputs = getActionNode().getOutAssociations();
if (outputs != null && !outputs.isEmpty()) {
for (DataAssociation output : outputs) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, getActionNode().getOutAssociations().get(0).getTarget());
if (variableScopeInstance != null) {
if (output.getTransformation() != null) {
Transformation transformation = output.getTransformation();
DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
if (transformer != null) {
final Object currentValue = variable;
Map<String, Object> dataSet = new HashMap<String, Object>();
for (String source : output.getSources()) {
dataSet.put(source, currentValue);
}
variable = transformer.transform(transformation.getCompiledExpression(), dataSet);
}
}
Variable var = variableScopeInstance.getVariableScope().getVariables().stream().filter(v -> v.getId().equals(output.getTarget())).findFirst().orElse(null);
if (var != null) {
variableScopeInstance.setVariable(var.getName(), variable);
} else {
variableScopeInstance.setVariable(getActionNode().getOutAssociations().get(0).getTarget(), variable);
}
}
}
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class RuleSetNodeInstance method processOutputs.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void processOutputs(Map<String, Object> inputs, Map<String, Object> objects) {
logger.debug("Rules evaluation results {}", 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) {
Map<String, Object> dataSet = new HashMap<String, Object>();
if (getNodeInstanceContainer() instanceof CompositeContextNodeInstance) {
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) getNodeInstanceContainer()).getContextInstance(VariableScope.VARIABLE_SCOPE);
if (variableScopeInstance != null) {
dataSet.putAll(variableScopeInstance.getVariables());
}
}
dataSet.putAll(inputs);
dataSet.putAll(objects);
Object parameterValue = transformer.transform(transformation.getCompiledExpression(), dataSet);
VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
if (variableScopeInstance != null && parameterValue != null) {
variableScopeInstance.setVariable(this, association.getTarget(), parameterValue);
} else {
logger.warn("Could not find variable scope for variable {}", association.getTarget());
logger.warn("Continuing without setting variable.");
}
}
} 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 {
ExpressionEvaluator evaluator = (ExpressionEvaluator) ((WorkflowProcess) getProcessInstance().getProcess()).getDefaultContext(ExpressionEvaluator.EXPRESSION_EVALUATOR);
value = evaluator.evaluate(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(this, association.getTarget(), value);
} 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(objects);
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());
}
}
}
}
}
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method getPublicVariables.
@Override
public Map<String, Object> getPublicVariables() {
Map<String, Object> variables = new HashMap<>(getVariables());
VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
for (Variable variable : variableScope.getVariables()) {
if (variable.hasTag(Variable.SENSITIVE_TAG)) {
variables.remove(variable.getName());
}
}
return variables;
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class SplitNodeVisitor method visitNode.
@Override
public void visitNode(WorkflowProcess process, String factoryField, Split node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, SplitFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Split")).addStatement(getFactoryMethod(getNodeId(node), METHOD_TYPE, new IntegerLiteralExpr(node.getType())));
visitMetaData(node.getMetaData(), body, getNodeId(node));
if (node.getType() == Split.TYPE_OR || node.getType() == Split.TYPE_XOR) {
for (Entry<ConnectionRef, Constraint> entry : node.getConstraints().entrySet()) {
if (entry.getValue() != null) {
String dialect = entry.getValue().getDialect();
if ("jq".equals(dialect)) {
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_CONSTRAINT, new LongLiteralExpr(entry.getKey().getNodeId()), new StringLiteralExpr(getOrDefault(entry.getKey().getConnectionId(), "")), new StringLiteralExpr(entry.getKey().getToType()), new StringLiteralExpr(entry.getValue().getDialect()), new StringLiteralExpr(entry.getValue().getConstraint()), new IntegerLiteralExpr(entry.getValue().getPriority())));
} else {
BlockStmt actionBody = new BlockStmt();
LambdaExpr lambda = new // (kcontext) ->
LambdaExpr(// (kcontext) ->
new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
for (Variable v : variableScope.getVariables()) {
actionBody.addStatement(makeAssignment(v));
}
variableScope.getVariables().stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(actionBody::addStatement);
if (entry.getValue().getConstraint().contains(System.getProperty("line.separator"))) {
BlockStmt constraintBody = new BlockStmt();
constraintBody.addStatement(entry.getValue().getConstraint());
actionBody.addStatement(constraintBody);
} else {
actionBody.addStatement(new ReturnStmt(new EnclosedExpr(new NameExpr(entry.getValue().getConstraint()))));
}
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_CONSTRAINT, new LongLiteralExpr(entry.getKey().getNodeId()), new StringLiteralExpr(getOrDefault(entry.getKey().getConnectionId(), "")), new StringLiteralExpr(entry.getKey().getToType()), new StringLiteralExpr(entry.getValue().getDialect()), lambda, new IntegerLiteralExpr(entry.getValue().getPriority())));
}
}
}
}
body.addStatement(getDoneMethod(getNodeId(node)));
}
use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.
the class StartNodeVisitor method handleTrigger.
protected void handleTrigger(StartNode startNode, Map<String, Object> nodeMetaData, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
if (EVENT_TYPE_SIGNAL.equalsIgnoreCase((String) startNode.getMetaData(TRIGGER_TYPE))) {
Variable variable = null;
Map<String, String> variableMapping = startNode.getOutMappings();
String variableType = null;
if (variableMapping != null && !variableMapping.isEmpty()) {
Entry<String, String> varInfo = variableMapping.entrySet().iterator().next();
body.addStatement(getFactoryMethod(getNodeId(startNode), METHOD_TRIGGER, new StringLiteralExpr((String) nodeMetaData.get(MESSAGE_TYPE)), getOrNullExpr(varInfo.getKey()), getOrNullExpr(varInfo.getValue())));
Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(varInfo.getValue());
if (matcher.find()) {
variableType = (String) nodeMetaData.get(TRIGGER_REF);
} else {
variable = variableScope.findVariable(varInfo.getValue());
if (variable == null) {
// check parent node container
VariableScope vscope = (VariableScope) startNode.resolveContext(VariableScope.VARIABLE_SCOPE, varInfo.getKey());
variable = vscope.findVariable(varInfo.getValue());
}
variableType = variable != null ? variable.getType().getStringType() : null;
}
} else {
body.addStatement(getFactoryMethod(getNodeId(startNode), METHOD_TRIGGER, new StringLiteralExpr((String) nodeMetaData.get(MESSAGE_TYPE)), new StringLiteralExpr(getOrDefault((String) nodeMetaData.get(TRIGGER_MAPPING), ""))));
}
metadata.addSignal((String) nodeMetaData.get(MESSAGE_TYPE), variableType, startNode);
} else if (EVENT_TYPE_CONDITION.equalsIgnoreCase((String) startNode.getMetaData(TRIGGER_TYPE))) {
ConstraintTrigger constraintTrigger = (ConstraintTrigger) startNode.getTriggers().get(0);
body.addStatement(getFactoryMethod(getNodeId(startNode), METHOD_CONDITION, createLambdaExpr(constraintTrigger.getConstraint(), variableScope)));
} else {
String triggerMapping = (String) nodeMetaData.get(TRIGGER_MAPPING);
body.addStatement(getFactoryMethod(getNodeId(startNode), METHOD_TRIGGER, new StringLiteralExpr((String) nodeMetaData.get(TRIGGER_REF)), new StringLiteralExpr(getOrDefault((String) nodeMetaData.get(TRIGGER_MAPPING), "")), new StringLiteralExpr(getOrDefault(startNode.getOutMapping(triggerMapping), ""))));
}
}
Aggregations