Search in sources :

Example 21 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class TruthMaintenanceTest method testStatedWithShadowAndDeleteException.

@Test(timeout = 10000)
public void testStatedWithShadowAndDeleteException() {
    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" + "";
    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);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go1");
    session.fireAllRules();
    // TMS is now enabled
    assertNotNull(fh1.getEqualityKey());
    assertNotNull(fh2.getEqualityKey());
    // EqualtyKey shows both are stated
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertEquals(EqualityKey.STATED, fh2.getEqualityKey().getStatus());
    // Only fh1 has a logical
    assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
    assertNull(fh2.getEqualityKey().getBeliefSet());
    // Get the logical Handle too
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    EqualityKey key = jfh1.getEqualityKey();
    assertSame(fh1.getEqualityKey(), key);
    assertNotSame(fh1, jfh1);
    assertEquals(2, key.size());
    assertSame(jfh1, key.getLogicalFactHandle());
}
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)

Example 22 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class TruthMaintenanceTest method testStatedShadowLogicalThenLogicalOnly.

@Test(timeout = 10000)
public void testStatedShadowLogicalThenLogicalOnly() {
    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);
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    FactHandle g1 = session.insert("go1");
    session.fireAllRules();
    // This removes the stated position, but it should still be logical now and exist
    session.delete(fh1, FactHandle.State.STATED);
    session.insert("go2");
    session.fireAllRules();
    // fh1 is invalid and no longer in the WM. However it's now reverted to it's Justified version and will still be there
    assertFalse(fh1.isValid());
    // Make sure f1 is still there, but logical only now
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    InternalFactHandle jfh1 = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    assertSame(jfh1, session.getFactHandle("f1"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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) 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)

Example 23 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class MarshallingTest method testSerializeAdd3.

/*
     *  Works Fine if both the scenarios mentioned above are skipped.
     */
@Test
public void testSerializeAdd3() throws Exception {
    // Create a rulebase, a session, and test it
    Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
    kBase.addPackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    List results = new ArrayList();
    KieSession session = kBase.newKieSession();
    session.setGlobal("results", results);
    InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
    InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(stilton1.getObject(), results.get(0));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
    InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
    InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
    InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
    session.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(stilton2.getObject(), results.get(1));
    assertEquals(bob2.getObject(), results.get(2));
    assertEquals(bob1.getObject(), results.get(3));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    // dynamic add pkgs
    kpkgs = loadKnowledgePackages("../test_Dynamic1_2.drl");
    kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
    InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
    InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
    InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
    InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
    session.fireAllRules();
    assertEquals(9, results.size());
    assertEquals(stilton3.getObject(), results.get(4));
    assertEquals(bob4.getObject(), results.get(5));
    assertEquals(bob3.getObject(), results.get(6));
    assertEquals(addr2.getObject(), results.get(7));
    assertEquals(addr1.getObject(), results.get(8));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
    InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
    InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
    InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
    InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
    session.fireAllRules();
    assertEquals(14, results.size());
    assertEquals(stilton4.getObject(), results.get(9));
    assertEquals(bob6.getObject(), results.get(10));
    assertEquals(bob5.getObject(), results.get(11));
    assertEquals(addr4.getObject(), results.get(12));
    assertEquals(addr3.getObject(), results.get(13));
    // serialize session and rulebase
    kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
    session.dispose();
}
Also used : Address(org.drools.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 24 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveRuleChangeFHFirstLeftTuple.

@Test
public void testRemoveRuleChangeFHFirstLeftTuple() {
    final String[] rules = getRules1Pattern();
    final KieSession kieSession = TestUtil.createSession(rules);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE1_NAME);
        kieSession.fireAllRules();
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        assertNotNull(fh1.getFirstLeftTuple());
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 25 with InternalFactHandle

use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.

the class AddRemoveRulesTest method testRemoveChildLeftTupleThatWasMiddle.

@Test
public void testRemoveChildLeftTupleThatWasMiddle() {
    final String[] rules = getRules3Pattern();
    final KieSession kieSession = TestUtil.createSession(rules);
    try {
        final List resultsList = new ArrayList();
        kieSession.setGlobal("list", resultsList);
        TestUtil.insertFacts(kieSession, 3);
        kieSession.fireAllRules();
        Assertions.assertThat(resultsList).containsOnly(TestUtil.RULE1_NAME, TestUtil.RULE2_NAME, TestUtil.RULE3_NAME);
        resultsList.clear();
        TestUtil.removeRules(kieSession, TestUtil.RULES_PACKAGE_NAME, TestUtil.RULE2_NAME);
        kieSession.fireAllRules();
        final Map<String, Rule> rulesMap = rulestoMap(kieSession.getKieBase());
        final InternalFactHandle fh1 = (InternalFactHandle) kieSession.getFactHandle(3);
        final LeftTuple lt = fh1.getFirstLeftTuple().getFirstChild();
        assertSame(lt, fh1.getFirstLeftTuple().getLastChild());
        assertEquals(1, lt.getTupleSink().getAssociatedRuleSize());
        assertTrue(lt.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE1_NAME)));
        final LeftTuple peer = lt.getPeer();
        assertEquals(1, peer.getTupleSink().getAssociatedRuleSize());
        assertTrue(peer.getTupleSink().isAssociatedWith(rulesMap.get(TestUtil.RULE3_NAME)));
    } finally {
        kieSession.dispose();
    }
}
Also used : ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Test(org.junit.Test)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14