| 1 | |
package org.kuali.rice.krms.framework.engine; |
| 2 | |
|
| 3 | |
import java.util.List; |
| 4 | |
|
| 5 | |
import org.kuali.rice.krms.api.Action; |
| 6 | |
import org.kuali.rice.krms.api.ExecutionEnvironment; |
| 7 | |
import org.kuali.rice.krms.api.Proposition; |
| 8 | |
import org.kuali.rice.krms.api.ResultEvent; |
| 9 | |
import org.kuali.rice.krms.api.Rule; |
| 10 | |
import org.kuali.rice.krms.framework.engine.result.BasicResult; |
| 11 | |
|
| 12 | |
public class BasicRule implements Rule { |
| 13 | 0 | private static final ResultLogger LOG = ResultLogger.getInstance(); |
| 14 | |
|
| 15 | |
private String name; |
| 16 | |
private Proposition proposition; |
| 17 | |
private List<Action> actions; |
| 18 | |
|
| 19 | 0 | public BasicRule(String name, Proposition proposition, List<Action> actions) { |
| 20 | 0 | if (proposition == null) { |
| 21 | 0 | throw new IllegalArgumentException("Propsition cannot be null."); |
| 22 | |
} |
| 23 | 0 | this.name = name; |
| 24 | 0 | this.proposition = proposition; |
| 25 | 0 | this.actions = actions; |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
public BasicRule(Proposition proposition, List<Action> actions) { |
| 29 | 0 | this(null, proposition, actions); |
| 30 | 0 | } |
| 31 | |
|
| 32 | |
@Override |
| 33 | |
public boolean evaluate(ExecutionEnvironment environment) { |
| 34 | 0 | boolean result = proposition.evaluate(environment); |
| 35 | 0 | if (actions != null) { |
| 36 | 0 | for (Action action : actions) { |
| 37 | 0 | if (shouldExecuteAction(result)) { |
| 38 | 0 | action.execute(environment); |
| 39 | |
} |
| 40 | |
} |
| 41 | |
} |
| 42 | 0 | if (LOG.isEnabled(environment)){ |
| 43 | 0 | LOG.logResult(new BasicResult(ResultEvent.RuleEvaluated, this, environment, result)); |
| 44 | |
} |
| 45 | 0 | return result; |
| 46 | |
} |
| 47 | |
|
| 48 | |
protected boolean shouldExecuteAction(boolean ruleExecutionResult) { |
| 49 | 0 | return ruleExecutionResult; |
| 50 | |
} |
| 51 | |
|
| 52 | |
public String getName() { |
| 53 | 0 | return name; |
| 54 | |
} |
| 55 | |
|
| 56 | |
public String toString(){ |
| 57 | 0 | return name; |
| 58 | |
} |
| 59 | |
} |