use of org.kie.internal.builder.KnowledgeBuilder 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.kie.internal.builder.KnowledgeBuilder 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.kie.internal.builder.KnowledgeBuilder 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.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class BindTest method testBingWrongFieldName.
private void testBingWrongFieldName(final String drl) {
try {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (!kbuilder.hasErrors()) {
fail("KnowledgeBuilder should have errors");
}
} catch (final Exception e) {
e.printStackTrace();
fail("Exception should not be thrown ");
}
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class BindTest method testBindingToMissingField.
@Test
public void testBindingToMissingField() throws Exception {
// JBRULES-3047
String rule1 = "package org.drools.compiler\n";
rule1 += "rule rule1\n";
rule1 += "when\n";
rule1 += " Integer( $i : noSuchField ) \n";
rule1 += " eval( $i > 0 )\n";
rule1 += "then \n";
rule1 += "end\n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(rule1.getBytes()), ResourceType.DRL);
if (!kbuilder.hasErrors()) {
fail("this should have errors");
}
}
Aggregations