001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.framework.engine; 017 018import java.util.List; 019 020import org.kuali.rice.krms.api.engine.ExecutionEnvironment; 021import org.kuali.rice.krms.api.engine.ResultEvent; 022import org.kuali.rice.krms.framework.engine.result.BasicResult; 023 024/** 025 * A {@link org.kuali.rice.krms.framework.engine.Rule} that executes a {@link org.kuali.rice.krms.framework.engine.Action} when the {@link Proposition} is true. 026 * 027 * @author Kuali Rice Team (rice.collab@kuali.org) 028 */ 029public class BasicRule implements Rule { 030 private static final ResultLogger LOG = ResultLogger.getInstance(); 031 032 private String name; 033 private Proposition proposition; 034 private List<Action> actions; 035 036 /** 037 * Constructor 038 * @param name Rule name to set the name to 039 * @param proposition {@link Proposition} to set the proposition to, cannot be null 040 * @param actions Rule Actions to set the actions to 041 * @throws IllegalArgumentException if the {@link Proposition} is null 042 */ 043 public BasicRule(String name, Proposition proposition, List<Action> actions) { 044 if (proposition == null) { 045 throw new IllegalArgumentException("Proposition cannot be null."); 046 } 047 this.name = name; 048 this.proposition = proposition; 049 this.actions = actions; 050 } 051 052 /** 053 * Constructor 054 * @param proposition {@link Proposition} to set the proposition to, cannot be null 055 * @param actions Rule Actions to set the actions to 056 * @throws IllegalArgumentException if the {@link Proposition} is null 057 */ 058 public BasicRule(Proposition proposition, List<Action> actions) { 059 this(null, proposition, actions); 060 } 061 062 @Override 063 public boolean evaluate(ExecutionEnvironment environment) { 064 boolean result = proposition.evaluate(environment).getResult(); 065 if (actions != null) { 066 for (Action action : actions) { 067 if (shouldExecuteAction(result)) { 068 action.execute(environment); 069 } 070 } 071 } 072 if (LOG.isEnabled(environment)){ 073 LOG.logResult(new BasicResult(ResultEvent.RULE_EVALUATED, this, environment, result)); 074 } 075 return result; 076 } 077 078 /** 079 * Based on the ruleExecutionResult should the {@link Action} be executed? Default behavior is to return the given ruleExecutionResult. 080 * Over-writable by subclasses. 081 * @param ruleExecutionResult the result of the engines evaluation method. 082 * @return boolean should the action execute 083 */ 084 protected boolean shouldExecuteAction(boolean ruleExecutionResult) { 085 return ruleExecutionResult; 086 } 087 088 /** 089 * Return the Rule name 090 * @return name Rule name 091 */ 092 public String getName() { 093 return name; 094 } 095 096 @Override 097 public String toString(){ 098 return name; 099 } 100}