use of org.kie.dmn.api.core.ast.DecisionNode in project tck by dmn-tck.
the class DroolsTCKTest method executeTest.
@Override
public TestResult executeTest(Description description, TestSuiteContext context, TestCases.TestCase testCase) {
DroolsContext ctx = (DroolsContext) context;
logger.info("Executing test '{} / {}'\n", description.getClassName(), description.getMethodName());
DMNContext dmnctx = ctx.runtime.newContext();
testCase.getInputNode().forEach(in -> {
InputDataNode input = ctx.dmnmodel.getInputByName(in.getName());
DecisionNode decision = ctx.dmnmodel.getDecisionByName(in.getName());
if (input != null) {
// normally data input from file, should be pointing at a InputData in the model
dmnctx.set(in.getName(), parseValue(in, input));
} else if (decision != null) {
// the test case offers a pre-evaluated Decision
dmnctx.set(in.getName(), parseValue(in, decision));
} else {
throw new RuntimeException("Unable to locate InputData node or a Decision node for name: " + in.getName());
}
});
DMNContext resultctx = dmnctx;
List<String> failures = new ArrayList<>();
for (TestCases.TestCase.ResultNode rn : testCase.getResultNode()) {
try {
String name = rn.getName();
DMNResult dmnResult = ctx.runtime.evaluateDecisionByName(ctx.dmnmodel, name, resultctx);
if (!dmnResult.getMessages().isEmpty()) {
logger.info("Messages: \n-----\n{}-----\n", dmnResult.getMessages().stream().map(m -> m.toString()).collect(Collectors.joining("\n")));
}
resultctx = dmnResult.getContext();
Object expected = parseValue(rn, ctx.dmnmodel.getDecisionByName(name));
Object actual = resultctx.get(name);
if (rn.isErrorResult()) {
if (actual != null) {
failures.add("FAILURE: '" + name + "' expected error but found='" + actual + "'");
}
} else {
if (dmnResult.hasErrors()) {
for (DMNMessage msg : dmnResult.getMessages(DMNMessage.Severity.ERROR)) {
failures.add(msg.toString());
}
}
if (!isEquals(expected, actual)) {
failures.add("FAILURE: '" + name + "' expected='" + expected + "' but found='" + actual + "'");
}
}
} catch (Throwable t) {
failures.add("FAILURE: unnexpected exception executing test case '" + description.getClassName() + " / " + description.getMethodName() + "': " + t.getClass().getName());
logger.error("FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t);
}
}
// DMNResult dmnResult = ctx.runtime.evaluateAll( ctx.dmnmodel, dmnctx );
logger.info("Result context: {}\n", resultctx);
// if( ! dmnResult.getMessages().isEmpty() ) {
// logger.info( "Messages: \n-----\n{}-----\n", dmnResult.getMessages().stream().map( m -> m.toString() ).collect( Collectors.joining( "\n" ) ) );
// }
//
// List<String> failures = new ArrayList<>();
// if( dmnResult.hasErrors() ) {
// for( DMNMessage msg : dmnResult.getMessages( DMNMessage.Severity.ERROR ) ) {
// failures.add( msg.toString() );
// }
// }
// testCase.getResultNode().forEach( rn -> {
// try {
// String name = rn.getName();
// Object expected = parseValue( rn, ctx.dmnmodel.getDecisionByName( name ) );
// Object actual = resultctx.get( name );
// if( ! isEquals( expected, actual ) ) {
// failures.add( "FAILURE: '"+name+"' expected='"+expected+"' but found='"+actual+"'" );
// }
// } catch ( Throwable t ) {
// failures.add( "FAILURE: unnexpected exception executing test case '"+description.getClassName()+" / " +description.getMethodName()+"': "+t.getClass().getName() );
// logger.error( "FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t );
// }
// } );
TestResult.Result r = !failures.isEmpty() ? TestResult.Result.ERROR : TestResult.Result.SUCCESS;
return new TestResult(r, failures.stream().collect(Collectors.joining("\n")));
}
use of org.kie.dmn.api.core.ast.DecisionNode in project kie-wb-common by kiegroup.
the class DMNMarshallerTest method test_wrong_context.
@Test
public void test_wrong_context() throws IOException {
// DROOLS-2217
// SPECIAL CASE: to represent a partially edited DMN file.
// consider a LiteralExpression with null text as missing expression altogether.
final DMNRuntime runtime = roundTripUnmarshalMarshalThenUnmarshalDMN(this.getClass().getResourceAsStream("/wrong_context.dmn"));
DMNModel dmnModel = runtime.getModels().get(0);
// the DMN file is schema valid but is not a valid-DMN (a context-entry value is a literal expression missing text, which is null)
assertTrue(dmnModel.hasErrors());
// identify the error message for context-entry "ciao":
DMNMessage m0 = dmnModel.getMessages(DMNMessage.Severity.ERROR).get(0);
assertTrue("expected a message identifying the problem on a context entry for 'ciao'", m0.getMessage().startsWith("No expression defined for name 'ciao'"));
DecisionNode d0 = dmnModel.getDecisionById("_653b3426-933a-4050-9568-ab2a66b43c36");
// the identified DMN Decision is composed of a DMN Context where the first context-entry value is a literal expression missing text (text is null).
org.kie.dmn.model.v1_1.Context d0c = (org.kie.dmn.model.v1_1.Context) d0.getDecision().getExpression();
org.kie.dmn.model.v1_1.Expression contextEntryValue = d0c.getContextEntry().get(0).getExpression();
assertTrue(contextEntryValue instanceof org.kie.dmn.model.v1_1.LiteralExpression);
assertEquals(null, ((org.kie.dmn.model.v1_1.LiteralExpression) contextEntryValue).getText());
// -- Stunner side.
DMNMarshaller m = new DMNMarshaller(new XMLEncoderDiagramMetadataMarshaller(), applicationFactoryManager);
Graph<?, ?> g = m.unmarshall(null, this.getClass().getResourceAsStream("/wrong_context.dmn"));
DiagramImpl diagram = new DiagramImpl("", null);
Node<?, ?> decisionNode = g.getNode("_653b3426-933a-4050-9568-ab2a66b43c36");
assertNodeContentDefinitionIs(decisionNode, Decision.class);
View<Decision> view = ((View<Decision>) decisionNode.getContent());
// the identified DMN Decision is composed of a DMN Context where the first context-entry has missing Expression.
Context expression = (Context) view.getDefinition().getExpression();
assertEquals("a literalexpression with null text is threated as a missing expression altogether.", null, expression.getContextEntry().get(0).getExpression());
}
use of org.kie.dmn.api.core.ast.DecisionNode in project drools by kiegroup.
the class DMNCompilerImpl method linkRequirements.
public void linkRequirements(DMNModelImpl model, DMNBaseNode node) {
for (InformationRequirement ir : node.getInformationRequirement()) {
if (ir.getRequiredInput() != null) {
String id = getId(ir.getRequiredInput());
InputDataNode input = model.getInputById(id);
if (input != null) {
node.addDependency(input.getName(), input);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredInput(), model, null, null, Msg.REQ_INPUT_NOT_FOUND_FOR_NODE, id, node.getName());
}
} else if (ir.getRequiredDecision() != null) {
String id = getId(ir.getRequiredDecision());
DecisionNode dn = model.getDecisionById(id);
if (dn != null) {
node.addDependency(dn.getName(), dn);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredDecision(), model, null, null, Msg.REQ_DECISION_NOT_FOUND_FOR_NODE, id, node.getName());
}
}
}
for (KnowledgeRequirement kr : node.getKnowledgeRequirement()) {
if (kr.getRequiredKnowledge() != null) {
String id = getId(kr.getRequiredKnowledge());
BusinessKnowledgeModelNode bkmn = model.getBusinessKnowledgeModelById(id);
if (bkmn != null) {
node.addDependency(bkmn.getName(), bkmn);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, kr.getRequiredKnowledge(), model, null, null, Msg.REQ_BKM_NOT_FOUND_FOR_NODE, id, node.getName());
}
}
}
}
use of org.kie.dmn.api.core.ast.DecisionNode in project drools by kiegroup.
the class DMNCompilerImpl method detectCycles.
private void detectCycles(DMNModelImpl model) {
/*
Boolean.TRUE = node is either safe or already reported for having a cyclic dependency
Boolean.FALSE = node is being checked at the moment
*/
final Map<DecisionNodeImpl, Boolean> registry = new HashMap<>();
for (DecisionNode decision : model.getDecisions()) {
final DecisionNodeImpl decisionNode = (DecisionNodeImpl) decision;
detectCycles(decisionNode, registry, model);
}
}
use of org.kie.dmn.api.core.ast.DecisionNode in project drools by kiegroup.
the class DMNEvaluatorCompiler method compileContext.
private DMNExpressionEvaluator compileContext(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String contextName, Context expression) {
Context ctxDef = expression;
DMNContextEvaluator ctxEval = new DMNContextEvaluator(node.getName(), ctxDef);
ctx.enterFrame();
try {
for (ContextEntry ce : ctxDef.getContextEntry()) {
if (ce.getVariable() != null) {
String entryName = ce.getVariable().getName();
DMNCompilerHelper.checkVariableName(model, node.getSource(), entryName);
DMNType entryType = compiler.resolveTypeRef(model, node, ce.getVariable(), ce.getVariable(), ce.getVariable().getTypeRef());
// add context entry to the list of available variables for the following entries
ctx.setVariable(entryName, entryType);
DMNExpressionEvaluator evaluator = compileExpression(ctx, model, node, entryName, ce.getExpression());
ctxEval.addEntry(entryName, entryType, evaluator, ce);
} else {
// if the variable is not defined, then it should be the last
// entry in the context and the result of this context evaluation is the
// result of this expression itself
// TODO: if it is not the last entry, raise error message
DMNType type = null;
if (ctxDef.getParent() instanceof ContextEntry && ((ContextEntry) ctxDef.getParent()).getVariable() != null) {
ContextEntry parentEntry = (ContextEntry) ctxDef.getParent();
type = compiler.resolveTypeRef(model, node, parentEntry.getVariable(), parentEntry.getVariable(), parentEntry.getVariable().getTypeRef());
} else if (node instanceof BusinessKnowledgeModelNode) {
type = ((BusinessKnowledgeModelNode) node).getResultType();
} else if (node instanceof DecisionNode) {
type = ((DecisionNode) node).getResultType();
}
ctxEval.addEntry(DMNContextEvaluator.RESULT_ENTRY, type, compileExpression(ctx, model, node, contextName, ce.getExpression()), ce);
}
}
} finally {
ctx.exitFrame();
}
return ctxEval;
}
Aggregations