Search in sources :

Example 41 with BaseDescr

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();
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 42 with BaseDescr

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;
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) RuleConditionBuilder(org.drools.compiler.rule.builder.RuleConditionBuilder) RuleConditionElement(org.drools.core.rule.RuleConditionElement) Declaration(org.drools.core.rule.Declaration) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MultiAccumulate(org.drools.core.rule.MultiAccumulate) SingleAccumulate(org.drools.core.rule.SingleAccumulate) Accumulate(org.drools.core.rule.Accumulate)

Example 43 with BaseDescr

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);
}
Also used : RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Wireable(org.drools.core.spi.Wireable) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData) RuleClassBuilder(org.drools.compiler.rule.builder.RuleClassBuilder) LineMappings(org.drools.core.rule.LineMappings) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 44 with BaseDescr

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;
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) IOException(java.io.IOException)

Example 45 with BaseDescr

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(" )");
    }
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) ArrayList(java.util.ArrayList) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr)

Aggregations

BaseDescr (org.drools.compiler.lang.descr.BaseDescr)109 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)60 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)23 AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)16 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)11 RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)11 DeclareDescrBuilder (org.drools.compiler.lang.api.DeclareDescrBuilder)9 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)7 ArrayList (java.util.ArrayList)6 AccumulateDescrBuilder (org.drools.compiler.lang.api.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.compiler.lang.api.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.compiler.lang.api.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.compiler.lang.api.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.compiler.lang.api.BehaviorDescrBuilder)6 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)6 CollectDescrBuilder (org.drools.compiler.lang.api.CollectDescrBuilder)6 ConditionalBranchDescrBuilder (org.drools.compiler.lang.api.ConditionalBranchDescrBuilder)6