/*
*
* Copyright 2005 The Apache Software Foundation.
*
* 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.apache.commons.scxml.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.scxml.Context;
import org.apache.commons.scxml.ErrorReporter;
import org.apache.commons.scxml.Evaluator;
import org.apache.commons.scxml.EventDispatcher;
import org.apache.commons.scxml.SCInstance;
import org.apache.commons.scxml.SCXMLExpressionException;
/**
* The class in this SCXML object model that corresponds to the
* <if> SCXML element, which serves as a container for conditionally
* executed elements. <else> and <elseif> can optionally
* appear within an <if> as immediate children, and serve to partition
* the elements within an <if>.
*
*/
public class If extends Action {
/**
* An conditional expression which can be evaluated to true or false.
*/
private String cond;
/**
* The set of executable elements (those that inheriting from
* Action) that are contained in this <if> element.
*/
private List actions;
/**
* The boolean value that dictates whether the particular child action
* should be executed.
*/
private boolean execute;
/**
* Constructor.
*/
public If() {
super();
this.actions = new ArrayList();
this.execute = false;
}
/**
* Get the executable actions contained in this <if>.
*
* @return Returns the actions.
*/
public final List getActions() {
return actions;
}
/**
* Add an Action to the list of executable actions contained in
* this <if>.
*
* @param action The action to add.
*/
public final void addAction(final Action action) {
if (action != null) {
this.actions.add(action);
}
}
/**
* Get the conditional expression.
*
* @return Returns the cond.
*/
public final String getCond() {
return cond;
}
/**
* Set the conditional expression.
*
* @param cond The cond to set.
*/
public final void setCond(final String cond) {
this.cond = cond;
}
/**
* {@inheritDoc}
*/
public void execute(final EventDispatcher evtDispatcher,
final ErrorReporter errRep, final SCInstance scInstance,
final Log appLog, final Collection derivedEvents)
throws ModelException, SCXMLExpressionException {
State parentState = getParentState();
Context ctx = scInstance.getContext(parentState);
Evaluator eval = scInstance.getEvaluator();
execute = eval.evalCond(ctx, cond).booleanValue();
// The "if" statement is a "container"
for (Iterator ifiter = actions.iterator(); ifiter.hasNext();) {
Action aa = (Action) ifiter.next();
if (execute && !(aa instanceof ElseIf)
&& !(aa instanceof Else)) {
aa.execute(evtDispatcher, errRep, scInstance, appLog,
derivedEvents);
} else if (execute
&& (aa instanceof ElseIf || aa instanceof Else)) {
break;
} else if (aa instanceof Else) {
execute = true;
} else if (aa instanceof ElseIf) {
execute = eval.evalCond(ctx, ((ElseIf) aa).getCond())
.booleanValue();
}
}
}
}