Search in sources :

Example 16 with Resource

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

the class KnowledgeBuilderTest method testResourceMapping.

@Ignore
@Test
public void testResourceMapping() throws Exception {
    ServiceRegistryImpl serviceRegistry = (ServiceRegistryImpl) ServiceRegistry.getInstance();
    serviceRegistry.reset();
    ServiceDiscoveryImpl.getInstance().addService(PMMLCompiler.class.getCanonicalName(), new PMMLCompiler() {

        public String compile(InputStream stream, ClassLoader cl) {
            return "rule R2 when then end";
        }

        @Override
        public List<KnowledgeBuilderResult> getResults() {
            return Collections.emptyList();
        }

        @Override
        public void clearResults() {
        // To change body of implemented methods use File | Settings | File Templates.
        }

        public Resource[] transform(Resource input, ClassLoader classLoader) {
            return new Resource[0];
        }

        public String getCompilerVersion() {
            return "KnowledgeBuilderTest";
        }

        @Override
        public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public List<PMMLResource> precompile(String fileName, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public Map<String, String> getJavaClasses(InputStream stream) {
            return Collections.emptyMap();
        }

        @Override
        public Map<String, String> getJavaClasses(String fileName) {
            return Collections.emptyMap();
        }
    });
    serviceRegistry.reload();
    String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " \n" + "then\n" + "end\n";
    Resource res1 = ResourceFactory.newByteArrayResource(rule.getBytes());
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(res1, ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KiePackage kp1 = kbuilder.getKnowledgePackages().iterator().next();
    assertEquals(1, kp1.getRules().size());
    Rule r = kp1.getRules().iterator().next();
    assertEquals(res1, ((RuleImpl) r).getResource());
    String pmml = "<PMML version=\"4.0\"><Header/></PMML>";
    Resource res2 = ResourceFactory.newByteArrayResource(pmml.getBytes());
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(res2, ResourceType.PMML);
    assertFalse(kbuilder2.getErrors().toString(), kbuilder2.hasErrors());
    KiePackage kp2 = kbuilder2.getKnowledgePackages().iterator().next();
    assertEquals(1, kp2.getRules().size());
    Rule r2 = kp2.getRules().iterator().next();
    assertEquals(res2, ((RuleImpl) r2).getResource());
}
Also used : ServiceRegistryImpl(org.kie.api.internal.utils.ServiceRegistryImpl) InputStream(java.io.InputStream) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) PMMLResource(org.drools.compiler.compiler.PMMLResource) Resource(org.kie.api.io.Resource) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Resource

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

the class LifecycleTest method initSession.

@Before
public void initSession() {
    String drlString = "package org.jboss.brms\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role( event )\n" + "    @expires( 4s )\n" + "end\n" + "rule \"TestEventReceived\"\n" + "    when\n" + "        $event : StockTick() over window:time(4s) from entry-point EventStream\n" + "    then\n" + "        // do something;\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    final Resource drl = ks.getResources().newByteArrayResource(drlString.getBytes()).setTargetPath("org/jboss/brms/lifecycle.drl");
    kfs.write(drl);
    KieModuleModel kmoduleModel = ks.newKieModuleModel();
    kmoduleModel.newKieBaseModel("defaultKBase").addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("defaultKieSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
    kfs.writeKModuleXML(kmoduleModel.toXML());
    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size());
    this.kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
Also used : 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) Before(org.junit.Before)

Example 18 with Resource

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

the class DynamicRuleLoadTest method testKJarUpgradeWithJavaClass.

@Test
public void testKJarUpgradeWithJavaClass() throws Exception {
    KieServices ks = KieServices.Factory.get();
    String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.0.0");
    Resource javaResource = ResourceFactory.newByteArrayResource(javaSrc.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
    Resource drlResource = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
    KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
    // Create a session and fire rules
    kieContainer = ks.newKieContainer(km.getReleaseId());
    ksession = kieContainer.newKieSession();
    Class<?> clazz = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject");
    Object person = clazz.newInstance();
    ksession.insert(person);
    ksession.fireAllRules();
    assertNotNull(person);
    Object personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 1", personId);
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.1.0");
    Resource javaResource2 = ResourceFactory.newByteArrayResource(javaSrc_2.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
    Resource drlResource2 = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
    createAndDeployJar(ks, kmodule, releaseId2, javaResource2, drlResource2);
    // update container
    kieContainer.updateToVersion(releaseId2);
    assertEquals(releaseId2, kieContainer.getReleaseId());
    // now let's run the rules
    ksession = kieContainer.newKieSession();
    person = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject").newInstance();
    ksession.insert(person);
    ksession.fireAllRules();
    assertNotNull(person);
    personId = valueOf(person, "id");
    assertNotNull(personId);
    assertEquals("Person from version 2", personId);
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 19 with Resource

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

the class CommonTestMethodBase method deployJarIntoRepository.

private static KieModule deployJarIntoRepository(KieServices ks, byte[] jar) {
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    return km;
}
Also used : Resource(org.kie.api.io.Resource) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule)

Example 20 with Resource

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

the class CommonTestMethodBase method deployJar.

public static KieModule deployJar(KieServices ks, byte[] jar) {
    // Deploy jar into the repository
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    return km;
}
Also used : Resource(org.kie.api.io.Resource) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule)

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