Search in sources :

Example 1 with Resource

use of org.kie.api.io.Resource in project graylog2-server by Graylog2.

the class DroolsEngine method createKJar.

private byte[] createKJar(KieServices ks, ReleaseId releaseId, String pom, String... drls) throws RulesCompilationException {
    KieFileSystem kfs = ks.newKieFileSystem();
    if (pom != null) {
        kfs.write("pom.xml", pom);
    } else {
        kfs.generateAndWritePomXML(releaseId);
    }
    for (int i = 0; i < drls.length; i++) {
        if (drls[i] != null) {
            kfs.write("src/main/resources/r" + i + ".drl", drls[i]);
        }
    }
    for (URI builtinRuleUrl : builtinRuleUrls) {
        final String rulesFileName = FilenameUtils.getName(builtinRuleUrl.getPath());
        final String path = "src/main/resources/" + rulesFileName;
        final URL url;
        try {
            url = builtinRuleUrl.toURL();
        } catch (MalformedURLException e) {
            throw new RulesCompilationException(Collections.<org.kie.api.builder.Message>emptyList());
        }
        final Resource resource = ResourceFactory.newUrlResource(url).setSourcePath(path).setResourceType(ResourceType.DRL);
        kfs.write(resource);
    }
    final KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
    if (kb.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        throw new RulesCompilationException(kb.getResults().getMessages());
    }
    final InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    return kieModule.getBytes();
}
Also used : MalformedURLException(java.net.MalformedURLException) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.graylog2.plugin.Message) Resource(org.kie.api.io.Resource) URI(java.net.URI) URL(java.net.URL) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 2 with Resource

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

the class ClassHierarchyManager method buildDescrsFromFields.

private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, Map<String, TypeFieldDescr> fieldMap) {
    ClassFieldInspector inspector = null;
    try {
        inspector = new ClassFieldInspector(klass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    for (String name : inspector.getGetterMethods().keySet()) {
        // classFieldAccessor requires both getter and setter
        if (inspector.getSetterMethods().containsKey(name)) {
            if (!inspector.isNonGetter(name) && !"class".equals(name)) {
                Resource resource = typeDescr.getResource();
                PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
                patternDescr.setResource(resource);
                TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
                inheritedFlDescr.setResource(resource);
                inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
                if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
                    fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
        }
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Resource(org.kie.api.io.Resource) IOException(java.io.IOException) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 3 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 4 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 5 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)

Aggregations

Resource (org.kie.api.io.Resource)268 Test (org.junit.Test)169 KieServices (org.kie.api.KieServices)85 KieSession (org.kie.api.runtime.KieSession)71 KieBase (org.kie.api.KieBase)64 ReleaseId (org.kie.api.builder.ReleaseId)54 ArrayList (java.util.ArrayList)51 KieContainer (org.kie.api.runtime.KieContainer)50 KieBuilder (org.kie.api.builder.KieBuilder)42 KieFileSystem (org.kie.api.builder.KieFileSystem)42 StringReader (java.io.StringReader)39 KieModule (org.kie.api.builder.KieModule)39 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)31 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)31 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)28 ClassPathResource (org.drools.core.io.impl.ClassPathResource)28 FactType (org.kie.api.definition.type.FactType)27 KieModuleModel (org.kie.api.builder.model.KieModuleModel)21 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)20 IOException (java.io.IOException)18