Search in sources :

Example 11 with DataType

use of io.automatiko.engine.api.workflow.datatype.DataType 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());
                    }
                }
            }
        }
    }
}
Also used : Transformation(io.automatiko.engine.workflow.process.core.node.Transformation) Serializable(java.io.Serializable) Variable(io.automatiko.engine.workflow.base.core.context.variable.Variable) RuleSetNode(io.automatiko.engine.workflow.process.core.node.RuleSetNode) DataAssociation(io.automatiko.engine.workflow.process.core.node.DataAssociation) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ExpressionEvaluator(io.automatiko.engine.api.expression.ExpressionEvaluator) DataTransformer(io.automatiko.engine.api.runtime.process.DataTransformer) VariableScopeInstance(io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DataType(io.automatiko.engine.api.workflow.datatype.DataType) WorkflowProcess(io.automatiko.engine.workflow.process.core.WorkflowProcess)

Example 12 with DataType

use of io.automatiko.engine.api.workflow.datatype.DataType in project automatiko-engine by automatiko-io.

the class ModelMetaData method applyPersistence.

private void applyPersistence(DataType dataType, FieldDeclaration fd, List<String> tags) {
    if (asEntity) {
        NodeList<Expression> cascade;
        if (removeAtCompletion) {
            cascade = NodeList.nodeList(new NameExpr("javax.persistence.CascadeType.ALL"));
        } else {
            cascade = NodeList.nodeList(new NameExpr("javax.persistence.CascadeType.PERSIST"), new NameExpr("javax.persistence.CascadeType.MERGE"), new NameExpr("javax.persistence.CascadeType.REFRESH"), new NameExpr("javax.persistence.CascadeType.DETACH"));
        }
        Type type = fd.getVariable(0).getType();
        if (type.isArrayType() || Collection.class.isAssignableFrom(dataType.getClassType())) {
            fd.addAnnotation(new NormalAnnotationExpr(new Name("javax.persistence.OneToMany"), NodeList.nodeList(new MemberValuePair("cascade", new ArrayInitializerExpr(cascade)), new MemberValuePair("fetch", new NameExpr("javax.persistence.FetchType.EAGER")))));
        } else if (dataType.getClassType() != null && Stream.of(dataType.getClassType().getAnnotations()).anyMatch(a -> a.annotationType().getName().equals("javax.persistence.Entity"))) {
            fd.addAnnotation(new NormalAnnotationExpr(new Name("javax.persistence.OneToOne"), NodeList.nodeList(new MemberValuePair("cascade", new ArrayInitializerExpr(cascade)), new MemberValuePair("fetch", new NameExpr("javax.persistence.FetchType.EAGER")))));
            fd.addAnnotation(new NormalAnnotationExpr(new Name("javax.persistence.JoinColumn"), NodeList.nodeList()));
        } else {
            fd.addAnnotation(new NormalAnnotationExpr(new Name("javax.persistence.Column"), NodeList.nodeList(new MemberValuePair("name", new StringLiteralExpr(camelToSnake(fd.getVariable(0).getNameAsString().toUpperCase()))))));
        }
    }
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DataType(io.automatiko.engine.api.workflow.datatype.DataType) Type(com.github.javaparser.ast.type.Type) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Collection(java.util.Collection) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) Name(com.github.javaparser.ast.expr.Name)

Aggregations

DataType (io.automatiko.engine.api.workflow.datatype.DataType)12 ObjectDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.ObjectDataType)6 Variable (io.automatiko.engine.workflow.base.core.context.variable.Variable)5 ItemDefinition (io.automatiko.engine.workflow.bpmn2.core.ItemDefinition)5 Map (java.util.Map)5 BooleanDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.BooleanDataType)4 FloatDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.FloatDataType)4 IntegerDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.IntegerDataType)4 StringDataType (io.automatiko.engine.workflow.base.core.datatype.impl.type.StringDataType)4 VariableScopeInstance (io.automatiko.engine.workflow.base.instance.context.variable.VariableScopeInstance)4 DataAssociation (io.automatiko.engine.workflow.process.core.node.DataAssociation)4 NodeInstanceResolverFactory (io.automatiko.engine.workflow.process.instance.impl.NodeInstanceResolverFactory)4 Serializable (java.io.Serializable)4 HashMap (java.util.HashMap)4 Matcher (java.util.regex.Matcher)4 VariableScope (io.automatiko.engine.workflow.base.core.context.variable.VariableScope)3 ExpressionEvaluator (io.automatiko.engine.api.expression.ExpressionEvaluator)2 DataTransformer (io.automatiko.engine.api.runtime.process.DataTransformer)2 NamedDataType (io.automatiko.engine.api.workflow.NamedDataType)2 EventTransformer (io.automatiko.engine.workflow.base.core.event.EventTransformer)2