utils.alc 3.4 KB

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