Search in sources :

Example 6 with KiePMMLDroolsAST

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsAST 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 7 with KiePMMLDroolsAST

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsAST in project drools by kiegroup.

the class KiePMMLScorecardModelASTFactory method getKiePMMLDroolsAST.

/**
 * Returns the <code>KiePMMLDroolsAST</code> built out of the given parameters.
 * It also <b>populate</b> the <b>fieldNameTypeNameMap</b> with mapping between
 * original field' name and <b>original type/generated type</b> tupla
 *
 * @param fields
 * @param model
 * @param fieldTypeMap
 * @param types
 * @return
 */
public static KiePMMLDroolsAST getKiePMMLDroolsAST(final List<Field<?>> fields, final Scorecard model, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap, final List<KiePMMLDroolsType> types) {
    logger.trace("getKiePMMLDroolsAST {} {} {}", fields, model, fieldTypeMap);
    DATA_TYPE targetType = getTargetFieldType(fields, model);
    List<OutputField> outputFields = model.getOutput() != null ? model.getOutput().getOutputFields() : Collections.emptyList();
    KiePMMLScorecardModelCharacteristicASTFactory factory = KiePMMLScorecardModelCharacteristicASTFactory.factory(fieldTypeMap, outputFields, targetType);
    if (model.isUseReasonCodes()) {
        factory = factory.withReasonCodes(model.getBaselineScore(), REASONCODE_ALGORITHM.byName(model.getReasonCodeAlgorithm().value()));
    }
    final List<KiePMMLDroolsRule> rules = factory.declareRulesFromCharacteristics(model.getCharacteristics(), "", model.getInitialScore());
    return new KiePMMLDroolsAST(types, rules);
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) OutputField(org.dmg.pmml.OutputField) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE)

Example 8 with KiePMMLDroolsAST

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsAST in project drools by kiegroup.

the class KiePMMLScorecardModelASTFactoryTest method getKiePMMLDroolsSampleAST.

@Test
public void getKiePMMLDroolsSampleAST() {
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(samplePmml.getDataDictionary(), samplePmml.getTransformationDictionary(), scorecardModel.getLocalTransformations());
    List<KiePMMLDroolsType> types = Collections.emptyList();
    KiePMMLDroolsAST retrieved = KiePMMLScorecardModelASTFactory.getKiePMMLDroolsAST(getFieldsFromDataDictionary(samplePmml.getDataDictionary()), scorecardModel, fieldTypeMap, types);
    assertNotNull(retrieved);
    assertEquals(types, retrieved.getTypes());
    assertFalse(retrieved.getRules().isEmpty());
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 9 with KiePMMLDroolsAST

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsAST in project drools by kiegroup.

the class KiePMMLScorecardModelFactoryTest method getKiePMMLDroolsAST.

@Test
public void getKiePMMLDroolsAST() {
    final DataDictionary dataDictionary = pmml.getDataDictionary();
    final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = getFieldTypeMap(dataDictionary, pmml.getTransformationDictionary(), scorecardModel.getLocalTransformations());
    KiePMMLDroolsAST retrieved = KiePMMLScorecardModelFactory.getKiePMMLDroolsAST(getFieldsFromDataDictionary(dataDictionary), scorecardModel, fieldTypeMap, Collections.emptyList());
    assertNotNull(retrieved);
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) DataDictionary(org.dmg.pmml.DataDictionary) CommonTestingUtils.getFieldsFromDataDictionary(org.kie.pmml.compiler.api.CommonTestingUtils.getFieldsFromDataDictionary) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Test(org.junit.Test)

Example 10 with KiePMMLDroolsAST

use of org.kie.pmml.models.drools.ast.KiePMMLDroolsAST in project drools by kiegroup.

the class DroolsModelProvider method getKiePMMLDroolsASTCommon.

/**
 * @param fields Should contain all fields retrieved from model, i.e. DataFields from DataDictionary,
 * DerivedFields from Transformations/LocalTransformations, OutputFields
 * @param model
 * @param fieldTypeMap
 * @return
 */
protected KiePMMLDroolsAST getKiePMMLDroolsASTCommon(final List<Field<?>> fields, final T model, final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap) {
    List<KiePMMLDroolsType> types = fieldTypeMap.values().stream().map(kiePMMLOriginalTypeGeneratedType -> {
        String type = DATA_TYPE.byName(kiePMMLOriginalTypeGeneratedType.getOriginalType()).getMappedClass().getSimpleName();
        return new KiePMMLDroolsType(kiePMMLOriginalTypeGeneratedType.getGeneratedType(), type);
    }).collect(Collectors.toList());
    types.addAll(KiePMMLDataDictionaryASTFactory.factory(fieldTypeMap).declareTypes(fields));
    return getKiePMMLDroolsAST(fields, model, fieldTypeMap, types);
}
Also used : KiePMMLDroolsModel(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModel) DescrResource(org.drools.core.io.impl.DescrResource) EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE(org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE) KiePMMLOriginalTypeGeneratedType(org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType) Model(org.dmg.pmml.Model) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) DroolsCompilationDTO(org.kie.pmml.models.drools.dto.DroolsCompilationDTO) KiePMMLDescrFactory.getBaseDescr(org.kie.pmml.models.drools.commons.factories.KiePMMLDescrFactory.getBaseDescr) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) LoggerFactory(org.slf4j.LoggerFactory) StringUtils.getPkgUUID(org.drools.core.util.StringUtils.getPkgUUID) HashMap(java.util.HashMap) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ModelImplementationProvider(org.kie.pmml.compiler.api.provider.ModelImplementationProvider) ResourceType(org.kie.api.io.ResourceType) ArrayList(java.util.ArrayList) Field(org.dmg.pmml.Field) Map(java.util.Map) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KiePMMLModelWithSources(org.kie.pmml.commons.model.KiePMMLModelWithSources) ModelBuilderImpl(org.drools.modelcompiler.builder.ModelBuilderImpl) CompilationDTO(org.kie.pmml.compiler.api.dto.CompilationDTO) PackageSources(org.drools.modelcompiler.builder.PackageSources) Logger(org.slf4j.Logger) PackageDescr(org.drools.drl.ast.descr.PackageDescr) IOException(java.io.IOException) HasKnowledgeBuilder(org.kie.pmml.kie.dependencies.HasKnowledgeBuilder) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) GeneratedFile(org.drools.modelcompiler.builder.GeneratedFile) KiePMMLDataDictionaryASTFactory(org.kie.pmml.models.drools.ast.factories.KiePMMLDataDictionaryASTFactory) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReleaseIdImpl(org.kie.util.maven.support.ReleaseIdImpl) DroolsError(org.drools.drl.parser.DroolsError) KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLDroolsModelWithSources(org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources) CompositePackageDescr(org.drools.compiler.lang.descr.CompositePackageDescr) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType)

Aggregations

KiePMMLDroolsAST (org.kie.pmml.models.drools.ast.KiePMMLDroolsAST)15 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)11 Test (org.junit.Test)9 PackageDescr (org.drools.drl.ast.descr.PackageDescr)6 KiePMMLDroolsType (org.kie.pmml.models.drools.ast.KiePMMLDroolsType)6 HashMap (java.util.HashMap)5 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)4 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)4 HasKnowledgeBuilder (org.kie.pmml.kie.dependencies.HasKnowledgeBuilder)4 IOException (java.io.IOException)3 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)3 KiePMMLDroolsModelWithSources (org.kie.pmml.models.drools.commons.model.KiePMMLDroolsModelWithSources)3 ArrayList (java.util.ArrayList)2 DataDictionary (org.dmg.pmml.DataDictionary)2 DataField (org.dmg.pmml.DataField)2 Field (org.dmg.pmml.Field)2 OutputField (org.dmg.pmml.OutputField)2 EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE (org.kie.pmml.commons.Constants.EXPECTING_HAS_KNOWLEDGEBUILDER_TEMPLATE)2