utils.alc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (type == "Association"):
  31. result = result + ", \"__source\": \"" + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m])) + "\""
  32. result = result + ", \"__target\": \"" + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m])) + "\""
  33. // Has attributes
  34. attrs = getAttributeList(model, v_m)
  35. attr_keys = dict_keys(attrs)
  36. while (0 < set_len(attr_keys)):
  37. attr_key = set_pop(attr_keys)
  38. attr_value = read_attribute(model, v_m, attr_key)
  39. if (read_type(model["metamodel"], attrs[attr_key]) == "ActionLanguage"):
  40. if (element_eq(attr_value, read_root())):
  41. result = result + ", \"" + attr_key + "\": {\"AL\": \"\"}"
  42. else:
  43. result = result + ", \"" + attr_key + "\": {\"AL\": " + cast_value(attr_value) + "}"
  44. else:
  45. if (element_eq(attr_value, read_root())):
  46. result = result + ", \"" + attr_key + "\": null"
  47. else:
  48. if (is_physical_boolean(attr_value)):
  49. if (attr_value):
  50. result = result + ", \"" + attr_key + "\": true"
  51. else:
  52. result = result + ", \"" + attr_key + "\": false"
  53. else:
  54. result = result + ", \"" + attr_key + "\": " + cast_value(attr_value)
  55. result = result + "}"
  56. result = result + "]"
  57. return result!
  58. Element function list_reverse(lst : Element):
  59. Element result
  60. result = list_create()
  61. Integer i
  62. i = list_len(lst) - 1
  63. while (i >= 0):
  64. list_append(result, list_read(lst, i))
  65. i = i - 1
  66. return result!
  67. Element function list_splice(lst : Element, start : Integer, end : Integer):
  68. Element result
  69. result = list_create()
  70. Integer i
  71. i = start
  72. while (i < end):
  73. list_append(result, list_read(lst, i))
  74. i = i + 1
  75. return result!
  76. Void function list_extend(lst : Element, ext : Element):
  77. Integer i
  78. i = 0
  79. while (i < list_len(ext)):
  80. list_append(lst, list_read(ext, i))
  81. i = i + 1
  82. return!
  83. String function get_taskname():
  84. return reverseKeyLookup(read_root(), read_taskroot())!
  85. Element function string_split_nr(str : String, split : String, count : Integer):
  86. Element splitted
  87. String new
  88. splitted = string_split(str, split)
  89. count = count + 1
  90. while (list_len(splitted) > count):
  91. new = split + cast_string(list_pop_final(splitted))
  92. new = cast_string(list_pop_final(splitted)) + new
  93. list_append(splitted, new)
  94. return splitted!
  95. Element function alphabet():
  96. Element chars
  97. chars = list_create()
  98. list_append(chars, "a")
  99. list_append(chars, "b")
  100. list_append(chars, "c")
  101. list_append(chars, "d")
  102. list_append(chars, "e")
  103. list_append(chars, "f")
  104. list_append(chars, "g")
  105. list_append(chars, "h")
  106. list_append(chars, "i")
  107. list_append(chars, "j")
  108. list_append(chars, "k")
  109. list_append(chars, "l")
  110. list_append(chars, "m")
  111. list_append(chars, "n")
  112. list_append(chars, "o")
  113. list_append(chars, "p")
  114. list_append(chars, "q")
  115. list_append(chars, "s")
  116. list_append(chars, "t")
  117. list_append(chars, "u")
  118. list_append(chars, "v")
  119. list_append(chars, "w")
  120. list_append(chars, "x")
  121. list_append(chars, "y")
  122. list_append(chars, "z")
  123. return chars!