use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class PatternBuilder method rewriteCompositeExpressions.
private String rewriteCompositeExpressions(RuleBuildContext context, Pattern pattern, ConstraintConnectiveDescr d) {
int i = 0;
StringBuilder sb = new StringBuilder();
for (BaseDescr subDescr : d.getDescrs()) {
if (subDescr instanceof BindingDescr) {
continue;
}
if (i++ > 0) {
sb.append(" ").append(d.getConnective().getConnective()).append(" ");
}
String normalizedExpr;
if (subDescr instanceof RelationalExprDescr && isSimpleExpr((RelationalExprDescr) subDescr)) {
RelationalExprDescr relDescr = (RelationalExprDescr) subDescr;
if (relDescr.getExpression() != null) {
normalizedExpr = normalizeExpression(context, pattern, relDescr, relDescr.getExpression());
} else {
i--;
normalizedExpr = "";
}
} else if (subDescr instanceof ConstraintConnectiveDescr) {
String rewrittenExpr = rewriteCompositeExpressions(context, pattern, (ConstraintConnectiveDescr) subDescr);
if (rewrittenExpr == null) {
return null;
}
normalizedExpr = "(" + rewrittenExpr + ")";
} else if (subDescr instanceof AtomicExprDescr) {
normalizedExpr = ((AtomicExprDescr) subDescr).getRewrittenExpression();
} else {
return null;
}
sb.append(normalizedExpr);
}
return sb.toString();
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class JavaAccumulateBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
// build source
BaseDescr input = accumDescr.getInput();
if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
input = ((AndDescr) input).getDescrs().get(0);
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
final RuleConditionElement source = builder.build(context, input);
if (source == null) {
return null;
}
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
Accumulate accumulate;
if (accumDescr.isExternalFunction()) {
// if it uses 1+ external function, build methods for them
accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
} else {
// if it uses inline code, build the class for it
accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
}
return accumulate;
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class JavaDialect method addRule.
/**
* This will add the rule for compiling later on.
* It will not actually call the compiler
*/
public void addRule(final RuleBuildContext context) {
final RuleImpl rule = context.getRule();
final RuleDescr ruleDescr = context.getRuleDescr();
RuleClassBuilder classBuilder = context.getDialect().getRuleClassBuilder();
String ruleClass = classBuilder.buildRule(context);
// return if there is no ruleclass name;
if (ruleClass == null) {
return;
}
// The compilation result is for the entire rule, so difficult to associate with any descr
addClassCompileTask(this.pkg.getName() + "." + ruleDescr.getClassName(), ruleDescr, ruleClass, this.src, new RuleErrorHandler(ruleDescr, rule, "Rule Compilation error"));
JavaDialectRuntimeData data = (JavaDialectRuntimeData) this.pkg.getDialectRuntimeRegistry().getDialectData(ID);
for (Map.Entry<String, String> invokers : context.getInvokers().entrySet()) {
final String className = invokers.getKey();
// Check if an invoker - returnvalue, predicate, eval or consequence has been associated
// If so we add it to the PackageCompilationData as it will get wired up on compilation
final Object invoker = context.getInvokerLookup(className);
if (invoker instanceof Wireable) {
data.putInvoker(className, (Wireable) invoker);
}
final String text = invokers.getValue();
final BaseDescr descr = context.getDescrLookup(className);
addClassCompileTask(className, descr, text, this.src, new RuleInvokerErrorHandler(descr, rule, "Unable to generate rule invoker."));
}
// setup the line mappins for this rule
final String name = this.pkg.getName() + "." + StringUtils.ucFirst(ruleDescr.getClassName());
final LineMappings mapping = new LineMappings(name);
mapping.setStartLine(ruleDescr.getConsequenceLine());
mapping.setOffset(ruleDescr.getConsequenceOffset());
this.pkg.getDialectRuntimeRegistry().getLineMappings().put(name, mapping);
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class MVELDialect method analyzeExpression.
public AnalysisResult analyzeExpression(final PackageBuildContext context, final BaseDescr descr, final Object content, final BoundIdentifiers availableIdentifiers, final Map<String, Class<?>> localTypes) {
AnalysisResult result = null;
// the following is required for proper error handling
BaseDescr temp = context.getParentDescr();
context.setParentDescr(descr);
try {
result = MVELExprAnalyzer.analyzeExpression(context, (String) content, availableIdentifiers, localTypes, "drools", KnowledgeHelper.class);
} catch (final Exception e) {
DialectUtil.copyErrorLocation(e, descr);
context.addError(new DescrBuildError(context.getParentDescr(), descr, null, "Unable to determine the used declarations.\n" + e.getMessage()));
} finally {
// setting it back to original parent descr
context.setParentDescr(temp);
}
return result;
}
use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.
the class MVELDumper method processConnectiveDescr.
protected void processConnectiveDescr(StringBuilder sbuilder, BaseDescr base, ConstraintConnectiveDescr parent, int parentPriority, boolean isInsideRelCons, MVELDumperContext context) {
ConstraintConnectiveDescr ccd = (ConstraintConnectiveDescr) base;
boolean wrapParenthesis = parentPriority > ccd.getConnective().getPrecedence();
if (wrapParenthesis) {
sbuilder.append("( ");
}
boolean first = true;
List<BaseDescr> descrs = new ArrayList<BaseDescr>(ccd.getDescrs());
for (BaseDescr constr : descrs) {
if (!(constr instanceof BindingDescr)) {
if (first) {
first = false;
} else {
sbuilder.append(" ");
sbuilder.append(ccd.getConnective().toString());
sbuilder.append(" ");
}
}
context.incOpenCcd();
dump(sbuilder, constr, ccd, ccd.getDescrs().indexOf(constr), ccd.getConnective().getPrecedence(), isInsideRelCons, context);
context.decOpenCcd();
}
if (first) {
// means all children were actually only bindings, replace by just true
sbuilder.append("true");
}
if (wrapParenthesis) {
sbuilder.append(" )");
}
}
Aggregations