diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNDecisionResult.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNDecisionResult.java new file mode 100644 index 00000000000..c54ddb2c8fe --- /dev/null +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNDecisionResult.java @@ -0,0 +1,57 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.kie.dmn.core.api; + +import java.util.List; + +/** + * Stores the result of the evaluation of a decision + * + */ +public interface DMNDecisionResult { + + /** + * Returns the decision ID + * + * @return the decision ID + */ + String getDecisionId(); + + /** + * Returns the decision name + * + * @return the decision name + */ + String getDecisionName(); + + /** + * Returns the result of the evaluation + * of the decision + * + * @return the result of the decision + */ + Object getResult(); + + /** + * Returns a list of DMN messages generated + * during the evaluation of this decision. + * + * @return a list of messages, or an empty list if + * no message was generated + */ + List getMessages(); +} diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNMessage.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNMessage.java index bb4a8924774..09297a99ed4 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNMessage.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNMessage.java @@ -27,4 +27,6 @@ enum Severity { String getMessage(); Throwable getException(); + + String getSourceId(); } diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNResult.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNResult.java index 861462ff6e5..7c9ed67f234 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNResult.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/api/DMNResult.java @@ -36,25 +36,35 @@ public interface DMNResult { DMNContext getContext(); /** - * Returns a map containing all the results - * of the decisions executed, by name. + * Returns a list containing all the results + * of the decisions executed * - * @return A map where the name of the decision - * is the key, and the result of the - * decision is the value of the map entry + * @return A list with the result of the + * decisions */ - Map getDecisionResults(); + List getDecisionResults(); /** * Returns the result of a single decision. * * @param name the name of the decision * - * @return the resulting value of the decision + * @return the result of the decision * or null if the decision was not - * executed. + * evaluated. */ - Object getDecisionResult( String name ); + DMNDecisionResult getDecisionResultByName( String name ); + + /** + * Returns the result of a single decision. + * + * @param id the id of the decision + * + * @return the result of the decision + * or null if the decision was not + * evaluated. + */ + DMNDecisionResult getDecisionResultById( String id ); /** * Returns a list of all the messages produced diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNDecisionResultImpl.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNDecisionResultImpl.java new file mode 100644 index 00000000000..71016a4196c --- /dev/null +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNDecisionResultImpl.java @@ -0,0 +1,77 @@ +/* + * Copyright 2016 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.kie.dmn.core.impl; + +import org.kie.dmn.core.api.DMNMessage; + +import java.util.ArrayList; +import java.util.List; + +public class DMNDecisionResultImpl + implements org.kie.dmn.core.api.DMNDecisionResult { + private String decisionId; + private String decisionName; + private Object result; + private List messages; + + public DMNDecisionResultImpl(String decisionId, String decisionName) { + this( decisionId, decisionName, null, new ArrayList<>( ) ); + } + + public DMNDecisionResultImpl(String decisionId, String decisionName, Object result, List messages) { + this.decisionId = decisionId; + this.decisionName = decisionName; + this.result = result; + this.messages = messages; + } + + @Override + public String getDecisionId() { + return decisionId; + } + + public void setDecisionId(String decisionId) { + this.decisionId = decisionId; + } + + @Override + public String getDecisionName() { + return decisionName; + } + + public void setDecisionName(String decisionName) { + this.decisionName = decisionName; + } + + @Override + public Object getResult() { + return result; + } + + public void setResult(Object result) { + this.result = result; + } + + @Override + public List getMessages() { + return messages; + } + + public void setMessages(List messages) { + this.messages = messages; + } +} diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNMessageImpl.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNMessageImpl.java index c20f83c32a8..1cc88ee63af 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNMessageImpl.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNMessageImpl.java @@ -21,20 +21,20 @@ public class DMNMessageImpl implements DMNMessage { private Severity severity; private String message; + private String sourceId; private Throwable exception; public DMNMessageImpl() { } - public DMNMessageImpl(Severity severity, String message) { - this.severity = severity; - this.message = message; - this.exception = null; + public DMNMessageImpl(Severity severity, String message, String sourceId) { + this( severity, message, sourceId, null); } - public DMNMessageImpl(Severity severity, String message, Throwable exception) { + public DMNMessageImpl(Severity severity, String message, String sourceId, Throwable exception) { this.severity = severity; this.message = message; + this.sourceId = sourceId; this.exception = exception; } @@ -48,8 +48,23 @@ public String getMessage() { return message; } + @Override + public String getSourceId() { + return sourceId; + } + @Override public Throwable getException() { return exception; } + + @Override + public String toString() { + return "DMNMessage{" + + " severity=" + severity + + ", message='" + message + '\'' + + ", sourceId='" + sourceId + '\'' + + ", exception='" + exception.getClass().getName() + " : " + exception.getMessage() + + "' }"; + } } diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNResultImpl.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNResultImpl.java index 1561e9eeb76..2272331421e 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNResultImpl.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNResultImpl.java @@ -17,6 +17,7 @@ package org.kie.dmn.core.impl; import org.kie.dmn.core.api.DMNContext; +import org.kie.dmn.core.api.DMNDecisionResult; import org.kie.dmn.core.api.DMNMessage; import org.kie.dmn.core.api.DMNResult; @@ -26,7 +27,7 @@ public class DMNResultImpl implements DMNResult { private DMNContext context; private List messages; - private Map decisionResults; + private Map decisionResults; public DMNResultImpl() { messages = new ArrayList<>( ); @@ -62,24 +63,30 @@ public void addMessage( DMNMessage msg ) { this.messages.add( msg ); } - public void addMessage( DMNMessage.Severity severity, String message ) { - this.messages.add( new DMNMessageImpl( severity, message ) ); + public DMNMessage addMessage( DMNMessage.Severity severity, String message, String sourceId ) { + DMNMessageImpl msg = new DMNMessageImpl( severity, message, sourceId ); + this.messages.add( msg ); + return msg; + } + + public void addMessage( DMNMessage.Severity severity, String message, String sourceId, Throwable exception ) { + this.messages.add( new DMNMessageImpl( severity, message, sourceId, exception ) ); } - public void addMessage( DMNMessage.Severity severity, String message, Throwable exception ) { - this.messages.add( new DMNMessageImpl( severity, message, exception ) ); + public List getDecisionResults() { + return new ArrayList<>( decisionResults.values() ); } - public Map getDecisionResults() { - return decisionResults; + public DMNDecisionResult getDecisionResultByName( String name ) { + return decisionResults.values().stream().filter( dr -> dr.getDecisionName().equals( name ) ).findFirst().get(); } - public Object getDecisionResult( String name ) { - return decisionResults.get( name ); + public DMNDecisionResult getDecisionResultById( String id ) { + return decisionResults.get( id ); } - public void setDecisionResult( String name, Object value ) { - this.decisionResults.put( name, value ); + public void setDecisionResult( String id, DMNDecisionResult result ) { + this.decisionResults.put( id, result ); } @Override @@ -89,4 +96,5 @@ public String toString() { ", messages=" + messages + '}'; } + } diff --git a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java index 92ddd7ed104..e059e9a2903 100644 --- a/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java +++ b/kie-dmn/kie-dmn-core/src/main/java/org/kie/dmn/core/impl/DMNRuntimeImpl.java @@ -63,7 +63,7 @@ public DMNResult evaluateDecisionByName(DMNModel model, String decisionName, DMN if( decision != null ) { evaluateDecision( context, result, decision ); } else { - result.addMessage( DMNMessage.Severity.ERROR, "Decision not found for name '"+decisionName+"'" ); + result.addMessage( DMNMessage.Severity.ERROR, "Decision not found for name '"+decisionName+"'", null ); } return result; } @@ -75,7 +75,7 @@ public DMNResult evaluateDecisionById(DMNModel model, String decisionId, DMNCont if( decision != null ) { evaluateDecision( context, result, decision ); } else { - result.addMessage( DMNMessage.Severity.ERROR, "Decision not found for id '"+decisionId+"'" ); + result.addMessage( DMNMessage.Severity.ERROR, "Decision not found for id '"+decisionId+"'", decisionId ); } return result; } @@ -88,13 +88,18 @@ private DMNResultImpl createResult(DMNContext context) { private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode decision) { boolean missingInput = false; + DMNDecisionResultImpl dr = new DMNDecisionResultImpl( decision.getId(), decision.getName() ); + result.setDecisionResult( decision.getId(), dr ); for( DMNNode dep : decision.getDependencies().values() ) { if( ! context.isDefined( dep.getName() ) ) { if( dep instanceof DecisionNode ) { evaluateDecision( context, result, (DecisionNode) dep ); } else { missingInput = true; - result.addMessage( DMNMessage.Severity.ERROR, "Missing input for decision '"+decision.getName()+"': input name='" + dep.getName() + "' input id='" + dep.getId() + "'" ); + DMNMessage msg = result.addMessage( DMNMessage.Severity.ERROR, + "Missing input for decision '"+decision.getName()+"': input name='" + dep.getName() + "' input id='" + dep.getId() + "'", + decision.getId() ); + dr.getMessages().add( msg ); } } } @@ -103,9 +108,8 @@ private boolean evaluateDecision(DMNContext context, DMNResultImpl result, Decis } Object val = decision.getEvaluator().evaluate( result ); result.getContext().set( decision.getDecision().getVariable().getName(), val ); - result.setDecisionResult( decision.getName(), val ); + dr.setResult( val ); return true; } - } diff --git a/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNRuntimeTest.java b/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNRuntimeTest.java index 20c915f6956..40afa1c4962 100644 --- a/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNRuntimeTest.java +++ b/kie-dmn/kie-dmn-core/src/test/java/org/kie/dmn/core/DMNRuntimeTest.java @@ -61,7 +61,7 @@ public void testSimpleEvaluateAll() { DMNResult dmnResult = runtime.evaluateAll( dmnModel, context ); assertThat( dmnResult.getDecisionResults().size(), is(1) ); - assertThat( dmnResult.getDecisionResult( "Greeting Message" ), is( "Hello John Doe" ) ); + assertThat( dmnResult.getDecisionResultByName( "Greeting Message" ).getResult(), is( "Hello John Doe" ) ); DMNContext result = dmnResult.getContext(); @@ -80,7 +80,7 @@ public void testSimpleEvaluateDecisionByName() { DMNResult dmnResult = runtime.evaluateDecisionByName( dmnModel, "Greeting Message", context ); assertThat( dmnResult.getDecisionResults().size(), is(1) ); - assertThat( dmnResult.getDecisionResult( "Greeting Message" ), is( "Hello John Doe" ) ); + assertThat( dmnResult.getDecisionResultByName( "Greeting Message" ).getResult(), is( "Hello John Doe" ) ); DMNContext result = dmnResult.getContext(); @@ -99,7 +99,7 @@ public void testSimpleEvaluateDecisionById() { DMNResult dmnResult = runtime.evaluateDecisionById( dmnModel, "d_GreetingMessage", context ); assertThat( dmnResult.getDecisionResults().size(), is(1) ); - assertThat( dmnResult.getDecisionResult( "Greeting Message" ), is( "Hello John Doe" ) ); + assertThat( dmnResult.getDecisionResultById( "d_GreetingMessage" ).getResult(), is( "Hello John Doe" ) ); DMNContext result = dmnResult.getContext();