use of org.jbpm.process.instance.impl.MVELReturnValueEvaluator in project jbpm by kiegroup.
the class MVELReturnValueConstraintEvaluatorBuilderTest method testSimpleReturnValueConstraintEvaluator.
@Test
public void testSimpleReturnValueConstraintEvaluator() throws Exception {
final InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
ReturnValueDescr descr = new ReturnValueDescr();
descr.setText("return value");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
PackageBuildContext context = new PackageBuildContext();
context.init(pkgBuilder, pkg, null, dialectRegistry, mvelDialect, null);
pkgBuilder.addPackageFromDrl(new StringReader("package pkg1;\nglobal Boolean value;"));
ReturnValueConstraintEvaluator node = new ReturnValueConstraintEvaluator();
final MVELReturnValueEvaluatorBuilder builder = new MVELReturnValueEvaluatorBuilder();
builder.build(context, node, descr, null);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(pkgBuilder.getPackages()));
final KieSession ksession = kbase.newKieSession();
ksession.setGlobal("value", true);
RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
processInstance.setKnowledgeRuntime((InternalKnowledgeRuntime) ksession);
SplitInstance splitInstance = new SplitInstance();
splitInstance.setProcessInstance(processInstance);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkgBuilder.getPackage("pkg1").getDialectRuntimeRegistry().getDialectData("mvel");
((MVELReturnValueEvaluator) node.getReturnValueEvaluator()).compile(data);
assertTrue(node.evaluate(splitInstance, null, null));
ksession.setGlobal("value", false);
assertFalse(node.evaluate(splitInstance, null, null));
}
use of org.jbpm.process.instance.impl.MVELReturnValueEvaluator in project jbpm by kiegroup.
the class MVELReturnValueEvaluatorBuilder method buildReturnValueEvaluator.
public void buildReturnValueEvaluator(final PackageBuildContext context, final ReturnValueConstraintEvaluator constraintNode, final ReturnValueDescr descr, final ContextResolver contextResolver, final MVELDialect dialect, final MVELAnalysisResult analysis, final String text, Map<String, Class<?>> variables) throws Exception {
Set<String> variableNames = analysis.getNotBoundedIdentifiers();
if (contextResolver != null) {
for (String variableName : variableNames) {
if (analysis.getMvelVariables().keySet().contains(variableName) || variableName.equals("kcontext") || variableName.equals("context")) {
continue;
}
VariableScope variableScope = (VariableScope) contextResolver.resolveContext(VariableScope.VARIABLE_SCOPE, variableName);
if (variableScope == null) {
context.getErrors().add(new DescrBuildError(context.getParentDescr(), descr, null, "Could not find variable '" + variableName + "' for action '" + descr.getText() + "'"));
} else {
variables.put(variableName, context.getDialect().getTypeResolver().resolveType(variableScope.findVariable(variableName).getType().getStringType()));
}
}
}
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(text, analysis, null, null, variables, context, "context", org.kie.api.runtime.process.ProcessContext.class, false, MVELCompilationUnit.Scope.EXPRESSION);
// MVELReturnValueExpression expr = new MVELReturnValueExpression( unit, context.getDialect().getId() );
MVELReturnValueEvaluator expr = new MVELReturnValueEvaluator(unit, dialect.getId());
// expr.setVariableNames(variableNames);
constraintNode.setEvaluator(expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData(dialect.getId());
data.addCompileable(constraintNode, expr);
expr.compile(data);
collectTypes("MVELReturnValue", analysis, (ProcessBuildContext) context);
}
Aggregations