Search in sources :

Example 1 with PMMLCompiler

use of org.drools.compiler.compiler.PMMLCompiler 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 2 with PMMLCompiler

use of org.drools.compiler.compiler.PMMLCompiler 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)

Aggregations

PMMLCompiler (org.drools.compiler.compiler.PMMLCompiler)2 PMMLResource (org.drools.compiler.compiler.PMMLResource)2 Resource (org.kie.api.io.Resource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 DiskResourceReader (org.drools.compiler.commons.jci.readers.DiskResourceReader)1 ResourceReader (org.drools.compiler.commons.jci.readers.ResourceReader)1 JavaDialectConfiguration (org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 KieFileSystem (org.kie.api.builder.KieFileSystem)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 KiePackage (org.kie.api.definition.KiePackage)1 Rule (org.kie.api.definition.rule.Rule)1 ServiceRegistryImpl (org.kie.api.internal.utils.ServiceRegistryImpl)1