SCXML-tutorial

Contents Overview Examples Editor Forum

<assign>

Video version

The element is used to modify the data model.

Example:

<assign location="Var1" expr="5"/>

Attribute Details

NameRequiredAttribute ConstraintsTypeDefault ValueValid ValuesDescription
locationtruepath expressionnoneAny valid location expression.The location in the data model into which to insert the new value. See 5.9.2 Location Expressions for details.
exprfalseThis attribute must not occur in an <assign> element that has children.value expressionnoneAny valid value expression An expression returning the value to be assigned. See 5.9.3 Legal Data Values and Value Expressions for details.

Children

The children of the <assign> element provide an in-line specification of the legal data value (see 5.9.3 Legal Data Values and Value Expressions) to be inserted into the data model at the specified location.

Examples:

1. Assigning data by value from 'expr' attribute.

assign - location expr

<scxml datamodel="lua" name="Scxml" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<datamodel>
		<data expr="0" id="Var1"/>
	</datamodel>
	<state id="Shape1">
		<onentry>
			<log expr="Var1" label="Var1"/>
			<assign expr="5" location="Var1"/>
			<log expr="Var1" label="Var1"/>
			<assign expr="Var1 + 10" location="Var1"/>
			<log expr="Var1" label="Var1"/>
			<assign expr="Var1 * 10" location="Var1"/>
			<log expr="Var1" label="Var1"/>
		</onentry>
		<transition target="End"/>
	</state>
	<final id="End"/>
</scxml>

Output:

[Log] Var1: 0

[Log] Var1: 5

[Log] Var1: 15

[Log] Var1: 150

2. Multi-level location and assigning data by children element value.

assign - location to table

<scxml datamodel="lua" name="Scxml" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<datamodel>
		<data expr="{ Name=&quot;default&quot; }" id="VarTable"/>
	</datamodel>
	<state id="Shape1">
		<onentry>
			<log expr="VarTable.Name" label="VarTable.Name"/>
			<assign location="VarTable.Name">&quot;new name&quot;</assign>
			<log expr="VarTable.Name" label="VarTable.Name"/>
		</onentry>
		<transition target="End"/>
	</state>
	<final id="End"/>
</scxml>

Output:

[Log] VarTable.Name: "default"

[Log] VarTable.Name: "new name"

2.1 Assign data to EcmaScript array element

Foreach_Example

Source code

<scxml datamodel="ecmascript" name="ScxmlForeach" version="1.0" xmlns="http://www.w3.org/2005/07/scxml">
	<datamodel>
		<data expr="[ 0, 0, 0 ]" id="t_INPUTS"/>
	</datamodel>
	<parallel id="p">
		<state id="state_3">
			<state id="state_3_off">
				<transition cond="_event.data==1" event="event.2" target="state_3_on"/>
			</state>
			<state id="state_3_on">
				<transition cond="! (_event.data==1)" event="event.2" target="state_3_off"/>
			</state>
		</state>
		<state id="state_2">
			<state id="state_2_off">
				<transition cond="_event.data==1" event="event.1" target="state_2_on"/>
			</state>
			<state id="state_2_on">
				<transition cond="! (_event.data==1)" event="event.1" target="state_2_off"/>
			</state>
		</state>
		<state id="state_1">
			<state id="state_1_off">
				<transition cond="_event.data==1" event="event.0" target="state_1_on"/>
			</state>
			<state id="state_1_on">
				<transition cond="! (_event.data==1)" event="event.0" target="state_1_off"/>
			</state>
		</state>
		<state id="inputs">
			<state id="configuration">
				<onentry>
					<foreach array="t_INPUTS" index="varIndex" item="varItem">
						<send eventexpr="'event.' + varIndex">
							<content expr="varItem"/>
						</send>
					</foreach>
				</onentry>
				<transition event="change.inputs" target="configuration">
					<assign expr="_event.data.x" location="t_INPUTS[0]"/>
					<assign expr="_event.data.y" location="t_INPUTS[1]"/>
					<assign expr="_event.data.z" location="t_INPUTS[2]"/>
				</transition>
			</state>
		</state>
	</parallel>
</scxml>

W3C IRP tests

1. Test 286

If the location expression of an assign does not denote a valid location in the datamodel the processor MUST place the error error.execution in the internal event queue.

test286

2. Test 287

If the location expression of an assign denotes a valid location in the datamodel and if the value specified by 'expr' is a legal value for the location specified, the processor MUST place the specified value at the specified location.

test287

3. Test 487

If the value specified (by 'expr' or children) is not a legal value for the location specified, the processor MUST place the error error.execution in the internal event queue.

test487

TOP Contents Overview Examples Editor Forum