use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class JavaRuleBuilderHelper method createJavaAnalysisResult.
public static JavaAnalysisResult createJavaAnalysisResult(final RuleBuildContext context, String consequenceName, Map<String, Declaration> decls) {
final RuleDescr ruleDescr = context.getRuleDescr();
BoundIdentifiers bindings = new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(decls), context, Collections.EMPTY_MAP, KnowledgeHelper.class);
String consequenceStr = (RuleImpl.DEFAULT_CONSEQUENCE_NAME.equals(consequenceName)) ? (String) ruleDescr.getConsequence() : (String) ruleDescr.getNamedConsequences().get(consequenceName);
consequenceStr = consequenceStr + "\n";
return (JavaAnalysisResult) context.getDialect().analyzeBlock(context, ruleDescr, consequenceStr, bindings);
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class MVELSalienceBuilder method build.
public void build(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(), context.getRuleDescr().getSalience(), 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, SortDeclarations.instance);
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(context.getRuleDescr().getSalience(), analysis, previousDeclarations, null, null, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELSalienceExpression expr = new MVELSalienceExpression(unit, dialect.getId());
context.getRule().setSalience(KiePolicyHelper.isPolicyEnabled() ? new SafeSalience(expr) : expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data, context.getRule());
} catch (final Exception e) {
copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'salience' : " + e.getMessage() + "'" + context.getRuleDescr().getSalience() + "'"));
} finally {
context.setTypesafe(typesafe);
}
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class PatternBuilder method getDeclarationsForReturnValue.
private Declaration[] getDeclarationsForReturnValue(RuleBuildContext context, RelationalExprDescr relDescr, String operator, String value2) {
Pattern pattern = (Pattern) context.getDeclarationResolver().peekBuildStack();
ReturnValueRestrictionDescr returnValueRestrictionDescr = new ReturnValueRestrictionDescr(operator, relDescr, value2);
AnalysisResult analysis = context.getDialect().analyzeExpression(context, returnValueRestrictionDescr, returnValueRestrictionDescr.getContent(), new BoundIdentifiers(pattern, context, null, pattern.getObjectType().getClassType()));
if (analysis == null) {
// something bad happened
return null;
}
final BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final List<Declaration> tupleDeclarations = new ArrayList<Declaration>();
final List<Declaration> factDeclarations = new ArrayList<Declaration>();
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
Declaration decl = context.getDeclarationResolver().getDeclaration(id);
if (decl.getPattern() == pattern) {
factDeclarations.add(decl);
} else {
tupleDeclarations.add(decl);
}
}
createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), usedIdentifiers, factDeclarations);
final Declaration[] previousDeclarations = tupleDeclarations.toArray(new Declaration[tupleDeclarations.size()]);
final Declaration[] localDeclarations = factDeclarations.toArray(new Declaration[factDeclarations.size()]);
Arrays.sort(previousDeclarations, SortDeclarations.instance);
Arrays.sort(localDeclarations, SortDeclarations.instance);
final String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
Declaration[] requiredDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length];
System.arraycopy(previousDeclarations, 0, requiredDeclarations, 0, previousDeclarations.length);
System.arraycopy(localDeclarations, 0, requiredDeclarations, previousDeclarations.length, localDeclarations.length);
Declaration[] declarations = new Declaration[requiredDeclarations.length + requiredGlobals.length];
int i = 0;
for (Declaration requiredDeclaration : requiredDeclarations) {
declarations[i++] = requiredDeclaration;
}
for (String requiredGlobal : requiredGlobals) {
declarations[i++] = context.getDeclarationResolver().getDeclaration(requiredGlobal);
}
return declarations;
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class PatternBuilder method getUsedDeclarations.
public static Declaration[][] getUsedDeclarations(RuleBuildContext context, Pattern pattern, AnalysisResult analysis) {
BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
final List<Declaration> tupleDeclarations = new ArrayList<Declaration>();
final List<Declaration> factDeclarations = new ArrayList<Declaration>();
for (String id : usedIdentifiers.getDeclrClasses().keySet()) {
Declaration decl = context.getDeclarationResolver().getDeclaration(id);
if (decl.getPattern() == pattern) {
factDeclarations.add(decl);
} else {
tupleDeclarations.add(decl);
}
}
createImplicitBindings(context, pattern, analysis.getNotBoundedIdentifiers(), analysis.getBoundIdentifiers(), factDeclarations);
Declaration[][] usedDeclarations = new Declaration[2][];
usedDeclarations[0] = tupleDeclarations.toArray(new Declaration[tupleDeclarations.size()]);
usedDeclarations[1] = factDeclarations.toArray(new Declaration[factDeclarations.size()]);
return usedDeclarations;
}
use of org.drools.compiler.compiler.BoundIdentifiers in project drools by kiegroup.
the class PatternBuilder method buildEval.
@SuppressWarnings("unchecked")
protected Constraint buildEval(final RuleBuildContext context, final Pattern pattern, final PredicateDescr predicateDescr, final Map<String, OperatorDescr> aliases, final String expr, final boolean isEvalExpression) {
AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
if (analysis == null) {
// something bad happened
return null;
}
Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
Declaration[] previousDeclarations = usedDeclarations[0];
Declaration[] localDeclarations = usedDeclarations[1];
BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
Arrays.sort(previousDeclarations, SortDeclarations.instance);
Arrays.sort(localDeclarations, SortDeclarations.instance);
boolean isJavaEval = isEvalExpression && context.getDialect() instanceof JavaDialect;
if (isJavaEval) {
final PredicateConstraint predicateConstraint = new PredicateConstraint(null, previousDeclarations, localDeclarations);
final PredicateBuilder builder = context.getDialect().getPredicateBuilder();
builder.build(context, usedIdentifiers, previousDeclarations, localDeclarations, predicateConstraint, predicateDescr, analysis);
return predicateConstraint;
}
MVELCompilationUnit compilationUnit = getConstraintBuilder(context).buildCompilationUnit(context, previousDeclarations, localDeclarations, predicateDescr, analysis);
String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
Declaration[] mvelDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length + requiredGlobals.length];
int i = 0;
for (Declaration d : previousDeclarations) {
mvelDeclarations[i++] = d;
}
for (Declaration d : localDeclarations) {
mvelDeclarations[i++] = d;
}
for (String global : requiredGlobals) {
mvelDeclarations[i++] = context.getDeclarationResolver().getDeclaration(global);
}
boolean isDynamic = !pattern.getObjectType().getClassType().isArray() && !context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType().getClassType()).isTypesafe();
return getConstraintBuilder(context).buildMvelConstraint(context.getPkg().getName(), expr, mvelDeclarations, getOperators(usedIdentifiers.getOperators()), compilationUnit, isDynamic);
}
Aggregations