Search in sources :

Example 66 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class FusionAfterBeforeTest method testAfterBeforeOperators.

@Test
public void testAfterBeforeOperators() {
    final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("fusionAfterBeforeTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    final KieSession ksession = kieBase.newKieSession(ksconf, null);
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final EntryPoint stream = ksession.getEntryPoint("EventStream");
    SessionPseudoClock clock = ksession.getSessionClock();
    try {
        for (int i = 0; i < 3; i++) {
            final MessageEvent tc = new MessageEvent(MessageEvent.Type.received, new Message());
            stream.insert(tc);
            ksession.fireAllRules();
            clock.advanceTime(8, TimeUnit.SECONDS);
        }
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
    Assertions.assertThat(listener.isRuleFired("AfterMessageEvent")).as("Rule 'AfterMessageEvent' was no fired!").isTrue();
    Assertions.assertThat(listener.isRuleFired("BeforeMessageEvent")).as("Rule 'BeforeMessageEvent' was no fired!").isTrue();
    // each rules should be fired 2 times
    int firedCount = 2;
    int actuallyFired = listener.ruleFiredCount("AfterMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'AfterMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
    firedCount = listener.ruleFiredCount("BeforeMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'BeforeMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
}
Also used : Message(org.drools.testcoverage.common.model.Message) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Resource(org.kie.api.io.Resource) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 67 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class BenchmarkUtil method writeDomainModelToKJar.

private static void writeDomainModelToKJar(final KieServices kieServices, final KieFileSystem kieFileSystem) {
    final String javaSrc = Person.class.getCanonicalName().replace('.', File.separatorChar) + ".java";
    final Resource javaResource = kieServices.getResources().newFileSystemResource("src/test/java/" + javaSrc);
    kieFileSystem.write("src/main/java/" + javaSrc, javaResource);
}
Also used : Resource(org.kie.api.io.Resource)

Example 68 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class PMML4Compiler method prepareTemplate.

private static void prepareTemplate(String ntempl) {
    try {
        String path = TEMPLATE_PATH + ntempl;
        Resource res = ResourceFactory.newClassPathResource(path, org.kie.pmml.pmml_4_2.PMML4Compiler.class);
        if (res != null) {
            InputStream stream = res.getInputStream();
            if (stream != null) {
                registry.addNamedTemplate(path.substring(path.lastIndexOf('/') + 1), TemplateCompiler.compileTemplate(stream));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : InputStream(java.io.InputStream) Resource(org.kie.api.io.Resource) PMMLResource(org.drools.compiler.compiler.PMMLResource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) IOException(java.io.IOException)

Example 69 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class PMML4Compiler method compile.

public String compile(String resource, ClassLoader classLoader) {
    String theory = null;
    Resource cpr = new ClassPathResource(resource);
    try {
        theory = compile(cpr.getInputStream(), classLoader);
    } catch (IOException e) {
        results.add(new PMMLError(e.toString()));
        e.printStackTrace();
    }
    return theory;
}
Also used : Resource(org.kie.api.io.Resource) PMMLResource(org.drools.compiler.compiler.PMMLResource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) IOException(java.io.IOException) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 70 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class PMML4Compiler method getInputStreamByFileName.

private InputStream getInputStreamByFileName(String fileName) {
    InputStream is = null;
    Resource res = ResourceFactory.newClassPathResource(fileName);
    try {
        is = res.getInputStream();
    } catch (Exception e) {
    }
    if (is == null) {
        res = ResourceFactory.newFileResource(fileName);
    }
    try {
        is = res.getInputStream();
    } catch (Exception e) {
        this.results.add(new PMMLError("Unable to retrieve file based resource: " + fileName));
    }
    return is;
}
Also used : InputStream(java.io.InputStream) Resource(org.kie.api.io.Resource) PMMLResource(org.drools.compiler.compiler.PMMLResource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

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