use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeAccessorInvocation.
private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
if (accessorNode instanceof GetterAccessor) {
return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
}
if (accessorNode instanceof MethodAccessor) {
MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
Method method = methodAccessor.getMethod();
MethodInvocation invocation = new MethodInvocation(method);
boolean isVarArgs = method.isVarArgs();
readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
return invocation;
}
if (accessorNode instanceof ConstructorAccessor) {
ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
Constructor constructor = constructorAccessor.getConstructor();
ConstructorInvocation invocation = new ConstructorInvocation(constructor);
readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
return invocation;
}
if (accessorNode instanceof ArrayAccessor) {
ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
}
if (accessorNode instanceof ArrayAccessorNest) {
ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
}
if (accessorNode instanceof ArrayLength) {
return new ArrayLengthInvocation();
}
if (accessorNode instanceof ListAccessor) {
Class<?> listType = getListType(formerInvocation);
ListAccessor listAccessor = (ListAccessor) accessorNode;
return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
}
if (accessorNode instanceof ListAccessorNest) {
Class<?> listType = getListType(formerInvocation);
ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
}
if (accessorNode instanceof MapAccessor) {
MapAccessor mapAccessor = (MapAccessor) accessorNode;
return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
}
if (accessorNode instanceof MapAccessorNest) {
Class<?> keyType = Object.class;
Class<?> valueType = Object.class;
Type[] generics = getGenerics(formerInvocation);
if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
keyType = (Class<?>) generics[0];
if (generics[1] instanceof Class)
valueType = (Class<?>) generics[1];
}
MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
ExecutableStatement statement = mapAccessor.getProperty();
if (statement instanceof ExecutableLiteral) {
return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
} else {
return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
}
}
if (accessorNode instanceof FieldAccessor) {
return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
}
if (accessorNode instanceof StaticVarAccessor) {
Field field = ((StaticVarAccessor) accessorNode).getField();
return new FieldAccessInvocation(field);
}
if (accessorNode instanceof ThisValueAccessor) {
return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
}
throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class MvelConditionEvaluator method ensureCompleteEvaluation.
private void ensureCompleteEvaluation(InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple) {
if (!evaluated) {
ASTNode rootNode = getRootNode();
if (rootNode != null) {
ensureCompleteEvaluation(rootNode, handle, workingMemory, tuple);
}
evaluated = true;
}
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeAccessorInvocation.
private Invocation analyzeAccessorInvocation(AccessorNode accessorNode, ASTNode containingNode, Invocation formerInvocation, Class<?> variableType) {
if (accessorNode instanceof GetterAccessor) {
return new MethodInvocation(((GetterAccessor) accessorNode).getMethod(), variableType == null ? conditionClass : variableType.getName());
}
if (accessorNode instanceof MethodAccessor) {
MethodAccessor methodAccessor = (MethodAccessor) accessorNode;
Method method = methodAccessor.getMethod();
MethodInvocation invocation = new MethodInvocation(method);
boolean isVarArgs = method.isVarArgs();
readInvocationParams(invocation, methodAccessor.getParms(), methodAccessor.getParameterTypes(), isVarArgs);
return invocation;
}
if (accessorNode instanceof ConstructorAccessor) {
ConstructorAccessor constructorAccessor = (ConstructorAccessor) accessorNode;
Constructor constructor = constructorAccessor.getConstructor();
ConstructorInvocation invocation = new ConstructorInvocation(constructor);
readInvocationParams(invocation, constructorAccessor.getParameters(), constructorAccessor.getParameterTypes(), constructor.isVarArgs());
return invocation;
}
if (accessorNode instanceof ArrayAccessor) {
ArrayAccessor arrayAccessor = (ArrayAccessor) accessorNode;
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, new FixedExpression(int.class, arrayAccessor.getIndex()));
}
if (accessorNode instanceof ArrayAccessorNest) {
ArrayAccessorNest arrayAccessorNest = (ArrayAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) arrayAccessorNest.getIndex();
return new ArrayAccessInvocation(formerInvocation != null ? formerInvocation.getReturnType() : Object[].class, analyzeNode(index.getNode()));
}
if (accessorNode instanceof ArrayLength) {
return new ArrayLengthInvocation();
}
if (accessorNode instanceof ListAccessor) {
Class<?> listType = getListType(formerInvocation);
ListAccessor listAccessor = (ListAccessor) accessorNode;
return new ListAccessInvocation(listType, new FixedExpression(int.class, listAccessor.getIndex()));
}
if (accessorNode instanceof ListAccessorNest) {
Class<?> listType = getListType(formerInvocation);
ListAccessorNest listAccessorNest = (ListAccessorNest) accessorNode;
ExecutableAccessor index = (ExecutableAccessor) listAccessorNest.getIndex();
return new ListAccessInvocation(listType, analyzeNode(index.getNode()));
}
if (accessorNode instanceof MapAccessor) {
MapAccessor mapAccessor = (MapAccessor) accessorNode;
return new MapAccessInvocation(Object.class, Object.class, new FixedExpression(Object.class, mapAccessor.getProperty()));
}
if (accessorNode instanceof MapAccessorNest) {
Class<?> keyType = Object.class;
Class<?> valueType = Object.class;
Type[] generics = getGenerics(formerInvocation);
if (generics != null && generics.length == 2 && generics[0] instanceof Class) {
keyType = (Class<?>) generics[0];
if (generics[1] instanceof Class)
valueType = (Class<?>) generics[1];
}
MapAccessorNest mapAccessor = (MapAccessorNest) accessorNode;
ExecutableStatement statement = mapAccessor.getProperty();
if (statement instanceof ExecutableLiteral) {
return new MapAccessInvocation(keyType, valueType, new FixedExpression(keyType, ((ExecutableLiteral) statement).getLiteral()));
} else {
return new MapAccessInvocation(keyType, valueType, analyzeNode(((ExecutableAccessor) statement).getNode()));
}
}
if (accessorNode instanceof FieldAccessor) {
return new FieldAccessInvocation(((FieldAccessor) accessorNode).getField());
}
if (accessorNode instanceof StaticVarAccessor) {
Field field = ((StaticVarAccessor) accessorNode).getField();
return new FieldAccessInvocation(field);
}
if (accessorNode instanceof ThisValueAccessor) {
return new ThisInvocation(accessorNode.getNextNode() == null ? containingNode.getEgressType() : Object.class);
}
throw new RuntimeException("Unknown AccessorNode type: " + accessorNode.getClass().getName());
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeNode.
private Expression analyzeNode(ASTNode node) {
node = analyzeRegEx(analyzeSubstatement(node));
if (node instanceof LiteralNode) {
LiteralNode literalNode = (LiteralNode) node;
return new FixedExpression(literalNode.getEgressType(), literalNode.getLiteralValue());
}
if (node instanceof BinaryOperation) {
BinaryOperation op = (BinaryOperation) node;
return new AritmeticExpression(analyzeNode(op.getLeft()), AritmeticOperator.fromMvelOpCode(op), analyzeNode(op.getRight()));
}
if (node instanceof TypeCast) {
ExecutableStatement statement = ((TypeCast) node).getStatement();
if (statement instanceof ExecutableAccessor) {
ExecutableAccessor accessor = (ExecutableAccessor) statement;
return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode()));
} else {
ExecutableLiteral literal = (ExecutableLiteral) statement;
return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral()));
}
}
if (node instanceof Union) {
ASTNode main = ((Union) node).getMain();
Accessor accessor = node.getAccessor();
EvaluatedExpression expression = new EvaluatedExpression();
expression.firstExpression = analyzeNode(main);
if (accessor instanceof DynamicGetAccessor) {
AccessorNode accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
} else if (accessor instanceof AccessorNode) {
AccessorNode accessorNode = (AccessorNode) accessor;
while (accessorNode != null) {
expression.addInvocation(analyzeAccessorInvocation(accessorNode, node, null, null));
accessorNode = accessorNode.getNextNode();
}
} else {
throw new RuntimeException("Unexpected accessor: " + accessor);
}
return expression;
}
if (node instanceof Sign) {
ExecutableStatement statement = getFieldValue(Sign.class, "stmt", (Sign) node);
if (statement instanceof ExecutableAccessor) {
ExecutableAccessor accessor = (ExecutableAccessor) statement;
return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, analyzeNode(accessor.getNode()));
} else {
ExecutableLiteral literal = (ExecutableLiteral) statement;
return new AritmeticExpression(new FixedExpression(0), AritmeticOperator.SUB, new FixedExpression(literal.getLiteral()));
}
}
Accessor accessor = node.getAccessor();
if (accessor instanceof IndexedVariableAccessor) {
String variableName = node.getName();
int dot = variableName.indexOf('.');
if (dot > 0) {
variableName = variableName.substring(0, dot);
}
Class<?> variableType = getVariableType(variableName);
return new VariableExpression(variableName, analyzeExpressionNode(((AccessorNode) accessor).getNextNode(), node, variableType), variableType != null ? variableType : node.getEgressType());
}
if (accessor == null && node instanceof NewObjectNode) {
accessor = ((NewObjectNode) node).getNewObjectOptimizer();
}
if (accessor instanceof VariableAccessor) {
VariableAccessor variableAccessor = (VariableAccessor) accessor;
AccessorNode accessorNode = variableAccessor.getNextNode();
if (accessorNode == null || !isStaticAccessor(accessorNode)) {
String variableName = (String) (variableAccessor.getProperty());
Class<?> variableType = getVariableType(variableName);
if (variableType != null) {
return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType);
} else {
if (node.getLiteralValue() instanceof ParserContext) {
ParserContext pCtx = (ParserContext) node.getLiteralValue();
// it's not a variable but a method invocation on this
Class<?> thisClass = pCtx.getInputs().get("this");
try {
return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName)));
} catch (NoSuchMethodException e) {
if (node.getEgressType() == Class.class) {
// there's no method on this with the given name, check if it is a class literal
Class<?> classLiteral = pCtx.getParserConfiguration().getImport(variableName);
if (classLiteral != null) {
return new FixedExpression(Class.class, classLiteral);
}
}
throw new RuntimeException(e);
}
}
}
}
}
if (accessor == null) {
throw new RuntimeException("Null accessor on node: " + node);
}
return analyzeNodeAccessor(accessor, node);
}
use of org.mvel2.ast.ASTNode in project drools by kiegroup.
the class ConditionAnalyzer method analyzeNodeAccessor.
private Expression analyzeNodeAccessor(Accessor accessor, ASTNode node) {
AccessorNode accessorNode;
if (accessor instanceof DynamicGetAccessor) {
accessorNode = (AccessorNode) ((DynamicGetAccessor) accessor).getSafeAccessor();
} else if (accessor instanceof AccessorNode) {
accessorNode = (AccessorNode) accessor;
} else if (accessor instanceof CompiledExpression) {
return analyzeNode(((CompiledExpression) accessor).getFirstNode());
} else if (accessor instanceof ListCreator) {
return analyzeListCreation(((ListCreator) accessor));
} else if (accessor instanceof ArrayCreator) {
return analyzeArrayCreation(((ArrayCreator) accessor));
} else {
throw new RuntimeException("Unknown accessor type: " + accessor);
}
if (accessorNode instanceof VariableAccessor) {
if (isStaticAccessor(accessorNode)) {
while (accessorNode instanceof VariableAccessor) {
accessorNode = accessorNode.getNextNode();
}
} else {
return analyzeNodeAccessor(accessorNode, node);
}
}
while (accessorNode instanceof StaticReferenceAccessor) {
StaticReferenceAccessor staticReferenceAccessor = ((StaticReferenceAccessor) accessorNode);
Object literal = staticReferenceAccessor.getLiteral();
accessorNode = accessorNode.getNextNode();
if (accessorNode == null) {
return new FixedExpression(literal.getClass(), literal);
}
}
return analyzeExpressionNode(accessorNode, node, null);
}
Aggregations