use of org.mvel2.templates.res.Node in project mvel by mikebrock.
the class MacroProcessorTest method testMacroSupportWithDebugging.
public void testMacroSupportWithDebugging() {
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("foo", new Foo());
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
Map<String, Macro> macros = new HashMap<String, Macro>();
interceptors.put("Modify", new Interceptor() {
public int doBefore(ASTNode node, VariableResolverFactory factory) {
((WithNode) node).getNestedStatement().getValue(null, factory);
factory.createVariable("mod", "FOOBAR!");
return 0;
}
public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
return 0;
}
});
macros.put("modify", new Macro() {
public String doMacro() {
return "@Modify with";
}
});
ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("System.out.println('hello');\n" + "System.out.println('bye');\n" + "modify (foo) { aValue = 'poo', \n" + " aValue = 'poo' };\n mod", macros));
// compiler.setDebugSymbols(true);
ParserContext ctx = new ParserContext(null, interceptors, null);
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
CompiledExpression compiled = compiler.compile(ctx);
MVELRuntime.setThreadDebugger(new Debugger() {
public int onBreak(Frame frame) {
System.out.println(frame.getSourceName() + ":" + frame.getLineNumber());
return Debugger.STEP;
}
});
MVELRuntime.registerBreakpoint("test.mv", 3);
System.out.println(DebugTools.decompile(compiled));
Assert.assertEquals("FOOBAR!", MVEL.executeDebugger(compiled, null, new MapVariableResolverFactory(vars)));
}
use of org.mvel2.templates.res.Node in project mvel by mikebrock.
the class CoreConfidenceTests method testInterceptors.
public void testInterceptors() {
Interceptor testInterceptor = new Interceptor() {
public int doBefore(ASTNode node, VariableResolverFactory factory) {
System.out.println("BEFORE Node: " + node.getName());
return 0;
}
public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
System.out.println("AFTER Node: " + node.getName());
return 0;
}
};
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
interceptors.put("test", testInterceptor);
executeExpression(compileExpression("@test System.out.println('MIDDLE');", null, interceptors));
}
use of org.mvel2.templates.res.Node in project mvel by mikebrock.
the class TemplateTests method testPluginNode.
public void testPluginNode() {
Map<String, Class<? extends Node>> plugins = new HashMap<String, Class<? extends Node>>();
plugins.put("testNode", TestPluginNode.class);
TemplateCompiler compiler = new TemplateCompiler("Foo:@testNode{}!!", plugins);
CompiledTemplate compiled = compiler.compile();
assertEquals("Foo:THIS_IS_A_TEST!!", TemplateRuntime.execute(compiled));
}
use of org.mvel2.templates.res.Node in project drools by kiegroup.
the class ConditionAnalyzer method analyzeSingleCondition.
private SingleCondition analyzeSingleCondition(ASTNode node, boolean isNegated) {
SingleCondition condition = new SingleCondition(isNegated);
if (node instanceof BinaryOperation) {
BinaryOperation binaryOperation = (BinaryOperation) node;
condition.left = analyzeNode(binaryOperation.getLeft());
condition.operation = BooleanOperator.fromMvelOpCode(binaryOperation.getOperation());
condition.right = analyzeNode(binaryOperation.getRight());
} else if (node instanceof RegExMatch) {
condition.left = analyzeNode(node);
condition.operation = BooleanOperator.MATCHES;
RegExMatch regExNode = (RegExMatch) node;
Pattern pattern = regExNode.getPattern();
if (pattern != null) {
condition.right = new FixedExpression(String.class, pattern.pattern());
} else {
condition.right = analyzeNode(((ExecutableAccessor) regExNode.getPatternStatement()).getNode());
}
} else if (node instanceof Contains) {
condition.left = analyzeNode(((Contains) node).getFirstStatement());
condition.operation = BooleanOperator.CONTAINS;
condition.right = analyzeNode(((Contains) node).getSecondStatement());
} else if (node instanceof Soundslike) {
condition.left = analyzeNode(((Soundslike) node).getStatement());
condition.operation = BooleanOperator.SOUNDSLIKE;
condition.right = analyzeNode(((Soundslike) node).getSoundslike());
} else if (node instanceof Instance) {
condition.left = analyzeNode(((Instance) node).getStatement());
condition.operation = BooleanOperator.INSTANCEOF;
condition.right = analyzeNode(((Instance) node).getClassStatement());
} else {
condition.left = analyzeNode(node);
}
return condition;
}
use of org.mvel2.templates.res.Node in project drools by kiegroup.
the class ModifyInterceptor method calculateModificationMask.
private void calculateModificationMask(KnowledgeHelper knowledgeHelper, WithNode node) {
Class<?> nodeClass = node.getEgressType();
TypeDeclaration typeDeclaration = knowledgeHelper.getWorkingMemory().getKnowledgeBase().getTypeDeclaration(nodeClass);
if (typeDeclaration == null || !typeDeclaration.isPropertyReactive()) {
modificationMask = allSetButTraitBitMask();
return;
}
List<String> settableProperties = typeDeclaration.getAccessibleProperties();
modificationMask = getEmptyPropertyReactiveMask(settableProperties.size());
// TODO: access parmValuePairs without reflection
WithNode.ParmValuePair[] parmValuePairs = getFieldValue(WithNode.class, "withExpressions", node);
for (WithNode.ParmValuePair parmValuePair : parmValuePairs) {
Method method = extractMethod(parmValuePair);
if (method == null) {
modificationMask = allSetButTraitBitMask();
return;
}
String propertyName = setter2property(method.getName());
if (propertyName != null) {
int index = settableProperties.indexOf(propertyName);
if (index >= 0) {
modificationMask = setPropertyOnMask(modificationMask, index);
}
}
List<String> modifiedProps = typeDeclaration.getTypeClassDef().getModifiedPropsByMethod(method);
if (modifiedProps != null) {
for (String modifiedProp : modifiedProps) {
int index = settableProperties.indexOf(modifiedProp);
if (index >= 0) {
modificationMask = setPropertyOnMask(modificationMask, index);
}
}
}
}
}
Aggregations