use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.
the class AbstractNodeVisitor method createLambdaExpr.
protected static LambdaExpr createLambdaExpr(String consequence, VariableScope scope) {
BlockStmt conditionBody = new BlockStmt();
List<Variable> variables = scope.getVariables();
variables.stream().map(ActionNodeVisitor::makeAssignment).forEach(conditionBody::addStatement);
variables.stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(conditionBody::addStatement);
conditionBody.addStatement(new ReturnStmt(new EnclosedExpr(new NameExpr(consequence))));
return new // (kcontext) ->
LambdaExpr(// (kcontext) ->
new Parameter(new UnknownType(), KCONTEXT_VAR), conditionBody);
}
use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.
the class ActionNodeVisitor method visitNode.
@Override
public void visitNode(WorkflowProcess process, String factoryField, ActionNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, ActionNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "Script"));
// if there is trigger defined on end event create TriggerMetaData for it
if (node.getMetaData(TRIGGER_REF) != null) {
LambdaExpr lambda = TriggerMetaData.buildLambdaExpr(node, metadata, variableScope);
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
} else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Compensation")) {
// compensation
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.ProcessInstanceCompensationAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("CompensationEvent"))))));
} else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Signal")) {
// throw signal
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.SignalProcessInstanceAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("Ref")), node.getMetaData("Variable") != null ? new StringLiteralExpr((String) node.getMetaData("Variable")) : new NullLiteralExpr()))));
} else {
String consequence = getActionConsequence(node.getAction());
if (consequence == null || consequence.trim().isEmpty()) {
throw new IllegalStateException("Action node " + node.getId() + " name '" + node.getName() + "' has no action defined");
}
BlockStmt actionBody = new BlockStmt();
List<Variable> variables = variableScope.getVariables();
variables.stream().filter(v -> consequence.contains(v.getName())).map(ActionNodeVisitor::makeAssignment).forEach(actionBody::addStatement);
variables.stream().filter(v -> v.hasTag(Variable.VERSIONED_TAG)).map(ActionNodeVisitor::makeAssignmentVersions).forEach(actionBody::addStatement);
actionBody.addStatement(new NameExpr(consequence));
LambdaExpr lambda = new // (kcontext) ->
LambdaExpr(// (kcontext) ->
new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
}
visitMetaData(node.getMetaData(), body, getNodeId(node));
for (DataAssociation association : node.getInAssociations()) {
}
body.addStatement(getDoneMethod(getNodeId(node)));
}
use of com.github.javaparser.ast.expr.LambdaExpr in project automatiko-engine by automatiko-io.
the class EndNodeVisitor method visitNode.
@Override
public void visitNode(WorkflowProcess process, String factoryField, EndNode node, BlockStmt body, VariableScope variableScope, ProcessMetaData metadata) {
body.addStatement(getAssignedFactoryMethod(factoryField, EndNodeFactory.class, getNodeId(node), getNodeKey(), new LongLiteralExpr(node.getId()))).addStatement(getNameMethod(node, "End")).addStatement(getFactoryMethod(getNodeId(node), METHOD_TERMINATE, new BooleanLiteralExpr(node.isTerminate())));
// if there is trigger defined on end event create TriggerMetaData for it
if (node.getMetaData(TRIGGER_REF) != null) {
LambdaExpr lambda = TriggerMetaData.buildLambdaExpr(node, metadata, variableScope);
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
} else if (node.getMetaData(TRIGGER_TYPE) != null && node.getMetaData(TRIGGER_TYPE).equals("Compensation")) {
// compensation
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, "io.automatiko.engine.workflow.base.instance.impl.actions.ProcessInstanceCompensationAction"), NodeList.nodeList(new StringLiteralExpr((String) node.getMetaData("CompensationEvent"))))));
} else if (node.getMetaData(REF) != null && EVENT_TYPE_SIGNAL.equals(node.getMetaData(EVENT_TYPE))) {
MethodCallExpr getProcessInstance = getFactoryMethod(KCONTEXT_VAR, "getProcessInstance");
MethodCallExpr signalEventMethod = new MethodCallExpr(getProcessInstance, "signalEvent").addArgument(new StringLiteralExpr((String) node.getMetaData(REF))).addArgument(new NullLiteralExpr());
BlockStmt actionBody = new BlockStmt();
actionBody.addStatement(signalEventMethod);
LambdaExpr lambda = new LambdaExpr(new Parameter(new UnknownType(), KCONTEXT_VAR), actionBody);
body.addStatement(getFactoryMethod(getNodeId(node), METHOD_ACTION, lambda));
}
visitMetaData(node.getMetaData(), body, getNodeId(node));
body.addStatement(getDoneMethod(getNodeId(node)));
}
use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.
the class PatternExpressionBuilder method createIndexedByLambda.
private LambdaExpr createIndexedByLambda(SingleDrlxParseSuccess drlxParseResult, TypedExpression left, TypedExpression right) {
LambdaExpr indexedByLeftOperandExtractor = new LambdaExpr();
indexedByLeftOperandExtractor.setEnclosingParameters(true);
indexedByLeftOperandExtractor.setBody(new ExpressionStmt(left.containThis() ? left.getExpression() : right.getExpression()));
indexedByLeftOperandExtractor.addParameter(new Parameter(drlxParseResult.getPatternJPType(), THIS_PLACEHOLDER));
return indexedByLeftOperandExtractor;
}
use of com.github.javaparser.ast.expr.LambdaExpr in project drools by kiegroup.
the class PatternExpressionBuilder method buildIndexedBy.
private Optional<MethodCallExpr> buildIndexedBy(SingleDrlxParseSuccess drlxParseResult) {
if (drlxParseResult.isUnification()) {
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
indexedByDSL.addArgument(org.drools.model.Index.ConstraintType.class.getCanonicalName() + ".EQUAL");
indexedByDSL.addArgument("-1");
indexedByDSL.addArgument(indexedByLeftOperandExtractor);
indexedByDSL.addArgument("" + null);
return Optional.of(indexedByDSL);
}
if (!shouldCreateIndex(drlxParseResult)) {
return Optional.empty();
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Expression rightExpression = right.getExpression();
if (!drlxParseResult.isBetaConstraint() && !(rightExpression instanceof LiteralExpr || isStringToDateExpression(rightExpression) || isNumberToStringExpression(rightExpression))) {
return Optional.empty();
}
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr(org.drools.model.Index.ConstraintType.class.getCanonicalName()), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument(getIndexIdArgument(drlxParseResult, left));
indexedByDSL.addArgument(indexedByLeftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
java.lang.reflect.Type leftType = left.getType();
if (drlxParseResult.isBetaConstraint()) {
addIndexedByDeclaration(left, right, left.containThis(), indexedByDSL, usedDeclarations);
} else {
indexedByDSL.addArgument(narrowExpressionToType(right, leftType));
}
return Optional.of(indexedByDSL);
}
Aggregations