use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.
the class MVELExprAnalyzer method analyze.
/**
* Analyse an expression.
* @throws RecognitionException
* If an error occurs in the parser.
*/
private static MVELAnalysisResult analyze(final Set<String> identifiers, final BoundIdentifiers availableIdentifiers) {
MVELAnalysisResult result = new MVELAnalysisResult();
result.setIdentifiers(identifiers);
final Set<String> notBound = new HashSet<String>(identifiers);
notBound.remove("this");
Map<String, Class<?>> usedDecls = new HashMap<String, Class<?>>();
Map<String, Class<?>> usedGlobals = new HashMap<String, Class<?>>();
Map<String, EvaluatorWrapper> usedOperators = new HashMap<String, EvaluatorWrapper>();
for (Entry<String, Class<?>> entry : availableIdentifiers.getDeclrClasses().entrySet()) {
if (identifiers.contains(entry.getKey())) {
usedDecls.put(entry.getKey(), entry.getValue());
notBound.remove(entry.getKey());
}
}
for (String identifier : identifiers) {
Class<?> type = availableIdentifiers.resolveVarType(identifier);
if (type != null) {
usedGlobals.put(identifier, type);
notBound.remove(identifier);
}
}
for (Map.Entry<String, EvaluatorWrapper> op : availableIdentifiers.getOperators().entrySet()) {
if (identifiers.contains(op.getKey())) {
usedOperators.put(op.getKey(), op.getValue());
notBound.remove(op.getKey());
}
}
BoundIdentifiers boundIdentifiers = new BoundIdentifiers(usedDecls, availableIdentifiers.getContext(), usedOperators, availableIdentifiers.getThisClass());
boundIdentifiers.setGlobals(usedGlobals);
result.setBoundIdentifiers(boundIdentifiers);
result.setNotBoundedIdentifiers(notBound);
return result;
}
use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.
the class MvelConditionEvaluator method evaluate.
private boolean evaluate(ExecutableStatement statement, InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple) {
if (compilationUnit == null) {
Map<String, Object> vars = valuesAsMap(handle.getObject(), workingMemory, tuple, declarations);
if (operators.length > 0) {
if (vars == null) {
vars = new HashMap<String, Object>();
}
InternalFactHandle[] handles = tuple != null ? tuple.toFactHandles() : new InternalFactHandle[0];
for (EvaluatorWrapper operator : operators) {
vars.put(operator.getBindingName(), operator);
operator.loadHandles(workingMemory, handles, handle);
}
}
return evaluate(statement, handle.getObject(), vars);
}
VariableResolverFactory factory = compilationUnit.createFactory();
compilationUnit.updateFactory(handle, tuple, null, workingMemory, workingMemory.getGlobalResolver(), factory);
return (Boolean) MVELSafeHelper.getEvaluator().executeExpression(statement, handle.getObject(), factory);
}
use of org.drools.core.base.EvaluatorWrapper in project drools by kiegroup.
the class MVELCompilationUnit method updateFactory.
private void updateFactory(Object knowledgeHelper, Declaration[] prevDecl, Rule rule, InternalFactHandle rightHandle, Object rightObject, Tuple tuple, Object[] otherVars, InternalWorkingMemory workingMemory, GlobalResolver globals, VariableResolverFactory factory) {
int varLength = inputIdentifiers.length;
int i = 0;
if ("this".equals(inputIdentifiers[0])) {
factory.getIndexedVariableResolver(i++).setValue(rightObject);
}
factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
factory.getIndexedVariableResolver(i++).setValue(knowledgeHelper);
if (inputIdentifiers.length > i && "rule".equals(inputIdentifiers[i])) {
factory.getIndexedVariableResolver(i++).setValue(rule);
}
if (globalIdentifiers != null) {
for (String globalIdentifier : globalIdentifiers) {
factory.getIndexedVariableResolver(i++).setValue(globals.resolveGlobal(globalIdentifier));
}
}
InternalFactHandle[] handles = tuple instanceof LeftTuple ? ((LeftTuple) tuple).toFactHandles() : null;
if (operators.length > 0) {
for (EvaluatorWrapper operator : operators) {
// TODO: need to have one operator per working memory
factory.getIndexedVariableResolver(i++).setValue(operator);
operator.loadHandles(workingMemory, handles, rightHandle);
}
}
Object[] objs = null;
if (tuple != null) {
if (handles == null) {
objs = tuple.toObjects();
}
if (this.previousDeclarations != null && this.previousDeclarations.length > 0) {
// Consequences with 'or's will have different declaration offsets, so use the one's from the RTN's subrule.
if (prevDecl == null) {
// allows the caller to override the member var
// used for rules, salience and timers so they work with 'or' CEs
prevDecl = this.previousDeclarations;
}
for (Declaration decl : prevDecl) {
int offset = decl.getPattern().getOffset();
Object o = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
factory.getIndexedVariableResolver(i++).setValue(o);
}
}
}
if (this.localDeclarations != null && this.localDeclarations.length > 0) {
for (Declaration decl : this.localDeclarations) {
Object value;
if (readLocalsFromTuple && tuple != null) {
int offset = decl.getPattern().getOffset();
value = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
} else {
value = decl.getValue(workingMemory, rightObject);
}
factory.getIndexedVariableResolver(i++).setValue(value);
}
}
int otherVarsPos = 0;
if (otherVars != null) {
otherVarsPos = i;
for (Object o : otherVars) {
factory.getIndexedVariableResolver(i++).setValue(o);
}
}
int otherVarsLength = i - otherVarsPos;
for (i = varLength; i < this.allVarsLength; i++) {
// null all local vars
factory.getIndexedVariableResolver(i).setValue(null);
}
DroolsVarFactory df = (DroolsVarFactory) factory.getNextFactory();
df.setOtherVarsPos(otherVarsPos);
df.setOtherVarsLength(otherVarsLength);
if (knowledgeHelper instanceof KnowledgeHelper) {
KnowledgeHelper kh = (KnowledgeHelper) knowledgeHelper;
df.setKnowledgeHelper(kh);
}
}
Aggregations