Search in sources :

Example 1 with KiePMMLDroolsModelWithSources

use of org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLModelWithSourcesCompiled.

@Override
public KiePMMLModelWithSources getKiePMMLModelWithSourcesCompiled(final CompilationDTO<T> compilationDTO) {
    logger.trace("getKiePMMLModelWithSourcesCompiled {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
    if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
        throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
    }
    try {
        HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
        KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
        final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
        KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
        final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
        Map<String, String> sourcesMap = getKiePMMLDroolsModelSourcesMap(droolsCompilationDTO);
        compilationDTO.compileAndLoadClass(sourcesMap);
        PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, compilationDTO.getPackageName());
        String pkgUUID = getPkgUUID(knowledgeBuilder.getReleaseId(), compilationDTO.getPackageName());
        packageDescr.setPreferredPkgUUID(pkgUUID);
        Map<String, String> rulesSourceMap = Collections.unmodifiableMap(getRulesSourceMap(packageDescr));
        KiePMMLDroolsModelWithSources toReturn = new KiePMMLDroolsModelWithSources(compilationDTO.getModelName(), compilationDTO.getPackageName(), compilationDTO.getKieMiningFields(), compilationDTO.getKieOutputFields(), compilationDTO.getKieTargetFields(), sourcesMap, pkgUUID, rulesSourceMap);
        // Needed to compile Rules from PackageDescr
        CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
        knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
        return toReturn;
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)

Example 2 with KiePMMLDroolsModelWithSources

use of org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLModelWithSources.

@Override
public KiePMMLDroolsModelWithSources getKiePMMLModelWithSources(final CompilationDTO<T> compilationDTO) {
    logger.trace("getKiePMMLModelWithSources {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
    if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
        throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
    }
    try {
        final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
        KiePMMLDroolsAST kiePMMLDroolsAST = getKiePMMLDroolsASTCommon(compilationDTO.getFields(), compilationDTO.getModel(), fieldTypeMap);
        final DroolsCompilationDTO<T> droolsCompilationDTO = DroolsCompilationDTO.fromCompilationDTO(compilationDTO, fieldTypeMap);
        Map<String, String> sourcesMap = getKiePMMLDroolsModelSourcesMap(droolsCompilationDTO);
        PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, compilationDTO.getPackageName());
        HasKnowledgeBuilder hasKnowledgeBuilder = (HasKnowledgeBuilder) compilationDTO.getHasClassloader();
        KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) hasKnowledgeBuilder.getKnowledgeBuilder();
        String pkgUUID = getPkgUUID(knowledgeBuilder.getReleaseId(), compilationDTO.getPackageName());
        packageDescr.setPreferredPkgUUID(pkgUUID);
        Map<String, String> rulesSourceMap = Collections.unmodifiableMap(getRulesSourceMap(packageDescr));
        KiePMMLDroolsModelWithSources toReturn = new KiePMMLDroolsModelWithSources(compilationDTO.getModelName(), compilationDTO.getPackageName(), compilationDTO.getKieMiningFields(), compilationDTO.getKieOutputFields(), compilationDTO.getKieTargetFields(), sourcesMap, pkgUUID, rulesSourceMap);
        knowledgeBuilder.registerPackage(packageDescr);
        return toReturn;
    } catch (Exception e) {
        throw new KiePMMLException(e);
    }
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) IOException(java.io.IOException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)

Example 3 with KiePMMLDroolsModelWithSources

use of org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources in project drools by kiegroup.

the class ScorecardModelImplementationProviderTest method getKiePMMLModelWithSources.

@Test
public void getKiePMMLModelWithSources() throws Exception {
    final PMML pmml = getPMML(SOURCE_1);
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, (Scorecard) pmml.getModels().get(0), new HasKnowledgeBuilderMock(knowledgeBuilder));
    final KiePMMLDroolsModelWithSources retrieved = PROVIDER.getKiePMMLModelWithSources(compilationDTO);
    assertNotNull(retrieved);
    commonVerifyIsDeepCloneable(retrieved);
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PMML(org.dmg.pmml.PMML) Scorecard(org.dmg.pmml.scorecard.Scorecard) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) Test(org.junit.Test)

Example 4 with KiePMMLDroolsModelWithSources

use of org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources in project drools by kiegroup.

the class DroolsModelProviderTest method getKiePMMLModelWithSourcesWithKnowledgeBuilder.

@Test
public void getKiePMMLModelWithSourcesWithKnowledgeBuilder() {
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<Scorecard> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, scorecard, new HasKnowledgeBuilderMock(knowledgeBuilder));
    KiePMMLDroolsModelWithSources retrieved = droolsModelProvider.getKiePMMLModelWithSources(compilationDTO);
    assertNotNull(retrieved);
    assertEquals(SOURCE_MAP, retrieved.getSourcesMap());
    String expectedPackageName = compilationDTO.getPackageName();
    assertEquals(expectedPackageName, retrieved.getKModulePackageName());
    assertEquals(scorecard.getModelName(), retrieved.getName());
    PackageDescr packageDescr = knowledgeBuilder.getPackageDescrs(expectedPackageName).get(0);
    commonVerifyPackageDescr(packageDescr, expectedPackageName);
    assertNotNull(retrieved);
    final String rootPath = expectedPackageName + ".";
    commonVerifyRulesSourcesMap(retrieved.getRulesSourcesMap(), packageDescr, rootPath);
}
Also used : HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Scorecard(org.dmg.pmml.scorecard.Scorecard) PackageDescr(org.drools.drl.ast.descr.PackageDescr) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) Test(org.junit.Test)

Example 5 with KiePMMLDroolsModelWithSources

use of org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources in project drools by kiegroup.

the class TreeModelImplementationProviderTest method getKiePMMLModelWithSources.

@Test
public void getKiePMMLModelWithSources() throws Exception {
    final PMML pmml = getPMML(SOURCE_1);
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    final CommonCompilationDTO<TreeModel> compilationDTO = CommonCompilationDTO.fromGeneratedPackageNameAndFields(PACKAGE_NAME, pmml, (TreeModel) pmml.getModels().get(0), new HasKnowledgeBuilderMock(knowledgeBuilder));
    final KiePMMLDroolsModelWithSources retrieved = PROVIDER.getKiePMMLModelWithSources(compilationDTO);
    assertNotNull(retrieved);
    commonVerifyIsDeepCloneable(retrieved);
}
Also used : TreeModel(org.dmg.pmml.tree.TreeModel) KiePMMLTreeModel(org.kie.pmml.models.drools.tree.model.KiePMMLTreeModel) HasKnowledgeBuilderMock(org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PMML(org.dmg.pmml.PMML) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) Test(org.junit.Test)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 KiePMMLDroolsModelWithSources (org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)5 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)3 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 Test (org.junit.Test)3 HasKnowledgeBuilderMock (org.kie.pmml.models.drools.commons.implementations.HasKnowledgeBuilderMock)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 PMML (org.dmg.pmml.PMML)2 Scorecard (org.dmg.pmml.scorecard.Scorecard)2 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)2 HasKnowledgeBuilder (org.kie.pmml.kie.dependencies.HasKnowledgeBuilder)2 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)2 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)2 TreeModel (org.dmg.pmml.tree.TreeModel)1 KiePMMLTreeModel (org.kie.pmml.models.drools.tree.model.KiePMMLTreeModel)1