Browse Source

Move HUTN compilation to the Modelverse, instead of in the client

Yentl Van Tendeloo 8 years ago
parent
commit
4a31756fba

+ 13 - 4
bootstrap/compiler.alc

@@ -22,28 +22,37 @@ Element function generic_compile(code : String, port : String):
 
 Element function compile_code(code : String):
 	String port
-	port = comm_connect("compiler")
+	port = ""
+	while (port == ""):
+		port = comm_connect("compiler")
+
 	comm_set(port, "code")
 
 	Element list
 	list = generic_compile(code, port)
 
 	if (element_eq(list, read_root())):
+		log("ERROR IN CODE COMPILATION")
 		return read_root()!
 
 	else:
 		return construct_function_list(list)!
 
-Element function compile_model(code : String, model : Element):
+Element function compile_model(code : String, metamodel : Element):
 	String port
-	port = comm_connect("compiler")
+	port = ""
+	while (port == ""):
+		port = comm_connect("compiler")
+
 	comm_set(port, "model")
 
 	Element list
 	list = generic_compile(code, port)
 
 	if (element_eq(list, read_root())):
+		log("ERROR in model compile")
 		return read_root()!
 
 	else:
-		return construct_model_list(model, list)!
+		log("Creating model...")
+		return construct_model_list(instantiate_model(metamodel), list)!

+ 0 - 12
bootstrap/constructors.alc

@@ -8,18 +8,6 @@ include "utils.alh"
 Element while_stack = ?
 Element variable_map = ?
 
-Element function construct_function():
-	Integer len
-	Element list
-
-	len = input()
-	list = list_create()
-
-	while (list_len(list) < len):
-		list_append(list, input())
-
-	return construct_function_list(list)!
-
 Element function construct_function_list(list : Element):
 	String command
 	Element result

+ 5 - 4
bootstrap/core_algorithm.alc

@@ -12,6 +12,7 @@ include "metamodels.alh"
 include "utils.alh"
 include "conformance_finding.alh"
 include "typing.alh"
+include "compiler.alh"
 
 String core_model_location = "models/core"
 
@@ -998,7 +999,7 @@ String function cmd_model_add(type : String, name : String):
 							return "Type is not typed by formalisms/SimpleClassDiagrams: " + type!
 
 						output("Waiting for model constructors...")
-						new_model = construct_model_raw(mm)
+						new_model = compile_model(input(), mm)
 						model_create(new_model, name, type_id, "Model")
 						return "Success"!
 					else:
@@ -1296,7 +1297,7 @@ String function cmd_model_overwrite(model_name : String, metamodel_name : String
 					return "Metamodel does not conform to formalisms/SimpleClassDiagrams: " + metamodel_name!
 
 				output("Waiting for model constructors...")
-				new_model = construct_model_raw(mm)
+				new_model = compile_model(input(), mm)
 				model_overwrite(new_model, model_id, get_entry_id(metamodel_name))
 				return "Success"!
 			else:
@@ -1518,7 +1519,7 @@ String function transformation_add(source_models : Element, target_models : Elem
 		elif (operation_type == "actionlanguage"):
 			// Finished with all information, now create the model itself!
 			output("Waiting for code constructors...")
-			add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, construct_function())
+			add_code_model(get_full_model(get_entry_id("formalisms/ActionLanguage"), get_entry_id("formalisms/SimpleClassDiagrams")), "AL/" + operation_name, compile_code(input()))
 			model_create(import_node("AL/" + operation_name), operation_name, get_entry_id("formalisms/ActionLanguage"), "ActionLanguage")
 			model_id = get_entry_id(operation_name)
 
@@ -1650,7 +1651,7 @@ String function cmd_transformation_add_MT(source_models : Element, target_models
 		String new_model
 		// Finished with all information, now create the model itself!
 		output("Waiting for model constructors...")
-		new_model = construct_model_raw(get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
+		new_model = compile_model(input(), get_full_model(ramified_metamodel_id, get_entry_id("formalisms/SimpleClassDiagrams")))
 		model_create(new_model, operation_name, ramified_metamodel_id, "ModelTransformation")
 		model_id = get_entry_id(operation_name)
 

+ 3 - 2
bootstrap/mini_modify.alc

@@ -7,6 +7,7 @@ include "io.alh"
 include "metamodels.alh"
 include "modelling.alh"
 include "typing.alh"
+include "compiler.alh"
 
 Boolean verbose = True
 
@@ -87,7 +88,7 @@ String function cmd_upload(write : Boolean, model : Element):
 	Element new_model
 	if (write):
 		output("Waiting for model constructors...")
-		new_model = construct_model_raw(model["metamodel"])
+		new_model = compile_model(input(), model["metamodel"])
 		dict_overwrite(model, "model", new_model["model"])
 		set_type_mapping(model, get_type_mapping(new_model))
 		return "Success"!
@@ -172,7 +173,7 @@ String function cmd_attr_add_code(write : Boolean, model : Element, element_name
 			attrs = getAttributeList(model, element_name)
 			if (set_in(dict_keys(attrs), attr_name)):
 				output("Waiting for code constructors...")
-				instantiate_attribute_code(model, element_name, attr_name, construct_function())
+				instantiate_attribute_code(model, element_name, attr_name, compile_code(input()))
 				return "Success"!
 			else:
 				return "Attribute not found: " + attr_name!

+ 0 - 17
bootstrap/modelling.alc

@@ -537,23 +537,6 @@ String function add_AL(model : Element, element : Element):
 
 	return reverseKeyLookup(model["model"], element)!
 
-Element function construct_model_raw(metamodel : Element):
-	String command
-	Element model
-
-	Integer i
-	Integer count
-	Element lst
-	i = 0
-	count = input()
-	lst = list_create()
-
-	while (i < count):
-		list_append(lst, input())
-		i = i + 1
-	
-	return construct_model_list(instantiate_model(metamodel), lst)!
-
 Element function get_func_AL_model(al_model : Element):
 	Element initial_function
 

+ 1 - 1
interface/HUTN/grammars/actionlanguage.g

@@ -1,5 +1,5 @@
 grammar{
-    start: (include | definition | funcdecl | newline)+;
+    start: (include | definition | funcdecl | newline)*;
 
     statement 
         : (definition newline)

+ 11 - 0
models/SCCD_execute.alc

@@ -633,7 +633,9 @@ Void function execute_actions(model : Element, source_states : Element, target_s
 		if (element_neq(action, read_root())):
 			// Got a script, so execute!
 			action = resolve_function(action, data)
+			log("Executing script 'action'!")
 			action(data["current_class_handle"]["attributes"])
+			log("DONE")
 
 		// Raise events
 		events = allAssociationDestinations(model, state, "SCCD/onExitRaise")
@@ -645,7 +647,9 @@ Void function execute_actions(model : Element, source_states : Element, target_s
 			if (element_neq(parameter_action, read_root())):
 				// Got a parameter to evaluate
 				parameter_action = resolve_function(parameter_action, data)
+				log("Execution script 'parameter_action'!")
 				parameter_action = parameter_action(data["current_class_handle"]["attributes"])
+				log("DONE")
 
 			process_raised_event(model, event, parameter_action, data)
 
@@ -664,7 +668,9 @@ Void function execute_actions(model : Element, source_states : Element, target_s
 		if (element_neq(action, read_root())):
 			// Got a script, so execute!
 			action = resolve_function(action, data)
+			log("Executing script 'act2'!")
 			action(data["current_class_handle"]["attributes"])
+			log("DONE")
 
 		// Raise events
 		events = allAssociationDestinations(model, state, "SCCD/onEntryRaise")
@@ -676,7 +682,9 @@ Void function execute_actions(model : Element, source_states : Element, target_s
 			if (element_neq(parameter_action, read_root())):
 				// Got a parameter to evaluate
 				parameter_action = resolve_function(parameter_action, data)
+				log("Executing script 'param2'!")
 				parameter_action = parameter_action(data["current_class_handle"]["attributes"])
+				log("DONE")
 
 			process_raised_event(model, event, parameter_action, data)
 
@@ -688,7 +696,10 @@ Void function execute_actions(model : Element, source_states : Element, target_s
 		while (set_len(timed_transitions) > 0):
 			transition = set_pop(timed_transitions)
 			after = resolve_function(read_attribute(model, transition, "after"), data)
+			log("Executing script 'after'!")
+			log("Function: " + cast_e2s(after))
 			dict_add(data["current_class_handle"]["timers"], transition, float_addition(data["time_sim"], after(data["current_class_handle"]["attributes"])))
+			log("DONE")
 
 	return !
 

+ 22 - 16
scripts/HUTN_service.py

@@ -14,36 +14,42 @@ init(sys.argv[1])
 print("INITTED HUTN")
 login("HUTN", "HUTN")
 
+def clean_code(code):
+    if code == "":
+        return code
+
+    code_fragments = code.split("\n")
+    code_fragments = [i.rstrip() for i in code_fragments if i.strip() != ""]
+    code_fragments = [i.replace("    ", "\t") for i in code_fragments]
+    initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
+    code_fragments = [i[initial_tabs:] for i in code_fragments]
+    code_fragments.append("")
+    code = "\n".join(code_fragments)
+    return code
+
 def compile_service(port):
     start = time.time()
     temp_file = ".tmp_%s" % port
+
     def compile_AL(code):
-        code_fragments = code.split("\n")
-        code_fragments = [i for i in code_fragments if i.strip() != ""]
-        code_fragments = [i.replace("    ", "\t") for i in code_fragments]
-        initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
-        code_fragments = [i[initial_tabs:] for i in code_fragments]
-        code_fragments.append("")
-        code = "\n".join(code_fragments)
+        code = clean_code(code)
+
+        if code == "":
+            return [0]
 
         with open(temp_file, "w") as f:
             f.write(code)
             f.flush()
 
-        ss = time.time()
         compiled = do_compile(temp_file, COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
-        print("    Compile: %ss" % (time.time() - ss))
         os.remove(temp_file)
         return compiled
 
     def compile_model(code):
-        code_fragments = code.split("\n")
-        code_fragments = [i for i in code_fragments if i.strip() != ""]
-        code_fragments = [i.replace("    ", "\t") for i in code_fragments]
-        initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
-        code_fragments = [i[initial_tabs:] for i in code_fragments]
-        code_fragments.append("")
-        code = "\n".join(code_fragments)
+        code = clean_code(code)
+
+        if code == "":
+            return [0]
 
         with open(temp_file, "w") as f:
             f.write(code)

+ 1 - 1
scripts/prompt.py

@@ -31,7 +31,7 @@ local_print("Welcome to the local shell!")
 try:
     address = sys.argv[1]
 except IndexError:
-    address = "127.0.0.1:8001"
+    address = "http://127.0.0.1:8001"
 try:
     taskname = sys.argv[2]
 except IndexError:

+ 1 - 1
scripts/run_basic_tests.py

@@ -13,4 +13,4 @@ subprocess.check_call([sys.executable, "-m", "pytest"], cwd="kernel")
 
 subprocess.check_call([sys.executable, "-m", "pytest"], cwd="interface/HUTN")
 
-subprocess.check_call([sys.executable, "-m", "pytest", "unit", "-x", "-v"])
+subprocess.check_call([sys.executable, "-m", "pytest", "unit", "-x", "-v", "-s"])

+ 23 - 14
wrappers/modelverse.py

@@ -409,11 +409,12 @@ def login(username, password):
     else:
         raise InterfaceMismatch(_last_output())
 
-def model_add(model_name, metamodel_name, model_code=None):
+def model_add(model_name, metamodel_name, model_code=""):
     """Instantiate a new model."""
     _goto_mode(MODE_MODELLING)
 
     # Do this before creating the model, as otherwise compilation errors would make us inconsistent
+    """
     if model_code is not None:
         try:
             compiled = _compile_model(model_code)
@@ -421,22 +422,27 @@ def model_add(model_name, metamodel_name, model_code=None):
             raise CompilationError(e)
     else:
         compiled = [0]
+    """
 
     _input(["model_add", metamodel_name, model_name])
     _handle_output("Waiting for model constructors...")
-    _input(compiled)
+    _input(model_code)
     _output("Success")
 
     global registered_metamodels
     registered_metamodels[model_name] = metamodel_name
 
 def upload_code(code):
+    _input(code)
+
+    """
     try:
         compiled = _compile_AL(code)
     except Exception as e:
         raise CompilationError(e)
 
     _input(compiled)
+    """
 
 def model_delete(model_name):
     """Delete an existing model."""
@@ -475,10 +481,11 @@ def verify(model_name, metamodel_name=None):
     _input(["verify", model_name, metamodel_name])
     return _handle_output("Success: ", split=True)[0]
 
-def model_overwrite(model_name, new_model=None, metamodel_name=None):
+def model_overwrite(model_name, new_model="", metamodel_name=None):
     """Upload a new model and overwrite an existing model."""
     _goto_mode(MODE_MODIFY, model_name)
 
+    """
     if new_model is not None:
         try:
             compiled = _compile_model(new_model)
@@ -486,10 +493,11 @@ def model_overwrite(model_name, new_model=None, metamodel_name=None):
             raise CompilationError(e)
     else:
         compiled = [0]
+    """
 
     _input("upload")
     _handle_output("Waiting for model constructors...")
-    _input(compiled)
+    _input(new_model)
     _output("Success")
 
     if metamodel_name is not None:
@@ -528,15 +536,13 @@ def transformation_add_MT(source_metamodels, target_metamodels, operation_name,
     """Create a new model transformation."""
     global mode
     _goto_mode(MODE_MODELLING)
-    import time
 
-    start = time.time()
+    """
     try:
         compiled = _compile_model(code)
     except Exception as e:
         raise CompilationError(e)
-    #print("Compilation took: %ss" % (time.time() - start))
-    start = time.time()
+    """
 
     mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
     _input(["transformation_add_MT"] + mv_dict_rep + [operation_name])
@@ -548,24 +554,23 @@ def transformation_add_MT(source_metamodels, target_metamodels, operation_name,
         callback()
         _input("exit")
         mode = MODE_MODELLING
-    #print("Callbacks took: %ss" % (time.time() - start))
-    start = time.time()
 
     # Done, so RAMify and upload the model
     _handle_output("Waiting for model constructors...")
-    _input(compiled)
+    _input(code)
     _handle_output("Success")
-    #print("Upload and RAMify took: %ss" % (time.time() - start))
 
 def transformation_add_AL(source_metamodels, target_metamodels, operation_name, code, callback=lambda: None):
     """Create a new action language model, which can be executed."""
     global mode
     _goto_mode(MODE_MODELLING)
 
+    """
     try:
         compiled = _compile_AL(code)
     except Exception as e:
         raise CompilationError(e)
+    """
 
     mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
     _input(["transformation_add_AL"] + mv_dict_rep + [operation_name])
@@ -579,7 +584,8 @@ def transformation_add_AL(source_metamodels, target_metamodels, operation_name,
         mode = MODE_MODELLING
 
     _handle_output("Waiting for code constructors...")
-    _input(compiled)
+    #_input(compiled)
+    _input(code)
     _output("Success")
 
 def transformation_add_MANUAL(source_metamodels, target_metamodels, operation_name, callback=lambda: None):
@@ -916,16 +922,19 @@ def attr_assign(model_name, ID, attr, value):
 def attr_assign_code(model_name, ID, attr, code):
     """Assign a piece of Action Language code to the attribute"""
     _check_type(code)
+    """
     try:
         compiled = _compile_AL(code)
     except Exception as e:
         raise CompilationError(e)
+    """
 
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["attr_add_code", ID, attr])
     _handle_output("Waiting for code constructors...")
-    _input(compiled)
+    #_input(compiled)
+    _input(code)
     _output("Success")
 
 def attr_delete(model_name, ID, attr):