use of org.kie.api.KieBase in project drools by kiegroup.
the class WindowTest method initialization.
@Before
public void initialization() {
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.write("src/main/resources/kbase1/window_test.drl", drl);
KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
kbuilder.buildAll();
List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
assertEquals(res.toString(), 0, res.size());
KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId()).newKieBase(kbconf);
KieSessionConfiguration ksconfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconfig.setOption(ClockTypeOption.get("pseudo"));
ksession = kbase.newKieSession(ksconfig, null);
clock = ksession.getSessionClock();
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class MultithreadTest method testConcurrentFireAndDispose.
@Test(timeout = 10000)
public void testConcurrentFireAndDispose() throws InterruptedException {
// DROOLS-1103
final String drl = "rule R no-loop timer( int: 1s )\n" + "when\n" + " String()\n" + "then\n" + "end";
final KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
final KieBase kbase = helper.build(EventProcessingOption.STREAM);
final KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption(TimedRuleExecutionOption.YES);
final KieSession ksession = kbase.newKieSession(ksconf, null);
final Thread t1 = new Thread() {
@Override
public void run() {
LOG.info("before: sleep, dispose().");
try {
Thread.sleep(100);
} catch (final InterruptedException _e) {
}
LOG.info("before: dispose().");
ksession.dispose();
LOG.info("after: dispose().");
}
};
t1.setDaemon(true);
t1.start();
try {
int i = 0;
LOG.info("before: while.");
while (true) {
ksession.insert("" + i++);
ksession.fireAllRules();
}
} catch (final IllegalStateException e) {
// java.lang.IllegalStateException: Illegal method call. This session was previously disposed.
// ignore and exit
LOG.info("after: while.");
} catch (final java.util.concurrent.RejectedExecutionException e) {
e.printStackTrace();
Assertions.fail("java.util.concurrent.RejectedExecutionException should not happen");
}
LOG.info("last line of test.");
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class MultithreadTest method testClassLoaderRace.
@Test(timeout = 10000)
public void testClassLoaderRace() throws InterruptedException {
final String drl = "package org.drools.integrationtests;\n" + "" + "rule \"average temperature\"\n" + "when\n" + " $avg := Number( ) from accumulate ( " + " $x : Integer ( ); " + " average ($x) )\n" + "then\n" + " System.out.println( $avg );\n" + "end\n" + "\n";
final KieBase kbase = loadKnowledgeBaseFromString(drl);
final KieSession session = kbase.newKieSession();
final Thread t = new Thread() {
public void run() {
session.fireUntilHalt();
}
};
t.start();
session.fireAllRules();
for (int j = 0; j < 100; j++) {
session.insert(j);
}
try {
Thread.sleep(1000);
System.out.println("Halting ..");
session.halt();
} catch (final Exception e) {
Assertions.fail(e.getMessage());
}
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class MultithreadTest method testJittingShortComparison.
@Test(timeout = 20000)
public void testJittingShortComparison() {
// DROOLS-1633
final String drl = "import " + BeanA.class.getCanonicalName() + "\n;" + "global java.util.List list;" + "rule R when\n" + " $a1: BeanA($sv1 : shortValue)\n" + " $b2: BeanA(shortValue != $sv1)\n" + "then\n" + " list.add(\"FIRED\");\n" + "end";
final List<String> list = Collections.synchronizedList(new ArrayList());
final RuleBaseConfiguration rbc = (RuleBaseConfiguration) KieServices.Factory.get().newKieBaseConfiguration();
rbc.setJittingThreshold(0);
final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(rbc);
final int threadNr = 1000;
final Thread[] threads = new Thread[threadNr];
for (int i = 0; i < threadNr; i++) {
threads[i] = new Thread(new SessionRunner(kbase, list));
}
for (int i = 0; i < threadNr; i++) {
threads[i].start();
}
for (int i = 0; i < threadNr; i++) {
try {
threads[i].join();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(list).hasSize(0);
}
use of org.kie.api.KieBase in project drools by kiegroup.
the class MultithreadTest method testConcurrencyWithChronThreads.
@Test
@Ignore
public void testConcurrencyWithChronThreads() throws InterruptedException {
final String drl = "package it.intext.drools.fusion.bug;\n" + "\n" + "import " + MyFact.class.getCanonicalName() + ";\n " + " global java.util.List list; \n" + "\n" + "declare MyFact\n" + "\t@role( event )\n" + "\t@expires( 1s )\n" + "end\n" + "\n" + "rule \"Dummy\"\n" + "timer( cron: 0/1 * * * * ? )\n" + "when\n" + " Number( $count : intValue ) from accumulate( MyFact( ) over window:time(1s); sum(1) )\n" + "then\n" + " System.out.println($count+\" myfact(s) seen in the last 1 seconds\");\n" + " list.add( $count ); \n" + "end";
final KieBaseConfiguration kbconfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconfig.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconfig, drl);
final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("REALTIME"));
final KieSession ksession = kbase.newKieSession(conf, null);
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
final Runner t = new Runner(ksession);
t.start();
final int FACTS_PER_POLL = 1000;
final int POLL_INTERVAL = 500;
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(new Runnable() {
public void run() {
for (int j = 0; j < FACTS_PER_POLL; j++) {
ksession.insert(new MyFact());
}
}
}, 0, POLL_INTERVAL, TimeUnit.MILLISECONDS);
Thread.sleep(10200);
executor.shutdownNow();
ksession.halt();
t.join();
if (t.getError() != null) {
Assertions.fail(t.getError().getMessage());
}
System.out.println("Final size " + ksession.getObjects().size());
// assertEquals( 2000, ksession.getObjects().size() );
ksession.dispose();
}
Aggregations