| Contents | Overview | Examples | Editor | Forum | |—|—|—|—|—|
The element is used to create an instance of an external service (for example: nested state machine)
Suppose you have multiple blocks with the same state machine logic

```xml
You may put similar logic into external state machine, define params which should be passed from the main state machine. And then you may create multiple instances using <invoke> element. You just need to path individual parameters for each external state machine

```xml
```xml
A URI specifying the type of the external service. Valid values are http://www.w3.org/TR/scxml/ (Default), http://www.w3.org/TR/ccxml/, http://www.w3.org/TR/voicexml30/, http://www.w3.org/TR/voicexml21/ plus other platform-specific values. Must not occur with the ‘typeexpr’ attribute.
Any value expression that evaluates to a URI that would be a valid value for ‘type’. A dynamic alternative to ‘type’. If this attribute is present, the SCXML Processor must evaluate it when the parent
A URI to be passed to the external service. Must not occur with the ‘srcexpr’ attribute or the <content> element.
<state id="Parent">
<invoke src="Child.scxml" />
</state>
Any expression evaluating to a valid URI. A dynamic alternative to ‘src’. If this attribute is present, the SCXML Processor must evaluate it when the parent
<state id="Parent">
<invoke srcexpr="s_APP_PATH + 'Child.scxml'" />
</state>
A string literal to be used as the identifier for this instance of <invoke>. Must not occur with the ‘idlocation’ attribute.
<state id="Parent">
<invoke id="ID_MODULE_CHILD" src="Child.scxml" autoforward="false" />
<onentry>
<!-- invoked session with id 'ID_MODULE_CHILD' will receive event 'Timeout' after 2 seconds -->
<send delay="2s" event="Timeout" target="#_ID_MODULE_CHILD"/>
</onentry>
</state>
Any valid data model expression evaluating to a data model location. See 5.9.2 Location Expressions for details. Must not occur with the ‘id’ attribute.
A space-separated list of one or more data model locations to be passed as attribute/value pairs to the invoked service. (The name of the location is the attribute and the value stored at the location is the value.). Must not occur with the <param> element.
A boolean flag indicating whether to forward events to the invoked process. Default value is ‘false’.
<state id="Parent">
<invoke src="Child1.scxml" autoforward="true" />
<invoke src="Child2.scxml" autoforward="true" />
<invoke src="Child3.scxml" autoforward="true" />
<onentry>
<!-- all invoked sessions with 'autoforward=true' will receive event 'Timeout' after 2 seconds -->
<send delay="2s" event="Timeout" />
</onentry>
</state>
NOTE: Autoforwared events may cause circlular event loops. To prevent this you need to disable autoforward and send events by submachine ID
In the example below parent and child machine will generate the same events and we need to convince that they do not affect each other

```xml

```xml
Output:
[Log] CHILD COUNT: 0
[Log] PARENT COUNT: 0
[Log] CHILD COUNT: 1
[Log] PARENT COUNT: 1
[Log] CHILD COUNT: 2
[Log] PARENT COUNT: 2
[Log] CHILD COUNT: 3
[Log] PARENT COUNT: 3
...

The example shows how to communicate between multiple invoked submachines


```xml

```xml
Qt widget-based application that demonstrates how to invoke child state machines with QScxmlCppDataModel

| Number | Name and link | Source |
|---|---|---|
| 1 | Test 215 | https://www.w3.org/Voice/2013/scxml-irp/215/test215.txml |
| 2 | Test 216 | https://www.w3.org/Voice/2013/scxml-irp/216/test216.txml |
| 3 | Test 220 | https://www.w3.org/Voice/2013/scxml-irp/220/test220.txml |
| 4 | Test 223 | https://www.w3.org/Voice/2013/scxml-irp/223/test223.txml |
| 5 | Test 224 | https://www.w3.org/Voice/2013/scxml-irp/224/test224.txml |
| 6 | Test 225 | https://www.w3.org/Voice/2013/scxml-irp/225/test225.txml |
| 7 | Test 226 | https://www.w3.org/Voice/2013/scxml-irp/226/test226.txml |
| 8 | Test 228 | https://www.w3.org/Voice/2013/scxml-irp/228/test228.txml |
| 9 | Test 229 | https://www.w3.org/Voice/2013/scxml-irp/229/test229.txml |
| 10 | Test 230 | https://www.w3.org/Voice/2013/scxml-irp/230/test230.txml |
| 11 | Test 232 | https://www.w3.org/Voice/2013/scxml-irp/232/test232.txml |
| 12 | Test 233 | https://www.w3.org/Voice/2013/scxml-irp/233/test233.txml |
| 13 | Test 234 | https://www.w3.org/Voice/2013/scxml-irp/234/test234.txml |
| 14 | Test 235 | https://www.w3.org/Voice/2013/scxml-irp/235/test235.txml |
| 15 | Test 236 | https://www.w3.org/Voice/2013/scxml-irp/236/test236.txml |
| 16 | Test 237 | https://www.w3.org/Voice/2013/scxml-irp/237/test237.txml |
| 17 | Test 239 | https://www.w3.org/Voice/2013/scxml-irp/239/test239.txml |
| 18 | Test 240 | https://www.w3.org/Voice/2013/scxml-irp/240/test240.txml |
| 19 | Test 241 | https://www.w3.org/Voice/2013/scxml-irp/241/test241.txml |
| 20 | Test 242 | https://www.w3.org/Voice/2013/scxml-irp/242/test242.txml |
| 21 | Test 243 | https://www.w3.org/Voice/2013/scxml-irp/243/test243.txml |
| 22 | Test 244 | https://www.w3.org/Voice/2013/scxml-irp/244/test244.txml |
| 23 | Test 245 | https://www.w3.org/Voice/2013/scxml-irp/245/test245.txml |
| 24 | Test 247 | https://www.w3.org/Voice/2013/scxml-irp/247/test247.txml |
| 25 | Test 250 | https://www.w3.org/Voice/2013/scxml-irp/250/test250.txml |
| 26 | Test 252 | https://www.w3.org/Voice/2013/scxml-irp/252/test252.txml |
| 27 | Test 253 | https://www.w3.org/Voice/2013/scxml-irp/253/test253.txml |
| 28 | Test 530 | https://www.w3.org/Voice/2013/scxml-irp/530/test530.txml |
| 29 | Test 554 | https://www.w3.org/Voice/2013/scxml-irp/554/test554.txml |
If the ‘typeexpr’ attribute is present, the SCXML Processor MUST evaluate it when the parent invoke element is evaluated and treat the result as if it had been entered as the value of ‘type’.


If the srcexpr attribute is present, the SCXML Processor MUST evaluate it when the parent invoke element is evaluated and treat the result as if it had been entered as the value of ‘src’.


Platforms MUST support http://www.w3.org/TR/scxml/, as a value for the ‘type’ attribute.


If the ‘idlocation’ attribute is present, the SCXML Processor MUST generate an id automatically when the invoke element is evaluated and store it in the location specified by ‘idlocation’.


When the platform generates an identifier for ‘idlocation’, the identifier MUST have the form stateid.platformid, where stateid is the id of the state containing this element and platformid is automatically generated.


In the automatically generated invoke identifier, platformid MUST be unique within the current session.



When the invoke element is executed, the SCXML Processor MUST start a new logical instance of the external service specified in ‘type’ or ‘typexpr’, passing it the URL specified by ‘src’ or the data specified by <content>, or <param>.


The Processor MUST keep track of the unique invokeid and insure that it is included in all events that the invoked service returns to the invoking session.


When the ‘autoforward’ attribute is set to true, the SCXML Processor MUST send an exact copy of every external event it receives to the invoked process.


When the SCXML Processor autoforwards an event to the invoked process, all the fields specified in 5.10.1 The Internal Structure of Events MUST have the same values in the forwarded copy of the event.


Output:
External Event: childToParent, interpreter [Scxml_Test230]
[Log] name is : "childToParent"
[Log] type is : "external"
[Log] sendid is : nil
External Event: childToParent, interpreter [ScxmlChild]
[Log] origin is : "#_scxml_35ebd509-4b45-419f-b021-3df9060f38cf"
[Log] origintype is : "http://www.w3.org/TR/scxml/#SCXMLEventProcessor"
[Log] name is : "childToParent"
[Log] invokeid is : "s0.3b6c2a1a-8bdf-42fe-bc0d-386cd510bf74"
[Log] type is : "external"
[Log] data is : nil
[Log] sendid is : nil
[Log] origin is : "#_scxml_35ebd509-4b45-419f-b021-3df9060f38cf"
[Log] origintype is : "http://www.w3.org/TR/scxml/#SCXMLEventProcessor"
[Log] invokeid is : "s0.3b6c2a1a-8bdf-42fe-bc0d-386cd510bf74"
[Log] data is : nil
Platform Event: done.invoke.s0.3b6c2a1a-8bdf-42fe-bc0d-386cd510bf74, interpreter [Scxml_Test230]
The invoked external service MAY return multiple events while it is processing.


If there is a finalize handler in the instance of invoke that created the service that generated the event, the SCXML Processor MUST execute the code in that finalize handler right before it removes the event from the event queue for processing.


It MUST NOT execute the finalize handler in any other instance of invoke besides the one in the instance of invoke that created the service that generated the event.



Once the invoked external service has finished processing it MUST return a special event ‘done.invoke.id’ to the external event queue of the invoking process, where id is the invokeid for the corresponding invoke element.


The external service MUST NOT generate any other events after the invoke.done.invokeid event.


If the invoking session takes a transition out of the state containing the invoke before it receives the ‘done.invoke.id’ event, the SCXML Processor MUST automatically cancel the invoked component and stop its processing.


Invoked services of type http://www.w3.org/TR/scxml/, http://www.w3.org/TR/ccxml/, http://www.w3.org/TR/voicexml30/, or http://www.w3.org/TR/voicexml21 MUST interpret values specified by the content element or ‘src’ attribute as markup to be executed.



Invoked services of type http://www.w3.org/TR/scxml/, http://www.w3.org/TR/ccxml/, http://www.w3.org/TR/voicexml30/, or http://www.w3.org/TR/voicexml21 MUST interpret values specified by param element or ‘namelist’ attribute as values that are to be injected into their data models.



Invoked services MUST treat values specified by param and namelist identically.




Invoked services MUST also treat values specified by ‘src’ and content identically.




If the invoked process is of type http://www.w3.org/TR/scxml/ and ‘name’ of a param element in the invoke matches the ‘id’ of a data element in the top-level data declarations of the invoked session, the SCXML Processor MUST use the value of the param element as the initial value of the corresponding data element.


If the invoked process is of type http://www.w3.org/TR/scxml/ and the key of namelist item in the invoke matches the ‘id’ of a data element in the top-level data declarations of the invoked session, the SCXML Processor MUST use the corresponding value as the initial value of the corresponding data element.


If the invoked process is of type http://www.w3.org/TR/scxml/, and the name of a param element or the key of a namelist item do not match the name of a data element in the invoked process, the Processor MUST NOT add the value of the param element or namelist key/value pair to the invoked session’s data model.


If the invoked state machine is of type http://www.w3.org/TR/scxml/ and it reaches a top-level final state, the Processor MUST place the event done.invoke.id on the external event queue of the invoking machine, where id is the invokeid for this invocation.


When an invoked process of type http://www.w3.org/TR/scxml/ is cancelled by the invoking process, the Processor MUST execute the onexit handlers for all active states in the invoked session.


Output:
External Event: foo, interpreter [Scxml_Test250]
[Log] "Exiting sub01"
[Log] "Exiting sub0"
Once it cancels an invoked session, the Processor MUST NOT insert any events it receives from the invoked session into the external event queue of the invoking session.


When the invoked session is of type http://www.w3.org/TR/scxml/, The SCXML Processor MUST support the use of SCXML Event/IO processor to communicate between the invoking and the invoked sessions.


The SCXML Processor MUST evaluate a child content element when the parent invoke element is evaluated and pass the resulting data to the invoked service.


If the evaluation of the invoke element’s arguments produces an error, the SCXML Processor MUST terminate the processing of the element without further action.


| TOP | Contents | Overview | Examples | Editor | Forum | |—|—|—|—|—|—|