use of org.kie.dmn.core.pmml.PMMLModelInfo in project drools by kiegroup.
the class PMMLInfoTest method testPMMLInfo.
@Test
public void testPMMLInfo() throws Exception {
InputStream inputStream = PMMLInfoTest.class.getResourceAsStream("test_scorecard.pmml");
PMMLInfo<PMMLModelInfo> p0 = PMMLInfo.from(inputStream);
assertThat(p0.getModels(), hasSize(1));
assertThat(p0.getHeader().getPmmlNSURI(), is("http://www.dmg.org/PMML-4_2"));
PMMLModelInfo m0 = p0.getModels().iterator().next();
assertThat(m0.getName(), is("Sample Score"));
assertThat(m0.getInputFieldNames(), containsInAnyOrder(is("age"), is("occupation"), is("residenceState"), is("validLicense")));
assertThat(m0.getTargetFieldNames(), containsInAnyOrder(is("overallScore")));
assertThat(m0.getOutputFieldNames(), containsInAnyOrder(is("calculatedScore")));
}
use of org.kie.dmn.core.pmml.PMMLModelInfo in project kie-wb-common by kiegroup.
the class PMMLIncludedDocumentFactoryTest method testGetDocumentByPathWithKnownPathWithIncludedModel.
@Test
public void testGetDocumentByPathWithKnownPathWithIncludedModel() {
final Path path = mock(Path.class);
final PMMLInfo<PMMLModelInfo> pmmlInfo = makePMMLInfo();
final PMMLIncludedModel includedModel = makePMMLIncludedModel();
when(path.toURI()).thenReturn(URI);
doReturn(pmmlInfo).when(factory).loadPMMLInfo(path);
final PMMLDocumentMetadata document = factory.getDocumentByPath(path, includedModel);
assertThat(document).isNotNull();
assertThat(document.getPath()).isEqualTo(URI);
assertThat(document.getImportType()).isEqualTo(NAMESPACE);
assertThat(document.getName()).isEqualTo(DOCUMENT_NAME);
assertThat(document.getModels()).hasSize(1);
final PMMLModelMetadata model = document.getModels().get(0);
assertThat(model.getName()).isEqualTo(MODEL_NAME);
assertThat(model.getInputParameters()).hasSize(INPUT_FIELDS_COUNT);
assertThat(model.getInputParameters()).usingElementComparator(comparing(PMMLParameterMetadata::getName, naturalOrder())).containsExactlyInAnyOrder(expectedPMMLParameterMetadata());
}
use of org.kie.dmn.core.pmml.PMMLModelInfo in project kie-wb-common by kiegroup.
the class PMMLIncludedDocumentFactoryTest method testGetDocumentByPathWithKnownPath.
@Test
public void testGetDocumentByPathWithKnownPath() {
final Path path = mock(Path.class);
final PMMLInfo<PMMLModelInfo> pmmlInfo = makePMMLInfo();
when(path.toURI()).thenReturn(URI);
doReturn(pmmlInfo).when(factory).loadPMMLInfo(path);
final PMMLDocumentMetadata document = factory.getDocumentByPath(path);
assertThat(document).isNotNull();
assertThat(document.getPath()).isEqualTo(URI);
assertThat(document.getImportType()).isEqualTo(NAMESPACE);
assertThat(document.getModels()).hasSize(1);
final PMMLModelMetadata model = document.getModels().get(0);
assertThat(model.getName()).isEqualTo(MODEL_NAME);
assertThat(model.getInputParameters()).hasSize(INPUT_FIELDS_COUNT);
assertThat(model.getInputParameters()).usingElementComparator(comparing(PMMLParameterMetadata::getName, naturalOrder())).containsExactlyInAnyOrder(expectedPMMLParameterMetadata());
}
use of org.kie.dmn.core.pmml.PMMLModelInfo in project drools by kiegroup.
the class DMNEvaluatorCompiler method compileFunctionDefinitionPMML.
private DMNExpressionEvaluator compileFunctionDefinitionPMML(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition funcDef) {
if (funcDef.getExpression() instanceof Context) {
Context context = (Context) funcDef.getExpression();
String pmmlDocument = null;
String pmmlModel = null;
for (ContextEntry ce : context.getContextEntry()) {
if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() instanceof LiteralExpression) {
LiteralExpression ceLitExpr = (LiteralExpression) ce.getExpression();
if (ce.getVariable().getName().equals("document")) {
if (ceLitExpr.getText() != null) {
pmmlDocument = stripQuotes(ceLitExpr.getText().trim());
}
} else if (ce.getVariable().getName().equals("model")) {
if (ceLitExpr.getText() != null) {
pmmlModel = stripQuotes(ceLitExpr.getText().trim());
}
}
}
}
final String nameLookup = pmmlDocument;
Optional<Import> lookupImport = model.getDefinitions().getImport().stream().filter(x -> x.getName().equals(nameLookup)).findFirst();
if (lookupImport.isPresent()) {
Import theImport = lookupImport.get();
logger.trace("theImport: {}", theImport);
Resource pmmlResource = DMNCompilerImpl.resolveRelativeResource(getRootClassLoader(), model, theImport, funcDef, ctx.getRelativeResolver());
logger.trace("pmmlResource: {}", pmmlResource);
DMNImportPMMLInfo pmmlInfo = model.getPmmlImportInfo().get(pmmlDocument);
logger.trace("pmmlInfo: {}", pmmlInfo);
if (pmmlModel == null || pmmlModel.isEmpty()) {
List<String> pmmlModelNames = pmmlInfo.getModels().stream().map(PMMLModelInfo::getName).filter(x -> x != null).collect(Collectors.toList());
if (pmmlModelNames.size() > 0) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_MODEL_NAME, pmmlModelNames.stream().collect(Collectors.joining(",")));
}
}
AbstractPMMLInvocationEvaluator invoker = PMMLInvocationEvaluatorFactory.newInstance(model, getRootClassLoader(), funcDef, pmmlResource, pmmlModel, pmmlInfo);
DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node, funcDef);
for (InformationItem p : funcDef.getFormalParameter()) {
DMNCompilerHelper.checkVariableName(model, p, p.getName());
DMNType dmnType = compiler.resolveTypeRef(model, p, p, p.getTypeRef());
func.addParameter(p.getName(), dmnType);
invoker.addParameter(p.getName(), dmnType);
}
func.setEvaluator(invoker);
return func;
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_ENTRY, functionName, node.getIdentifierString());
}
} else {
// error, PMML function definitions require a context
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_BODY_NOT_CONTEXT, node.getIdentifierString());
}
return new DMNFunctionDefinitionEvaluator(node, funcDef);
}
Aggregations