use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateVersionWithKSessionLogger.
@Test
public void testUpdateVersionWithKSessionLogger() {
// DROOLS-790
String drl1 = "import java.util.List\n" + "import java.util.ArrayList\n" + "\n" + "rule \"Test1\"\n" + "\n" + "when\n" + " $a : Integer()\n" + "then\n" + " insert(new ArrayList());\n" + "end\n";
String drl2 = "rule \"Test2\"\n" + "when\n" + " $b : List()\n" + " then\n" + " $b.isEmpty();\n" + "end";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJar(ks, releaseId1, drl1);
KieContainer kc = ks.newKieContainer(km.getReleaseId());
StatelessKieSession statelessKieSession = kc.newStatelessKieSession();
KieRuntimeLogger kieRuntimeLogger = ks.getLoggers().newConsoleLogger(statelessKieSession);
List<Command> cmds = new ArrayList<Command>();
cmds.add(CommandFactory.newInsertElements(new ArrayList()));
FireAllRulesCommand fireAllRulesCommand = (FireAllRulesCommand) CommandFactory.newFireAllRules();
cmds.add(fireAllRulesCommand);
cmds.add(CommandFactory.newGetObjects("returnedObjects"));
BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(cmds);
statelessKieSession.execute(batchExecutionCommand);
kieRuntimeLogger.close();
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl1 + drl2);
kc.updateToVersion(km.getReleaseId());
}
use of org.kie.api.runtime.StatelessKieSession 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.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testBasicOperation.
@Test
public void testBasicOperation() throws Exception {
KieBase kbase = loadKnowledgeBase(kconf, "simpleSequential.drl");
StatelessKieSession ksession = createStatelessKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p1 = new Person("p1", "stilton");
final Person p2 = new Person("p2", "cheddar");
final Person p3 = new Person("p3", "stilton");
final Cheese stilton = new Cheese("stilton", 15);
final Cheese cheddar = new Cheese("cheddar", 15);
ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { p1, stilton, p2, cheddar, p3 })));
assertEquals(3, list.size());
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class SequentialTest method testSequentialWithNoLoop.
@Test(timeout = 10000L)
public void testSequentialWithNoLoop() throws Exception {
// BZ-1228098
String str = "package org.drools.compiler.test\n" + "import \n" + Message.class.getCanonicalName() + ";" + "rule R1 no-loop when\n" + " $s : String( )" + " $m : Message( )\n" + "then\n" + " modify($m) { setMessage($s) };\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write("src/main/resources/r0.drl", str);
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results = kieBuilder.getResults();
if (results.hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
throw new RuntimeException(results.getMessages().toString());
}
KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieBaseConfiguration kieBaseConf = ks.newKieBaseConfiguration();
kieBaseConf.setOption(SequentialOption.YES);
StatelessKieSession sequentialKsession = kieContainer.newKieBase(kieBaseConf).newStatelessKieSession();
List result = (List) sequentialKsession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
StatelessKieSession ksession = kieContainer.getKieBase().newStatelessKieSession();
result = (List) ksession.execute(CommandFactory.newInsertElements(Arrays.asList("test", new Message())));
assertEquals(2, result.size());
}
use of org.kie.api.runtime.StatelessKieSession in project drools by kiegroup.
the class StatelessSessionTest method getSession2.
private StatelessKieSession getSession2(final Resource resource) throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(resource, ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors());
}
assertFalse(kbuilder.hasErrors());
final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
final StatelessKieSession session = kbase.newStatelessKieSession();
session.setGlobal("list", this.list);
session.setGlobal("cheesery", this.cheesery);
return session;
}
Aggregations