Search in sources :

Example 6 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class StreamsTest method testMultipleWindows.

@Test(timeout = 10000)
public void testMultipleWindows() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule FaultsCoincide\n" + "when\n" + "   f1 : StockTick( company == \"RHT\" ) over window:length( 1 )\n" + "   f2 : StockTick( company == \"JBW\" ) over window:length( 1 )\n" + "then\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    StockTick st1 = new StockTick(1, "RHT", 10, 1000);
    ksession.insert(st1);
    StockTick st2 = new StockTick(2, "JBW", 10, 1000);
    ksession.insert(st2);
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat((StockTick) aafe.getMatch().getDeclarationValue("f1"), is(st1));
    assertThat((StockTick) aafe.getMatch().getDeclarationValue("f2"), is(st2));
}
Also used : AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 7 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class StreamsTest method testEventExpirationValue.

@Test(timeout = 10000)
public void testEventExpirationValue() throws Exception {
    String drl1 = "package org.drools.pkg1\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.pkg2\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> otns = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectType stot = new ClassObjectType(StockTick.class);
    for (ObjectTypeNode otn : otns) {
        if (otn.getObjectType().isAssignableFrom(stot)) {
            assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
        }
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 8 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration 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)

Example 9 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated.

@Test(timeout = 10000)
public void testLogicalWithStatedShadowThenDeleteLogicalThenDeleteStated() {
    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 in ('go2', 'go3', 'go4') ) " + "    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");
    session.insert("go2");
    session.fireAllRules();
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertEquals(1, fh1.getEqualityKey().getBeliefSet().size());
    assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
    assertNotSame(fh1, jfh1);
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    list.clear();
    tms.delete(jfh1);
    session.insert("go3");
    session.fireAllRules();
    assertNull(fh1.getEqualityKey().getBeliefSet());
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
    list.clear();
    session.delete(fh1);
    session.insert("go4");
    session.fireAllRules();
    assertEquals(0, list.size());
}
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 10 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration 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)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)151 Test (org.junit.Test)131 KieSession (org.kie.api.runtime.KieSession)116 KieBase (org.kie.api.KieBase)99 ArrayList (java.util.ArrayList)76 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)57 List (java.util.List)50 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 StockTick (org.drools.compiler.StockTick)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)31 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)24 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)21 FactHandle (org.kie.api.runtime.rule.FactHandle)20 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)17 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)14 StockTickInterface (org.drools.compiler.StockTickInterface)13 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)13