Search in sources :

Example 1 with Variable

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

the class MessageProducerGenerator method generate.

public String generate() {
    String sanitizedName = CodegenUtils.triggerSanitizedName(trigger, process.getVersion());
    String connector = CodegenUtils.getConnector(OUTGOING_PROP_PREFIX + sanitizedName + ".connector", context, (String) trigger.getContext("connector"));
    if (connector != null) {
        context.setApplicationProperty(OUTGOING_PROP_PREFIX + sanitizedName + ".connector", connector);
        appendConnectorSpecificProperties(connector);
    }
    CompilationUnit clazz = parse(this.getClass().getResourceAsStream(producerTemplate(connector)));
    clazz.setPackageDeclaration(process.getPackageName());
    // add functions so they can be easily accessed in message producer classes
    clazz.addImport(new ImportDeclaration(BaseFunctions.class.getCanonicalName(), true, true));
    context.getBuildContext().classThatImplement(Functions.class.getCanonicalName()).forEach(c -> clazz.addImport(new ImportDeclaration(c, true, true)));
    ClassOrInterfaceDeclaration template = clazz.findFirst(ClassOrInterfaceDeclaration.class).get();
    template.setName(resourceClazzName);
    template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateTypes(cls, trigger.getDataType()));
    template.findAll(ClassOrInterfaceType.class).forEach(cls -> interpolateEventTypes(cls, messageDataEventClassName));
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("produce")).forEach(md -> {
        md.getParameters().stream().filter(p -> p.getNameAsString().equals(EVENT_DATA_VAR)).forEach(p -> p.setType(trigger.getDataType()));
        if (context.getBuildContext().hasClassAvailable("org.eclipse.microprofile.opentracing.Traced")) {
            md.addAnnotation("org.eclipse.microprofile.opentracing.Traced");
        }
    });
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("configure")).forEach(md -> md.addAnnotation("javax.annotation.PostConstruct"));
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("marshall")).forEach(md -> {
        md.getParameters().stream().filter(p -> p.getNameAsString().equals(EVENT_DATA_VAR)).forEach(p -> p.setType(trigger.getDataType()));
        md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataEventType$", messageDataEventClassName)));
    });
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("convert")).forEach(md -> {
        md.setType(md.getTypeAsString().replace("$DataType$", trigger.getDataType()));
        md.findAll(CastExpr.class).forEach(c -> c.setType(c.getTypeAsString().replace("$DataType$", trigger.getDataType())));
        md.findAll(ClassOrInterfaceType.class).forEach(t -> t.setName(t.getNameAsString().replace("$DataType$", trigger.getDataType())));
    });
    // used by MQTT to get topic name based on expression
    String topicExpression = (String) trigger.getContext("topicExpression");
    if (topicExpression != null) {
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("topic")).forEach(md -> {
            BlockStmt body = new BlockStmt();
            ClassOrInterfaceType stringType = new ClassOrInterfaceType(null, String.class.getCanonicalName());
            if (topicExpression.contains("id")) {
                VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "id");
                body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getId"), AssignExpr.Operator.ASSIGN));
            }
            if (topicExpression.contains("businessKey")) {
                VariableDeclarationExpr businessKeyField = new VariableDeclarationExpr(stringType, "businessKey");
                body.addStatement(new AssignExpr(businessKeyField, new MethodCallExpr(new NameExpr("pi"), "getCorrelationKey"), AssignExpr.Operator.ASSIGN));
            }
            VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
            for (Variable var : variableScope.getVariables()) {
                if (topicExpression.contains(var.getSanitizedName())) {
                    ClassOrInterfaceType varType = new ClassOrInterfaceType(null, var.getType().getStringType());
                    VariableDeclarationExpr v = new VariableDeclarationExpr(varType, var.getSanitizedName());
                    body.addStatement(new AssignExpr(v, new CastExpr(varType, new MethodCallExpr(new MethodCallExpr(new NameExpr("pi"), "getVariables"), "get").addArgument(new StringLiteralExpr(var.getName()))), AssignExpr.Operator.ASSIGN));
                }
            }
            body.addStatement(new ReturnStmt(new NameExpr(topicExpression)));
            md.setBody(body);
        });
    }
    // used by AMQP to get address name based on expression
    String addressExpression = (String) trigger.getContext("addressExpression");
    if (addressExpression != null) {
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("address")).forEach(md -> {
            BlockStmt body = new BlockStmt();
            ClassOrInterfaceType stringType = new ClassOrInterfaceType(null, String.class.getCanonicalName());
            if (addressExpression.contains("id")) {
                VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "id");
                body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getId"), AssignExpr.Operator.ASSIGN));
            }
            if (addressExpression.contains("businessKey")) {
                VariableDeclarationExpr businessKeyField = new VariableDeclarationExpr(stringType, "businessKey");
                body.addStatement(new AssignExpr(businessKeyField, new MethodCallExpr(new NameExpr("pi"), "getCorrelationKey"), AssignExpr.Operator.ASSIGN));
            }
            VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
            for (Variable var : variableScope.getVariables()) {
                if (addressExpression.contains(var.getSanitizedName())) {
                    ClassOrInterfaceType varType = new ClassOrInterfaceType(null, var.getType().getStringType());
                    VariableDeclarationExpr v = new VariableDeclarationExpr(varType, var.getSanitizedName());
                    body.addStatement(new AssignExpr(v, new CastExpr(varType, new MethodCallExpr(new MethodCallExpr(new NameExpr("pi"), "getVariables"), "get").addArgument(new StringLiteralExpr(var.getName()))), AssignExpr.Operator.ASSIGN));
                }
            }
            body.addStatement(new ReturnStmt(new NameExpr(addressExpression)));
            md.setBody(body);
        });
    }
    // used by FunctionFlow to set subject (used by reply to)
    String subjectExpression = (String) trigger.getContext("subjectExpression");
    if (subjectExpression != null) {
        template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("subject")).forEach(md -> {
            BlockStmt body = new BlockStmt();
            ClassOrInterfaceType stringType = new ClassOrInterfaceType(null, String.class.getCanonicalName());
            if (subjectExpression.contains("id")) {
                VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "id");
                body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getId"), AssignExpr.Operator.ASSIGN));
            }
            if (subjectExpression.contains("businessKey")) {
                VariableDeclarationExpr businessKeyField = new VariableDeclarationExpr(stringType, "businessKey");
                body.addStatement(new AssignExpr(businessKeyField, new MethodCallExpr(new NameExpr("pi"), "getCorrelationKey"), AssignExpr.Operator.ASSIGN));
            }
            if (subjectExpression.contains("referenceId")) {
                VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "referenceId");
                body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getReferenceId"), AssignExpr.Operator.ASSIGN));
            }
            VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
            for (Variable var : variableScope.getVariables()) {
                if (subjectExpression.contains(var.getSanitizedName())) {
                    ClassOrInterfaceType varType = new ClassOrInterfaceType(null, var.getType().getStringType());
                    VariableDeclarationExpr v = new VariableDeclarationExpr(varType, var.getSanitizedName());
                    body.addStatement(new AssignExpr(v, new CastExpr(varType, new MethodCallExpr(new MethodCallExpr(new NameExpr("pi"), "getVariables"), "get").addArgument(new StringLiteralExpr(var.getName()))), AssignExpr.Operator.ASSIGN));
                }
            }
            body.addStatement(new ReturnStmt(new NameExpr(subjectExpression)));
            md.setBody(body);
        });
    }
    // Camal or HTTP headers
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("headers")).forEach(md -> {
        StringBuilder allHeaderValues = new StringBuilder();
        for (Entry<String, Object> entry : trigger.getContext().entrySet()) {
            if (entry.getKey().startsWith("Camel") || entry.getKey().startsWith("HTTP")) {
                allHeaderValues.append(entry.getValue().toString()).append(" ");
            }
        }
        String allHeaderValuesStr = allHeaderValues.toString();
        BlockStmt body = new BlockStmt();
        ClassOrInterfaceType stringType = new ClassOrInterfaceType(null, String.class.getCanonicalName());
        if (allHeaderValuesStr.contains("id")) {
            VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "id");
            body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getId"), AssignExpr.Operator.ASSIGN));
        }
        if (allHeaderValuesStr.contains("businessKey")) {
            VariableDeclarationExpr businessKeyField = new VariableDeclarationExpr(stringType, "businessKey");
            body.addStatement(new AssignExpr(businessKeyField, new MethodCallExpr(new NameExpr("pi"), "getCorrelationKey"), AssignExpr.Operator.ASSIGN));
        }
        VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        for (Variable var : variableScope.getVariables()) {
            if (allHeaderValuesStr.contains(var.getSanitizedName())) {
                ClassOrInterfaceType varType = new ClassOrInterfaceType(null, var.getType().getStringType());
                VariableDeclarationExpr v = new VariableDeclarationExpr(varType, var.getSanitizedName());
                body.addStatement(new AssignExpr(v, new CastExpr(varType, new MethodCallExpr(new MethodCallExpr(new NameExpr("pi"), "getVariables"), "get").addArgument(new StringLiteralExpr(var.getName()))), AssignExpr.Operator.ASSIGN));
            }
        }
        for (Entry<String, Object> entry : trigger.getContext().entrySet()) {
            if (entry.getKey().startsWith("Camel")) {
                body.addStatement(new MethodCallExpr(new NameExpr("metadata"), "putHeader").addArgument(new StringLiteralExpr(entry.getKey())).addArgument(new NameExpr(entry.getValue().toString())));
            } else if (entry.getKey().startsWith("HTTP")) {
                body.addStatement(new MethodCallExpr(new NameExpr("builder"), "addHeader").addArgument(new StringLiteralExpr(entry.getKey().replaceFirst("HTTP", ""))).addArgument(new NameExpr(entry.getValue().toString())));
            }
        }
        if (!md.getTypeAsString().equalsIgnoreCase("void")) {
            body.addStatement(new ReturnStmt(new NameExpr("metadata")));
        }
        md.setBody(body);
    });
    // JMS properties
    template.findAll(MethodDeclaration.class).stream().filter(md -> md.getNameAsString().equals("properties")).forEach(md -> {
        StringBuilder allHeaderValues = new StringBuilder();
        for (Entry<String, Object> entry : trigger.getContext().entrySet()) {
            if (entry.getKey().startsWith("JMS")) {
                allHeaderValues.append(entry.getValue().toString()).append(" ");
            }
        }
        String allHeaderValuesStr = allHeaderValues.toString();
        BlockStmt body = new BlockStmt();
        ClassOrInterfaceType stringType = new ClassOrInterfaceType(null, String.class.getCanonicalName());
        if (allHeaderValuesStr.contains("id")) {
            VariableDeclarationExpr idField = new VariableDeclarationExpr(stringType, "id");
            body.addStatement(new AssignExpr(idField, new MethodCallExpr(new NameExpr("pi"), "getId"), AssignExpr.Operator.ASSIGN));
        }
        if (allHeaderValuesStr.contains("businessKey")) {
            VariableDeclarationExpr businessKeyField = new VariableDeclarationExpr(stringType, "businessKey");
            body.addStatement(new AssignExpr(businessKeyField, new MethodCallExpr(new NameExpr("pi"), "getCorrelationKey"), AssignExpr.Operator.ASSIGN));
        }
        VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.process.core.WorkflowProcess) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        for (Variable var : variableScope.getVariables()) {
            if (allHeaderValuesStr.contains(var.getSanitizedName())) {
                ClassOrInterfaceType varType = new ClassOrInterfaceType(null, var.getType().getStringType());
                VariableDeclarationExpr v = new VariableDeclarationExpr(varType, var.getSanitizedName());
                body.addStatement(new AssignExpr(v, new CastExpr(varType, new MethodCallExpr(new MethodCallExpr(new NameExpr("pi"), "getVariables"), "get").addArgument(new StringLiteralExpr(var.getName()))), AssignExpr.Operator.ASSIGN));
            }
        }
        for (Entry<String, Object> entry : trigger.getContext().entrySet()) {
            if (entry.getKey().startsWith("JMS")) {
                body.addStatement(new MethodCallExpr(new NameExpr("builder"), "with").addArgument(new StringLiteralExpr(entry.getKey().replaceFirst("JMS", ""))).addArgument(new NameExpr(entry.getValue().toString())));
            }
        }
        body.addStatement(new ReturnStmt(new NameExpr("builder")));
        md.setBody(body);
    });
    template.findAll(MethodDeclaration.class).forEach(md -> {
        md.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$Trigger$", trigger.getName())));
    });
    template.findAll(MethodDeclaration.class).forEach(md -> {
        md.findAll(StringLiteralExpr.class).forEach(str -> str.setString(str.asString().replace("$TriggerType$", (String) trigger.getContext(Metadata.TRIGGER_TYPE_ATTR, trigger.getName()))));
    });
    if (useInjection()) {
        annotator.withApplicationComponent(template);
        template.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("emitter")).forEach(emitterField -> {
            annotator.withInjection(emitterField);
            annotator.withOutgoingMessage(emitterField, sanitizedName);
        });
        template.findAll(FieldDeclaration.class, fd -> fd.getVariables().get(0).getNameAsString().equals("converter")).forEach(fd -> {
            annotator.withInjection(fd);
            fd.getVariable(0).setType(fd.getVariable(0).getTypeAsString().replace("$DataType$", trigger.getDataType()));
        });
        template.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("useCloudEvents")).forEach(fd -> annotator.withConfigInjection(fd, "quarkus.automatiko.messaging.as-cloudevents"));
        template.findAll(FieldDeclaration.class, fd -> fd.getVariable(0).getNameAsString().equals("useCloudEventsBinary")).forEach(fd -> annotator.withConfigInjection(fd, "quarkus.automatiko.messaging.as-cloudevents-binary"));
    }
    // add connector and message name as static fields of the class
    FieldDeclaration connectorField = new FieldDeclaration().setStatic(true).setFinal(true).addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "String"), "CONNECTOR", new StringLiteralExpr(connector)));
    template.addMember(connectorField);
    FieldDeclaration messageNameField = new FieldDeclaration().setStatic(true).setFinal(true).addVariable(new VariableDeclarator(new ClassOrInterfaceType(null, "String"), "MESSAGE", new StringLiteralExpr(trigger.getName())));
    template.addMember(messageNameField);
    if (workflowType.equals(Process.FUNCTION_FLOW_TYPE)) {
        String destination = (String) trigger.getContext("functionType", sanitizedName);
        String sourcePrefix = process.getPackageName() + "." + processId + "." + sanitizedName;
        template.findAll(StringLiteralExpr.class).forEach(vv -> {
            String s = vv.getValue();
            String interpolated = s.replace("$destination$", destination);
            interpolated = interpolated.replace("$sourcePrefix$", sourcePrefix);
            vv.setString(interpolated);
        });
    }
    template.getMembers().sort(new BodyDeclarationComparator());
    ImportsOrganizer.organize(clazz);
    return clazz.toString();
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) StaticJavaParser.parse(com.github.javaparser.StaticJavaParser.parse) Metadata(io.automatiko.engine.workflow.process.executable.core.Metadata) CAMEL_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.CAMEL_CONNECTOR) GeneratorContext(io.automatiko.engine.codegen.GeneratorContext) CastExpr(com.github.javaparser.ast.expr.CastExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) AMQP_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.AMQP_CONNECTOR) MQTT_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.MQTT_CONNECTOR) CodegenUtils.interpolateTypes(io.automatiko.engine.codegen.CodegenUtils.interpolateTypes) StringUtils(io.automatiko.engine.services.utils.StringUtils) CompilationUnit(com.github.javaparser.ast.CompilationUnit) TriggerMetaData(io.automatiko.engine.workflow.compiler.canonical.TriggerMetaData) HTTP_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.HTTP_CONNECTOR) KAFKA_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.KAFKA_CONNECTOR) DependencyInjectionAnnotator(io.automatiko.engine.codegen.di.DependencyInjectionAnnotator) FUNCTION_FLOW_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.FUNCTION_FLOW_CONNECTOR) Process(io.automatiko.engine.api.definition.process.Process) OUTGOING_PROP_PREFIX(io.automatiko.engine.codegen.CodeGenConstants.OUTGOING_PROP_PREFIX) CodegenUtils(io.automatiko.engine.codegen.CodegenUtils) BaseFunctions(io.automatiko.engine.services.execution.BaseFunctions) JMS_CONNECTOR(io.automatiko.engine.codegen.CodeGenConstants.JMS_CONNECTOR) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) WorkflowProcess(io.automatiko.engine.api.definition.process.WorkflowProcess) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) CodegenUtils.interpolateEventTypes(io.automatiko.engine.codegen.CodegenUtils.interpolateEventTypes) NameExpr(com.github.javaparser.ast.expr.NameExpr) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Entry(java.util.Map.Entry) Functions(io.automatiko.engine.api.Functions) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ImportsOrganizer(io.automatiko.engine.codegen.ImportsOrganizer) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CastExpr(com.github.javaparser.ast.expr.CastExpr) BodyDeclarationComparator(io.automatiko.engine.codegen.BodyDeclarationComparator) 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) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with Variable

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

the class AbstractProtobufProcessInstanceMarshaller method readVariableScope.

protected void readVariableScope(MarshallerReaderContext context, Process process, WorkflowProcessInstanceImpl processInstance, AutomatikoMessages.ProcessInstance _instance) throws IOException {
    if (_instance.getVariableCount() > 0) {
        VariableScope variableScope = (VariableScope) ((io.automatiko.engine.workflow.base.core.Process) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(variableScope);
        for (AutomatikoMessages.Variable _variable : _instance.getVariableList()) {
            try {
                Object _value = ProtobufProcessMarshaller.unmarshallVariableValue(context, _variable);
                if ((boolean) context.env.getOrDefault("_import_", false)) {
                    VariableInitializer initializer = ((ProcessRuntimeServiceProvider) context.env.get("_services_")).getVariableInitializer();
                    for (VariableAugmentor augmentor : initializer.augmentors()) {
                        Variable var = variableScope.findVariable(_variable.getName());
                        if (augmentor.accept(var, _value)) {
                            _value = augmentor.augmentOnCreate(process.getId(), process.getVersion(), _instance.getId(), var, _value);
                        }
                    }
                }
                variableScopeInstance.internalSetVariable(_variable.getName(), _value);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Could not reload variable " + _variable.getName());
            }
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) VariableAugmentor(io.automatiko.engine.api.workflow.VariableAugmentor) ProcessRuntimeServiceProvider(io.automatiko.engine.workflow.base.instance.ProcessRuntimeServiceProvider) VariableInitializer(io.automatiko.engine.api.workflow.VariableInitializer) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

Example 3 with Variable

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

the class AbstractProtobufProcessInstanceMarshaller method writeVariableScope.

protected void writeVariableScope(MarshallerWriteContext context, WorkflowProcessInstanceImpl workFlow, AutomatikoMessages.ProcessInstance.Builder _instance) throws IOException {
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) workFlow.getContextInstance(VariableScope.VARIABLE_SCOPE);
    List<Map.Entry<String, Object>> variables = new ArrayList<Map.Entry<String, Object>>(variableScopeInstance.getVariables().entrySet());
    Collections.sort(variables, new Comparator<Map.Entry<String, Object>>() {

        public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
            return o1.getKey().compareTo(o2.getKey());
        }
    });
    for (Map.Entry<String, Object> variable : variables) {
        if (variable.getValue() != null) {
            Variable v = variableScopeInstance.getVariableScope().findVariable(variable.getKey());
            if (v != null && v.hasTag(Variable.TRANSIENT_TAG)) {
                continue;
            }
            _instance.addVariable(ProtobufProcessMarshaller.marshallVariable(context, variable.getKey(), variable.getValue(), true));
        }
    }
}
Also used : Entry(java.util.Map.Entry) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Variable

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

the class DmnDecisionInProcessTest method createProcess.

private ExecutableProcess createProcess(String namespace, String modelName, String decisionName) {
    DMNRuntime dmnRuntime = DmnRuntimeProvider.fromClassPath("PersonDecisions.dmn");
    DmnDecisionModel dmnDecisionModel = new DmnDecisionModel(dmnRuntime, namespace, modelName);
    ExecutableProcess process = new ExecutableProcess();
    process.setId("process");
    process.setName("Process");
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable1 = new Variable();
    variable1.setName("person");
    variable1.setType(new ObjectDataType(Person.class));
    variables.add(variable1);
    Variable variable2 = new Variable();
    variable2.setName("isAdult");
    variable2.setType(new BooleanDataType());
    variables.add(variable2);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    RuleSetNode ruleSetNode = new RuleSetNode();
    ruleSetNode.setName("RuleSetNode");
    ruleSetNode.setId(2);
    ruleSetNode.setRuleType(RuleSetNode.RuleType.decision(namespace, modelName, null));
    ruleSetNode.setLanguage(RuleSetNode.DMN_LANG);
    ruleSetNode.setDecisionModel(() -> dmnDecisionModel);
    ruleSetNode.addInMapping("Person", "person");
    ruleSetNode.addOutMapping("isAdult", "isAdult");
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    connect(startNode, ruleSetNode);
    connect(ruleSetNode, endNode);
    process.addNode(startNode);
    process.addNode(ruleSetNode);
    process.addNode(endNode);
    return process;
}
Also used : StartNode(io.automatiko.engine.workflow.process.core.node.StartNode) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) EndNode(io.automatiko.engine.workflow.process.core.node.EndNode) RuleSetNode(io.automatiko.engine.workflow.process.core.node.RuleSetNode) DmnDecisionModel(io.automatiko.engine.decision.dmn.DmnDecisionModel) ArrayList(java.util.ArrayList) ExecutableProcess(io.automatiko.engine.workflow.process.executable.core.ExecutableProcess) ObjectDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType) BooleanDataType(io.automatiko.engine.workflow.base.core.datatype.impl.type.BooleanDataType) DMNRuntime(org.kie.dmn.api.core.DMNRuntime)

Example 5 with Variable

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

the class LightProcessRuntimeContext method setupParameters.

@Override
public void setupParameters(ProcessInstance processInstance, Map<String, Object> parameters, VariableInitializer variableInitializer) {
    Map<String, Object> variables = new HashMap<>();
    Process process = processInstance.getProcess();
    VariableScope variableScope = (VariableScope) ((ContextContainer) process).getDefaultContext(VariableScope.VARIABLE_SCOPE);
    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    // set input parameters
    if (parameters != null) {
        if (variableScope != null) {
            for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                variableScope.validateVariable(process.getName(), entry.getKey(), entry.getValue());
                variables.put(entry.getKey(), entry.getValue());
            }
        } else {
            throw new IllegalArgumentException("This process does not support parameters!");
        }
    }
    for (Variable var : variableScope.getVariables()) {
        if ((var.hasTag(Variable.AUTO_INITIALIZED_TAG) || var.getMetaData(Variable.DEFAULT_VALUE) != null) && variables.get(var.getName()) == null) {
            Object value = variableInitializer.initialize(process, var, variables);
            variableScope.validateVariable(process.getName(), var.getName(), value);
            variableScopeInstance.setVariable(var.getName(), value);
        }
        if (var.hasTag(Variable.INITIATOR_TAG) && variables.get(var.getName()) != null) {
            processInstance.setInitiator(variables.get(var.getName()).toString());
        }
    }
}
Also used : Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) Process(io.automatiko.engine.api.definition.process.Process) HashMap(java.util.HashMap) Map(java.util.Map) VariableScope(io.automatiko.engine.workflow.base.core.context.variable.VariableScope)

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