AIML Docs

State Element

Element for defining standard states in the state machine

State Element

The <state> element represents a standard state in the state machine:

<state id="processing" initial="step1">
<state id="step1">
  <transition target="step2" />
</state>
 
<state id="step2">
  <transition target="final" />
</state>
</state>
PropTypeDefault
id?
string
-
initial?
string
-

Allowed Children

  • transition: State transitions
  • state: Child states
  • parallel: Parallel states
  • final: Final states
  • history: History pseudo-states
  • datamodel: Data model declarations
  • invoke: Service invocations
  • data: Data declarations

Examples

Simple State

<state id="idle">
<transition event="start" target="active" />
</state>

Compound State

<state id="processing" initial="validate">
<state id="validate">
  <transition 
    event="valid" 
    target="compute" 
  />
  <transition 
    event="invalid" 
    target="error" 
  />
</state>
 
<state id="compute">
  <transition 
    event="complete" 
    target="done" 
  />
</state>
 
<state id="error">
  <transition 
    event="retry" 
    target="validate" 
  />
</state>
</state>

State with Data Model

<state id="form">
<datamodel>
  <data id="formData" type="JSON" value={{}} />
  <data id="errors" type="JSON" value={[]} />
  <data id="isValid" type="BOOLEAN" value={false} />
</datamodel>
 
<transition 
  event="submit"
  condition={({state}) => state.isValid}
  target="success"
/>
</state>

Usage Notes

  • Can be simple or compound (containing other states)
  • Can define entry/exit actions
  • Can include transitions
  • Can contain data model
  • Can invoke external services

Common Patterns

Data Processing State

<state id="processData">
<datamodel>
  <data id="input" type="JSON" />
  <data id="output" type="JSON" value={{}} />
  <data id="error" type="STRING" />
</datamodel>
 
<script>
  try {
    const result = processInput(state.input);
    _dataModel.setValue("state.output", result);
  } catch (err) {
    _dataModel.setValue("state.error", err.message);
    _dataModel.setValue("state.status", "error");
  }
</script>
 
<transition event="error" target="errorState" />
<transition event="complete" target="successState" />
</state>

Form State

<state id="userForm">
<datamodel>
  <data id="fields" type="JSON" value={{
    name: "",
    email: "",
    age: null
  }} />
  <data id="validation" type="JSON" value={{}} />
</datamodel>
 
<transition 
  event="field.change"
  target="userForm"
>
  <assign 
    location="state.fields"
    expr={({event}) => ({
      ...state.fields,
      [event.field]: event.value
    })}
  />
</transition>
</state>

Service State

<state id="apiConnection">
<invoke 
  id="api"
  type="http"
  src="https://api.example.com"
>
  <finalize>
    <assign 
      location="state.response" 
      expr={({event}) => event.data}
    />
  </finalize>
</invoke>
 
<transition event="error" target="errorState" />
<transition event="done" target="successState" />
</state>

On this page