use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class PerformanceExample method main.
public static void main(final String[] args) throws Exception {
final long numberOfRulesToBuild = 10;
boolean useAccumulate = true;
// noticed performance difference between java and mvel dialects
String dialect = "mvel";
boolean usekjars = false;
boolean collectionBasedRules = true;
System.out.println("********* Numbers of rules: " + numberOfRulesToBuild + " kjars: " + usekjars + " accumulate: " + useAccumulate + " dialect: " + dialect + " *********");
String rules = getRules(numberOfRulesToBuild, useAccumulate, dialect, collectionBasedRules);
// System.out.println(rules);
long startTime = System.currentTimeMillis();
StatelessKieSession kSession;
FactType ft;
if (usekjars) {
KieContainer kContainer = loadContainerFromString(rules);
kSession = kContainer.newStatelessKieSession();
ft = kContainer.getKieBase().getFactType("org.drools.examples.performance", "TransactionC");
} else {
/* Alternative way to load knowledge base without using kjars.
Found slowness issue with internalInvalidateSegmentPrototype() when number of rules are increased.*/
KieBase kbase = loadKnowledgeBaseFromString(rules);
kSession = kbase.newStatelessKieSession();
ft = kbase.getFactType("org.drools.examples.performance", "TransactionC");
}
long endTime = System.currentTimeMillis();
System.out.println("Total time to build and load knowledgebase: " + (endTime - startTime) + " ms");
ArrayList output = new ArrayList();
kSession.setGlobal("mo", output);
Object o = ft.newInstance();
Gson gConverter = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
Object fo = gConverter.fromJson(getFact(), o.getClass());
// initial execute
kSession.execute(fo);
startTime = System.currentTimeMillis();
kSession.execute(fo);
endTime = System.currentTimeMillis();
System.out.println("Execution time: " + (endTime - startTime) + " ms");
String rulesOutput = gConverter.toJson(output);
System.out.println(rulesOutput);
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSequentialPlusPhreakOperationComplex.
@Test
public void testSequentialPlusPhreakOperationComplex() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import " + A.class.getCanonicalName() + "\n";
str += "global " + List.class.getCanonicalName() + " list\n";
// Focus is done as g1, g2, g1 to demonstrate that groups will not re-activate
str += "rule r0 when\n";
str += "then\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g2' ).setFocus();\n";
str += " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup( 'g1' ).setFocus();\n";
str += "end\n";
str += "rule r1 agenda-group 'g1' when\n";
str += " a : A( object > 0 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += " modify(a) { setObject( 3 ) };\n";
str += "end\n";
// r1_x is here to show they do not react when g2.r9 changes A o=2,
// i.e. checking that re-activating g1 won't cause it to pick up previous non evaluated rules.
// this is mostly checking that the no linking doesn't interfere with the expected results.
str += "rule r1_x agenda-group 'g1' when\n";
str += " a : A( object == 2 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
// r1_y is here to show it does not react when A is changed to o=5 in r3
str += "rule r1_y agenda-group 'g1' when\n";
str += " a : A( object == 5 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
str += "rule r2 agenda-group 'g1' when\n";
str += " a : A( object < 3 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
str += "rule r3 agenda-group 'g1' when\n";
str += " a : A(object >= 3 )\n";
str += "then\n";
str += " modify(a) { setObject( 5 ) };\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
// Checks that itself, f3 and r1_y do not react as they are higher up
str += "rule r4 agenda-group 'g1' when\n";
str += " a : A(object >= 3 )\n";
str += "then\n";
str += " modify(a) { setObject( 5 ) };\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
// Checks that while this at one point matches, it does not match by the time g2 is entered
// nor does it react when r9 changes a o=2
str += "rule r6 agenda-group 'g2' when\n";
str += " a : A(object < 5 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
str += "rule r7 agenda-group 'g2' when\n";
str += " a : A(object >= 3 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
str += "rule r8 agenda-group 'g2' when\n";
str += " a : A(object >= 5 )\n";
str += "then\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
// This changes A o=2 to check if g1.r1_x incorrect reacts when g1 is re-entered
str += "rule r9 agenda-group 'g2' when\n";
str += " a : A(object >= 5 )\n";
str += "then\n";
str += " modify(a) { setObject( 2 ) };\n";
str += " list.add( drools.getRule().getName() );\n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { new A(1) })));
assertEquals(6, list.size());
assertEquals("r1", list.get(0));
assertEquals("r3", list.get(1));
assertEquals("r4", list.get(2));
assertEquals("r7", list.get(3));
assertEquals("r8", list.get(4));
assertEquals("r9", list.get(5));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSequentialWithRulebaseUpdate.
// JBRULES-1567 - ArrayIndexOutOfBoundsException in sequential execution after calling RuleBase.addPackage(..)
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase(kconf, "simpleSalience.drl");
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.execute(new Person("pob"));
kbase.addPackages(loadKnowledgePackagesFromString(new String(IoUtils.readBytesFromInputStream(DynamicRulesTest.class.getResource("test_Dynamic3.drl").openStream()))));
ksession = kbase.newStatelessKieSession();
ksession.setGlobal("list", list);
Person person = new Person("bop");
ksession.execute(person);
assertEquals(7, list.size());
assertEquals("rule 3", list.get(0));
assertEquals("rule 2", list.get(1));
assertEquals("rule 1", list.get(2));
assertEquals("rule 3", list.get(3));
assertEquals("rule 2", list.get(4));
assertEquals("rule 1", list.get(5));
assertEquals(person, list.get(6));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testKnowledgeRuntimeAccess.
@Test
public void testKnowledgeRuntimeAccess() throws Exception {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import org.drools.compiler.Message\n";
str += "rule \"Hello World\"\n";
str += "when\n";
str += " Message( )\n";
str += "then\n";
str += " System.out.println( drools.getKieRuntime() );\n";
str += "end\n";
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
ksession.execute(new Message("help"));
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method runTestProfileManyRulesAndFacts.
private void runTestProfileManyRulesAndFacts(boolean sequentialMode, String message, int timetoMeasureIterations, String file) throws DroolsParserException, IOException, Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
if (sequentialMode) {
kconf.setOption(SequentialOption.YES);
} else {
kconf.setOption(SequentialOption.NO);
}
KieBase kbase = loadKnowledgeBase(kconf, file);
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
Object[] data = new Object[50000];
for (int i = 0; i < data.length; i++) {
if (i % 2 == 0) {
final Person p = new Person("p" + i, "stilton");
data[i] = p;
} else {
data[i] = new Cheese("cheddar", i);
}
}
if (timetoMeasureIterations == 0) {
// one shot measure
long start = System.currentTimeMillis();
ksession.execute(CommandFactory.newInsertElements(Arrays.asList(data)));
System.out.println("Time for " + message + ":" + (System.currentTimeMillis() - start));
assertTrue(list.size() > 0);
} else {
// lots of shots
// test throughput
long start = System.currentTimeMillis();
long end = start + timetoMeasureIterations;
int count = 0;
while (System.currentTimeMillis() < end) {
StatelessKieSession sess2 = createStatelessKnowledgeSession(kbase);
List list2 = new ArrayList();
sess2.setGlobal("list", list2);
sess2.execute(CommandFactory.newInsertElements(Arrays.asList(data)));
// session.execute( data );
count++;
}
System.out.println("Iterations in for " + message + " : " + count);
}
}
Aggregations