utils.alc 3.4 KB

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