Search in sources :

Example 6 with ObjectDataType

use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.

the class LambdaSubProcessNodeVisitor method unbind.

private BlockStmt unbind(VariableScope variableScope, SubProcessNode subProcessNode) {
    BlockStmt stmts = new BlockStmt();
    for (Map.Entry<String, String> e : subProcessNode.getOutMappings().entrySet()) {
        // check if given mapping is an expression
        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(e.getValue());
        if (matcher.find()) {
            String expression = matcher.group(1);
            String topLevelVariable = expression.split("\\.")[0];
            Map<String, String> dataOutputs = (Map<String, String>) subProcessNode.getMetaData("BPMN.OutputTypes");
            Variable variable = new Variable();
            variable.setName(topLevelVariable);
            variable.setType(new ObjectDataType(constructClass(dataOutputs.get(e.getKey())), dataOutputs.get(e.getKey())));
            stmts.addStatement(makeAssignment(variableScope.findVariable(topLevelVariable)));
            stmts.addStatement(makeAssignmentFromModel(variable, e.getKey()));
            stmts.addStatement(dotNotationToSetExpression(expression, e.getKey()));
            stmts.addStatement(new MethodCallExpr().setScope(new NameExpr(KCONTEXT_VAR)).setName("setVariable").addArgument(new StringLiteralExpr(topLevelVariable)).addArgument(topLevelVariable));
        } else {
            stmts.addStatement(makeAssignmentFromModel(variableScope.findVariable(e.getValue()), e.getKey()));
            stmts.addStatement(new MethodCallExpr().setScope(new NameExpr(KCONTEXT_VAR)).setName("setVariable").addArgument(new StringLiteralExpr(e.getValue())).addArgument(e.getKey()));
        }
    }
    return stmts;
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) Matcher(java.util.regex.Matcher) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with ObjectDataType

use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.

the class UserTaskModelMetaData method compilationUnitOutput.

@SuppressWarnings({ "unchecked" })
private CompilationUnit compilationUnitOutput() {
    CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream("/class-templates/TaskOutputTemplate.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 output model for user task '" + humanTaskNode.getName() + "' in process '" + processId + "'"));
    addUserTaskAnnotation(modelClass);
    modelClass.setName(outputModelClassSimpleName);
    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(("Output model for " + humanTaskNode.getName()).replaceAll("\\s", ""))), new MemberValuePair("description", new StringLiteralExpr("Task output 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.getOutMappings().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_OUTPUTS);
                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());
            }
        }
        // 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));
    }
    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.getOutMappings().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_OUTPUTS);
                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());
            }
        }
        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.OUTPUT);
        fd.createGetter();
        fd.createSetter();
        // 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 8 with ObjectDataType

use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.

the class TypeHandler method start.

public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    TypeObject typeable = (TypeObject) parser.getParent();
    String name = attrs.getValue("name");
    emptyAttributeCheck(localName, "name", name, parser);
    DataType dataType = null;
    try {
        dataType = (DataType) Class.forName(name).newInstance();
        // TODO make this pluggable so datatypes can read in other properties as well
        if (dataType instanceof ObjectDataType) {
            String className = attrs.getValue("className");
            if (className == null) {
                className = "java.lang.Object";
            }
            ((ObjectDataType) dataType).setClassName(className);
        }
    } catch (ClassNotFoundException e) {
        throw new SAXParseException("Could not find datatype " + name, parser.getLocator());
    } catch (InstantiationException e) {
        throw new SAXParseException("Could not instantiate datatype " + name, parser.getLocator());
    } catch (IllegalAccessException e) {
        throw new SAXParseException("Could not access datatype " + name, parser.getLocator());
    }
    typeable.setType(dataType);
    return dataType;
}
Also used : SAXParseException(org.xml.sax.SAXParseException) TypeObject(io.automatiko.engine.workflow.base.core.TypeObject) DataType(io.automatiko.engine.api.workflow.datatype.DataType) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)

Example 9 with ObjectDataType

use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.

the class ForEachNodeHandler method handleNode.

protected void handleNode(final Node node, final Element element, final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    super.handleNode(node, element, uri, localName, parser);
    ForEachNode forEachNode = (ForEachNode) node;
    final String variableName = element.getAttribute("variableName");
    if (variableName != null && variableName.length() != 0) {
        forEachNode.setVariable(variableName, new ObjectDataType());
    }
    final String collectionExpression = element.getAttribute("collectionExpression");
    if (collectionExpression != null && collectionExpression.length() != 0) {
        forEachNode.setCollectionExpression(collectionExpression);
    }
    final String waitForCompletion = element.getAttribute("waitForCompletion");
    if ("false".equals(waitForCompletion)) {
        forEachNode.setWaitForCompletion(false);
    }
}
Also used : ForEachNode(io.automatiko.engine.workflow.process.core.node.ForEachNode) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)

Example 10 with ObjectDataType

use of io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType in project automatiko-engine by automatiko-io.

the class ProcessToExecModelGeneratorTest method testScriptAndWorkItemModelGeneration.

@Test
public void testScriptAndWorkItemModelGeneration() {
    ExecutableProcessFactory factory = ExecutableProcessFactory.createProcess("demo.orders");
    factory.variable("order", new ObjectDataType(Integer.class)).variable("order", new ObjectDataType(List.class, "java.util.List<String>")).variable("approver", new ObjectDataType(String.class)).name("orders").packageName("com.myspace.demo").dynamic(false).version("1.0").workItemNode(1).name("Log").workName("Log").done().actionNode(2).name("Dump order").action("java", "System.out.println(\"Order has been created \" + order);").done().endNode(3).name("end").terminate(false).done().startNode(4).name("start").done().connection(2, 1).connection(4, 2).connection(1, 3);
    Process process = factory.validate().getProcess();
    ModelMetaData modelMetadata = ProcessToExecModelGenerator.INSTANCE.generateModel((WorkflowProcess) process);
    assertNotNull(modelMetadata, "Dumper should return non null class for process");
    logger.debug(modelMetadata.generate());
    assertEquals("com.myspace.demo.Orders_1_0Model", modelMetadata.getModelClassName());
}
Also used : ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) Process(io.automatiko.engine.api.definition.process.Process) ExecutableProcessFactory(io.automatiko.engine.workflow.process.executable.core.ExecutableProcessFactory) ModelMetaData(io.automatiko.engine.workflow.compiler.canonical.ModelMetaData) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)24 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)11 ArrayList (java.util.ArrayList)8 Map (java.util.Map)7 Test (org.junit.jupiter.api.Test)7 DataType (io.automatiko.engine.api.workflow.datatype.DataType)5 ItemDefinition (io.automatiko.engine.workflow.bpmn2.core.ItemDefinition)5 WorkItemNode (io.automatiko.engine.workflow.process.core.node.WorkItemNode)5 EndNode (io.automatiko.engine.workflow.process.core.node.EndNode)4 StartNode (io.automatiko.engine.workflow.process.core.node.StartNode)4 ExecutableProcess (io.automatiko.engine.workflow.process.executable.core.ExecutableProcess)4 HashMap (java.util.HashMap)4 List (java.util.List)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 NameExpr (com.github.javaparser.ast.expr.NameExpr)3 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)3 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)3 Work (io.automatiko.engine.workflow.base.core.Work)3 BooleanDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.BooleanDataType)3 IntegerDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.IntegerDataType)3