SCXML-tutorial

Contents Overview Examples Editor Forum

Video version

<if>

Container for conditionally executed elements.

Execution is defined by attribute 'cond', which is the boolean conditional expression.

<else>

Empty element that partitions the content of an <if>. It is equivalent to an <elseif> with a 'cond' that always evaluates to true.

<elseif>

Empty element that partitions the content of an <if>, and provides a condition that determines whether the partition is executed.

Here is an example:

<if cond="cond1">
          <!-- selected when "cond1" is true -->
      <elseif cond="cond2"/>
          <!-- selected when "cond1" is false and "cond2" is true -->
      <elseif cond="cond3"/>
          <!-- selected when "cond1" and "cond2" are false and "cond3" is true -->
      <else/>
          <!-- selected when "cond1", "cond2", and "cond3" are false -->
</if>

Example_If_Else

Source code

<scxml datamodel="ecmascript" name="Scxml_If_Else" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<datamodel>
		<data expr="0" id="VarMode"/>
	</datamodel>
	<state id="Mode">
		<transition cond="_event.data != VarMode" event="Change.Mode" target="Mode">
			<assign expr="_event.data" location="VarMode"/>
		</transition>
		<state id="Mode_Check">
			<onentry>
				<if cond="VarMode == 1">
					<raise event="Set.Mode.1"/>
					<elseif cond="VarMode == 2"/>
					<raise event="Set.Mode.2"/>
					<else/>
					<raise event="Set.Mode.0"/>
				</if>
			</onentry>
			<transition event="Set.Mode.0" target="Mode_0"/>
			<transition event="Set.Mode.1" target="Mode_1"/>
			<transition event="Set.Mode.2" target="Mode_2"/>
		</state>
		<state id="Mode_0"/>
		<state id="Mode_1"/>
		<state id="Mode_2"/>
	</state>
</scxml>

W3C IRP tests

1. Test 147

When the if element is executed, the SCXML processor MUST execute the first partition in document order that is defined by a tag whose 'cond' attribute evaluates to true, if there is one.

test147

<scxml datamodel="lua" initial="s0" name="Scxml" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<datamodel>
		<data expr="0" id="Var1"/>
	</datamodel>
	<state id="s0">
		<onentry>
			<if cond="false">
				<raise event="foo"/>
				<assign expr="Var1 + 1" location="Var1"/>
				<elseif cond="true"/>
				<raise event="bar"/>
				<assign expr="Var1 + 1" location="Var1"/>
				<else/>
				<raise event="baz"/>
				<assign expr="Var1 + 1" location="Var1"/>
			</if>
			<raise event="bat"/>
		</onentry>
		<transition cond="Var1==1" event="bar" target="pass"/>
		<transition event="*" target="fail"/>
	</state>
	<final id="pass">
		<onentry>
			<log expr="'pass'" label="Outcome"/>
		</onentry>
	</final>
	<final id="fail">
		<onentry>
			<log expr="'fail'" label="Outcome"/>
		</onentry>
	</final>
</scxml>

2. Test 148

When the if element is executed, if no 'cond'attribute evaluates to true, the SCXML Processor must execute the partition defined by the else tag, if there is one.

test148

3. Test 149

When it executes an if element, if no 'cond' attribute evaluates to true and there is no else element, the SCXML processor must not evaluate any executable content within the element.

test149

TOP Contents Overview Examples Editor Forum