use of org.kie.api.io.Resource in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.
@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() 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 300ms
String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + " timer (cron: * * * * * ?) \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);
List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
ksession.setGlobal("list", list);
ksession.setGlobal("list", list);
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(10, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, list.size());
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(3, TimeUnit.SECONDS);
Assert.assertEquals(4, list.size());
ksession = disposeAndReloadSession(ksession, kbase);
ksession.setGlobal("list", list);
clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
clock.advanceTime(2, TimeUnit.SECONDS);
// if the rule is fired, the list size will greater than one.
Assert.assertEquals(6, list.size());
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class TimerAndCalendarTest method testTimerWithRemovingRule.
@Test
public void testTimerWithRemovingRule() throws Exception {
// DROOLS-576
// Only reproducible with RETEOO
InternalKnowledgeBase kbase1 = KnowledgeBaseFactory.newKnowledgeBase();
String str1 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R1\n" + " timer ( int: 5s )\n" + "when\n" + " $s : String( )\n" + "then\n" + " list.add( $s );\n" + "end\n";
Resource resource1 = ResourceFactory.newByteArrayResource(str1.getBytes());
Collection<KiePackage> kpackages1 = buildKnowledgePackage(resource1, ResourceType.DRL);
kbase1.addPackages(kpackages1);
StatefulKnowledgeSession ksession1 = JPAKnowledgeService.newStatefulKnowledgeSession(kbase1, null, createEnvironment(context));
long ksessionId = ksession1.getIdentifier();
ArrayList<String> list = new ArrayList<String>();
ksession1.setGlobal("list", list);
ksession1.insert("hello");
ksession1.fireAllRules();
// dispose before firing
ksession1.dispose();
Assert.assertEquals(0, list.size());
Thread.sleep(5000);
// A new kbase without the timer's activated rule
InternalKnowledgeBase kbase2 = KnowledgeBaseFactory.newKnowledgeBase();
String str2 = "package org.test; " + "import java.util.*; " + "global java.util.List list; " + "rule R2\n" + "when\n" + " $s : Integer( )\n" + "then\n" + " list.add( $s );\n" + "end\n";
Resource resource2 = ResourceFactory.newByteArrayResource(str2.getBytes());
Collection<KiePackage> kpackages2 = buildKnowledgePackage(resource2, ResourceType.DRL);
kbase2.addPackages(kpackages2);
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(ksessionId, kbase2, null, createEnvironment(context));
ksession2.setGlobal("list", list);
ksession2.fireAllRules();
ksession2.dispose();
Assert.assertEquals(0, list.size());
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DroolsAbstractPMMLTest method readKnowledgeBase.
private static KieBase readKnowledgeBase(List<InputStream> theory) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
for (int j = 0; j < theory.size(); j++) {
Resource res = ks.getResources().newInputStreamResource(theory.get(j));
kfs.write(RESOURCE_PATH + "source_" + j + ".drl", res);
}
KieModuleModel model = ks.newKieModuleModel();
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
kfs.writeKModuleXML(model.toXML());
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
return kContainer.getKieBase();
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DroolsAbstractPMMLTest method getModelSession.
protected KieSession getModelSession(String[] pmmlSources, boolean verbose) {
KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();
for (int j = 0; j < pmmlSources.length; j++) {
Resource res = ResourceFactory.newClassPathResource(pmmlSources[j]).setResourceType(ResourceType.PMML);
kfs.write(res);
}
KieModuleModel model = ks.newKieModuleModel();
model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).setDefault(true).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.STREAM);
kfs.writeKModuleXML(model.toXML());
KieBuilder kb = ks.newKieBuilder(kfs);
kb.buildAll();
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}
KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
KieBase kieBase = kContainer.getKieBase();
setKbase(kieBase);
return kieBase.newKieSession();
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class XmlChangeSetReaderTest method testResourceAttributes.
@Test
public void testResourceAttributes() throws Exception {
SemanticModules semanticModules = new SemanticModules();
semanticModules.addSemanticModule(new ChangeSetSemanticModule());
XmlChangeSetReader changeSetReader = new XmlChangeSetReader(semanticModules);
changeSetReader.setClassLoader(XmlChangeSetReaderTest.class.getClassLoader(), null);
ChangeSet changeSet = changeSetReader.read(XmlChangeSetReaderTest.class.getClassLoader().getResourceAsStream("org/drools/core/xml/test-change-set.xml"));
Assert.assertNotNull(changeSet);
Collection<Resource> resourcesAdded = changeSet.getResourcesAdded();
Assert.assertNotNull(resourcesAdded);
Assert.assertEquals(4, resourcesAdded.size());
InternalResource resource1 = null;
InternalResource resource2 = null;
InternalResource resource3 = null;
InternalResource secureResource = null;
for (Resource r : resourcesAdded) {
InternalResource resource = (InternalResource) r;
if (resource.getSourcePath() != null && resource.getSourcePath().equals("resource1")) {
resource1 = resource;
} else if (resource.getSourcePath() != null && resource.getSourcePath().equals("secureResource")) {
secureResource = resource;
} else if (resource.getSourcePath() == null && resource.getDescription() == null) {
resource3 = resource;
} else if (resource.getSourcePath() == null) {
resource2 = resource;
}
}
Assert.assertNotNull(resource1);
Assert.assertNotNull(resource2);
Assert.assertNotNull(resource3);
Assert.assertNotNull(secureResource);
Assert.assertNull(resource1.getDescription());
Assert.assertEquals("another description", resource2.getDescription());
Assert.assertEquals("some useful description", secureResource.getDescription());
Assert.assertEquals(2, changeSetReader.getParser().getAttrs().getLength());
Assert.assertEquals("DRL", changeSetReader.getParser().getAttrs().getValue("type"));
}
Aggregations