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);
}
}
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);
}
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());
}
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);
}
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);
}
Aggregations