utils.alc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 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. type_attr = read_type(model, reverseKeyLookup(model["model"], attr_value))
  47. if (read_type(model["metamodel"], type_attr) == "ComplexAttribute"):
  48. result = result + ", \"" + attr_key + "\": {\"AL\": " + cast_value(attr_value) + "}"
  49. else:
  50. result = result + ", \"" + attr_key + "\": " + cast_value(attr_value)
  51. result = result + "}"
  52. result = result + "]"
  53. return result!
  54. Element function list_reverse(lst : Element):
  55. Element result
  56. result = list_create()
  57. Integer i
  58. i = list_len(lst) - 1
  59. while (i >= 0):
  60. list_append(result, list_read(lst, i))
  61. i = i - 1
  62. return result!
  63. Element function list_splice(lst : Element, start : Integer, end : Integer):
  64. Element result
  65. result = list_create()
  66. Integer i
  67. i = start
  68. while (i < end):
  69. list_append(result, list_read(lst, i))
  70. i = i + 1
  71. return result!
  72. Void function list_extend(lst : Element, ext : Element):
  73. Integer i
  74. i = 0
  75. while (i < list_len(ext)):
  76. list_append(lst, list_read(ext, i))
  77. i = i + 1
  78. return!
  79. String function get_taskname():
  80. return reverseKeyLookup(read_root(), read_taskroot())!
  81. Element function string_split_nr(str : String, split : String, count : Integer):
  82. Element splitted
  83. String new
  84. splitted = string_split(str, split)
  85. count = count + 1
  86. while (list_len(splitted) > count):
  87. new = split + cast_string(list_pop_final(splitted))
  88. new = cast_string(list_pop_final(splitted)) + new
  89. list_append(splitted, new)
  90. return splitted!
  91. Element function alphabet():
  92. Element chars
  93. chars = list_create()
  94. list_append(chars, "a")
  95. list_append(chars, "b")
  96. list_append(chars, "c")
  97. list_append(chars, "d")
  98. list_append(chars, "e")
  99. list_append(chars, "f")
  100. list_append(chars, "g")
  101. list_append(chars, "h")
  102. list_append(chars, "i")
  103. list_append(chars, "j")
  104. list_append(chars, "k")
  105. list_append(chars, "l")
  106. list_append(chars, "m")
  107. list_append(chars, "n")
  108. list_append(chars, "o")
  109. list_append(chars, "p")
  110. list_append(chars, "q")
  111. list_append(chars, "s")
  112. list_append(chars, "t")
  113. list_append(chars, "u")
  114. list_append(chars, "v")
  115. list_append(chars, "w")
  116. list_append(chars, "x")
  117. list_append(chars, "y")
  118. list_append(chars, "z")
  119. return chars!