Search in sources :

Example 46 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class MVELDumper method processInlineCast.

private String[] processInlineCast(String expr, AtomicExprDescr atomicExpr, ConstraintConnectiveDescr ccd, MVELDumperContext context, int sharpPos, int parentIdx, int childIdx) {
    // convert "field1#Class.field2" in ["field1 instanceof Class && ", "((Class)field1).field2"]
    String field1 = expr.substring(0, sharpPos).trim();
    int sharpPos2 = expr.indexOf('#', sharpPos + 1);
    String part2 = sharpPos2 < 0 ? expr.substring(sharpPos + 1).trim() : expr.substring(sharpPos + 1, sharpPos2).trim();
    String[] classAndField = splitInClassAndField(part2, context);
    BaseDescr desc = parentIdx >= 0 ? ccd.getDescrs().get(parentIdx) : null;
    if (classAndField == null) {
        return new String[] { "", expr };
    } else if (desc instanceof AtomicExprDescr && classAndField.length == 1) {
        return new String[] { "", field1 + " instanceof " + classAndField[0] };
    }
    String className = classAndField[0];
    String castedExpression = classAndField.length == 1 ? "((" + className + ")" + field1 + ")" : "((" + className + ")" + field1 + ")." + classAndField[1] + (sharpPos2 > 0 ? expr.substring(sharpPos2) : "");
    RelationalExprDescr check = new RelationalExprDescr("instanceof", false, null, new AtomicExprDescr(field1), new AtomicExprDescr(className));
    if (ccd.getConnective() == ConnectiveType.AND || ccd.getConnective() == ConnectiveType.INC_AND) {
        ccd.getDescrs().add(childIdx, check);
    } else {
        if (desc instanceof ConstraintConnectiveDescr) {
            ((ConstraintConnectiveDescr) desc).getDescrs().add(childIdx, check);
        } else {
            ConstraintConnectiveDescr localAnd = new ConstraintConnectiveDescr(ConnectiveType.AND);
            BaseDescr original = ccd.getDescrs().remove(parentIdx);
            localAnd.getDescrs().add(check);
            localAnd.getDescrs().add(original);
            ccd.getDescrs().add(parentIdx, localAnd);
        }
    }
    atomicExpr.setRewrittenExpression(castedExpression);
    String innerCheck = check.toString() + " && ";
    return new String[] { innerCheck, castedExpression };
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AtomicExprDescr(org.drools.compiler.lang.descr.AtomicExprDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RelationalExprDescr(org.drools.compiler.lang.descr.RelationalExprDescr)

Example 47 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class PackageDescrResourceVisitor method visit.

protected void visit(final AccumulateDescr descr) {
    checkResource(descr);
    visit(descr.getInputPattern());
    for (BaseDescr d : descr.getDescrs()) {
        visit(d);
    }
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr)

Example 48 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class PackageDescrResourceVisitor method visit.

protected void visit(final PatternDescr descr) {
    checkResource(descr);
    visit(descr.getConstraint());
    for (BaseDescr behaDescr : descr.getBehaviors()) {
        visit(behaDescr);
    }
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr)

Example 49 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class FromCollectVisitor method trasformFromCollectToCollectList.

public void trasformFromCollectToCollectList(PatternDescr pattern, CollectDescr collectDescr) {
    // The inner pattern of the "from collect" needs to be processed to have the binding
    final PatternDescr collectDescrInputPattern = collectDescr.getInputPattern();
    collectDescrInputPattern.accept(parentVisitor);
    final AccumulateDescr accumulateDescr = new AccumulateDescr();
    accumulateDescr.setInputPattern(collectDescrInputPattern);
    accumulateDescr.addFunction("collectList", null, false, new String[] { collectDescrInputPattern.getIdentifier() });
    final PatternDescr transformedPatternDescr = new PatternDescr(pattern.getObjectType());
    for (BaseDescr o : pattern.getConstraint().getDescrs()) {
        transformedPatternDescr.addConstraint(o);
    }
    transformedPatternDescr.setSource(accumulateDescr);
    transformedPatternDescr.accept(parentVisitor);
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr)

Example 50 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class AccumulateVisitor method visit.

public void visit(AccumulateDescr descr, PatternDescr basePattern) {
    final MethodCallExpr accumulateDSL = new MethodCallExpr(null, "accumulate");
    context.addExpression(accumulateDSL);
    final MethodCallExpr accumulateExprs = new MethodCallExpr(null, "and");
    accumulateDSL.addArgument(accumulateExprs);
    context.pushExprPointer(accumulateExprs::addArgument);
    BaseDescr input = descr.getInputPattern() == null ? descr.getInput() : descr.getInputPattern();
    boolean inputPatternHasConstraints = (input instanceof PatternDescr) && (!((PatternDescr) input).getConstraint().getDescrs().isEmpty());
    input.accept(modelGeneratorVisitor);
    if (accumulateExprs.getArguments().isEmpty()) {
        accumulateDSL.remove(accumulateExprs);
    } else if (accumulateExprs.getArguments().size() == 1) {
        accumulateDSL.setArgument(0, accumulateExprs.getArguments().get(0));
    }
    if (!descr.getFunctions().isEmpty()) {
        for (AccumulateDescr.AccumulateFunctionCallDescr function : descr.getFunctions()) {
            final Optional<NewBinding> optNewBinding = visit(context, function, accumulateDSL, basePattern, inputPatternHasConstraints);
            processNewBinding(optNewBinding);
        }
    } else if (descr.getFunctions().isEmpty() && descr.getInitCode() != null) {
        // LEGACY: Accumulate with inline custom code
        if (input instanceof PatternDescr) {
            visitAccInlineCustomCode(context, descr, accumulateDSL, basePattern, (PatternDescr) input);
        } else {
            throw new UnsupportedOperationException("I was expecting input to be of type PatternDescr. " + input);
        }
    } else {
        throw new UnsupportedOperationException("Unknown type of Accumulate.");
    }
    context.popExprPointer();
    postVisit();
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

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