utils.alc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. include "modelling.alh"
  2. include "primitives.alh"
  3. include "object_operations.alh"
  4. include "model_management.alh"
  5. include "random.alh"
  6. String function JSON_print(model : Element):
  7. String result
  8. Element keys_m
  9. String v_m
  10. String type
  11. String attr_key
  12. Element attr_keys
  13. Boolean first
  14. Element attr_value
  15. result = "["
  16. keys_m = dict_keys(model["model"])
  17. first = True
  18. while (set_len(keys_m) > 0):
  19. v_m = set_pop(keys_m)
  20. type = read_type(model["metamodel"], read_type(model, v_m))
  21. if (bool_or(type == "Class", type == "Association")):
  22. if (bool_not(first)):
  23. result = result + ","
  24. else:
  25. first = False
  26. result = result + "{"
  27. result = result + "\"id\": \"" + v_m + "\""
  28. result = result + "," + "\"type\": \"" + read_type(model, v_m) + "\""
  29. if (type == "Association"):
  30. result = result + ", \"__source\": \"" + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m])) + "\""
  31. result = result + ", \"__target\": \"" + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])) + "\""
  32. // Has attributes
  33. attr_keys = dict_keys(getAttributeList(model, v_m))
  34. while (0 < set_len(attr_keys)):
  35. attr_key = set_pop(attr_keys)
  36. attr_value = read_attribute(model, v_m, attr_key)
  37. if (element_eq(attr_value, read_root())):
  38. result = result + ", \"" + attr_key + "\": null"
  39. else:
  40. if (is_physical_boolean(attr_value)):
  41. if (attr_value):
  42. result = result + ", \"" + attr_key + "\": true"
  43. else:
  44. result = result + ", \"" + attr_key + "\": false"
  45. else:
  46. result = result + ", \"" + attr_key + "\": " + cast_value(attr_value)
  47. result = result + "}"
  48. result = result + "]"
  49. return result!
  50. Element function list_reverse(lst : Element):
  51. Element result
  52. result = list_create()
  53. Integer i
  54. i = list_len(lst) - 1
  55. while (i >= 0):
  56. list_append(result, list_read(lst, i))
  57. i = i - 1
  58. return result!
  59. Element function list_splice(lst : Element, start : Integer, end : Integer):
  60. Element result
  61. result = list_create()
  62. Integer i
  63. i = start
  64. while (i < end):
  65. list_append(result, list_read(lst, i))
  66. i = i + 1
  67. return result!
  68. Void function list_extend(lst : Element, ext : Element):
  69. Integer i
  70. i = 0
  71. while (i < list_len(ext)):
  72. list_append(lst, list_read(ext, i))
  73. i = i + 1
  74. return!
  75. String function get_taskname():
  76. return reverseKeyLookup(read_root(), read_taskroot())!
  77. Element function string_split_nr(str : String, split : String, count : Integer):
  78. Element splitted
  79. String new
  80. splitted = string_split(str, split)
  81. count = count + 1
  82. while (list_len(splitted) > count):
  83. new = split + cast_string(list_pop_final(splitted))
  84. new = cast_string(list_pop_final(splitted)) + new
  85. list_append(splitted, new)
  86. return splitted!
  87. Element function alphabet():
  88. Element chars
  89. chars = list_create()
  90. list_append(chars, "a")
  91. list_append(chars, "b")
  92. list_append(chars, "c")
  93. list_append(chars, "d")
  94. list_append(chars, "e")
  95. list_append(chars, "f")
  96. list_append(chars, "g")
  97. list_append(chars, "h")
  98. list_append(chars, "i")
  99. list_append(chars, "j")
  100. list_append(chars, "k")
  101. list_append(chars, "l")
  102. list_append(chars, "m")
  103. list_append(chars, "n")
  104. list_append(chars, "o")
  105. list_append(chars, "p")
  106. list_append(chars, "q")
  107. list_append(chars, "s")
  108. list_append(chars, "t")
  109. list_append(chars, "u")
  110. list_append(chars, "v")
  111. list_append(chars, "w")
  112. list_append(chars, "x")
  113. list_append(chars, "y")
  114. list_append(chars, "z")
  115. return chars!
  116. String function spawn_TODO(function : Element, arguments : Element):
  117. // Define a new task, with all the required stack information
  118. // This is taken from the "task_management" code
  119. Element task_root
  120. Element task_frame
  121. Element output_value
  122. Element input_value
  123. task_root = create_node()
  124. task_frame = create_node()
  125. output_value = create_node()
  126. input_value = create_node()
  127. dict_add_fast(task_root, "frame", task_frame)
  128. dict_add_fast(task_root, "globals", create_node())
  129. dict_add_fast(task_root, "output", output_value)
  130. dict_add_fast(task_root, "last_output", output_value)
  131. dict_add_fast(task_root, "input", input_value)
  132. dict_add_fast(task_root, "last_input", input_value)
  133. dict_add_fast(task_frame, "evalstack", create_node())
  134. dict_add_fast(task_frame, "returnvalue", create_node())
  135. dict_add_fast(task_frame, "phase", "init")
  136. dict_add_fast(task_frame, "IP", dict_read(dict_read(read_root(), "__hierarchy"), "__IP"))
  137. dict_add_fast(task_frame, "symbols", create_node())
  138. log("Setting IP to " + cast_value(function["body"]))
  139. log("Has outputs: " + set_to_string(dict_keys(function)))
  140. if (dict_in(function, "params")):
  141. // And add the arguments to the symbol table
  142. Element symbols
  143. String arg_names_call
  144. Element param_dict
  145. Integer arg_i
  146. symbols = task_frame["symbols"]
  147. arg_names_call = "abcdefghijklmnopqrstuvwxyz"
  148. arg_i = 0
  149. param_dict = function["params"]
  150. arguments = list_copy(arguments)
  151. Element t
  152. while (list_len(arguments) > 0):
  153. t = create_edge(symbols, list_pop(arguments, 0))
  154. create_edge(t, param_dict[arg_names_call[arg_i]])
  155. arg_i = arg_i + 1
  156. // Add this only at the end, as otherwise the task will already be detected and executed
  157. String taskname
  158. taskname = random_string(30)
  159. while (dict_in(read_root(), taskname)):
  160. taskname = random_string(30)
  161. dict_add_fast(read_root(), taskname, task_root)
  162. return taskname!
  163. String function spawn(function : Element, arguments : Element):
  164. // Define a new task, with all the required stack information
  165. // This is taken from the "task_management" code
  166. Element task_root
  167. Element task_frame
  168. Element output_value
  169. Element input_value
  170. task_root = create_node()
  171. task_frame = create_node()
  172. output_value = create_node()
  173. input_value = create_node()
  174. dict_add_fast(task_root, "frame", task_frame)
  175. dict_add_fast(task_root, "globals", create_node())
  176. dict_add_fast(task_root, "output", output_value)
  177. dict_add_fast(task_root, "last_output", output_value)
  178. dict_add_fast(task_root, "input", input_value)
  179. dict_add_fast(task_root, "last_input", input_value)
  180. dict_add_fast(task_frame, "evalstack", create_node())
  181. dict_add_fast(task_frame, "returnvalue", create_node())
  182. dict_add_fast(task_frame, "phase", "init")
  183. dict_add_fast(task_frame, "IP", dict_read(dict_read(read_root(), "__hierarchy"), "__IP"))
  184. dict_add_fast(task_frame, "symbols", create_node())
  185. dict_add_fast(task_root["globals"], "initial_function", function)
  186. dict_add_fast(task_root["globals"], "initial_arguments", arguments)
  187. // Add this only at the end, as otherwise the task will already be detected and executed
  188. String taskname
  189. taskname = random_string(30)
  190. while (dict_in(read_root(), taskname)):
  191. taskname = random_string(30)
  192. dict_add_fast(read_root(), taskname, task_root)
  193. return taskname!