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));
}
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());
}
}
}
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));
}
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());
}
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());
}
Aggregations