use of org.kie.api.KieBase in project drools by kiegroup.
the class SegmentCreationTest method testMultiSharedPattern.
@Test
public void testMultiSharedPattern() throws Exception {
KieBase kbase = buildKnowledgeBase(" A() \n", " A() B() \n", " A() B() C() \n");
InternalWorkingMemory wm = ((InternalWorkingMemory) kbase.newKieSession());
ObjectTypeNode aotn = getObjectTypeNode(kbase, LinkingTest.A.class);
LeftInputAdapterNode liaNode = (LeftInputAdapterNode) aotn.getObjectSinkPropagator().getSinks()[0];
RuleTerminalNode rtn1 = (RuleTerminalNode) liaNode.getSinkPropagator().getSinks()[0];
JoinNode bNode = (JoinNode) liaNode.getSinkPropagator().getSinks()[1];
RuleTerminalNode rtn2 = (RuleTerminalNode) bNode.getSinkPropagator().getSinks()[0];
JoinNode cNode = (JoinNode) bNode.getSinkPropagator().getSinks()[1];
RuleTerminalNode rtn3 = (RuleTerminalNode) cNode.getSinkPropagator().getSinks()[0];
wm.insert(new LinkingTest.A());
wm.insert(new LinkingTest.B());
wm.insert(new LinkingTest.C());
wm.flushPropagations();
// LiaNode is in it's own segment
LiaNodeMemory liaMem = (LiaNodeMemory) wm.getNodeMemory(liaNode);
SegmentMemory smem = liaMem.getSegmentMemory();
assertEquals(liaNode, smem.getRootNode());
assertEquals(liaNode, smem.getTipNode());
SegmentMemory rtnSmem1 = smem.getFirst();
assertEquals(rtn1, rtnSmem1.getRootNode());
assertEquals(rtn1, rtnSmem1.getTipNode());
SegmentMemory bSmem = rtnSmem1.getNext();
assertEquals(bNode, bSmem.getRootNode());
assertEquals(bNode, bSmem.getTipNode());
// child segment is not yet initialised, so null
assertNull(bSmem.getFirst());
// there is no next
assertNull(bSmem.getNext());
// child segments should now be initialised
wm.fireAllRules();
wm.flushPropagations();
SegmentMemory rtnSmem2 = bSmem.getFirst();
assertEquals(rtn2, rtnSmem2.getRootNode());
assertEquals(rtn2, rtnSmem2.getTipNode());
SegmentMemory cSmem = rtnSmem2.getNext();
assertEquals(cNode, cSmem.getRootNode());
// note rtn3 is in the same segment as C
assertEquals(rtn3, cSmem.getTipNode());
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class StatelessStressTest method testLotsOfStateless.
@Test
@Ignore
public void testLotsOfStateless() throws Exception {
final KieBase kbase = loadKnowledgeBase("thread_class_test.drl");
int numThreads = 100;
Thread[] ts = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
Runnable run = new Runnable() {
public void run() {
long start = 0;
long previous = 0;
while (true) {
start = System.currentTimeMillis();
StatelessKieSession sess = kbase.newStatelessKieSession();
try {
sess = SerializationHelper.serializeObject(sess);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
Person p = new Person();
p.setName("Michael");
Address add1 = new Address();
add1.setStreet("High");
Address add2 = new Address();
add2.setStreet("Low");
List l = new ArrayList();
l.add(add1);
l.add(add2);
p.setAddresses(l);
sess.execute(p);
long current = System.currentTimeMillis() - start;
if (previous != 0) {
float f = current / previous;
if (f > 1) {
System.err.println("SLOWDOWN");
}
}
previous = current;
}
}
};
Thread t = new Thread(run);
t.start();
ts[i] = t;
}
for (int i = 0; i < ts.length; i++) {
ts[i].join();
}
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class StreamsTest method testModifyOnEntryPointFacts.
// (timeout=10000)
@Test
public void testModifyOnEntryPointFacts() throws Exception {
String str = "package org.drools.compiler\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule R1 salience 100\n" + " when\n" + " $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep1\n" + " then\n" + " StockTick s = $s1;\n" + " modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R2 salience 90\n" + " when\n" + " $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep2\n" + " then\n" + " StockTick s = $s1;\n" + " modify( s ) { setPrice( 50 ) };\n" + "end\n" + "rule R3 salience 80\n" + " when\n" + " $s1 : StockTick( company == 'RHT', price == 10 ) from entry-point ep3\n" + " then\n" + " StockTick s = $s1;\n" + " modify( s ) { setPrice( 50 ) };\n" + "end\n";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
KieSession ksession = createKnowledgeSession(kbase);
org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(ael);
EntryPoint ep1 = ksession.getEntryPoint("ep1");
EntryPoint ep2 = ksession.getEntryPoint("ep2");
EntryPoint ep3 = ksession.getEntryPoint("ep3");
ep1.insert(new StockTick(1, "RHT", 10, 1000));
ep2.insert(new StockTick(1, "RHT", 10, 1000));
ep3.insert(new StockTick(1, "RHT", 10, 1000));
int rulesFired = ksession.fireAllRules();
assertEquals(3, rulesFired);
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(captor.capture());
List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(1).getMatch().getRule().getName(), is("R2"));
assertThat(aafe.get(2).getMatch().getRule().getName(), is("R3"));
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class StreamsTest method testAtomicActivationFiring.
@Test(timeout = 10000)
public void testAtomicActivationFiring() throws Exception {
// JBRULES-3383
String str = "package org.drools.compiler.test\n" + "declare Event\n" + " @role(event)\n" + " name : String\n" + "end\n" + "declare Monitor\n" + " @role(event)\n" + " event : Event\n" + " name : String\n" + "end\n" + "\n" + "rule \"start monitoring\"\n" + "when\n" + " $e : Event( $in : name )\n" + " not Monitor( name == $in )\n" + "then\n" + " Monitor m = new Monitor( $e, $in );\n" + " insert( m );\n" + "end\n" + "\n" + "rule \"stop monitoring\"\n" + "timer( int: 1s )\n" + "when\n" + " $m : Monitor( $in : name )\n" + " $e : Event( name == $in )\n" + "then\n" + " retract( $m );\n" + " retract( $m.getEvent() );\n" + "end\n" + "rule \"halt\"\n" + "salience -1\n" + "when\n" + " not Event( )\n" + "then\n" + " drools.halt();\n" + "end\n";
KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kBaseConfig.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kBaseConfig, str);
KieSession ksession = createKnowledgeSession(kbase);
ksession.addEventListener(new org.kie.api.event.rule.DebugAgendaEventListener());
FactType eventType = kbase.getFactType("org.drools.compiler.test", "Event");
Object event = eventType.newInstance();
eventType.set(event, "name", "myName");
ksession.insert(event);
ksession.fireUntilHalt();
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class StreamsTest method testDeclaredEntryPoint.
@Test(timeout = 10000)
public void testDeclaredEntryPoint() {
final String drl = "package org.jboss.qa.brms.declaredep\n" + "declare entry-point UnusedEntryPoint\n" + "end\n" + "rule HelloWorld\n" + " when\n" + " String( ) from entry-point UsedEntryPoint\n" + " then\n" + " // consequences\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, drl);
KieSession ksession = kbase.newKieSession();
assertNotNull(ksession.getEntryPoint("UsedEntryPoint"));
assertNotNull(ksession.getEntryPoint("UnusedEntryPoint"));
ksession.dispose();
}
Aggregations