run.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from sccd.statechart.parser.xml import *
  2. from sccd.statechart.dynamic.statechart_instance import *
  3. if __name__ == "__main__":
  4. # Load model
  5. globals = Globals()
  6. rules = statechart_parser_rules(globals, path=".")
  7. statechart_model = parse_f("statechart_semantics.xml", [("statechart", rules)])
  8. globals.init_durations()
  9. # Generate semantic variants, and filter invalid ones
  10. variants = statechart_model.generate_semantic_variants()
  11. print("Total variants:", len(variants))
  12. # These rules perfectly partition the set of variants into "valid" and "invalid":
  13. def is_valid(semantics):
  14. # Combo-steps should be smaller (or equal to) big-steps, obviously.
  15. if semantics.combo_step_maximality > semantics.big_step_maximality:
  16. return False
  17. # I cannot think of a reason why someone would ever set assignment memory protocol to a different value from enabledness memory protocol.
  18. # The model currently only detects enabledness memory protocol, anyway.
  19. if semantics.assignment_memory_protocol != semantics.enabledness_memory_protocol:
  20. return False
  21. # Combo steps only make sense if another semantic option refers to them
  22. must_have_combo_steps = (semantics.input_event_lifeline == InputEventLifeline.FIRST_COMBO_STEP or
  23. semantics.internal_event_lifeline == InternalEventLifeline.NEXT_COMBO_STEP or
  24. semantics.enabledness_memory_protocol == MemoryProtocol.COMBO_STEP)
  25. # "Combo Take One" is the default combo-step maximality option in SCCD, and is also the option
  26. # that is chosen when no combo-steps are being defined. Therefore, options different from "Combo Take One"
  27. # are only allowed when combo-step semantics are being used.
  28. if not must_have_combo_steps and semantics.combo_step_maximality > Maximality.TAKE_ONE:
  29. return False
  30. # If big-step maximality is "Take One", a big-step will always equal a combo step.
  31. # Therefore combo-steps do not really exist as entities that should be referred to by other semantic options.
  32. # E.g. Input event lifeline "Present in First Combo Step" should be "Present in Whole" instead.
  33. # E.g. Internal event lifeline "Present in Next Combo Step" makes no sense, the event will never be sensed!
  34. # E.g. Memory protocol "Combo Step" should be "Big Step" instead.
  35. if semantics.big_step_maximality == Maximality.TAKE_ONE and must_have_combo_steps:
  36. return False
  37. return True
  38. valid_variants = [v for v in variants if is_valid(v.semantics)]
  39. invalid_variants = [v for v in variants if not is_valid(v.semantics)]
  40. print("Valid variants:", len(valid_variants))
  41. print("Invalid variants:", len(invalid_variants))
  42. # We'll need these mappings to translate the statechart's post-big-step configuration to a semantic configuration
  43. state_name_to_semantics = {
  44. "/P/BigStepMaximality/TakeOne": ("big_step_maximality", Maximality.TAKE_ONE),
  45. "/P/BigStepMaximality/Syntactic": ("big_step_maximality", Maximality.SYNTACTIC),
  46. "/P/BigStepMaximality/TakeMany": ("big_step_maximality", Maximality.TAKE_MANY),
  47. "/P/ComboStepMaximality/ComboStepMaximality/TakeOne": ("combo_step_maximality", Maximality.TAKE_ONE),
  48. "/P/ComboStepMaximality/ComboStepMaximality/Syntactic": ("combo_step_maximality", Maximality.SYNTACTIC),
  49. "/P/ComboStepMaximality/ComboStepMaximality/TakeMany": ("combo_step_maximality", Maximality.TAKE_MANY),
  50. "/P/InputEventLifeline/FirstSmallStep": ("input_event_lifeline", InputEventLifeline.FIRST_SMALL_STEP),
  51. "/P/InputEventLifeline/FirstComboStep": ("input_event_lifeline", InputEventLifeline.FIRST_COMBO_STEP),
  52. "/P/InputEventLifeline/Whole": ("input_event_lifeline", InputEventLifeline.WHOLE),
  53. "/P/InternalEventLifeline/InternalEventLifeline/NextSmallStep": ("internal_event_lifeline", InternalEventLifeline.NEXT_SMALL_STEP),
  54. "/P/InternalEventLifeline/InternalEventLifeline/NextComboStep": ("internal_event_lifeline", InternalEventLifeline.NEXT_COMBO_STEP),
  55. "/P/InternalEventLifeline/InternalEventLifeline/Remainder": ("internal_event_lifeline", InternalEventLifeline.REMAINDER),
  56. "/P/InternalEventLifeline/InternalEventLifeline/Queue": ("internal_event_lifeline", InternalEventLifeline.QUEUE),
  57. "/P/MemoryProtocol/MemoryProtocol/BigStep": ("enabledness_memory_protocol", MemoryProtocol.BIG_STEP),
  58. "/P/MemoryProtocol/MemoryProtocol/ComboStep": ("enabledness_memory_protocol", MemoryProtocol.COMBO_STEP),
  59. "/P/MemoryProtocol/MemoryProtocol/SmallStep": ("enabledness_memory_protocol", MemoryProtocol.SMALL_STEP),
  60. "/P/Priority/SourceParent": ("hierarchical_priority", HierarchicalPriority.SOURCE_PARENT),
  61. "/P/Priority/SourceChild": ("hierarchical_priority", HierarchicalPriority.SOURCE_CHILD),
  62. }
  63. state_id_to_semantics = {
  64. statechart_model.tree.state_dict[state_name].opt.state_id: tup
  65. for state_name, tup in state_name_to_semantics.items()
  66. }
  67. # Some mock callbacks that we have to pass to the StatechartInstance
  68. def on_output(e: OutputEvent):
  69. pass
  70. def schedule(after, event, targets):
  71. return 0
  72. def cancel(id):
  73. pass
  74. # List of input events for the big-step is always the same, so declare it outside the loop
  75. input0_id = globals.events.get_id("input0")
  76. input_events = [InternalEvent(id=input0_id, name="", params=[])]
  77. def check_variants(variants):
  78. correct = []
  79. incorrect = []
  80. for v in variants:
  81. instance = StatechartInstance(
  82. statechart=v,
  83. object_manager=None,
  84. output_callback=on_output,
  85. schedule_callback=schedule,
  86. cancel_callback=cancel)
  87. instance.initialize()
  88. instance.big_step(input_events)
  89. inferred_semantics = SemanticConfiguration()
  90. for state_id in bm_items(instance.execution.configuration):
  91. if state_id in state_id_to_semantics:
  92. aspect_name, aspect_val = state_id_to_semantics[state_id]
  93. setattr(inferred_semantics, aspect_name, aspect_val)
  94. inferred_semantics.assignment_memory_protocol = inferred_semantics.enabledness_memory_protocol
  95. # print("\nActual semantics:")
  96. # print(v.semantics)
  97. # print("\nInferred semantics:")
  98. # print(inferred_semantics)
  99. if v.semantics == inferred_semantics:
  100. correct.append(inferred_semantics)
  101. else:
  102. incorrect.append((v.semantics, inferred_semantics))
  103. return (correct, incorrect)
  104. correct, incorrect = check_variants(valid_variants)
  105. print("\nOf the valid variants, corrently inferred %d, incorrectly inferred %d." % (len(correct), len(incorrect)))
  106. correct, incorrect = check_variants(invalid_variants)
  107. print("Of the invalid variants, corrently inferred %d, incorrectly inferred %d." % (len(correct), len(incorrect)))