StateChart.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. namespace csharp_sccd_compiler
  6. {
  7. public class StateChart : Visitable
  8. {
  9. /// <summary>
  10. /// The total number of transitions present in this statechart that are trigger by time (AFTER).
  11. /// </summary>
  12. public int nr_of_after_transitions { get; private set; }
  13. /// <summary>
  14. /// Root node of the statechart.
  15. /// </summary>
  16. public StateChartNode root { get; private set; }
  17. /// <summary>
  18. /// All basic states.
  19. /// </summary>
  20. public List<StateChartNode> basics { get; private set; }
  21. /// <summary>
  22. /// All composite states.
  23. /// </summary>
  24. public List<StateChartNode> composites { get; private set; }
  25. /// <summary>
  26. /// All history states.
  27. /// </summary>
  28. public List<StateChartNode> histories { get; private set; }
  29. /// <summary>
  30. /// All nodes that need their state saved shallow.
  31. /// </summary>
  32. public List<StateChartNode> shallow_history_parents { get; private set; }
  33. /// <summary>
  34. /// All nodes that need their state saved deep.
  35. /// </summary>
  36. public List<StateChartNode> deep_history_parents { get; private set; }
  37. /// <summary>
  38. /// All nodes that need their state saved on leaving
  39. /// </summary>
  40. public List<StateChartNode> combined_history_parents { get; private set; }
  41. public StateChart(XElement xml)
  42. {
  43. this.root = new StateChartNode(xml);
  44. this.nr_of_after_transitions = 0;
  45. this.basics = new List<StateChartNode>();
  46. this.composites = new List<StateChartNode>();
  47. this.histories = new List<StateChartNode>();
  48. this.extractFromHierarchy(this.root);
  49. this.shallow_history_parents = new List<StateChartNode>();
  50. this.deep_history_parents = new List<StateChartNode>();
  51. this.combined_history_parents = new List<StateChartNode>();
  52. foreach (StateChartNode node in this.histories)
  53. {
  54. this.calculateHistory(node.parent, node.is_history_deep);
  55. }
  56. }
  57. /// <summary>
  58. /// Calculates which nodes need their state saved for history purposes.
  59. /// </summary>
  60. /// <param name="parent">The parent node of a history state.</param>
  61. /// <param name="is_history_deep">If set to <c>true</c> the history type is <c>deep</c>.</param>
  62. private void calculateHistory(StateChartNode parent, bool is_history_deep)
  63. {
  64. if (object.ReferenceEquals(parent, this.root)) //TODO use is_root property?
  65. throw new CompilerException("Root component cannot contain a history state.");
  66. if (!this.combined_history_parents.Contains(parent))
  67. {
  68. this.combined_history_parents.Add(parent);
  69. parent.save_state_on_exit = true;
  70. }
  71. if (is_history_deep)
  72. {
  73. if (! this.deep_history_parents.Contains(parent))
  74. this.deep_history_parents.Add(parent);
  75. }
  76. else
  77. {
  78. if (! this.shallow_history_parents.Contains(parent))
  79. this.shallow_history_parents.Add(parent);
  80. }
  81. if ( parent.is_parallel || is_history_deep)
  82. {
  83. foreach (StateChartNode child in parent.children)
  84. if (child.is_composite)
  85. this.calculateHistory(child, is_history_deep);
  86. }
  87. }
  88. private void extractFromHierarchy(StateChartNode node)
  89. {
  90. foreach (StateChartTransition transition in node.transitions)
  91. {
  92. TriggerEvent trigger = transition.trigger;
  93. if (trigger.is_after)
  94. {
  95. trigger.after_index = this.nr_of_after_transitions;
  96. string event_name = string.Format("_{0}after", trigger.after_index);
  97. trigger.event_name = event_name;
  98. this.nr_of_after_transitions += 1;
  99. }
  100. }
  101. if (node.is_basic)
  102. this.basics.Add(node);
  103. else if (node.is_composite)
  104. this.composites.Add(node);
  105. else if (node.is_history)
  106. this.histories.Add(node);
  107. foreach (StateChartNode child in node.children)
  108. this.extractFromHierarchy(child);
  109. }
  110. public override void accept(Visitor visitor)
  111. {
  112. visitor.visit (this);
  113. }
  114. }
  115. }