use of org.drools.compiler.compiler.BoundIdentifiers 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.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class MVELObjectExpressionBuilder method build.
public static MVELObjectExpression build(String expression, RuleBuildContext context) {
boolean typesafe = context.isTypesafe();
// pushing consequence LHS into the stack for variable resolution
context.getDeclarationResolver().pushOnBuildStack(context.getRule().getLhs());
try {
// This builder is re-usable in other dialects, so specify by name
MVELDialect dialect = (MVELDialect) context.getDialect("mvel");
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
MVELAnalysisResult analysis = (MVELAnalysisResult) dialect.analyzeExpression(context, context.getRuleDescr(), expression, new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
context.setTypesafe(analysis.isTypesafe());
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
int i = usedIdentifiers.getDeclrClasses().keySet().size();
Declaration[] previousDeclarations = new Declaration[i];
i = 0;
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
previousDeclarations[i++] = decls.get(id);
}
Arrays.sort(previousDeclarations, RuleTerminalNode.SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(expression, analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELObjectExpression expr = new MVELObjectExpression(unit, dialect.getId());
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data);
return expr;
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression : " + e.getMessage() + "'" + expression + "'"));
return null;
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class AbstractASMEvalBuilder method getUsedDeclarations.
private Declaration[] getUsedDeclarations(RuleBuildContext context, Pattern pattern, AnalysisResult analysis) {
BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final List<Declaration> declarations = new ArrayList<Declaration>();
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
declarations.add(context.getDeclarationResolver().getDeclaration(id));
}
createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), analysis.getBoundIdentifiers(), declarations);
return declarations.toArray(new Declaration[declarations.size()]);
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class AbstractASMEvalBuilder method build.
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr) {
// it must be an EvalDescr
final EvalDescr evalDescr = (EvalDescr) descr;
Map<String, Declaration> decls = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = context.getDialect().analyzeExpression(context, evalDescr, evalDescr.getContent(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context));
List<Declaration> requiredDeclarations = new ArrayList<Declaration>();
for (String usedIdentifier : analysis.getIdentifiers()) {
Declaration usedDec = decls.get(usedIdentifier);
if (usedDec != null) {
requiredDeclarations.add(usedDec);
}
}
final Declaration[] declarations = requiredDeclarations.toArray(new Declaration[requiredDeclarations.size()]);
return buildEval(context, evalDescr, analysis, declarations);
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class JavaAccumulateBuilder method buildAccumulator.
private Accumulator buildAccumulator(RuleBuildContext context, AccumulateDescr accumDescr, Map<String, Declaration> declsInScope, Map<String, Class<?>> declCls, boolean readLocalsFromTuple, Declaration[] sourceDeclArr, Set<Declaration> requiredDecl, AccumulateFunctionCallDescr fc, AccumulateFunction function) {
// analyze the expression
final JavaAnalysisResult analysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, fc.getParams().length > 0 ? fc.getParams()[0] : "\"\"", new BoundIdentifiers(declCls, context));
if (analysis == null) {
// not possible to get the analysis results - compilation error has been already logged
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
// create the array of used declarations
final Declaration[] previousDeclarations = collectRequiredDeclarations(declsInScope, requiredDecl, usedIdentifiers);
// generate the code template
return generateFunctionCallCodeTemplate(context, accumDescr, sourceDeclArr, fc, function, usedIdentifiers, previousDeclarations, readLocalsFromTuple);
}
Aggregations