Search in sources :

Example 41 with ClassObjectType

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

the class CrossProductTest method setUp.

@Before
public void setUp() throws Exception {
    final ObjectType list1ObjectType = new ClassObjectType(String.class);
    final ObjectType list2ObjectType = new ClassObjectType(String.class);
    final RuleImpl rule = new RuleImpl("rule-1");
    final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
    final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
    rule.addPattern(list1Pattern);
    rule.addPattern(list2Pattern);
    final Declaration s1Declaration = rule.getDeclaration("s1");
    final Declaration s2Declaration = rule.getDeclaration("s2");
    this.values = new ArrayList();
    rule.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final WorkingMemory workingMemory) throws Exception {
            final String s1 = (String) knowledgeHelper.get(s1Declaration);
            final String s2 = (String) knowledgeHelper.get(s2Declaration);
            CrossProductTest.this.values.add(new String[] { s1, s2 });
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    this.pkg = new KnowledgePackageImpl("org.drools");
    this.pkg.addRule(rule);
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) IOException(java.io.IOException) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Before(org.junit.Before)

Example 42 with ClassObjectType

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

the class NodeSegmentUnlinkingTest 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);
    MockTupleSource mockTupleSource = new MockTupleSource(9);
    rule1 = new RuleImpl("rule1");
    rule2 = new RuleImpl("rule2");
    rule3 = new RuleImpl("rule3");
    ObjectTypeNode otn = new ObjectTypeNode(3, null, new ClassObjectType(String.class), buildContext);
    liaNode = new LeftInputAdapterNode(4, otn, buildContext);
    // 3, 4, 5, 6 are in same shared segment
    n1 = createBetaNode(10, type[0], liaNode);
    n2 = createBetaNode(11, type[1], n1);
    RuleTerminalNode rtn1 = new RuleTerminalNode(18, n2, rule1, rule1.getLhs(), 0, buildContext);
    rtn1.attach(buildContext);
    n3 = createBetaNode(12, type[2], n1);
    n4 = createBetaNode(13, type[3], n3);
    n5 = createBetaNode(14, type[4], n4);
    n6 = createBetaNode(15, type[5], n5);
    RuleTerminalNode rtn2 = new RuleTerminalNode(19, n6, rule2, rule2.getLhs(), 0, buildContext);
    rtn2.attach(buildContext);
    n7 = createBetaNode(16, type[6], n6);
    n8 = createBetaNode(17, type[7], n7);
    RuleTerminalNode rtn3 = new RuleTerminalNode(20, n8, rule3, rule3.getLhs(), 0, buildContext);
    rtn3.attach(buildContext);
    // n1 -> n2 -> r1
    // \
    // n3 -> n4 -> n5 -> n6 -> r2
    // \
    // n7 -> n8 -> r3
    n1.addAssociation(rule1);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule1);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule2);
    n3.addAssociation(rule3);
    n4.addAssociation(rule2);
    n4.addAssociation(rule3);
    n5.addAssociation(rule2);
    n5.addAssociation(rule3);
    n6.addAssociation(rule2);
    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 43 with ClassObjectType

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

the class NodeSegmentUnlinkingTest method testSingleNodeinSegment.

@Test
public void testSingleNodeinSegment() {
    rule1 = new RuleImpl("rule1");
    rule2 = new RuleImpl("rule2");
    rule3 = new RuleImpl("rule3");
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    BuildContext buildContext = new BuildContext(kBase);
    MockObjectSource mockObjectSource = new MockObjectSource(8);
    MockTupleSource mockTupleSource = new MockTupleSource(9);
    // n2 is only node in it's segment
    ObjectTypeNode otn = new ObjectTypeNode(2, null, new ClassObjectType(String.class), buildContext);
    BetaNode n1 = new JoinNode(10, new LeftInputAdapterNode(3, otn, buildContext), mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n2 = new JoinNode(11, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n3 = new JoinNode(12, n1, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n4 = new JoinNode(13, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    BetaNode n5 = new JoinNode(14, n2, mockObjectSource, new EmptyBetaConstraints(), buildContext);
    n1.addAssociation(rule1);
    n1.addAssociation(rule2);
    n1.addAssociation(rule3);
    n2.addAssociation(rule2);
    n2.addAssociation(rule3);
    n3.addAssociation(rule1);
    n4.addAssociation(rule2);
    n5.addAssociation(rule3);
    mockObjectSource.attach(buildContext);
    mockTupleSource.attach(buildContext);
    n1.attach(buildContext);
    n2.attach(buildContext);
    n3.attach(buildContext);
    n4.attach(buildContext);
    n5.attach(buildContext);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    createSegmentMemory(n2, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n1);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n4);
    assertNull(bm.getSegmentMemory());
    bm = (BetaMemory) ksession.getNodeMemory(n2);
    assertEquals(1, bm.getNodePosMaskBit());
    assertEquals(1, bm.getSegmentMemory().getAllLinkedMaskTest());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) BuildContext(org.drools.core.reteoo.builder.BuildContext) EmptyBetaConstraints(org.drools.core.common.EmptyBetaConstraints) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Test(org.junit.Test)

Example 44 with ClassObjectType

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

the class PropertyChangeListenerTest method test1.

@Test
public void test1() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(State.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    final State a = new State("go");
    ksession.insert(a, true);
    ksession.fireAllRules();
    assertEquals(1, sink.getAsserted().size());
    a.setState("stop");
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Test(org.junit.Test)

Example 45 with ClassObjectType

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

the class ReteTest method testObjectTypeNodes.

/**
 * Tests ObjectTypeNodes are correctly added to the Rete object
 *
 * @throws Exception
 */
@Test
public void testObjectTypeNodes() throws Exception {
    final Rete rete = kBase.getRete();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Object.class), buildContext);
    objectTypeNode.attach(buildContext);
    final ObjectTypeNode stringTypeNode = new ObjectTypeNode(2, this.entryPoint, new ClassObjectType(String.class), buildContext);
    stringTypeNode.attach(buildContext);
    final List<ObjectTypeNode> list = rete.getObjectTypeNodes();
    // Check the ObjectTypeNodes are correctly added to Rete
    assertEquals(3, list.size());
    assertTrue(list.contains(objectTypeNode));
    assertTrue(list.contains(stringTypeNode));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) 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