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 org.kuali.rice.krms.api.engine.ExecutionEnvironment;
019
020/**
021 * A {@link AgendaTreeEntry} which executes its ifTrue {@link AgendaTree} if the given {@link Rule} result is true or
022 * its ifFalse {@link AgendaTree} if the result is false.
023 *
024 * @author Kuali Rice Team (rice.collab@kuali.org)
025 */
026public final class BasicAgendaTreeEntry implements AgendaTreeEntry {
027        
028        private final Rule rule;
029        private final AgendaTree ifTrue;
030        private final AgendaTree ifFalse;
031
032    /**
033     * Create a BasicAgendaTreeEntry with the given {@link Rule}.
034     * @param rule {@link Rule} to create the BasicAgendaTreeEntry with.
035     * @throws IllegalArgumentException if the rule is null.
036     */
037        public BasicAgendaTreeEntry(Rule rule) {
038                this(rule, null, null);
039        }
040
041    /**
042     * Create a BasicAgendaTreeEntry with the given {@link Rule} and ifTrue, ifFalse {@link AgendaTree}s.
043     * @param rule {@link Rule} to create the BasicAgendaTreeEntry with.
044     * @param ifTrue executed if the given rule's result is true.
045     * @param ifTrue executed if the given rule's result is false.
046     * @throws IllegalArgumentException if the rule is null.
047     */
048        public BasicAgendaTreeEntry(Rule rule, AgendaTree ifTrue, AgendaTree ifFalse) {
049                if (rule == null) {
050                        throw new IllegalArgumentException("rule was null");
051                }
052                this.rule = rule;
053                this.ifTrue = ifTrue;
054                this.ifFalse = ifFalse;
055        }
056        
057        @Override
058        public void execute(ExecutionEnvironment environment) {
059                boolean result = rule.evaluate(environment);
060                if (result && ifTrue != null) {
061                        ifTrue.execute(environment);
062                }
063                if (!result && ifFalse != null) {
064                        ifFalse.execute(environment);
065                }
066        }
067        
068}