Search in sources :

Example 81 with InternalFactHandle

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

the class TraitTest method testTupleIntegrityOnModification.

@Test
public void testTupleIntegrityOnModification() {
    String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + "    $core: Entity( ) " + "then " + "    A o = don( $core, A.class ); " + "    System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + "   $x: A( value == 0 ) " + "then " + "   list.add( 0 ); " + "end " + "rule Check when " + "   $x: A( value == 42 ) " + "then " + "   list.add( 42 ); " + "end " + "rule Mood when " + "  $x : A( value != 42 ) " + "then " + "  modify ( $x ) { setValue( 42 ); } " + "end ";
    KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Entity());
    ksession.fireAllRules();
    for (final Object o : ksession.getObjects(new ObjectFilter() {

        @Override
        public boolean accept(Object object) {
            return object.getClass().getName().contains("test.A");
        }
    })) {
        InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
        LeftTuple first = handle.getFirstLeftTuple();
        assertTrue(first instanceof RuleTerminalNodeLeftTuple);
        assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
    }
    assertEquals(Arrays.asList(0, 42), list);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) KieBase(org.kie.api.KieBase) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) ObjectFilter(org.drools.core.ObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) RuleTerminalNodeLeftTuple(org.drools.core.reteoo.RuleTerminalNodeLeftTuple) Test(org.junit.Test)

Example 82 with InternalFactHandle

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

the class CepEspTest method testEventAssertionWithDuration.

@Test(timeout = 10000)
public void testEventAssertionWithDuration() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDuration.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession session = createKnowledgeSession(kbase, conf);
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000, 5);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 11000, 10);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 12000, 8);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 13000, 7);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    EventFactHandle eh1 = (EventFactHandle) handle1;
    EventFactHandle eh2 = (EventFactHandle) handle2;
    EventFactHandle eh3 = (EventFactHandle) handle3;
    EventFactHandle eh4 = (EventFactHandle) handle4;
    assertEquals(tick1.getTime(), eh1.getStartTimestamp());
    assertEquals(tick2.getTime(), eh2.getStartTimestamp());
    assertEquals(tick3.getTime(), eh3.getStartTimestamp());
    assertEquals(tick4.getTime(), eh4.getStartTimestamp());
    assertEquals(tick1.getDuration(), eh1.getDuration());
    assertEquals(tick2.getDuration(), eh2.getDuration());
    assertEquals(tick3.getDuration(), eh3.getDuration());
    assertEquals(tick4.getDuration(), eh4.getDuration());
    session.fireAllRules();
    assertEquals(2, results.size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 83 with InternalFactHandle

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

the class CepEspTest method testCoincidesOnArbitraryDates.

@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
    100000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 50 milliseconds after DROO
    100050, 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(tick1, results.get(0));
    assertEquals(tick2, results.get(1));
    assertEquals(tick1, results.get(2));
    assertEquals(tick2, results.get(3));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 84 with InternalFactHandle

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

the class CepEspTest method testEventDeclarationForInterfaces.

@Test(timeout = 10000)
public void testEventDeclarationForInterfaces() throws Exception {
    // read in the source
    final KieBase kbase = loadKnowledgeBase("test_CEP_EventInterfaces.drl");
    KieSession session = createKnowledgeSession(kbase);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 85 with InternalFactHandle

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

the class CepEspTest method testBeforeOnArbitraryDates.

@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
    104000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
    100000, 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(tick1, results.get(0));
    assertEquals(tick2, results.get(1));
    assertEquals(tick1, results.get(2));
    assertEquals(tick2, results.get(3));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) 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