Search in sources :

Example 51 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class RuleUnlinkingTest method setUp.

public void setUp(int type) {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    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);
    liaNode = new LeftInputAdapterNode(5, otn, buildContext);
    n1 = (BetaNode) createNetworkNode(10, type, liaNode, 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);
    // n1 -> n2 -> n3 -> r1
    // \
    // n4 -> n5 -> r2
    // \
    // n6 -> n7 -> n8 -> r3
    liaNode.addAssociation(rule1);
    liaNode.addAssociation(rule2);
    liaNode.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 ClassObjectType

use of org.drools.core.base.ClassObjectType 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 53 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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 54 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testSingleEntry.

@Test
public void testSingleEntry() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(0, cheddar);
    assertEquals(0, map.size());
    assertNull(map.getFirst(new LeftTupleImpl(cheddarHandle1, null, true)));
    final Cheese stilton1 = new Cheese("stilton", 35);
    RightTuple stiltonRighTuple = new RightTupleImpl(new DefaultFactHandle(1, stilton1), null);
    map.add(stiltonRighTuple);
    assertEquals(1, map.size());
    assertEquals(1, tablePopulationSize(map));
    final Cheese stilton2 = new Cheese("stilton", 80);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
    final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
    assertSame(stiltonRighTuple.getFactHandle(), tuple.getFactHandle());
    assertNull(tuple.getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) RightTuple(org.drools.core.reteoo.RightTuple) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 55 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testTwoDifferentEntriesSameHashCode.

@Test
public void testTwoDifferentEntriesSameHashCode() throws Exception {
    final InternalReadAccessor extractor = store.getReader(TestClass.class, "object");
    final Pattern pattern = new Pattern(0, new ClassObjectType(TestClass.class));
    final Declaration declaration = new Declaration("theObject", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    final TestClass c1 = new TestClass(0, new TestClass(20, "stilton"));
    final InternalFactHandle ch1 = new DefaultFactHandle(1, c1);
    map.add(new RightTupleImpl(ch1, null));
    final TestClass c2 = new TestClass(0, new TestClass(20, "cheddar"));
    final InternalFactHandle ch2 = new DefaultFactHandle(2, c2);
    map.add(new RightTupleImpl(ch2, null));
    // same hashcode, but different values, so it should result in  a size of 2
    assertEquals(2, map.size());
    // however both are in the same table bucket
    assertEquals(1, tablePopulationSize(map));
    // this table bucket will have two FieldIndexEntries, as they are actually two different values
    Entry[] entries = getEntries(map);
    assertEquals(1, entries.length);
    TupleList list = (TupleList) entries[0];
    assertSame(ch2, list.getFirst().getFactHandle());
    assertNull(list.getFirst().getNext());
    assertSame(ch1, list.getNext().getFirst().getFactHandle());
    assertNull(list.getNext().getFirst().getNext());
    assertNull(list.getNext().getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) TupleList(org.drools.core.util.index.TupleList) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12