use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.
the class StatementVisitor method compileMVEL.
private TypedExpression compileMVEL(Node n) {
TypedExpression rhs = new RHSPhase(mvelCompilerContext).invoke(n);
TypedExpression lhs = new LHSPhase(mvelCompilerContext, ofNullable(rhs)).invoke(n);
Optional<TypedExpression> postProcessedRHS = new ReProcessRHSPhase(mvelCompilerContext).invoke(rhs, lhs);
TypedExpression postProcessedLHS = postProcessedRHS.map(ppr -> new LHSPhase(mvelCompilerContext, of(ppr)).invoke(n)).orElse(lhs);
return postProcessedLHS;
}
use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.
the class StatementVisitor method visit.
@Override
public TypedExpression visit(SwitchStmt n, Void arg) {
TypedExpression typedSelector = new RHSPhase(mvelCompilerContext).invoke(n.getSelector());
List<TypedExpression> typedEntries = n.getEntries().stream().map(e -> e.accept(this, arg)).collect(Collectors.toList());
return new SwitchStmtT(typedSelector, typedEntries);
}
use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.
the class CompilerTest method test.
default void test(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledBlockResult> resultAssert) {
Set<String> imports = new HashSet<>();
imports.add("java.util.List");
imports.add("java.util.ArrayList");
imports.add("java.util.HashMap");
imports.add("java.util.Map");
imports.add("java.math.BigDecimal");
imports.add("org.drools.Address");
imports.add(Person.class.getCanonicalName());
imports.add(Gender.class.getCanonicalName());
TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
testFunction.accept(mvelCompilerContext);
CompiledBlockResult compiled = new MvelCompiler(mvelCompilerContext).compileStatement(inputExpression);
verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
resultAssert.accept(compiled);
}
use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.
the class ConstraintCompilerTest method testExpression.
public void testExpression(Consumer<MvelCompilerContext> testFunction, String inputExpression, String expectedResult, Consumer<CompiledExpressionResult> resultAssert) {
Set<String> imports = new HashSet<>();
imports.add("java.util.List");
imports.add("java.util.ArrayList");
imports.add("java.util.HashMap");
imports.add("java.util.Map");
imports.add("java.math.BigDecimal");
imports.add("org.drools.Address");
imports.add(Person.class.getCanonicalName());
imports.add(Gender.class.getCanonicalName());
TypeResolver typeResolver = new ClassTypeResolver(imports, this.getClass().getClassLoader());
MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(typeResolver);
testFunction.accept(mvelCompilerContext);
CompiledExpressionResult compiled = new ConstraintCompiler(mvelCompilerContext).compileExpression(inputExpression);
verifyBodyWithBetterDiff(expectedResult, compiled.resultAsString());
resultAssert.accept(compiled);
}
use of org.drools.mvelcompiler.context.MvelCompilerContext in project drools by kiegroup.
the class DrlxParseUtil method createConstraintCompiler.
public static ConstraintCompiler createConstraintCompiler(RuleContext context, Optional<Class<?>> originalPatternType) {
MvelCompilerContext mvelCompilerContext = new MvelCompilerContext(context.getTypeResolver(), context.getCurrentScopeSuffix());
List<DeclarationSpec> allDeclarations = new ArrayList<>(context.getAllDeclarations());
originalPatternType.ifPresent(pt -> {
allDeclarations.add(new DeclarationSpec(THIS_PLACEHOLDER, pt));
mvelCompilerContext.setRootPatternPrefix(pt, THIS_PLACEHOLDER);
});
for (Map.Entry<String, Method> m : context.getPackageModel().getStaticMethods().entrySet()) {
mvelCompilerContext.addStaticMethod(m.getKey(), m.getValue());
}
for (DeclarationSpec ds : allDeclarations) {
mvelCompilerContext.addDeclaration(ds.getBindingId(), ds.getDeclarationClass());
}
return new ConstraintCompiler(mvelCompilerContext);
}
Aggregations