Search in sources :

Example 6 with Resource

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

the class KieBaseUpdater method updateResourcesIncrementally.

private int updateResourcesIncrementally(KnowledgeBuilderImpl kbuilder, CompositeKnowledgeBuilder ckbuilder) {
    int fileCount = ctx.modifiedClasses.size();
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() != ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.newKM, ctx.newKieBaseModel, resourceName)) {
                List<ResourceChange> changes = rcs.getChanges();
                if (!changes.isEmpty()) {
                    // we need to deal with individual parts of the resource
                    fileCount += AbstractKieModule.updateResource(ckbuilder, ctx.newKM, resourceName, rcs) ? 1 : 0;
                } else {
                    // the whole resource has to handled
                    if (rcs.getChangeType() == ChangeType.UPDATED) {
                        Resource resource = ctx.currentKM.getResource(resourceName);
                        kbuilder.removeObjectsGeneratedFromResource(resource);
                    }
                    fileCount += ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, resourceName, rcs) ? 1 : 0;
                }
            }
        }
        for (ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) ctx.kBase.getKiePackage(loadOrder.getPkgName());
            if (pkg != null) {
                RuleImpl rule = pkg.getRule(loadOrder.getRuleName());
                if (rule != null) {
                    // rule can be null, if it didn't exist before
                    rule.setLoadOrder(loadOrder.getLoadOrder());
                }
            }
        }
    }
    return fileCount;
}
Also used : Resource(org.kie.api.io.Resource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) ResourceChange(org.kie.internal.builder.ResourceChange) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint)

Example 7 with Resource

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

the class KieBuilderImpl method buildPMMLPojos.

private void buildPMMLPojos(List<String> pmmlFileNames, KieProject kProject) {
    PMMLCompiler compiler = PMMLCompilerFactory.getPMMLCompiler();
    System.out.println("!!!!! PMMLCompiler version = " + compiler.getCompilerVersion() + " !!!!!");
    KieFileSystem javaSource = KieServices.Factory.get().newKieFileSystem();
    Map<String, String> javaSources = new HashMap<>();
    for (String fname : pmmlFileNames) {
        Map<String, String> modelSources = null;
        Resource pmmlContent = ResourceFactory.newByteArrayResource(srcMfs.getBytes(fname));
        if (pmmlContent != null) {
            try {
                modelSources = compiler.getJavaClasses(pmmlContent.getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            modelSources = compiler.getJavaClasses(fname);
        }
        if (modelSources != null && !modelSources.isEmpty()) {
            javaSources.putAll(modelSources);
        }
    }
    for (String key : javaSources.keySet()) {
        String javaCode = javaSources.get(key);
        if (javaCode != null && !javaCode.trim().isEmpty()) {
            Resource res = ResourceFactory.newByteArrayResource(javaCode.getBytes()).setResourceType(ResourceType.JAVA);
            String sourcePath = key.replaceAll("\\.", "/") + ".java";
            res.setSourcePath(sourcePath);
            javaSource.write(res);
        }
    }
    ResourceReader src = ((KieFileSystemImpl) javaSource).asMemoryFileSystem();
    List<String> javaFileNames = getJavaFileNames(src);
    if (javaFileNames != null && !javaFileNames.isEmpty()) {
        ClassLoader classLoader = kProject.getClassLoader();
        KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
        JavaDialectConfiguration javaConf = (JavaDialectConfiguration) kconf.getDialectConfiguration("java");
        compileJavaClasses(javaConf, classLoader, javaFileNames, JAVA_ROOT, src);
    }
}
Also used : ResourceReader(org.drools.compiler.commons.jci.readers.ResourceReader) DiskResourceReader(org.drools.compiler.commons.jci.readers.DiskResourceReader) KieFileSystem(org.kie.api.builder.KieFileSystem) HashMap(java.util.HashMap) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) Resource(org.kie.api.io.Resource) PMMLResource(org.drools.compiler.compiler.PMMLResource) IOException(java.io.IOException) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)

Example 8 with Resource

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

the class KieRepositoryImpl method addKieModule.

public KieModule addKieModule(Resource resource, Resource... dependencies) {
    log.info("Adding KieModule from resource: " + resource);
    KieModule kModule = getKieModule(resource);
    if (dependencies != null && dependencies.length > 0) {
        for (Resource depRes : dependencies) {
            InternalKieModule depKModule = (InternalKieModule) getKieModule(depRes);
            ((InternalKieModule) kModule).addKieDependency(depKModule);
            log.info("Adding KieModule dependency from resource: " + resource);
        }
    }
    addKieModule(kModule);
    return kModule;
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) Resource(org.kie.api.io.Resource) KieModule(org.kie.api.builder.KieModule) KieBuilderImpl.setDefaultsforEmptyKieModule(org.drools.compiler.kie.builder.impl.KieBuilderImpl.setDefaultsforEmptyKieModule)

Example 9 with Resource

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

the class KieBuilderTest method testMultipleKBaseWithDrlError.

@Test
public void testMultipleKBaseWithDrlError() {
    // RHBRMS-2651
    final String drl = "package org.drools.compiler;\n" + "rule \"test\"\n" + "  when\n" + "    Smurf\n" + "  then\n" + "end";
    final KieServices ks = KieServices.Factory.get();
    final KieModuleModel kproj = ks.newKieModuleModel();
    kproj.newKieBaseModel("kbase1").newKieSessionModel("ksession1").setDefault(true);
    kproj.newKieBaseModel("kbase2").newKieSessionModel("ksession2");
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kproj.toXML());
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    kfs.write("src/main/resources/org/drools/compiler/drl1.drl", drlResource);
    final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    final List<org.kie.api.builder.Message> messages = kb.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertEquals(4, messages.size());
    assertTrue(messages.get(0).toString().contains("kbase1"));
    assertTrue(messages.get(1).toString().contains("kbase1"));
    assertTrue(messages.get(2).toString().contains("kbase2"));
    assertTrue(messages.get(3).toString().contains("kbase2"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 10 with Resource

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

the class KieBuilderTest method testStatelessSessionDeclarativeChannelRegistration.

@Test
public void testStatelessSessionDeclarativeChannelRegistration() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final 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\" type=\"stateless\">\n" + "       <channels>\n" + "         <channel name=\"testChannel\" type=\"org.drools.compiler.integrationtests.KieBuilderTest$MockChannel\" />\n" + "       </channels>\n" + "    </ksession>" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    StatelessKieSession statelessKieSession = kieContainer.newStatelessKieSession();
    assertEquals(1, statelessKieSession.getChannels().size());
    assertTrue(statelessKieSession.getChannels().keySet().contains("testChannel"));
}
Also used : Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) 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