Search in sources :

Example 51 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleUnlinkingWithSegmentMemoryTest method setUp.

public void setUp(int type) {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    buildContext = new BuildContext(kBase);
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
    ObjectTypeNode otn = new ObjectTypeNode(4, null, new ClassObjectType(String.class), buildContext);
    lian = new LeftInputAdapterNode(5, otn, buildContext);
    n1 = (BetaNode) createNetworkNode(10, type, lian, null);
    n2 = (BetaNode) createNetworkNode(11, type, n1, null);
    n3 = (BetaNode) createNetworkNode(12, type, n2, null);
    rule1 = new RuleImpl("rule1");
    rule1.setActivationListener("agenda");
    rtn1 = (RuleTerminalNode) createNetworkNode(18, RULE_TERMINAL_NODE, n3, rule1);
    n4 = (BetaNode) createNetworkNode(13, type, n3, null);
    n5 = (BetaNode) createNetworkNode(14, type, n4, null);
    rule2 = new RuleImpl("rule2");
    rule2.setActivationListener("agenda");
    rtn2 = (RuleTerminalNode) createNetworkNode(19, RULE_TERMINAL_NODE, n5, rule2);
    n6 = (BetaNode) createNetworkNode(15, type, n5, null);
    n7 = (BetaNode) createNetworkNode(16, type, n6, null);
    n8 = (BetaNode) createNetworkNode(17, type, n7, null);
    rule3 = new RuleImpl("rule3");
    rule3.setActivationListener("agenda");
    rtn3 = (RuleTerminalNode) createNetworkNode(20, RULE_TERMINAL_NODE, n8, rule3);
    lian.addAssociation(rule1);
    lian.addAssociation(rule2);
    lian.addAssociation(rule3);
    n1.addAssociation(rule1);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule1);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule1);
    n3.addAssociation(rule2);
    n3.addAssociation(rule3);
    n4.addAssociation(rule2);
    n4.addAssociation(rule3);
    n5.addAssociation(rule2);
    n5.addAssociation(rule3);
    n6.addAssociation(rule3);
    n7.addAssociation(rule3);
    n8.addAssociation(rule3);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl)

Example 52 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class ReteooRuleBuilderTest method testAddRuleWithPatterns.

@Test
public void testAddRuleWithPatterns() {
    final RuleImpl rule = new RuleImpl("only patterns");
    final Pattern c1 = new Pattern(0, new ClassObjectType(String.class));
    final Pattern c2 = new Pattern(1, new ClassObjectType(String.class));
    final Pattern c3 = new Pattern(2, new ClassObjectType(String.class));
    final GroupElement lhsroot = GroupElementFactory.newAndInstance();
    lhsroot.addChild(c1);
    lhsroot.addChild(c2);
    lhsroot.addChild(c3);
    rule.setLhs(lhsroot);
    final Consequence consequence = new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            System.out.println("Consequence!");
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    };
    rule.setConsequence(consequence);
    final List terminals = this.builder.addRule(rule, this.rulebase);
    assertEquals("Rule must have a single terminal node", 1, terminals.size());
    final RuleTerminalNode terminal = (RuleTerminalNode) terminals.get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) GroupElement(org.drools.core.rule.GroupElement) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) List(java.util.List) ObjectInput(java.io.ObjectInput) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 53 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class PrioritySetWithFormulaTest method test.

@Test
public void test() {
    // RULE CheeseWorld_11 has salience "=3+2"
    RuleImpl cheeseWorld11 = (RuleImpl) kieBase.getRule("test", "CheeseWorld_11");
    assertEquals(5, cheeseWorld11.getSalience().getValue());
    // RULE CheeseWorld_12 has salience "=ROW()"
    RuleImpl cheeseWorld12 = (RuleImpl) kieBase.getRule("test", "CheeseWorld_12");
    assertEquals(12, cheeseWorld12.getSalience().getValue());
}
Also used : RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 54 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class NamedConsequenceBuilder method buildTerminalNodeForNamedConsequence.

static RuleTerminalNode buildTerminalNodeForNamedConsequence(BuildContext context, NamedConsequence namedConsequence) {
    RuleImpl rule = context.getRule();
    GroupElement subrule = (GroupElement) context.peek();
    ActivationListenerFactory factory = context.getKnowledgeBase().getConfiguration().getActivationListenerFactory(rule.getActivationListener());
    context.setConsequenceName(namedConsequence.getConsequenceName());
    TerminalNode terminal = factory.createActivationListener(context.getNextId(), context.getTupleSource(), rule, subrule, // subruleIndex,
    0, context);
    context.setConsequenceName(null);
    return (RuleTerminalNode) terminal;
}
Also used : GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ActivationListenerFactory(org.drools.core.ActivationListenerFactory) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) TerminalNode(org.drools.core.reteoo.TerminalNode) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode)

Example 55 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleTest method testRuleEnabled.

@Test
public void testRuleEnabled() {
    WorkingMemory wm = (WorkingMemory) new KnowledgeBaseImpl("x", null).newKieSession();
    final RuleImpl rule = new RuleImpl("myrule");
    rule.setEnabled(EnabledBoolean.ENABLED_FALSE);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    final Calendar past = Calendar.getInstance();
    past.setTimeInMillis(10);
    rule.setDateEffective(past);
    assertFalse(rule.isEffective(null, new RuleTerminalNode(), wm));
    rule.setEnabled(EnabledBoolean.ENABLED_TRUE);
    assertTrue(rule.isEffective(null, new RuleTerminalNode(), wm));
}
Also used : WorkingMemory(org.drools.core.WorkingMemory) Calendar(java.util.Calendar) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11