utils.alc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. include "modelling.alh"
  2. include "primitives.alh"
  3. include "object_operations.alh"
  4. include "model_management.alh"
  5. String function JSON_print(model : Element):
  6. String result
  7. Element keys_m
  8. String v_m
  9. String type
  10. String attr_key
  11. Element attr_keys
  12. Boolean first
  13. Element attr_value
  14. String type_attr
  15. Element attrs
  16. result = "["
  17. keys_m = dict_keys(model["model"])
  18. first = True
  19. while (set_len(keys_m) > 0):
  20. v_m = set_pop(keys_m)
  21. type = read_type(model["metamodel"], read_type(model, v_m))
  22. if (bool_or(type == "Class", type == "Association")):
  23. if (bool_not(first)):
  24. result = result + ","
  25. else:
  26. first = False
  27. result = result + "{"
  28. result = result + "\"__id\": \"" + v_m + "\""
  29. result = result + "," + "\"__type\": \"" + read_type(model, v_m) + "\""
  30. log("Read type : " + cast_string(read_type(model, v_m)))
  31. if (type == "Association"):
  32. result = result + ", \"__source\": \"" + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m])) + "\""
  33. result = result + ", \"__target\": \"" + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])) + "\""
  34. // Has attributes
  35. attrs = getAttributeList(model, v_m)
  36. attr_keys = dict_keys(attrs)
  37. while (0 < set_len(attr_keys)):
  38. attr_key = set_pop(attr_keys)
  39. attr_value = read_attribute(model, v_m, attr_key)
  40. if (read_type(model["metamodel"], attrs[attr_key]) == "ActionLanguage"):
  41. if (element_eq(attr_value, read_root())):
  42. result = result + ", \"" + attr_key + "\": {\"AL\": \"\"}"
  43. else:
  44. result = result + ", \"" + attr_key + "\": {\"AL\": " + cast_value(attr_value) + "}"
  45. else:
  46. if (element_eq(attr_value, read_root())):
  47. result = result + ", \"" + attr_key + "\": null"
  48. else:
  49. if (is_physical_boolean(attr_value)):
  50. if (attr_value):
  51. result = result + ", \"" + attr_key + "\": true"
  52. else:
  53. result = result + ", \"" + attr_key + "\": false"
  54. else:
  55. result = result + ", \"" + attr_key + "\": " + cast_value(attr_value)
  56. result = result + "}"
  57. result = result + "]"
  58. return result!
  59. Element function list_reverse(lst : Element):
  60. Element result
  61. result = list_create()
  62. Integer i
  63. i = list_len(lst) - 1
  64. while (i >= 0):
  65. list_append(result, list_read(lst, i))
  66. i = i - 1
  67. return result!
  68. Element function list_splice(lst : Element, start : Integer, end : Integer):
  69. Element result
  70. result = list_create()
  71. Integer i
  72. i = start
  73. while (i < end):
  74. list_append(result, list_read(lst, i))
  75. i = i + 1
  76. return result!
  77. Void function list_extend(lst : Element, ext : Element):
  78. Integer i
  79. i = 0
  80. while (i < list_len(ext)):
  81. list_append(lst, list_read(ext, i))
  82. i = i + 1
  83. return!
  84. String function get_taskname():
  85. return reverseKeyLookup(read_root(), read_taskroot())!
  86. Element function string_split_nr(str : String, split : String, count : Integer):
  87. Element splitted
  88. String new
  89. splitted = string_split(str, split)
  90. count = count + 1
  91. while (list_len(splitted) > count):
  92. new = split + cast_string(list_pop_final(splitted))
  93. new = cast_string(list_pop_final(splitted)) + new
  94. list_append(splitted, new)
  95. return splitted!
  96. Element function alphabet():
  97. Element chars
  98. chars = list_create()
  99. list_append(chars, "a")
  100. list_append(chars, "b")
  101. list_append(chars, "c")
  102. list_append(chars, "d")
  103. list_append(chars, "e")
  104. list_append(chars, "f")
  105. list_append(chars, "g")
  106. list_append(chars, "h")
  107. list_append(chars, "i")
  108. list_append(chars, "j")
  109. list_append(chars, "k")
  110. list_append(chars, "l")
  111. list_append(chars, "m")
  112. list_append(chars, "n")
  113. list_append(chars, "o")
  114. list_append(chars, "p")
  115. list_append(chars, "q")
  116. list_append(chars, "s")
  117. list_append(chars, "t")
  118. list_append(chars, "u")
  119. list_append(chars, "v")
  120. list_append(chars, "w")
  121. list_append(chars, "x")
  122. list_append(chars, "y")
  123. list_append(chars, "z")
  124. return chars!