Search in sources :

Example 26 with Resource

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());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with Resource

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());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 28 with Resource

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();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 29 with Resource

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();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 30 with Resource

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"));
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) Resource(org.kie.api.io.Resource) InternalResource(org.drools.core.io.internal.InternalResource) ChangeSet(org.kie.internal.ChangeSet) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11