merge.alc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. include "primitives.alh"
  2. include "modelling.alh"
  3. include "object_operations.alh"
  4. include "conformance_scd.alh"
  5. include "utils.alh"
  6. include "typing.alh"
  7. include "mini_modify.alh"
  8. String function map_P2F(model : Element, name : String):
  9. Element destinations
  10. String pick
  11. destinations = allAssociationDestinations(model, name, "P2F_state")
  12. pick = name
  13. while (pick == name):
  14. pick = set_pop(destinations)
  15. return pick!
  16. Boolean function main(model : Element):
  17. Element all_states
  18. String element_name
  19. Boolean found_current
  20. Boolean flag_initial
  21. String new_current
  22. found_current = False
  23. new_current = read_root()
  24. if (set_len(allInstances(model, "FullRuntime/State")) == 0):
  25. // No execution in full runtime, so just flag the initial state
  26. flag_initial = True
  27. log("Searching for new initial state...")
  28. // And add a new time
  29. element_name = instantiate_node(model, "NewFullRuntime/Time", "")
  30. instantiate_attribute(model, element_name, "current_time", 0.0)
  31. else:
  32. flag_initial = False
  33. log("No searching for state!")
  34. // Copy the time though
  35. element_name = instantiate_node(model, "NewFullRuntime/Time", "")
  36. instantiate_attribute(model, element_name, "current_time", read_attribute(model, set_pop(allInstances(model, "FullRuntime/Time")), "current_time"))
  37. log("Time copied OK")
  38. log("Reading existing data")
  39. all_states = allInstances(model, "PartialRuntime/State")
  40. while (set_len(all_states) > 0):
  41. element_name = set_pop(all_states)
  42. if (set_len(allOutgoingAssociationInstances(model, element_name, "P2F_state")) > 0):
  43. // Element already exists in full, so check whether this is the current state
  44. log("Found existing state")
  45. if (read_attribute(model, map_P2F(model, element_name), "current")):
  46. found_current = True
  47. new_current = element_name
  48. else:
  49. // New state, so assume that it is not current
  50. log("Read value: " + cast_value(read_attribute(model, element_name, "initial")))
  51. if (bool_and(flag_initial, read_attribute(model, element_name, "initial"))):
  52. log("FOUND IT!")
  53. new_current = element_name
  54. instantiate_link(model, "P2F_state", "", element_name, element_name)
  55. log("Deleting elements...")
  56. all_states = allInstances(model, "NewFullRuntime/State")
  57. while (set_len(all_states) > 0):
  58. element_name = set_pop(all_states)
  59. model_delete_element(model, element_name)
  60. log("Copying elements...")
  61. Element all_elements
  62. String elem
  63. all_elements = dict_keys(model["model"])
  64. while (set_len(all_elements) > 0):
  65. elem = set_pop(all_elements)
  66. if (string_startswith(read_type(model, elem), "PartialRuntime/")):
  67. retype(model, elem, "NewFullRuntime/" + cast_string(list_read(string_split_nr(read_type(model, elem), "/", 1), 1)))
  68. if (read_type(model, elem) == "NewFullRuntime/State"):
  69. instantiate_attribute(model, elem, "current", elem == new_current)
  70. log("Setting current state...")
  71. String new_state
  72. if (found_current == False):
  73. if (False):
  74. // Prompt for new state
  75. output("FIX_NEW_STATE")
  76. new_state = input()
  77. all_states = allInstances(model, "NewFullRuntime/State")
  78. while (set_len(all_states) > 0):
  79. element_name = set_pop(all_states)
  80. if (value_eq(read_attribute(model, element_name, "name"), new_state)):
  81. instantiate_attribute(model, element_name, "current", True)
  82. break!
  83. else:
  84. // Reset to initial
  85. log("Resetting to initial state!")
  86. all_states = allInstances(model, "NewFullRuntime/State")
  87. while (set_len(all_states) > 0):
  88. element_name = set_pop(all_states)
  89. if (value_eq(read_attribute(model, element_name, "initial"), True)):
  90. log("Found initial state: " + cast_value(read_attribute(model, element_name, "name")))
  91. instantiate_attribute(model, element_name, "current", True)
  92. break!
  93. log("DONE!")
  94. return True!