use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicFunction.
@Test(timeout = 10000)
public void testDynamicFunction() throws Exception {
// JBRULES-1258 serialising a package breaks function removal -- left the serialisation commented out for now
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction1.drl"));
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
final KieSession workingMemory = createKnowledgeSession(kbase);
final List<?> list = new ArrayList<Object>();
workingMemory.setGlobal("list", list);
final Cheese stilton = new Cheese("stilton", 5);
workingMemory.insert(stilton);
workingMemory.fireAllRules();
assertEquals(new Integer(5), list.get(0));
// Check a function can be removed from a package.
// Once removed any efforts to use it should throw an Exception
kbase.removeFunction("org.drools.compiler.test", "addFive");
final Cheese cheddar = new Cheese("cheddar", 5);
workingMemory.insert(cheddar);
try {
workingMemory.fireAllRules();
fail("Function should have been removed and NoClassDefFoundError thrown from the Consequence");
} catch (final Throwable e) {
}
// Check a new function can be added to replace an old function
Collection<KiePackage> kpkgs2 = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction2.drl"));
kbase.addPackages(kpkgs2);
final Cheese brie = new Cheese("brie", 5);
workingMemory.insert(brie);
workingMemory.fireAllRules();
assertEquals(new Integer(6), list.get(1));
Collection<KiePackage> kpkgs3 = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicFunction3.drl"));
kbase.addPackages(kpkgs3);
final Cheese feta = new Cheese("feta", 5);
workingMemory.insert(feta);
workingMemory.fireAllRules();
assertEquals(new Integer(5), list.get(2));
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TimerAndCalendarTest method buildKnowledgePackage.
private Collection<KiePackage> buildKnowledgePackage(Resource resource, ResourceType resourceType) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(resource, resourceType);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors != null && errors.size() > 0) {
for (KnowledgeBuilderError error : errors) {
System.err.println("Error: " + error.getMessage());
}
Assert.fail("KnowledgeBase did not build");
}
Collection<KiePackage> packages = kbuilder.getKnowledgePackages();
return packages;
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TimerAndCalendarTest method testEventExpires.
@Test
public void testEventExpires() throws Exception {
String timerRule = "package org.drools.test\n" + "declare TestEvent \n" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "" + "rule TimerRule \n" + " when \n" + " TestEvent( ) from entry-point \"Test\"\n" + " then \n" + "end";
KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
kbase.addPackages(kpackages);
KieSession ksession = createSession(kbase);
FactType type = kbase.getFactType("org.drools.test", "TestEvent");
Assert.assertNotNull("could not get type", type);
ksession = disposeAndReloadSession(ksession, kbase);
ksession.getEntryPoint("Test").insert(type.newInstance());
ksession.fireAllRules();
ksession = disposeAndReloadSession(ksession, kbase);
ksession = disposeAndReloadSession(ksession, kbase);
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerRuleAfterIntReloadSession.
@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterIntReloadSession() throws Exception {
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = createSession(kbase);
// must advance time or it won't save.
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// if we do not call 'ksession.fireAllRules()', this test will run successfully.
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(300, TimeUnit.MILLISECONDS);
// build timer rule, if the rule is fired, the list size will increase every 500ms
String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + " timer (int:1000 500) \n" + "when \n" + "then \n" + " list.add(list.size()); \n" + " end";
Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
kbase.addPackages(kpackages);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
ksession.setGlobal("list", list);
Assert.assertEquals(0, list.size());
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1700, TimeUnit.MILLISECONDS);
Assert.assertEquals(2, list.size());
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(1000, TimeUnit.MILLISECONDS);
// if the rule is fired, the list size will greater than one.
Assert.assertEquals(4, list.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class HeaderTest method testPMMLHeader.
@Test
public void testPMMLHeader() {
String source = PMML4Helper.pmmlDefaultPackageName().replace(".", File.separator) + File.separator + "test_header.xml";
boolean header = false;
boolean timestamp = false;
boolean appl = false;
boolean descr = false;
boolean copyright = false;
boolean annotation = false;
PMML4Compiler compiler = new PMML4Compiler();
compiler.getHelper().setPack("org.drools.pmml.pmml_4_2.test");
String theory = compiler.compile(source, null);
BufferedReader reader = new BufferedReader(new StringReader(theory));
try {
String line = "";
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("// Imported PMML Model Theory"))
header = true;
else if (line.startsWith("// Creation timestamp :"))
timestamp = line.contains("now");
else if (line.startsWith("// Description :"))
descr = line.contains("test");
else if (line.startsWith("// Copyright :"))
copyright = line.contains("opensource");
else if (line.startsWith("// Annotation :"))
annotation = line.contains("notes here");
else if (line.startsWith("// Trained with :"))
appl = line.contains("handmade");
}
} catch (IOException ioe) {
ioe.printStackTrace();
fail();
}
assertTrue(header);
assertTrue(timestamp);
assertTrue(descr);
assertTrue(copyright);
assertTrue(annotation);
assertTrue(appl);
KieSession ksession = getSession(theory);
KiePackage pack = ksession.getKieBase().getKiePackage("org.drools.pmml.pmml_4_2.test");
assertNotNull(pack);
ksession.dispose();
}
Aggregations