Search in sources :

Example 1 with PMMLResource

use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.

private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
    List<PackageDescr> pkgDescrs = new ArrayList<>();
    DrlParser parser = new DrlParser(configuration.getLanguageLevel());
    for (PMMLResource res : resources) {
        for (String key : res.getRules().keySet()) {
            String src = res.getRules().get(key);
            PackageDescr descr = null;
            descr = parser.parse(false, src);
            if (descr != null) {
                descr.setResource(resource);
                pkgDescrs.add(descr);
                dumpGeneratedRule(descr, key, src);
            } else {
                addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
            }
        }
    }
    return pkgDescrs;
}
Also used : PMMLResource(org.drools.compiler.compiler.PMMLResource) ParserError(org.drools.compiler.compiler.ParserError) ArrayList(java.util.ArrayList) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr)

Example 2 with PMMLResource

use of org.drools.compiler.compiler.PMMLResource 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 3 with PMMLResource

use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.

the class PMML4Compiler method buildResourcesFromModel.

protected List<PMMLResource> buildResourcesFromModel(PMML pmml, Miningmodel miningModel, List<PMMLResource> resourcesList, ClassLoader classLoader, KieModuleModel module) {
    if (resourcesList == null) {
        resourcesList = new ArrayList<>();
    }
    // new PMMLResource(PMML_DROOLS+".mining.model_"+miningModel.getModelId());
    PMMLResource resource = new PMMLResource(miningModel.getModelPackageName());
    KieBaseModel rootKieBaseModel = module.newKieBaseModel(resource.getPackageName());
    rootKieBaseModel.addPackage(resource.getPackageName());
    rootKieBaseModel.setDefault(true);
    resource.setKieBaseModel(rootKieBaseModel);
    resource.addRules(miningModel.getModelId(), miningModel.generateRules());
    resourcesList.add(resource);
    getChildResources(pmml, miningModel, resourcesList, classLoader, module);
    return resourcesList;
}
Also used : PMMLResource(org.drools.compiler.compiler.PMMLResource) KieBaseModel(org.kie.api.builder.model.KieBaseModel)

Example 4 with PMMLResource

use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.

the class PMML4Compiler method precompile.

public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootKieBaseModel) {
    List<PMMLResource> resources = new ArrayList<>();
    KieServices services = KieServices.Factory.get();
    KieModuleModel module = services.newKieModuleModel();
    this.results = new ArrayList<KnowledgeBuilderResult>();
    PMML pmml = loadModel(PMML, stream);
    helper.setResolver(classLoader);
    PMML4Unit unit = new PMML4UnitImpl(pmml);
    if (unit.containsMiningModel()) {
        Miningmodel rootModel = unit.getRootMiningModel();
        resources = buildResourcesFromModel(pmml, rootModel, null, classLoader, module);
    } else {
        PMML4Model rootModel = unit.getRootModel();
        if (rootModel != null) {
            helper.setPack(rootModel.getModelPackageName());
            KieBaseModel kbm = module.newKieBaseModel(rootModel.getModelId());
            kbm.addPackage(helper.getPack()).setDefault(true).setEventProcessingMode(EventProcessingOption.CLOUD);
            PMMLResource resource = new PMMLResource(helper.getPack());
            resource.setKieBaseModel(kbm);
            resource.addRules(rootModel.getModelId(), this.compile(pmml, classLoader));
            resources.add(resource);
        }
    }
    return resources;
}
Also used : ArrayList(java.util.ArrayList) Miningmodel(org.kie.pmml.pmml_4_2.model.Miningmodel) KieServices(org.kie.api.KieServices) PMMLResource(org.drools.compiler.compiler.PMMLResource) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) PMML4UnitImpl(org.kie.pmml.pmml_4_2.model.PMML4UnitImpl) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 5 with PMMLResource

use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.

the class PMML4Compiler method buildResourceFromSegment.

protected PMMLResource buildResourceFromSegment(PMML pmml_origin, MiningSegment segment, ClassLoader classLoader, KieModuleModel module) {
    PMML pmml = new PMML();
    DataDictionary dd = pmml_origin.getDataDictionary();
    pmml.setDataDictionary(dd);
    pmml.setHeader(pmml_origin.getHeader());
    pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(segment.getModel().getRawModel());
    addMissingFieldDefinition(pmml, segment.getOwner(), segment);
    // PMML4Helper.pmmlDefaultPackageName()+".mining.segment_"+segment.getSegmentId());
    helper.setPack(segment.getModel().getModelPackageName());
    String rules = this.compile(pmml, classLoader);
    KieBaseModel kbModel = module.newKieBaseModel(segment.getOwner().getOwner().getModelId() + "_" + segment.getOwner().getSegmentationId() + "_SEGMENT_" + segment.getSegmentId());
    kbModel.addPackage(helper.getPack()).setDefault(false).setEventProcessingMode(EventProcessingOption.CLOUD);
    KieSessionModel ksm = kbModel.newKieSessionModel("SEGMENT_" + segment.getSegmentId());
    ksm.setDefault(true);
    PMMLResource resource = new PMMLResource(helper.getPack());
    resource.setKieBaseModel(kbModel);
    resource.addRules(segment.getModel().getModelId(), rules);
    return resource;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) PMMLResource(org.drools.compiler.compiler.PMMLResource) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) DataDictionary(org.kie.dmg.pmml.pmml_4_2.descr.DataDictionary) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Aggregations

PMMLResource (org.drools.compiler.compiler.PMMLResource)6 KieBaseModel (org.kie.api.builder.model.KieBaseModel)4 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 List (java.util.List)1 Map (java.util.Map)1 JAXBException (javax.xml.bind.JAXBException)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 PMMLCompiler (org.drools.compiler.compiler.PMMLCompiler)1 ParserError (org.drools.compiler.compiler.ParserError)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 KieServices (org.kie.api.KieServices)1 KieModuleModel (org.kie.api.builder.model.KieModuleModel)1 KieSessionModel (org.kie.api.builder.model.KieSessionModel)1