utils.alc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 (read_nr_out(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 < read_nr_out(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_v2s(attr_value))
  46. result = result + "}"
  47. result = result + "]"
  48. return result!