Search in sources :

Example 6 with RuleConditionElement

use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.

the class DeclarationScopeResolver method getDeclarations.

/**
 * Return all declarations scoped to the current
 * RuleConditionElement in the build stack
 */
public Map<String, Declaration> getDeclarations(RuleImpl rule, String consequenceName) {
    Map<String, Declaration> declarations = new HashMap<String, Declaration>();
    for (RuleConditionElement aBuildStack : this.buildStack) {
        // if we are inside of an OR we don't want each previous stack entry added because we can't see those variables
        if (aBuildStack instanceof GroupElement && ((GroupElement) aBuildStack).getType() == GroupElement.Type.OR) {
            continue;
        }
        // this may be optimized in the future to only re-add elements at
        // scope breaks, like "NOT" and "EXISTS"
        Map<String, Declaration> innerDeclarations = aBuildStack instanceof GroupElement ? ((GroupElement) aBuildStack).getInnerDeclarations(consequenceName) : aBuildStack.getInnerDeclarations();
        declarations.putAll(innerDeclarations);
    }
    if (null != rule.getParent()) {
        return getAllExtendedDeclaration(rule.getParent(), declarations);
    }
    return declarations;
}
Also used : HashMap(java.util.HashMap) GroupElement(org.drools.core.rule.GroupElement) Declaration(org.drools.core.rule.Declaration) RuleConditionElement(org.drools.core.rule.RuleConditionElement)

Example 7 with RuleConditionElement

use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.

the class DeclarationScopeResolver method isDuplicated.

public boolean isDuplicated(RuleImpl rule, final String name, final String type) {
    if (this.globalMap.containsKey((name))) {
        return true;
    }
    for (int i = this.buildStack.size() - 1; i >= 0; i--) {
        final RuleConditionElement rce = buildStack.get(i);
        final Declaration declaration = rce.resolveDeclaration(name);
        if (declaration != null) {
            // if it is an OR and it is duplicated, we can stop looking for duplication now
            // as it is a separate logical branch
            boolean inOr = ((rce instanceof GroupElement) && ((GroupElement) rce).isOr());
            if (!inOr || type == null) {
                return !inOr;
            }
            return !declaration.getDeclarationClass().getName().equals(type);
        }
    }
    // look at parent rules
    if (rule != null && rule.getParent() != null) {
        // recursive algorithm for each parent
        // -> lhs.getInnerDeclarations()
        Declaration parentDeclaration = getExtendedDeclaration(rule.getParent(), name);
        return null != parentDeclaration;
    }
    return false;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) RuleConditionElement(org.drools.core.rule.RuleConditionElement) Declaration(org.drools.core.rule.Declaration)

Example 8 with RuleConditionElement

use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.

the class AccumulateBuilder method build.

/**
 * @inheritDoc
 */
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
    final Accumulate accumulate = (Accumulate) rce;
    boolean existSubNetwort = false;
    context.pushRuleComponent(accumulate);
    final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
    final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
    RuleConditionElement source = accumulate.getSource();
    if (source instanceof GroupElement) {
        GroupElement ge = (GroupElement) source;
        if (ge.isAnd() && ge.getChildren().size() == 1) {
            source = ge.getChildren().get(0);
        }
    }
    // get builder for the pattern
    final ReteooComponentBuilder builder = utils.getBuilderFor(source);
    // save tuple source and current pattern offset for later if needed
    LeftTupleSource tupleSource = context.getTupleSource();
    final int currentPatternIndex = context.getCurrentPatternOffset();
    // builds the source pattern
    builder.build(context, utils, source);
    // if object source is null, then we need to adapt tuple source into a subnetwork
    if (context.getObjectSource() == null) {
        // attach right input adapter node to convert tuple source into an object source
        RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
        // attach right input adapter node to convert tuple source into an object source
        context.setObjectSource(utils.attachNode(context, riaNode));
        // restore tuple source from before the start of the sub network
        context.setTupleSource(tupleSource);
        // create a tuple start equals constraint and set it in the context
        final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
        final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
        betaConstraints.add(constraint);
        context.setBetaconstraints(betaConstraints);
        existSubNetwort = true;
    }
    NodeFactory nfactory = context.getComponentFactory().getNodeFactoryService();
    final BetaConstraints resultsBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
    final BetaConstraints sourceBinder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
    AccumulateNode accNode = nfactory.buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), sourceBinder, resultsBinder, accumulate, existSubNetwort, context);
    context.setTupleSource(utils.attachNode(context, accNode));
    // source pattern was bound, so nulling context
    context.setObjectSource(null);
    context.setCurrentPatternOffset(currentPatternIndex);
    context.popRuleComponent();
}
Also used : BetaConstraints(org.drools.core.common.BetaConstraints) AccumulateNode(org.drools.core.reteoo.AccumulateNode) GroupElement(org.drools.core.rule.GroupElement) ArrayList(java.util.ArrayList) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) RuleConditionElement(org.drools.core.rule.RuleConditionElement) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) TupleStartEqualsConstraint(org.drools.core.common.TupleStartEqualsConstraint) Accumulate(org.drools.core.rule.Accumulate) LeftTupleSource(org.drools.core.reteoo.LeftTupleSource) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) RightInputAdapterNode(org.drools.core.reteoo.RightInputAdapterNode)

Example 9 with RuleConditionElement

use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

RuleConditionElement (org.drools.core.rule.RuleConditionElement)9 Declaration (org.drools.core.rule.Declaration)5 GroupElement (org.drools.core.rule.GroupElement)5 Accumulate (org.drools.core.rule.Accumulate)4 MultiAccumulate (org.drools.core.rule.MultiAccumulate)3 SingleAccumulate (org.drools.core.rule.SingleAccumulate)3 ArrayList (java.util.ArrayList)2 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 RuleConditionBuilder (org.drools.compiler.rule.builder.RuleConditionBuilder)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 Pattern (org.drools.core.rule.Pattern)2 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)2 Constraint (org.drools.core.spi.Constraint)2 HashMap (java.util.HashMap)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 DrlDumper (org.drools.compiler.lang.DrlDumper)1