Search in sources :

Example 1 with StatefulKnowledgeSessionImpl

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

the class ScenarioRunner4JUnit method resetKieSession.

private void resetKieSession(final KieSession kieSession) {
    final StatefulKnowledgeSessionImpl statefulKnowledgeSession = (StatefulKnowledgeSessionImpl) kieSession;
    statefulKnowledgeSession.reset();
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl)

Example 2 with StatefulKnowledgeSessionImpl

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

the class TruthMaintenanceTest method testLogicalInsertionsNot.

@Test(timeout = 10000)
public // @Ignore("in Java 8, the byte[] generated by serialization are not the same and requires investigation")
void testLogicalInsertionsNot() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LogicalInsertionsNot.drl");
    KieSession ksession = kbase.newKieSession();
    final Person a = new Person("a");
    final Cheese cheese = new Cheese("brie", 1);
    ksession.setGlobal("cheese", cheese);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Collection list = ksession.getObjects();
    assertEquals("i was not asserted by not a => i.", 1, list.size());
    assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
    FactHandle h = ksession.insert(a);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    // no need to fire rules, assertion alone removes justification for i,
    // so it should be deleted.
    // workingMemory.fireAllRules();
    ksession.fireAllRules();
    list = ksession.getObjects();
    assertEquals("a was not asserted or i not deleted.", 1, list.size());
    assertEquals("a was asserted.", a, list.iterator().next());
    assertFalse("i was not rectracted.", list.contains(cheese));
    // no rules should fire, but nevertheless...
    // workingMemory.fireAllRules();
    assertEquals("agenda should be empty.", 0, ((InternalAgenda) ((StatefulKnowledgeSessionImpl) ksession).getAgenda()).agendaSize());
    h = getFactHandle(h, ksession);
    ksession.retract(h);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    list = ksession.getObjects();
    assertEquals("i was not asserted by not a => i.", 1, list.size());
    assertEquals("i was not asserted by not a => i.", cheese, list.iterator().next());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 3 with StatefulKnowledgeSessionImpl

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

the class FactHandleMarshallingTest method createWorkingMemory.

private StatefulKnowledgeSessionImpl createWorkingMemory(InternalKnowledgeBase kBase) {
    // WorkingMemoryEntryPoint
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    SessionConfiguration sessionConf = ((SessionConfiguration) ksconf);
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase, true, sessionConf, EnvironmentFactory.newEnvironment());
    return wm;
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 4 with StatefulKnowledgeSessionImpl

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

the class SegmentMemoryPrototypeTest method testSessionCache.

@Test
public void testSessionCache() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(DRL.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
    checkKieSession(ksession);
    ksession.dispose();
    ksession.reset();
    checkKieSession(ksession);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with StatefulKnowledgeSessionImpl

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

the class TruthMaintenanceTest method testLogicalThenStatedShadowSingleOccurance.

@Test(timeout = 10000)
public void testLogicalThenStatedShadowSingleOccurance() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    session.insert("go1");
    session.fireAllRules();
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go2");
    session.fireAllRules();
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
    assertNotSame(fh1, jfh1);
    EqualityKey key = jfh1.getEqualityKey();
    assertSame(fh1.getEqualityKey(), key);
    assertNotSame(fh1, jfh1);
    assertEquals(2, key.size());
    assertSame(jfh1, key.getLogicalFactHandle());
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) EqualityKey(org.drools.core.common.EqualityKey) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)84 Test (org.junit.Test)63 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)41 KieSession (org.kie.api.runtime.KieSession)24 ArrayList (java.util.ArrayList)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)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 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 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