<?xml version="1.0" ?>
<statechart>
  <semantics
    big_step_maximality="syntactic"
    concurrency="many"
    input_event_lifeline="whole"
    enabledness_memory_protocol="small_step"
    assignment_memory_protocol="small_step"/>

  <datamodel>
    c = 0;
    lp = 1234567890;
    p = 0;

    digit = func(i:int, pos:int) {
      result = i // 10**pos % 10;
      log("digit " + int_to_str(pos) + " of " + int_to_str(i) + " is " + int_to_str(result));
      return result;
    };

    numdigits = func(i:int) {
      if (i == 0)
        return 0;
      return float_to_int(log10(i)) + 1;
    };
  </datamodel>

  <inport name="in">
    <event name="redial"/>
  </inport>

  <outport name="out">
    <event name="out"/>
  </outport>

  <root>
    <parallel id="Dialing">
      <state id="Dialer" initial="WaitForDial">
        <state id="WaitForDial" stable="true">
          <!-- t1 -->
          <transition event="dial(d:int), not redial" cond="c &lt; 10" target=".">
            <code>
              c += 1;
              lp = lp * 10 + d;
            </code>
            <raise port="out" event="out">
              <param expr="d"/>
            </raise>
          </transition>
          <!-- t2 -->
          <transition event="dial(d:int), redial" cond="c == 0" target="../DialDigits">
            <code>
              log("got dial("+int_to_str(d)+")");
              lp = d;
              c = 1;
            </code>
            <raise port="out" event="out">
              <param expr="d"/>
            </raise>
          </transition>
        </state>
        <state id="DialDigits">
          <!-- t3 -->
          <transition event="dial(d:int)" cond="c &lt; 10" target=".">
            <code>
              log("got dial("+int_to_str(d)+")");
              lp = lp * 10 + d;
              c += 1;
            </code>
            <raise port="out" event="out">
              <param expr="d"/>
            </raise>
          </transition>
          <!-- t4 -->
          <transition cond="c == 10" target="../WaitForDial"/>
        </state>
      </state>

      <state id="Redialer" initial="WaitForRedial">
        <state id="WaitForRedial" stable="true">
          <!-- t5 -->
          <transition event="redial" cond="c == 0" target="../RedialDigits">
            <code>
              p = lp;
            </code>
            <raise event="dial">
              <param expr="digit(lp, 0)"/>
            </raise>
          </transition>
        </state>
        <state id="RedialDigits">
          <!-- t6 -->
          <transition cond="c &lt; numdigits(p)" target=".">
            <raise event="dial">
              <param expr="digit(p, c)"/>
            </raise>
          </transition>
          <!-- t7 -->
          <transition cond="c == numdigits(p)" target="../WaitForRedial"/>
        </state>
      </state>

    </parallel>
  </root>
</statechart>