Search in sources :

Example 46 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class UserTaskModelMetaData method compilationUnitInput.

private CompilationUnit compilationUnitInput() {
    // task input handling
    CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream("/class-templates/TaskInputTemplate.java"));
    compilationUnit.setPackageDeclaration(packageName);
    Optional<ClassOrInterfaceDeclaration> processMethod = compilationUnit.findFirst(ClassOrInterfaceDeclaration.class, sl1 -> true);
    if (!processMethod.isPresent()) {
        throw new RuntimeException("Cannot find class declaration in the template");
    }
    ClassOrInterfaceDeclaration modelClass = processMethod.get();
    compilationUnit.addOrphanComment(new LineComment("Task input model for user task '" + humanTaskNode.getName() + "' in process '" + processId + "'"));
    addUserTaskAnnotation(modelClass);
    modelClass.setName(inputModelClassSimpleName);
    if (templateSupported) {
        modelClass.addAnnotation("io.quarkus.qute.TemplateData");
    }
    modelClass.addAnnotation(new NormalAnnotationExpr(new Name("org.eclipse.microprofile.openapi.annotations.media.Schema"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(("Input model for " + humanTaskNode.getName()).replaceAll("\\s", ""))), new MemberValuePair("description", new StringLiteralExpr("Task input model for user task '" + humanTaskNode.getName() + "' in '" + processId + "'")))));
    // setup of static fromMap method body
    ClassOrInterfaceType modelType = new ClassOrInterfaceType(null, modelClass.getNameAsString());
    BlockStmt staticFromMap = new BlockStmt();
    VariableDeclarationExpr itemField = new VariableDeclarationExpr(modelType, "item");
    staticFromMap.addStatement(new AssignExpr(itemField, new ObjectCreationExpr(null, modelType, NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
    NameExpr item = new NameExpr("item");
    for (Entry<String, String> entry : humanTaskNode.getInMappings().entrySet()) {
        Variable variable = Optional.ofNullable(variableScope.findVariable(entry.getValue())).orElse(processVariableScope.findVariable(entry.getValue()));
        if (variable == null) {
            throw new IllegalStateException("Task " + humanTaskNode.getName() + " (input) " + entry.getKey() + " reference not existing variable " + entry.getValue());
        }
        FieldDeclaration fd = new FieldDeclaration().addVariable(new VariableDeclarator().setType(variable.getType().getStringType()).setName(entry.getKey())).addModifier(Modifier.Keyword.PRIVATE);
        modelClass.addMember(fd);
        addUserTaskParamAnnotation(fd, UserTaskParam.ParamType.INPUT);
        fd.createGetter();
        fd.createSetter();
        // fromMap static method body
        FieldAccessExpr field = new FieldAccessExpr(item, entry.getKey());
        ClassOrInterfaceType type = parseClassOrInterfaceType(variable.getType().getStringType());
        staticFromMap.addStatement(new AssignExpr(field, new CastExpr(type, new MethodCallExpr(new NameExpr("params"), "get").addArgument(new StringLiteralExpr(entry.getKey()))), AssignExpr.Operator.ASSIGN));
    }
    for (Entry<String, Object> entry : humanTaskNode.getWork().getParameters().entrySet()) {
        if (entry.getValue() == null || INTERNAL_FIELDS.contains(entry.getKey())) {
            continue;
        }
        Map<String, String> dataInputs = (Map<String, String>) humanTaskNode.getMetaData(DATA_INPUTS);
        FieldDeclaration fd = new FieldDeclaration().addVariable(new VariableDeclarator().setType(dataInputs.get(entry.getKey())).setName(entry.getKey())).addModifier(Modifier.Keyword.PUBLIC);
        modelClass.addMember(fd);
        addUserTaskParamAnnotation(fd, UserTaskParam.ParamType.INPUT);
        fd.createGetter();
        fd.createSetter();
        // fromMap static method body
        FieldAccessExpr field = new FieldAccessExpr(item, entry.getKey());
        ClassOrInterfaceType type = parseClassOrInterfaceType(fd.getVariable(0).getType().asString());
        staticFromMap.addStatement(new AssignExpr(field, new CastExpr(type, new MethodCallExpr(new NameExpr("params"), "get").addArgument(new StringLiteralExpr(entry.getKey()))), AssignExpr.Operator.ASSIGN));
    }
    Optional<MethodDeclaration> staticFromMapMethod = modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("fromMap") && sl.isStatic());
    if (staticFromMapMethod.isPresent()) {
        MethodDeclaration fromMap = staticFromMapMethod.get();
        fromMap.setType(modelClass.getNameAsString());
        staticFromMap.addStatement(new ReturnStmt(new NameExpr("item")));
        fromMap.setBody(staticFromMap);
    }
    // setup of the toMap method body
    BlockStmt toMapBody = new BlockStmt();
    ClassOrInterfaceType toMap = new ClassOrInterfaceType(null, new SimpleName(Map.class.getSimpleName()), NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getSimpleName()), new ClassOrInterfaceType(null, Object.class.getSimpleName())));
    VariableDeclarationExpr paramsField = new VariableDeclarationExpr(toMap, "params");
    toMapBody.addStatement(new AssignExpr(paramsField, new ObjectCreationExpr(null, new ClassOrInterfaceType(null, HashMap.class.getSimpleName()), NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
    for (Entry<String, String> entry : humanTaskNode.getInMappings().entrySet()) {
        if (entry.getValue() == null || INTERNAL_FIELDS.contains(entry.getKey())) {
            continue;
        }
        Variable variable = Optional.ofNullable(variableScope.findVariable(entry.getValue())).orElse(processVariableScope.findVariable(entry.getValue()));
        if (variable == null) {
            // check if given mapping is an expression
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(entry.getValue());
            if (matcher.find()) {
                Map<String, String> dataOutputs = (Map<String, String>) humanTaskNode.getMetaData(DATA_INPUTS);
                variable = new Variable();
                variable.setName(entry.getKey());
                variable.setType(new ObjectDataType(constructClass(dataOutputs.get(entry.getKey())), dataOutputs.get(entry.getKey())));
            } else {
                throw new IllegalStateException("Task " + humanTaskNode.getName() + " (output) " + entry.getKey() + " reference not existing variable " + entry.getValue());
            }
        }
        // toMap method body
        MethodCallExpr putVariable = new MethodCallExpr(new NameExpr("params"), "put");
        putVariable.addArgument(new StringLiteralExpr(entry.getKey()));
        putVariable.addArgument(new FieldAccessExpr(new ThisExpr(), entry.getKey()));
        toMapBody.addStatement(putVariable);
    }
    Optional<MethodDeclaration> toMapMethod = modelClass.findFirst(MethodDeclaration.class, sl -> sl.getName().asString().equals("toMap"));
    toMapBody.addStatement(new ReturnStmt(new NameExpr("params")));
    toMapMethod.ifPresent(methodDeclaration -> methodDeclaration.setBody(toMapBody));
    return compilationUnit;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Matcher(java.util.regex.Matcher) SimpleName(com.github.javaparser.ast.expr.SimpleName) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) LineComment(com.github.javaparser.ast.comments.LineComment) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) Name(com.github.javaparser.ast.expr.Name) SimpleName(com.github.javaparser.ast.expr.SimpleName) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) CastExpr(com.github.javaparser.ast.expr.CastExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) CompilationUnit(com.github.javaparser.ast.CompilationUnit) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) HashMap(java.util.HashMap) Map(java.util.Map) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 47 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class VariableDeclarations method ofRawInfo.

public static VariableDeclarations ofRawInfo(Map<String, String> vscope) {
    HashMap<String, Variable> vs = new HashMap<>();
    if (vscope != null) {
        for (Entry<String, String> entry : vscope.entrySet()) {
            Variable variable = new Variable();
            variable.setName(entry.getKey());
            variable.setType(new ObjectDataType(constructClass(entry.getValue()), entry.getValue()));
            vs.put(entry.getKey(), variable);
        }
    }
    return new VariableDeclarations(vs);
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) HashMap(java.util.HashMap) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)

Example 48 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class ProcessContextMetaModel method getVariable.

public Expression getVariable(String procVar) {
    String interpolatedVar = extractVariableFromExpression(procVar);
    Variable v = variableScope.findVariable(interpolatedVar);
    if (v == null) {
        throw new IllegalArgumentException("No such variable " + procVar);
    }
    MethodCallExpr getter = new MethodCallExpr().setScope(new NameExpr(kcontext)).setName("getVariable").addArgument(new StringLiteralExpr(interpolatedVar));
    CastExpr castExpr = new CastExpr().setExpression(new EnclosedExpr(getter)).setType(v.getType().getStringType());
    return castExpr;
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CastExpr(com.github.javaparser.ast.expr.CastExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 49 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class ProcessContextMetaModel method setVariable.

public MethodCallExpr setVariable(String procVar) {
    Variable v = variableScope.findVariable(procVar);
    if (v == null) {
        throw new IllegalArgumentException("No such variable " + procVar);
    }
    MethodCallExpr setter = new MethodCallExpr().setScope(new NameExpr(kcontext)).setName("setVariable").addArgument(new StringLiteralExpr(procVar));
    return setter;
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 50 with Variable

use of io.automatiko.engine.workflow.base.core.context.variable.Variable in project automatiko-engine by automatiko-io.

the class ProcessVisitor method visitVariableScope.

private void visitVariableScope(VariableScope variableScope, BlockStmt body, Set<String> visitedVariables) {
    if (variableScope != null && !variableScope.getVariables().isEmpty()) {
        for (Variable variable : variableScope.getVariables()) {
            if (!visitedVariables.add(variable.getName())) {
                continue;
            }
            ClassOrInterfaceType variableType = new ClassOrInterfaceType(null, ObjectDataType.class.getSimpleName());
            ObjectCreationExpr variableValue = new ObjectCreationExpr(null, variableType, new NodeList<>(new ClassExpr(new ClassOrInterfaceType(null, ClassUtils.parseClassname(variable.getType().getStringType()))), new StringLiteralExpr(variable.getType().getStringType())));
            body.addStatement(getAssignedFactoryMethod(FACTORY_FIELD_NAME, VariableFactory.class, "$var_" + variable.getSanitizedName(), "variable", new Expression[] { new StringLiteralExpr(variable.getId()), new StringLiteralExpr(variable.getName()), variableValue }));
            visitMetaData(variable.getMetaData(), body, "$var_" + variable.getSanitizedName());
            body.addStatement(getFactoryMethod("$var_" + variable.getSanitizedName(), METHOD_DONE));
        }
    }
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Expression(com.github.javaparser.ast.expr.Expression) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) VariableFactory(io.automatiko.engine.workflow.process.executable.core.factory.VariableFactory)

Aggregations

Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)57 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)27 Map (java.util.Map)22 HashMap (java.util.HashMap)18 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)16 ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)16 ArrayList (java.util.ArrayList)14 NameExpr (com.github.javaparser.ast.expr.NameExpr)12 Matcher (java.util.regex.Matcher)12 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)11 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)10 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)9 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)9 ActionNode (io.automatiko.engine.workflow.process.core.node.ActionNode)9 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)9 List (java.util.List)9 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)8 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)8 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)8 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)8