use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class WorkingMemoryActionsSerializationTest method before.
@Before
public void before() {
ruleCalls.clear();
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
ksession = kbase.newKieSession();
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterMatchFired(final AfterMatchFiredEvent event) {
final String name = event.getMatch().getRule().getName();
System.out.println(name + " fired!");
synchronized (ruleCalls) {
Integer calls = ruleCalls.get(name);
if (calls == null) {
calls = 1;
} else {
calls++;
}
ruleCalls.put(name, calls);
}
}
});
// Using fire until halt. If firaAllRules is called it works. But for other reasons, I need to run fireUntilHalt
new Thread(new Runnable() {
@Override
public void run() {
ksession.fireUntilHalt();
}
}).start();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class UnmarshallingTest method initializeKnowledgeBase.
private KieBase initializeKnowledgeBase(String rule) {
// Setup knowledge base
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new RuntimeException(kbuilder.getErrors().toString());
}
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
return knowledgeBase;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MultithreadTest method testConcurrentQueries.
@Test(timeout = 5000)
public void testConcurrentQueries() {
// DROOLS-175
final StringBuilder drl = new StringBuilder();
drl.append("package org.drools.test;\n" + "" + "query foo( ) \n" + " Object() from new Object() \n" + "end\n" + "" + "rule XYZ when then end \n");
final KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.toString().getBytes()), ResourceType.DRL);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
final KieSession ksession = kbase.newKieSession();
final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setDaemon(true);
return t;
}
});
final int THREAD_NR = 5;
final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
for (int i = 0; i < THREAD_NR; i++) {
ecs.submit(new Callable<Boolean>() {
public Boolean call() throws Exception {
boolean succ = false;
try {
final QueryResults res = ksession.getQueryResults("foo", Variable.v);
succ = (res.size() == 1);
return succ;
} catch (final Exception e) {
e.printStackTrace();
return succ;
}
}
});
}
boolean success = true;
for (int i = 0; i < THREAD_NR; i++) {
try {
success = ecs.take().get() && success;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Assertions.assertThat(success).isTrue();
ksession.dispose();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class TestUtil method buildSessionInSteps.
public static KieSession buildSessionInSteps(final boolean reuseKieBaseWhenAddingRules, final String... drls) {
if (drls == null || drls.length == 0) {
return KnowledgeBaseFactory.newKnowledgeBase().newKieSession();
} else {
String drl = drls[0];
final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, drl);
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession kSession = kbase.newKieSession();
for (int i = 1; i < drls.length; i++) {
drl = drls[i];
final KnowledgeBuilder kbuilder2;
if (reuseKieBaseWhenAddingRules) {
kbuilder2 = createKnowledgeBuilder(kSession.getKieBase(), drl);
} else {
kbuilder2 = createKnowledgeBuilder(null, drl);
}
((InternalKnowledgeBase) kSession.getKieBase()).addPackages(kbuilder2.getKnowledgePackages());
}
return kSession;
}
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd.
@Test
public void testSerializeAdd() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
InternalFactHandle stilton = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
// now recreate the rulebase, deserialize the session and test it
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
list = (List) session.getGlobal("list");
assertNotNull(list);
assertEquals(list.size(), 1);
assertEquals("stilton", list.get(0));
kpkgs = loadKnowledgePackages("../test_Dynamic3.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
kBase.addPackages(kpkgs);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(list.size(), 3);
assertEquals(bob.getObject(), list.get(2));
assertEquals("stilton", list.get(1));
session.dispose();
}
Aggregations