Search in sources :

Example 96 with InternalFactHandle

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

the class StreamsTest method testEntryPointReference.

// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
    KieSession session = kbase.newKieSession();
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("stream1");
    InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
    InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
    InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
    InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
    assertNotNull(handle5);
    assertNotNull(handle6);
    assertNotNull(handle7);
    assertNotNull(handle8);
    assertTrue(handle5.isEvent());
    assertTrue(handle6.isEvent());
    assertTrue(handle7.isEvent());
    assertTrue(handle8.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertSame(tick7, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 97 with InternalFactHandle

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

the class TruthMaintenanceTest method testLogicalWithDeleteException.

@Test(timeout = 10000)
public void testLogicalWithDeleteException() {
    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);
    session.insert("go1");
    session.fireAllRules();
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    assertSame(jfh1, session.getFactHandle("f1"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) 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 98 with InternalFactHandle

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

the class TruthMaintenanceTest method testJustificationStateOverridingBySuperClass.

@Test(timeout = 10000)
public void testJustificationStateOverridingBySuperClass() {
    // DROOLS-352
    String droolsSource = "package org.drools.tms.test; \n" + "" + "declare Foo end \n" + "declare Bar extends Foo end \n" + "" + "rule Justify_Sub \n" + "when \n" + "then \n" + " insertLogical( new Bar() ); \n" + " insertLogical( 42.0 ); \n" + "end \n" + "rule Justify_Sup \n" + "when \n" + " $d : Double() \n" + "then \n" + " insertLogical( new Foo() ); \n" + "end \n" + "" + "" + "rule Fuu when Foo() then end \n" + "rule Bor when Bar() then end \n";
    // ///////////////////////////////////
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.EQUALITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    session.fireAllRules();
    for (FactHandle fh : session.getFactHandles()) {
        InternalFactHandle ifh = (InternalFactHandle) fh;
        assertEquals(EqualityKey.JUSTIFIED, ifh.getEqualityKey().getStatus());
    }
}
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) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 99 with InternalFactHandle

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

the class TruthMaintenanceTest method testLogicalInsertionsSelfreferencing.

// (timeout=10000)
@Test
public // @Ignore
void testLogicalInsertionsSelfreferencing() throws Exception {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertionsSelfreferencing.drl", getClass()), ResourceType.DRL);
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    final KieSession session = createKnowledgeSession(kbase);
    final Person b = new Person("b");
    final Person a = new Person("a");
    session.setGlobal("b", b);
    FactHandle h1 = session.insert(a);
    session.fireAllRules();
    Collection<?> list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals(2, list.size());
    assertTrue(list.contains(a));
    assertTrue(list.contains(b));
    session.retract(h1);
    session.fireAllRules();
    list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", 1, list.size());
    assertEquals("b was deleted, but it should not have. Is backed by b => b being true.", b, list.iterator().next());
    h1 = session.getFactHandle(b);
    assertSame(((InternalFactHandle) h1).getEqualityKey().getLogicalFactHandle(), h1);
    ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem().delete(h1);
    session.fireAllRules();
    list = session.getObjects(new ClassObjectFilter(a.getClass()));
    assertEquals(0, list.size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectFilter(org.drools.core.ClassObjectFilter) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 100 with InternalFactHandle

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

the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.

@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
    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);
    // 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) 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

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