Skip to content

Commit

Permalink
Refactoring DMNResult to support retrieving decision results by ID an…
Browse files Browse the repository at this point in the history
…d segregating error messages.
  • Loading branch information
etirelli committed Nov 2, 2016
1 parent 8aa79c8 commit ccb608b
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 33 deletions.
@@ -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<DMNMessage> getMessages();
}
Expand Up @@ -27,4 +27,6 @@ enum Severity {
String getMessage(); String getMessage();


Throwable getException(); Throwable getException();

String getSourceId();
} }
Expand Up @@ -36,25 +36,35 @@ public interface DMNResult {
DMNContext getContext(); DMNContext getContext();


/** /**
* Returns a map containing all the results * Returns a list containing all the results
* of the decisions executed, by name. * of the decisions executed
* *
* @return A map where the name of the decision * @return A list with the result of the
* is the key, and the result of the * decisions
* decision is the value of the map entry
*/ */
Map<String, Object> getDecisionResults(); List<DMNDecisionResult> getDecisionResults();


/** /**
* Returns the result of a single decision. * Returns the result of a single decision.
* *
* @param name the name of the 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 * 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 * Returns a list of all the messages produced
Expand Down
@@ -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<DMNMessage> messages;

public DMNDecisionResultImpl(String decisionId, String decisionName) {
this( decisionId, decisionName, null, new ArrayList<>( ) );
}

public DMNDecisionResultImpl(String decisionId, String decisionName, Object result, List<DMNMessage> 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<DMNMessage> getMessages() {
return messages;
}

public void setMessages(List<DMNMessage> messages) {
this.messages = messages;
}
}
Expand Up @@ -21,20 +21,20 @@
public class DMNMessageImpl implements DMNMessage { public class DMNMessageImpl implements DMNMessage {
private Severity severity; private Severity severity;
private String message; private String message;
private String sourceId;
private Throwable exception; private Throwable exception;


public DMNMessageImpl() { public DMNMessageImpl() {
} }


public DMNMessageImpl(Severity severity, String message) { public DMNMessageImpl(Severity severity, String message, String sourceId) {
this.severity = severity; this( severity, message, sourceId, null);
this.message = message;
this.exception = null;
} }


public DMNMessageImpl(Severity severity, String message, Throwable exception) { public DMNMessageImpl(Severity severity, String message, String sourceId, Throwable exception) {
this.severity = severity; this.severity = severity;
this.message = message; this.message = message;
this.sourceId = sourceId;
this.exception = exception; this.exception = exception;
} }


Expand All @@ -48,8 +48,23 @@ public String getMessage() {
return message; return message;
} }


@Override
public String getSourceId() {
return sourceId;
}

@Override @Override
public Throwable getException() { public Throwable getException() {
return exception; return exception;
} }

@Override
public String toString() {
return "DMNMessage{" +
" severity=" + severity +
", message='" + message + '\'' +
", sourceId='" + sourceId + '\'' +
", exception='" + exception.getClass().getName() + " : " + exception.getMessage() +
"' }";
}
} }
Expand Up @@ -17,6 +17,7 @@
package org.kie.dmn.core.impl; package org.kie.dmn.core.impl;


import org.kie.dmn.core.api.DMNContext; 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.DMNMessage;
import org.kie.dmn.core.api.DMNResult; import org.kie.dmn.core.api.DMNResult;


Expand All @@ -26,7 +27,7 @@
public class DMNResultImpl implements DMNResult { public class DMNResultImpl implements DMNResult {
private DMNContext context; private DMNContext context;
private List<DMNMessage> messages; private List<DMNMessage> messages;
private Map<String, Object> decisionResults; private Map<String, DMNDecisionResult> decisionResults;


public DMNResultImpl() { public DMNResultImpl() {
messages = new ArrayList<>( ); messages = new ArrayList<>( );
Expand Down Expand Up @@ -62,24 +63,30 @@ public void addMessage( DMNMessage msg ) {
this.messages.add( msg ); this.messages.add( msg );
} }


public void addMessage( DMNMessage.Severity severity, String message ) { public DMNMessage addMessage( DMNMessage.Severity severity, String message, String sourceId ) {
this.messages.add( new DMNMessageImpl( severity, message ) ); 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 ) { public List<DMNDecisionResult> getDecisionResults() {
this.messages.add( new DMNMessageImpl( severity, message, exception ) ); return new ArrayList<>( decisionResults.values() );
} }


public Map<String, Object> getDecisionResults() { public DMNDecisionResult getDecisionResultByName( String name ) {
return decisionResults; return decisionResults.values().stream().filter( dr -> dr.getDecisionName().equals( name ) ).findFirst().get();
} }


public Object getDecisionResult( String name ) { public DMNDecisionResult getDecisionResultById( String id ) {
return decisionResults.get( name ); return decisionResults.get( id );
} }


public void setDecisionResult( String name, Object value ) { public void setDecisionResult( String id, DMNDecisionResult result ) {
this.decisionResults.put( name, value ); this.decisionResults.put( id, result );
} }


@Override @Override
Expand All @@ -89,4 +96,5 @@ public String toString() {
", messages=" + messages + ", messages=" + messages +
'}'; '}';
} }

} }
Expand Up @@ -63,7 +63,7 @@ public DMNResult evaluateDecisionByName(DMNModel model, String decisionName, DMN
if( decision != null ) { if( decision != null ) {
evaluateDecision( context, result, decision ); evaluateDecision( context, result, decision );
} else { } 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; return result;
} }
Expand All @@ -75,7 +75,7 @@ public DMNResult evaluateDecisionById(DMNModel model, String decisionId, DMNCont
if( decision != null ) { if( decision != null ) {
evaluateDecision( context, result, decision ); evaluateDecision( context, result, decision );
} else { } 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; return result;
} }
Expand All @@ -88,13 +88,18 @@ private DMNResultImpl createResult(DMNContext context) {


private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode decision) { private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode decision) {
boolean missingInput = false; boolean missingInput = false;
DMNDecisionResultImpl dr = new DMNDecisionResultImpl( decision.getId(), decision.getName() );
result.setDecisionResult( decision.getId(), dr );
for( DMNNode dep : decision.getDependencies().values() ) { for( DMNNode dep : decision.getDependencies().values() ) {
if( ! context.isDefined( dep.getName() ) ) { if( ! context.isDefined( dep.getName() ) ) {
if( dep instanceof DecisionNode ) { if( dep instanceof DecisionNode ) {
evaluateDecision( context, result, (DecisionNode) dep ); evaluateDecision( context, result, (DecisionNode) dep );
} else { } else {
missingInput = true; 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 );
} }
} }
} }
Expand All @@ -103,9 +108,8 @@ private boolean evaluateDecision(DMNContext context, DMNResultImpl result, Decis
} }
Object val = decision.getEvaluator().evaluate( result ); Object val = decision.getEvaluator().evaluate( result );
result.getContext().set( decision.getDecision().getVariable().getName(), val ); result.getContext().set( decision.getDecision().getVariable().getName(), val );
result.setDecisionResult( decision.getName(), val ); dr.setResult( val );
return true; return true;
} }



} }
Expand Up @@ -61,7 +61,7 @@ public void testSimpleEvaluateAll() {
DMNResult dmnResult = runtime.evaluateAll( dmnModel, context ); DMNResult dmnResult = runtime.evaluateAll( dmnModel, context );


assertThat( dmnResult.getDecisionResults().size(), is(1) ); 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(); DMNContext result = dmnResult.getContext();


Expand All @@ -80,7 +80,7 @@ public void testSimpleEvaluateDecisionByName() {
DMNResult dmnResult = runtime.evaluateDecisionByName( dmnModel, "Greeting Message", context ); DMNResult dmnResult = runtime.evaluateDecisionByName( dmnModel, "Greeting Message", context );


assertThat( dmnResult.getDecisionResults().size(), is(1) ); 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(); DMNContext result = dmnResult.getContext();


Expand All @@ -99,7 +99,7 @@ public void testSimpleEvaluateDecisionById() {
DMNResult dmnResult = runtime.evaluateDecisionById( dmnModel, "d_GreetingMessage", context ); DMNResult dmnResult = runtime.evaluateDecisionById( dmnModel, "d_GreetingMessage", context );


assertThat( dmnResult.getDecisionResults().size(), is(1) ); 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(); DMNContext result = dmnResult.getContext();


Expand Down

0 comments on commit ccb608b

Please sign in to comment.