Search in sources :

Example 16 with GroupElement

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

the class ConditionalBranchBuilder method getLastPattern.

private Pattern getLastPattern(RuleBuildContext context) {
    GroupElement ge = (GroupElement) context.getDeclarationResolver().peekBuildStack();
    Pattern lastPattern = getLastPattern(ge.getChildren());
    if (lastPattern == null) {
        RuleImpl parent = context.getRule().getParent();
        if (parent != null) {
            lastPattern = getLastPattern(parent.getLhs().getChildren());
        }
    }
    return lastPattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 17 with GroupElement

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

the class KiePackagesBuilder method populateLHS.

private void populateLHS(RuleContext ctx, KnowledgePackageImpl pkg, View view) {
    GroupElement lhs = ctx.getRule().getLhs();
    if (ctx.getRule().getRuleUnitClassName() != null) {
        lhs.addChild(addPatternForVariable(ctx, lhs, getUnitVariable(ctx, pkg, view)));
    }
    addSubConditions(ctx, lhs, view.getSubConditions());
    if (requiresLeftActivation(lhs)) {
        lhs.addChild(0, new Pattern(0, ClassObjectType.InitialFact_ObjectType));
    }
}
Also used : QueryCallPattern(org.drools.model.patterns.QueryCallPattern) AccumulatePattern(org.drools.model.AccumulatePattern) Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement)

Example 18 with GroupElement

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

the class RuleBuilder method build.

/**
 * Build the give rule into the
 */
public static void build(final RuleBuildContext context) {
    RuleDescr ruleDescr = context.getRuleDescr();
    final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(ruleDescr.getLhs().getClass());
    if (builder != null) {
        // this is established during pre-processing, if it's query
        Pattern prefixPattern = context.getPrefixPattern();
        final GroupElement ce = (GroupElement) builder.build(context, getLhsForRuleUnit(context.getRule(), ruleDescr.getLhs()), prefixPattern);
        context.getRule().setLhs(ce);
    } else {
        throw new RuntimeException("BUG: builder not found for descriptor class " + ruleDescr.getLhs().getClass());
    }
    // build all the rule's attributes
    // must be after building LHS because some attributes require bindings from the LHS
    buildAttributes(context);
    // generate the main rule from the previously generated s.
    if (!(ruleDescr instanceof QueryDescr)) {
        // do not build the consequence if we have a query
        ConsequenceBuilder consequenceBuilder = context.getDialect().getConsequenceBuilder();
        consequenceBuilder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
        for (String name : ruleDescr.getNamedConsequences().keySet()) {
            consequenceBuilder.build(context, name);
        }
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryDescr(org.drools.compiler.lang.descr.QueryDescr) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Example 19 with GroupElement

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

the class RuleUnlinkingTest method createNetworkNode.

private NetworkNode createNetworkNode(int id, int type, LeftTupleSource leftTupleSource, RuleImpl rule) {
    MockObjectSource mockObjectSource = new MockObjectSource(8);
    LeftTupleSink networkNode = null;
    switch(type) {
        case JOIN_NODE:
            {
                networkNode = new JoinNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case EXISTS_NODE:
            {
                networkNode = new ExistsNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case NOT_NODE:
            {
                networkNode = new NotNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
                break;
            }
        case RULE_TERMINAL_NODE:
            {
                networkNode = new RuleTerminalNode(id, leftTupleSource, rule, new GroupElement(Type.AND), 0, buildContext);
                break;
            }
    }
    mockObjectSource.attach();
    if (NodeTypeEnums.isLeftTupleSource(networkNode)) {
        ((LeftTupleSource) networkNode).attach(buildContext);
    } else {
        ((RuleTerminalNode) networkNode).attach(buildContext);
    }
    return networkNode;
}
Also used : EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) GroupElement(org.drools.core.rule.GroupElement)

Example 20 with GroupElement

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

the class BuildUtilsTest method testCalculateTemporalDistance.

/**
 * Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
 */
@Test
public void testCalculateTemporalDistance() {
    // input is here just for "documentation" purposes
    Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
    Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
    AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
    ClassObjectType ot = new ClassObjectType(StockTick.class, true);
    Pattern a = new Pattern(0, ot, "$a");
    Pattern b = new Pattern(1, ot, "$b");
    b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern c = new Pattern(2, ot, "$c");
    c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern d = new Pattern(3, ot, "$d");
    d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
    d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
    Pattern e = new Pattern(4, ot, "$e");
    e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
    GroupElement not = new GroupElement(Type.NOT);
    not.addChild(e);
    GroupElement and = new GroupElement(Type.AND);
    and.addChild(a);
    and.addChild(b);
    and.addChild(c);
    and.addChild(d);
    and.addChild(not);
    TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
    // printMatrix( matrix.getMatrix() );
    assertEqualsMatrix(expected, matrix.getMatrix());
    assertEquals(15, matrix.getExpirationOffset(a));
    assertEquals(11, matrix.getExpirationOffset(d));
    assertEquals(1, matrix.getExpirationOffset(e));
}
Also used : Pattern(org.drools.core.rule.Pattern) AfterEvaluatorDefinition(org.drools.core.base.evaluators.AfterEvaluatorDefinition) ClassObjectType(org.drools.core.base.ClassObjectType) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) TemporalDependencyMatrix(org.drools.core.time.TemporalDependencyMatrix) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) GroupElement(org.drools.core.rule.GroupElement) Declaration(org.drools.core.rule.Declaration) Interval(org.drools.core.time.Interval) Test(org.junit.Test)

Aggregations

GroupElement (org.drools.core.rule.GroupElement)29 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)14 Test (org.junit.Test)13 Declaration (org.drools.core.rule.Declaration)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)5 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)5 RuleConditionElement (org.drools.core.rule.RuleConditionElement)4 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 HashMap (java.util.HashMap)3 AndDescr (org.drools.compiler.lang.descr.AndDescr)3 WorkingMemory (org.drools.core.WorkingMemory)3 ClassObjectType (org.drools.core.base.ClassObjectType)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2