Search in sources :

Example 1 with IdGenerator

use of org.drools.core.reteoo.ReteooBuilder.IdGenerator 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().getIdGenerator();
    // 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().getObjectTypeConf(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 : ClassObjectType(org.drools.core.base.ClassObjectType) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) IdGenerator(org.drools.core.reteoo.ReteooBuilder.IdGenerator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ClassObjectType (org.drools.core.base.ClassObjectType)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)1 IdGenerator (org.drools.core.reteoo.ReteooBuilder.IdGenerator)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1