modelling.alc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. include "primitives.alh"
  2. include "io.alh"
  3. String function instantiated_name(element : Element, original : String):
  4. if (original == ""):
  5. return cast_id2s(element)
  6. else:
  7. return original
  8. Element function instantiate_bottom():
  9. // Just create a new node that serves as the basis for everything
  10. // We don't know anything about the model yet, so just create an empty one
  11. Element new_model
  12. // The actual root node of the model
  13. new_model = create_node()
  14. // Add an empty model and empty type mapping
  15. dict_add(new_model, "model", create_node())
  16. dict_add(new_model, "type_mapping", create_node())
  17. // Return the created model
  18. return new_model
  19. String function model_add_node(model : Element, name : String):
  20. // Adds a new node to the specified model with the desired name
  21. // This is a bottom operation, as it doesn't take any type
  22. Element new_node
  23. String actual_name
  24. new_node = create_node()
  25. actual_name = instantiated_name(new_node, name)
  26. dict_add(model["model"], actual_name, new_node)
  27. return actual_name
  28. String function model_add_value(model : Element, name : String, value : Element):
  29. // Similar to model_add_node, but add a value as well
  30. String actual_name
  31. actual_name = instantiated_name(value, name)
  32. dict_add(model["model"], actual_name, value)
  33. return actual_name
  34. String function model_add_edge(model : Element, name : String, source : String, destination : String):
  35. // Add an edge between the source and destination nodes
  36. // Nodes are specified using their string representation previously defined
  37. Element new_edge
  38. String actual_name
  39. new_edge = create_edge(model["model"][source], model["model"][destination])
  40. actual_name = instantiated_name(new_edge, name)
  41. dict_add(model["model"], actual_name, new_edge)
  42. return actual_name
  43. Void function retype_model(model : Element, metamodel : Element):
  44. // Remove the type mapping and add a new one for the specified metamodel
  45. dict_delete(model, "type_mapping")
  46. dict_add(model, "type_mapping", create_node())
  47. dict_add(model, "metamodel", metamodel)
  48. return
  49. Void function retype(model : Element, element : String, type : String):
  50. // Retype a model, deleting any previous type the element had
  51. // The type string is evaluated in the metamodel previously specified
  52. if (dict_in_node(model["type_mapping"], model["model"][element])):
  53. dict_delete(model["type_mapping"], model["model"][element])
  54. dict_add(model["type_mapping"], model["model"][element], model["metamodel"]["model"][type])
  55. return
  56. Element function instantiate_model(metamodel : Element):
  57. // Instantiate a model
  58. // Basically create an untyped model and retype it
  59. Element model
  60. model = instantiate_bottom()
  61. retype_model(model, metamodel)
  62. return model
  63. String function instantiate_node(model : Element, type_name : String, instance_name : String):
  64. // Create a node typed by a node from the metamodel
  65. // Basically create a node and type it immediately
  66. String actual_name
  67. actual_name = model_add_node(model, instance_name)
  68. retype(model, instance_name, type_name)
  69. return actual_name
  70. Void function instantiate_attribute(model : Element, element : String, attribute : String, value : Element):
  71. // Instantiate an attribute of something that needs to be instantiated
  72. // Actually a bit more difficult than all the rest, as we need to find the attribute to instantiate
  73. Element elem
  74. Element type
  75. Element attr_type
  76. elem = model["model"][element]
  77. type = model["type_mapping"][elem]
  78. //attr_type = find_attribute(model, type, attribute)
  79. instantiate_link(model, attr_type, "", elem, value)
  80. return
  81. String function instantiate_link(model : Element, type : String, name : String, source : String, destination : String):
  82. // Create a typed link between two nodes
  83. String actual_name
  84. actual_name = model_add_edge(model, name, source, destination)
  85. retype(model, actual_name, type)
  86. return actual_name
  87. Void function instantiate_named_value(model : Element, type : String, name : String, source : String, destination : Element):
  88. // Special helper function to create an edge and name it
  89. String actual_name
  90. actual_name = instantiate_link(model, type, "", source, destination)
  91. instantiate_attribute(model, model["model"][actual_name], "name", name)
  92. return
  93. Void function instantiate_named(model : Element, type : String, name : String, source : String, destination : String):
  94. // Special helper function to create an edge and name it
  95. instantiate_named(model, type, name, source, model["model"][destination])
  96. return
  97. Void function define_inheritance(model : Element, inheritance_name : String):
  98. // Set the inheritance link to the one defined in our own metamodel, given by the specified name
  99. dict_add(model, "inheritance", model["metamodel"]["model"][inheritance_name])
  100. return
  101. Void function construct_model():
  102. String command
  103. log("Enter modelling constructs!")
  104. while (True):
  105. command = input()
  106. if (command == "instantiate_bottom"):
  107. output(instantiate_bottom())
  108. elif (command == "add_node"):
  109. model_add_node(input(), input())
  110. elif (command == "add_value"):
  111. model_add_value(input(), input(), input())
  112. elif (command == "add_edge"):
  113. model_add_edge(input(), input(), input(), input())
  114. elif (command == "exit"):
  115. return
  116. elif (command == "retype_model"):
  117. retype_model(input(), input())
  118. elif (command == "retype"):
  119. retype(input(), input(), input())
  120. elif (command == "instantiate_model"):
  121. output(instantiate_model(input()))
  122. elif (command == "instantiate_attribute"):
  123. instantiate_named(input(), input(), input(), input(), input())
  124. elif (command == "define_inheritance"):
  125. define_inheritance(input(), input())
  126. else:
  127. log("Modelling error: did not understand command " + command)