use of org.drools.compiler.compiler.AnalysisResult in project drools by kiegroup.
the class JavaAccumulateBuilder method buildInlineAccumulate.
private Accumulate buildInlineAccumulate(final RuleBuildContext context, final AccumulateDescr accumDescr, final RuleConditionElement source, Map<String, Declaration> decls, Map<String, Class<?>> declCls, final boolean readLocalsFromTuple) {
// ELSE, if it is not an external function, build it using the regular java builder
final String className = "Accumulate" + context.getNextId();
accumDescr.setClassName(className);
BoundIdentifiers available = new BoundIdentifiers(declCls, context);
final JavaAnalysisResult initCodeAnalysis = (JavaAnalysisResult) context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getInitCode(), available);
final AnalysisResult actionCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
final AnalysisResult resultCodeAnalysis = context.getDialect().analyzeExpression(context, accumDescr, accumDescr.getResultCode(), available);
if (initCodeAnalysis == null || actionCodeAnalysis == null || resultCodeAnalysis == null) {
// not possible to get the analysis results - compilation error has been already logged
return null;
}
final Set<String> requiredDeclarations = new HashSet<String>(initCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
requiredDeclarations.addAll(actionCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
requiredDeclarations.addAll(resultCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
final Map<String, Class<?>> requiredGlobals = new HashMap<String, Class<?>>(initCodeAnalysis.getBoundIdentifiers().getGlobals());
requiredGlobals.putAll(actionCodeAnalysis.getBoundIdentifiers().getGlobals());
requiredGlobals.putAll(resultCodeAnalysis.getBoundIdentifiers().getGlobals());
if (accumDescr.getReverseCode() != null) {
final AnalysisResult reverseCodeAnalysis = context.getDialect().analyzeBlock(context, accumDescr, accumDescr.getActionCode(), available);
requiredDeclarations.addAll(reverseCodeAnalysis.getBoundIdentifiers().getDeclrClasses().keySet());
requiredGlobals.putAll(reverseCodeAnalysis.getBoundIdentifiers().getGlobals());
}
final Declaration[] declarations = new Declaration[requiredDeclarations.size()];
int i = 0;
for (Iterator<String> it = requiredDeclarations.iterator(); it.hasNext(); i++) {
declarations[i] = decls.get(it.next());
}
final Declaration[] sourceDeclArr = source.getOuterDeclarations().values().toArray(new Declaration[source.getOuterDeclarations().size()]);
Arrays.sort(sourceDeclArr, RuleTerminalNode.SortDeclarations.instance);
final Map<String, Object> map = createVariableContext(className, null, context, declarations, null, requiredGlobals);
map.put("className", accumDescr.getClassName());
map.put("innerDeclarations", sourceDeclArr);
map.put("isMultiPattern", readLocalsFromTuple ? Boolean.TRUE : Boolean.FALSE);
final String initCode = this.fixInitCode(initCodeAnalysis, accumDescr.getInitCode());
final String actionCode = accumDescr.getActionCode();
final String resultCode = accumDescr.getResultCode();
String[] attributesTypes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
String[] attributes = new String[initCodeAnalysis.getLocalVariablesMap().size()];
int index = 0;
for (Map.Entry<String, JavaLocalDeclarationDescr> entry : initCodeAnalysis.getLocalVariablesMap().entrySet()) {
attributes[index] = entry.getKey();
attributesTypes[index] = entry.getValue().getType();
index++;
}
map.put("attributes", attributes);
map.put("attributesTypes", attributesTypes);
map.put("initCode", initCode);
map.put("actionCode", actionCode);
map.put("resultCode", resultCode);
if (accumDescr.getReverseCode() == null) {
map.put("reverseCode", "");
map.put("supportsReverse", "false");
} else {
map.put("reverseCode", accumDescr.getReverseCode());
map.put("supportsReverse", "true");
}
map.put("hashCode", actionCode.hashCode());
SingleAccumulate accumulate = new SingleAccumulate(source, declarations);
generateTemplates("accumulateInnerClass", "accumulateInvoker", context, className, map, accumulate.new Wirer(), accumDescr);
return accumulate;
}
use of org.drools.compiler.compiler.AnalysisResult in project drools by kiegroup.
the class MVELEnabledBuilder method build.
public void build(RuleBuildContext context) {
// 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, Class<?>> otherVars = new HashMap<String, Class<?>>();
otherVars.put("rule", RuleImpl.class);
Map<String, Declaration> declrs = context.getDeclarationResolver().getDeclarations(context.getRule());
AnalysisResult analysis = dialect.analyzeExpression(context, context.getRuleDescr(), context.getRuleDescr().getEnabled(), new BoundIdentifiers(DeclarationScopeResolver.getDeclarationClasses(declrs), context), otherVars);
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++] = declrs.get(id);
}
Arrays.sort(previousDeclarations, SortDeclarations.instance);
String exprStr = context.getRuleDescr().getEnabled();
exprStr = exprStr.substring(1, exprStr.length() - 1) + " ";
MVELCompilationUnit unit = dialect.getMVELCompilationUnit(exprStr, analysis, previousDeclarations, null, otherVars, context, "drools", KnowledgeHelper.class, false, MVELCompilationUnit.Scope.EXPRESSION);
MVELEnabledExpression expr = new MVELEnabledExpression(unit, dialect.getId());
context.getRule().setEnabled(KiePolicyHelper.isPolicyEnabled() ? new SafeEnabled(expr) : expr);
MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
data.addCompileable(context.getRule(), expr);
expr.compile(data, context.getRule());
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, context.getRuleDescr());
context.addError(new DescrBuildError(context.getParentDescr(), context.getRuleDescr(), null, "Unable to build expression for 'enabled' : " + e.getMessage() + " '" + context.getRuleDescr().getEnabled() + "'"));
}
}
use of org.drools.compiler.compiler.AnalysisResult in project drools by kiegroup.
the class AbstractASMEvalBuilder method build.
public RuleConditionElement build(RuleBuildContext context, BaseDescr descr, Pattern prefixPattern) {
if (prefixPattern == null) {
return build(context, descr);
}
EvalDescr evalDescr = (EvalDescr) descr;
PredicateDescr predicateDescr = new PredicateDescr(context.getRuleDescr().getResource(), evalDescr.getContent());
AnalysisResult analysis = buildAnalysis(context, prefixPattern, predicateDescr, null);
Declaration[] declarations = getUsedDeclarations(context, prefixPattern, analysis);
return buildEval(context, evalDescr, analysis, declarations);
}
use of org.drools.compiler.compiler.AnalysisResult 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);
}
Aggregations