Search in sources :

Example 36 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteTest method testNotShadowed.

@Test
@Ignore
public void testNotShadowed() {
    Properties properties = new Properties();
    properties.setProperty("drools.shadowProxyExcludes", "org.drools.core.test.model.Cheese");
    RuleBaseConfiguration conf = new RuleBaseConfiguration(properties);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(conf);
    buildContext = new BuildContext(kBase);
    final StatefulKnowledgeSessionImpl ksession = new StatefulKnowledgeSessionImpl(1L, kBase);
    // Create a Rete network with ObjectTypeNodes for List, Collection and ArrayList
    final Rete rete = kBase.getRete();
    final EntryPointNode entryPoint = new EntryPointNode(0, rete, buildContext);
    entryPoint.attach(buildContext);
    final ObjectTypeNode objectTypeNode = new ObjectTypeNode(1, entryPoint, new ClassObjectType(Cheese.class), buildContext);
    objectTypeNode.attach(buildContext);
    final MockObjectSink sink1 = new MockObjectSink();
    objectTypeNode.addObjectSink(sink1);
    // There are no String ObjectTypeNodes, make sure its not propagated
    final Cheese cheese = new Cheese("brie", 15);
    final DefaultFactHandle h1 = new DefaultFactHandle(1, cheese);
    rete.assertObject(h1, pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null), ksession);
    ksession.fireAllRules();
    final Object[] results = (Object[]) sink1.getAsserted().get(0);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.core.test.model.Cheese) Properties(java.util.Properties) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 37 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl 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)

Example 38 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testBasicWorkingMemoryActions.

/*
     * @see JBRULES-356
     */
@Test
@Ignore
public void testBasicWorkingMemoryActions() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final TruthMaintenanceSystem tms = ((NamedEntryPoint) ksession.getWorkingMemoryEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
    final String string = "test";
    FactHandle fd = ksession.insert(string);
    FactHandle fz = ksession.getTruthMaintenanceSystem().insert(string, null, null, new MockActivation());
    assertEquals(1, tms.getEqualityKeyMap().size());
    EqualityKey key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.update(fd, string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fz, key.getFactHandle());
    assertEquals(2, key.size());
    ksession.retract(fd);
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    fd = ksession.insert(string);
    assertEquals(1, tms.getEqualityKeyMap().size());
    assertEquals(1, tms.getEqualityKeyMap().size());
    key = tms.get(string);
    assertSame(fd, key.getFactHandle());
    assertEquals(1, key.size());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) FactHandle(org.kie.api.runtime.rule.FactHandle) EqualityKey(org.drools.core.common.EqualityKey) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) MockActivation(org.drools.core.test.model.MockActivation) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 39 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testExecuteQueueActions.

@Test
@Ignore
public void testExecuteQueueActions() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ReentrantAction action = new ReentrantAction();
    ksession.queueWorkingMemoryAction(action);
    ksession.flushPropagations();
    assertEquals(2, action.counter.get());
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 40 with StatefulKnowledgeSessionImpl

use of org.drools.core.impl.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ReteooWorkingMemoryTest method testId.

@Test
public void testId() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    assertEquals(0, ksession.getIdentifier());
    ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    assertEquals(1, ksession.getIdentifier());
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)79 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 KieSession (org.kie.api.runtime.KieSession)22 List (java.util.List)20 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)17 ClassObjectType (org.drools.core.base.ClassObjectType)14 FactHandle (org.kie.api.runtime.rule.FactHandle)14 KieBase (org.kie.api.KieBase)13 HashMap (java.util.HashMap)9 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)8 Cheese (org.drools.core.test.model.Cheese)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)6 TruthMaintenanceSystem (org.drools.core.common.TruthMaintenanceSystem)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6