Search in sources :

Example 6 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class ReteTest method testAssertObject.

/**
 * Test asserts correctly propagate
 *
 * @throws Exception
 */
@Test
public void testAssertObject() throws Exception {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final String string = "String";
    final DefaultFactHandle h1 = new DefaultFactHandle(1, string);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    assertLength(0, sink1.getAsserted());
    // There is a List ObjectTypeNode, make sure it was propagated
    final List list = new ArrayList();
    final DefaultFactHandle h2 = new DefaultFactHandle(1, list);
    rete.assertObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final List asserted = sink1.getAsserted();
    assertLength(1, asserted);
    final Object[] results = (Object[]) asserted.get(0);
    assertSame(list, ((DefaultFactHandle) results[0]).getObject());
}
Also used : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 7 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class AddRemoveTest method testAdd.

@Test
public void testAdd() {
    /*
         * create a RuleBase with a single ObjectTypeNode we attach a
         * MockObjectSink so we can detect assertions and retractions
         */
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    BuildContext context = new BuildContext(kBase, Collections.emptyList());
    NodeFactory nFacotry = new PhreakNodeFactory();
    EntryPointNode entryPoint = context.getRuleBase().getRete().getEntryPointNodes().values().iterator().next();
    final ObjectTypeNode objectTypeNode = nFacotry.buildObjectTypeNode(0, entryPoint, new ClassObjectType(Object.class), context);
    objectTypeNode.attach(context);
    final MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    kBase.newKieSession();
// objectTypeNode.
}
Also used : EntryPointNode(org.drools.core.reteoo.EntryPointNode) BuildContext(org.drools.core.reteoo.builder.BuildContext) NodeFactory(org.drools.core.reteoo.builder.NodeFactory) PhreakNodeFactory(org.drools.core.reteoo.builder.PhreakNodeFactory) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MockObjectSink(org.drools.core.reteoo.MockObjectSink) PhreakNodeFactory(org.drools.core.reteoo.builder.PhreakNodeFactory) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 8 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class ReteTest method testCache.

/**
 * Tests that interfaces and parent classes for an asserted  class are  cached, for  quick future iterations
 */
@Test
public void testCache() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    MockObjectSink sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(Collection.class), buildContext);
    objectTypeNode.attach(buildContext);
    sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(ArrayList.class), buildContext);
    objectTypeNode.attach(buildContext);
    sink = new MockObjectSink();
    objectTypeNode.addObjectSink(sink);
    // ArrayList matches all three ObjectTypeNodes
    final DefaultFactHandle h1 = new DefaultFactHandle(1, new ArrayList());
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    // ArrayList matches two ObjectTypeNodes
    h1.setObject(new ArrayList());
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ClassObjectTypeConf conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
    assertLength(3, conf.getObjectTypeNodes());
    conf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
    assertLength(3, conf.getObjectTypeNodes());
}
Also used : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Example 9 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class ReteTest method testHierarchy.

@Test
@Ignore
public void testHierarchy() {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final Rete rete = kBase.getRete();
    final IdGenerator idGenerator = kBase.getReteooBuilder().getNodeIdsGenerator();
    // Attach a List ObjectTypeNode
    final ObjectTypeNode listOtn = new ObjectTypeNode(idGenerator.getNextId(), this.entryPoint, new ClassObjectType(List.class), buildContext);
    listOtn.attach(buildContext);
    // Will automatically create an ArrayList ObjectTypeNode
    FactHandle handle = ksession.insert(new ArrayList());
    // Check we have three ObjectTypeNodes, List, ArrayList and InitialFactImpl
    assertEquals(3, rete.getObjectTypeNodes().size());
    // double check that the List reference is the same as the one we created, i.e. engine should try and recreate it
    assertSame(listOtn, rete.getObjectTypeNodes(EntryPointId.DEFAULT).get(new ClassObjectType(List.class)));
    // ArrayConf should match two ObjectTypenodes for List and ArrayList
    ClassObjectTypeConf arrayConf = (ClassObjectTypeConf) ksession.getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(this.entryPoint.getEntryPoint(), new ArrayList());
    final ObjectTypeNode arrayOtn = arrayConf.getConcreteObjectTypeNode();
    assertEquals(2, arrayConf.getObjectTypeNodes().length);
    // Check it contains List and ArrayList
    List nodes = Arrays.asList(arrayConf.getObjectTypeNodes());
    assertEquals(2, nodes.size());
    assertTrue(nodes.contains(arrayOtn));
    assertTrue(nodes.contains(listOtn));
    // Nodes are there, retract the fact so we can check both nodes are populated
    ksession.retract(handle);
    // Add MockSinks so we can track assertions
    final MockObjectSink listSink = new MockObjectSink();
    listOtn.addObjectSink(listSink);
    final MockObjectSink arraySink = new MockObjectSink();
    listOtn.addObjectSink(arraySink);
    ksession.insert(new ArrayList());
    assertEquals(1, listSink.getAsserted().size());
    assertEquals(1, arraySink.getAsserted().size());
    // Add a Collection ObjectTypeNode, so that we can check that the data from ArrayList is sent to it
    final ObjectTypeNode collectionOtn = new ObjectTypeNode(idGenerator.getNextId(), this.entryPoint, new ClassObjectType(Collection.class), buildContext);
    final MockObjectSink collectionSink = new MockObjectSink();
    collectionOtn.addObjectSink(collectionSink);
    collectionOtn.attach(new TestBuildContext(kBase));
    assertEquals(1, collectionSink.getAsserted().size());
    // check that ArrayListConf was updated with the new ObjectTypeNode
    nodes = Arrays.asList(arrayConf.getObjectTypeNodes());
    assertEquals(3, nodes.size());
    assertTrue(nodes.contains(arrayOtn));
    assertTrue(nodes.contains(listOtn));
    assertTrue(nodes.contains(collectionOtn));
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectTypeConf(org.drools.core.reteoo.ClassObjectTypeConf) ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) IdGenerator(org.drools.core.reteoo.ReteooBuilder.IdGenerator) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with MockObjectSink

use of org.drools.core.reteoo.MockObjectSink in project drools by kiegroup.

the class ReteTest method testRetractObject.

/**
 * All objects deleted from a RootNode must be propagated to all children
 * ObjectTypeNodes.
 */
@Test
public void testRetractObject() throws Exception {
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, this.entryPoint, new ClassObjectType(List.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final String string = "String";
    final DefaultFactHandle h1 = new DefaultFactHandle(1, string);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    assertLength(0, sink1.getAsserted());
    assertLength(0, sink1.getRetracted());
    // There is a List ObjectTypeNode, make sure it was propagated
    final List list = new ArrayList();
    final DefaultFactHandle h2 = new DefaultFactHandle(1, list);
    // need  to assert first, to force it to build  up the cache
    rete.assertObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    rete.retractObject(h2, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final List retracted = sink1.getRetracted();
    assertLength(1, retracted);
    final Object[] results = (Object[]) retracted.get(0);
    assertSame(list, ((DefaultFactHandle) results[0]).getObject());
}
Also used : Rete(org.drools.core.reteoo.Rete) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ClassObjectType(org.drools.core.base.ClassObjectType) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MockObjectSink(org.drools.core.reteoo.MockObjectSink) Test(org.junit.Test)

Aggregations

MockObjectSink (org.drools.core.reteoo.MockObjectSink)11 Test (org.junit.Test)11 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)10 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)9 ClassObjectType (org.drools.core.base.ClassObjectType)8 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)8 Rete (org.drools.core.reteoo.Rete)6 BuildContext (org.drools.core.reteoo.builder.BuildContext)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 AlphaNode (org.drools.core.reteoo.AlphaNode)3 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 PropagationContext (org.drools.core.spi.PropagationContext)3 Cheese (org.drools.mvel.model.Cheese)3 MockObjectSource (org.drools.mvel.model.MockObjectSource)3 ClassObjectTypeConf (org.drools.core.reteoo.ClassObjectTypeConf)2 EntryPointNode (org.drools.core.reteoo.EntryPointNode)2