bootstrap.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ### Configuration for creating the bootstrap model using conformance_bottom.
  2. import glob
  3. import hashlib
  4. import tempfile
  5. import gzip
  6. def bootstrap():
  7. root = ["__hierarchy"]
  8. task_manager = "task_manager"
  9. initial_code_manager = "bootstrap/initial_code_manager.alc"
  10. initial_code_task = "bootstrap/initial_code_task.alc"
  11. task_data = [ "input",
  12. "output",
  13. "globals",
  14. "frame",
  15. ]
  16. task_frame = [ "evalstack",
  17. "returnvalue",
  18. ]
  19. primitives = { "integer_addition": ["Integer", "Integer", "Integer"],
  20. "integer_subtraction": ["Integer", "Integer", "Integer"],
  21. "integer_multiplication": ["Integer", "Integer", "Integer"],
  22. "integer_division": ["Integer", "Integer", "Integer"],
  23. "integer_gt": ["Boolean", "Integer", "Integer"],
  24. "float_addition": ["Float", "Float", "Float"],
  25. "float_subtraction": ["Float", "Float", "Float"],
  26. "float_multiplication": ["Float", "Float", "Float"],
  27. "float_division": ["Float", "Float", "Float"],
  28. "float_lt": ["Boolean", "Float", "Float"],
  29. "bool_and": ["Boolean", "Boolean", "Boolean"],
  30. "bool_or": ["Boolean", "Boolean", "Boolean"],
  31. "bool_not": ["Boolean", "Boolean"],
  32. "string_join": ["String", "String", "String"],
  33. "string_get": ["String", "String", "Integer"],
  34. "string_len": ["Integer", "String"],
  35. "string_split": ["Element", "String", "String"],
  36. "value_eq": ["Boolean", "Element", "Element"],
  37. "cast_i2f": ["Float", "Integer"],
  38. "cast_i2s": ["String", "Integer"],
  39. "cast_i2b": ["Boolean", "Integer"],
  40. "cast_f2i": ["Integer", "Float"],
  41. "cast_f2b": ["Boolean", "Float"],
  42. "cast_f2s": ["String", "Float"],
  43. "cast_s2i": ["Integer", "String"],
  44. "cast_s2f": ["Float", "String"],
  45. "cast_s2b": ["Boolean", "String"],
  46. "cast_b2i": ["Integer", "Boolean"],
  47. "cast_b2f": ["Float", "Boolean"],
  48. "cast_b2s": ["String", "Boolean"],
  49. "cast_e2s": ["String", "Element"],
  50. "cast_a2s": ["String", "Action"],
  51. "cast_v2s": ["String", "Element"],
  52. "cast_id2s": ["String", "Element"],
  53. "list_insert": ["Element", "Element", "Integer", "Element"],
  54. "list_delete": ["Element", "Element", "Integer"],
  55. "dict_add_fast": ["Element", "Element", "Element", "Element"],
  56. "dict_delete": ["Element", "Element", "Element"],
  57. "dict_delete_node": ["Element", "Element", "Element"],
  58. "dict_read": ["Element", "Element", "Element"],
  59. "dict_read_edge": ["Element", "Element", "Element"],
  60. "dict_read_node": ["Element", "Element", "Element"],
  61. "dict_in": ["Boolean", "Element", "Element"],
  62. "dict_in_node": ["Boolean", "Element", "Element"],
  63. "dict_keys": ["Element", "Element"],
  64. "set_remove": ["Element", "Element", "Element"],
  65. "set_remove_node": ["Element", "Element", "Element"],
  66. "set_in": ["Boolean", "Element", "Element"],
  67. "set_in_node": ["Boolean", "Element", "Element"],
  68. "is_physical_int": ["Boolean", "Element"],
  69. "is_physical_boolean": ["Boolean", "Element"],
  70. "is_physical_string": ["Boolean", "Element"],
  71. "is_physical_action": ["Boolean", "Element"],
  72. "is_physical_float": ["Boolean", "Element"],
  73. "create_node": ["Element"],
  74. "create_edge": ["Element", "Element", "Element"],
  75. "create_value": ["Element", "Element"],
  76. "is_edge": ["Boolean", "Element"],
  77. "read_nr_out": ["Integer", "Element"],
  78. "read_out": ["Element", "Element", "Integer"],
  79. "read_nr_in": ["Integer", "Element"],
  80. "read_in": ["Element", "Element", "Integer"],
  81. "read_edge_src": ["Element", "Element"],
  82. "read_edge_dst": ["Element", "Element"],
  83. "delete_element": ["Element", "Element"],
  84. "element_eq": ["Boolean", "Element", "Element"],
  85. "read_root": ["Element"],
  86. "read_taskroot": ["Element"],
  87. "log": ["String", "String"],
  88. "time": ["Float"],
  89. "hash": ["String", "String"],
  90. "__sleep": ["Float", "Float", "Boolean"],
  91. }
  92. jit_primitives = {
  93. "get_jit_enabled": ["Boolean"],
  94. "set_jit_enabled": ["Void", "Boolean"]
  95. }
  96. ### Actual script to generate the file
  97. import os
  98. import sys
  99. class Writer(object):
  100. def __init__(self, file_a, file_b):
  101. self.file_a = file_a
  102. self.file_b = file_b
  103. def write(self, text, both=True):
  104. self.file_a.write(text)
  105. if both:
  106. self.file_b.write(text)
  107. try:
  108. with gzip.GzipFile("bootstrap/bootstrap.m.gz", "wb", mtime=0) as fa:
  109. with gzip.GzipFile("bootstrap/minimal.m.gz", "wb", mtime=0) as fb:
  110. f = Writer(fa, fb)
  111. # Create the root first
  112. f.write("Node root()\n")
  113. # Create all children of the root
  114. for node in root:
  115. f.write("Node %s()\n" % node)
  116. f.write("Dict (root, \"%s\", %s)\n" % (node, node))
  117. def declare_primitive_class(primitive_class_name, primitive_decls):
  118. f.write("Node %s()\n" % primitive_class_name)
  119. f.write("Dict (__hierarchy, \"%s\", %s)\n" % (primitive_class_name, primitive_class_name))
  120. # Define all primitive functions
  121. for function, parameters in primitive_decls.iteritems():
  122. f.write("Node _func_signature_%s()\n" % function)
  123. f.write("Node _func_params_%s()\n" % function)
  124. f.write("Node _func_body_%s()\n" % function)
  125. f.write('Dict (%s, "%s", _func_signature_%s)\n' % (primitive_class_name, function, function))
  126. f.write('Dict (_func_signature_%s, "body", _func_body_%s)\n' % (function, function))
  127. f.write('Dict (_func_signature_%s, "params", _func_params_%s)\n' % (function, function))
  128. parameter_names = "abcdefghijklmnopqrstuvwxyz"
  129. for number, param in enumerate(parameters[1:]):
  130. param_encoding = "%s_%s" % (function, parameter_names[number])
  131. f.write("Node _func_params_%s()\n" % (param_encoding))
  132. f.write('Node _name_%s("%s")\n' % (param_encoding, parameter_names[number]))
  133. f.write("Edge _param_link_%s(_func_params_%s, _func_params_%s)\n" % (param_encoding, function, param_encoding))
  134. f.write("Edge _param_link_str_%s(_param_link_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  135. f.write('Node _name_str_%s("name")\n' % param_encoding)
  136. f.write("Edge _param_name_%s(_func_params_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  137. f.write("Edge _param_name_str_%s(_param_name_%s, _name_str_%s)\n" % (param_encoding, param_encoding, param_encoding))
  138. declare_primitive_class('primitives', primitives)
  139. declare_primitive_class('jit', jit_primitives)
  140. # Create the initial task
  141. f.write("Node task_root()\n")
  142. for data in task_data:
  143. f.write("Node task_%s()\n" % data)
  144. f.write('Dict (task_root, "%s", task_%s)\n' % (data, data))
  145. for data in task_frame:
  146. f.write("Node task_%s()\n" % data)
  147. f.write('Dict (task_frame, "%s", task_%s)\n' % (data, data))
  148. # Add last_input and last_output links
  149. for data in ["input", "output"]:
  150. f.write('Dict (task_root, "last_%s", task_%s)\n' % (data, data))
  151. # Bind task to the root
  152. f.write('Dict (root, "%s", task_root)\n' % (task_manager))
  153. def compile_code_AL(filename, target, prepend="", main=False):
  154. import sys
  155. sys.path.append("interface/HUTN/")
  156. from hutn_compiler.compiler import main as compile_code
  157. code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"])
  158. return code.replace("auto_initial_IP", target)
  159. # Create all library code
  160. # But first create the structure to hold compiled data
  161. f.write("Node __objects()\n", both=False)
  162. f.write('Dict (__hierarchy, "objects", __objects)\n', both=False)
  163. def compile_code_MO(filename, model_name):
  164. import sys
  165. sys.path.append("interface/HUTN/")
  166. from hutn_compiler.compiler import main as compile_code
  167. model_code = compile_code(filename, "interface/HUTN/grammars/modelling_bootstrap.g", "MB", ["--modelname:%s" % model_name])
  168. return model_code + "\n"
  169. # Compile all model definitions to ALC directly
  170. total_alc = ""
  171. binding_alc = 'Void function initialize_MMs():\n\tinitialize_SCD("models/SimpleClassDiagrams")\n'
  172. bootstrap_models = sorted(glob.glob("bootstrap/*.mvc"))
  173. for bootstrap_model in bootstrap_models:
  174. # Compile the subfile
  175. bootstrap_model = bootstrap_model.replace("\\", "/")
  176. model_name = bootstrap_model.rsplit(".mvc", 1)[0].rsplit("/", 1)[1]
  177. print("[MVC] %s" % model_name)
  178. alc = compile_code_MO(bootstrap_model, model_name)
  179. total_alc += alc
  180. binding_alc += "\tinitialize_%s()\n" % model_name
  181. # Write out the ALC to a new .metamodels.alc
  182. binding_alc += "\treturn!\n"
  183. new_metamodels_alc = open("bootstrap/metamodels.alt", 'r').read() + total_alc + binding_alc
  184. # Now overwrite the .metamodels.alc file
  185. with open("bootstrap/.metamodels.alc", "w") as mm:
  186. mm.write(new_metamodels_alc)
  187. # Compile all files and add to structure manually
  188. bootstrap_files = sorted(glob.glob("bootstrap/*.alc") + glob.glob("bootstrap/.*.alc"))
  189. for bootstrap_file in bootstrap_files:
  190. # Compile the subfile
  191. bootstrap_file = bootstrap_file.replace("\\", "/")
  192. print("[ALC] %s" % bootstrap_file)
  193. f.write(compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, main = bootstrap_file in [initial_code_manager, initial_code_task]), both=False)
  194. # Now link the code with the compilation manager structure
  195. f.write("Node elem()\n", both=False)
  196. f.write('Dict (__objects, "%s", elem)\n' % bootstrap_file, both=False)
  197. f.write('Dict (elem, "initializers", %s_initial_IP)\n' % bootstrap_file, both=False)
  198. # Create code for initial task
  199. print("[BOOT] task_manager")
  200. f.write('Dict (task_frame, "IP", %s_initial_IP)\n' % (initial_code_manager), both=False)
  201. f.write('Node __phase("init")\n', both=False)
  202. f.write('Dict (task_frame, "phase", __phase)\n', both=False)
  203. # Create code for new tasks to start at
  204. print("[BOOT] new_task")
  205. f.write('Dict (__hierarchy, "__IP", %s_initial_IP)\n' % initial_code_task, both=False)
  206. except:
  207. os.remove("bootstrap/bootstrap.m.gz")
  208. os.remove("bootstrap/minimal.m.gz")
  209. raise