Search in sources :

Example 1 with HasKnowledgeBuilder

use of org.kie.pmml.kie.dependencies.HasKnowledgeBuilder 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 HasKnowledgeBuilder

use of org.kie.pmml.kie.dependencies.HasKnowledgeBuilder in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLModel.

@Override
public E getKiePMMLModel(final CompilationDTO<T> compilationDTO) {
    logger.trace("getKiePMMLModel {} {} {}", compilationDTO.getPackageName(), compilationDTO.getFields(), compilationDTO.getModel());
    if (!(compilationDTO.getHasClassloader() instanceof HasKnowledgeBuilder)) {
        throw new KiePMMLException(String.format(EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE, compilationDTO.getHasClassloader().getClass().getName()));
    }
    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);
    E toReturn = getKiePMMLDroolsModel(droolsCompilationDTO);
    PackageDescr packageDescr = getPackageDescr(kiePMMLDroolsAST, toReturn.getKModulePackageName());
    // Needed to compile Rules from PackageDescr
    CompositePackageDescr compositePackageDescr = new CompositePackageDescr(null, packageDescr);
    knowledgeBuilder.buildPackages(Collections.singletonList(compositePackageDescr));
    return toReturn;
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) HashMap(java.util.HashMap) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) 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)

Example 3 with HasKnowledgeBuilder

use of org.kie.pmml.kie.dependencies.HasKnowledgeBuilder 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)

Aggregations

HashMap (java.util.HashMap)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)3 PackageDescr (org.drools.drl.ast.descr.PackageDescr)3 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 HasKnowledgeBuilder (org.kie.pmml.kie.dependencies.HasKnowledgeBuilder)3 KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)3 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)3 IOException (java.io.IOException)2 KiePMMLDroolsModelWithSources (org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)2 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)1 EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE (org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE)1