Forráskód Böngészése

Semi-working CTCBD to DTCBD

Yentl Van Tendeloo 7 éve
szülő
commit
534c9d431a

+ 7 - 4
bootstrap/transform.alc

@@ -299,7 +299,7 @@ Element function match(host_model : Element, schedule_model : Element, LHS : Str
 	set_add_node(mappings, initial_mapping)
 	while (list_len(schedule) > 0):
 		current_element = list_pop(schedule, list_len(schedule) - 1)
-		//log("Binding element with label " + cast_value(read_attribute(schedule_model, current_element, "label")))
+		log("Binding element with label " + cast_value(read_attribute(schedule_model, current_element, "label")))
 		new_mappings = dict_create()
 
 		while (set_len(mappings) > 0):
@@ -313,7 +313,7 @@ Element function match(host_model : Element, schedule_model : Element, LHS : Str
 				set_add_node(new_mappings, new_map)
 
 		mappings = new_mappings
-		//log("Remaining options: " + cast_value(set_len(mappings)))
+		log("Remaining options: " + cast_value(set_len(mappings)))
 
 		if (set_len(mappings) == 0):
 			// Stop because we have no more options remaining!
@@ -455,6 +455,9 @@ Void function rewrite(host_model : Element, schedule_model : Element, RHS : Stri
 	while (set_len(labels_to_remove) > 0):
 		// Remove the elements linked to these labels
 		label = set_pop(labels_to_remove)
+		log("Remove element of type " + read_type(host_model, mapping[label]))
+		log("Identifier: " + cast_value(mapping[label]))
+		log("Label: " + cast_value(label))
 		model_delete_element(host_model, mapping[label])
 		dict_delete(new_mapping, label)
 
@@ -554,7 +557,7 @@ Boolean function transform_forall(host_model : Element, schedule_model : Element
 	Element mapping
 	Boolean result
 
-	//log("Executing rule: " + current)
+	log("Executing rule: " + current)
 
 	mappings = full_match(host_model, schedule_model, current, False)
 
@@ -563,7 +566,7 @@ Boolean function transform_forall(host_model : Element, schedule_model : Element
 	else:
 		result = False
 
-	//log("Matches in forall: " + cast_string(set_len(mappings)))
+	log("Matches in forall: " + cast_string(set_len(mappings)))
 	while (set_len(mappings) > 0):
 		mapping = set_pop(mappings)
 		RHS = set_pop(allAssociationDestinations(schedule_model, current, "RHSLink"))

+ 9 - 4
bootstrap/typing.alc

@@ -54,15 +54,20 @@ Void function set_type_mapping(model : Element, type_mapping_model : Element):
 
 Element function elements_typed_by(model : Element, type_name : String):
 	Element result
-	result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
+	Element temp_result
+	String temp
+
+	result = set_create()
+	temp_result = reverseKeyLookupMulti(get_type_mapping_as_dict(model), type_name)
+	while (set_len(temp_result) > 0):
+		temp = set_pop(temp_result)
+		if (dict_in(model["model"], temp)):
+			set_add(result, temp)
 	return result!
 
 Element function get_type_mapping_as_dict(model : Element):
 	return model["type_mapping"]!
 
-Element function get_elements_typed_by(model : Element, type : String):
-	return reverseKeyLookupMulti(model["type_mapping"], type)!
-
 String function read_type(model : Element, name : String):
 	String result
 	Element tm

+ 42 - 41
models/ctcbd_toPartialRuntime.mvc

@@ -3,9 +3,8 @@ include "modelling.alh"
 include "object_operations.alh"
 
 Composite schedule {
-    {Contains} Failure failure {}
     {Contains} Success success {}
-    {Contains} ForAll expand {
+    {Contains} Atomic expand {
         LHS {
             Pre_Design/Block expand_lhs_0 {
                 label = "0"
@@ -42,10 +41,16 @@ Composite schedule {
             Post_Design/Block expand_rhs_3 {
                 label = "3"
             }
+            Post_Design/InitialCondition (expand_rhs_3, expand_rhs_1) {
+                label = "4"
+            }
+            Post_Design/Link (expand_rhs_0, expand_rhs_1) {
+                label = "5"
+            }
             Post_Design/ConstantBlock expand_rhs_7 {
                 label = "7"
-                value = $
-                            Integer function value(model : Element, name : String, mapping : Element):
+                value_value = $
+                            Float function value(model : Element, name : String, mapping : Element):
                                 return 0.01!
                         $
             }
@@ -63,8 +68,8 @@ Composite schedule {
             }
             Post_Design/ConstantBlock expand_rhs_12 {
                 label = "12"
-                value = $
-                            Integer function value(model : Element, name : String, mapping : Element):
+                value_value = $
+                            Float function value(model : Element, name : String, mapping : Element):
                                 return 0!
                         $
             }
@@ -109,10 +114,10 @@ Composite schedule {
             Pre_Design/Block opt_inv_lhs_2 {
                 label = "2"
             }
-            Pre_Design/Link opt_inv_lhs_3 {
+            Pre_Design/Link (opt_inv_lhs_0, opt_inv_lhs_1) {
                 label = "3"
             }
-            Pre_Design/Link opt_inv_lhs_4 {
+            Pre_Design/Link (opt_inv_lhs_1, opt_inv_lhs_2) {
                 label = "4"
             }
         }
@@ -131,7 +136,7 @@ Composite schedule {
             }
             Post_Design/ConstantBlock opt_inv_rhs_5 {
                 label = "5"
-                value = $
+                value_value = $
                             Float function value(model : Element, name : String, mapping : Element):
                                 return float_division(1, read_attribute(model, mapping["0"], "value"))!
                         $
@@ -153,10 +158,10 @@ Composite schedule {
             Pre_Design/Block opt_neg_lhs_2 {
                 label = "2"
             }
-            Pre_Design/Link opt_neg_lhs_3 {
+            Pre_Design/Link (opt_neg_lhs_0, opt_neg_lhs_1) {
                 label = "3"
             }
-            Pre_Design/Link opt_neg_lhs_4 {
+            Pre_Design/Link (opt_neg_lhs_1, opt_neg_lhs_2) {
                 label = "4"
             }
         }
@@ -175,7 +180,7 @@ Composite schedule {
             }
             Post_Design/ConstantBlock opt_neg_rhs_5 {
                 label = "5"
-                value = $
+                value_value = $
                             Float function value(model : Element, name : String, mapping : Element):
                                 return float_subtraction(0, read_attribute(model, mapping["0"], "value"))!
                         $
@@ -187,20 +192,18 @@ Composite schedule {
     }
 
     {Contains} ForAll remove_blocks {
-        NAC {
-            Pre_Design/Block rem_nac_0 {
-                label = "0"
-            }
-            Pre_Design/Block rem_nac_1 {
-                label = "1"
-            }
-            Pre_Design/Link rem_nac_2 {
-                label = "2"
-            }
-        }
         LHS {
             Pre_Design/Block rem_lhs_0 {
                 label = "0"
+                constraint = $
+                                Boolean function constraint(model : Element, name : String):
+                                    if (set_len(allOutgoingAssociationInstances(model, name, "Design/Link")) > 0):
+                                        return False!
+                                    elif (set_len(allOutgoingAssociationInstances(model, name, "Design/InitialCondition")) > 0):
+                                        return False!
+                                    else:
+                                        return read_type(model, name) != "Design/ProbeBlock"!
+                             $
             }
         }
         RHS {}
@@ -251,7 +254,7 @@ Composite schedule {
             }
             Post_Design/ConstantBlock opt_add_rhs_7 {
                 label = "7"
-                value = $
+                value_value = $
                             Float function value(model : Element, name : String, mapping : Element):
                                 return float_addition(read_attribute(model, mapping["0"], "value"),  read_attribute(model, mapping["1"], "value"))!
                         $
@@ -307,7 +310,7 @@ Composite schedule {
             }
             Post_Design/ConstantBlock opt_mult_rhs_7 {
                 label = "7"
-                value = $
+                value_value = $
                             Float function value(model : Element, name : String, mapping : Element):
                                 return float_multiplication(read_attribute(model, mapping["0"], "value"), read_attribute(model, mapping["1"], "value"))!
                         $
@@ -378,23 +381,28 @@ Composite schedule {
                             Element all_links
 
                             all_blocks = allInstances(model, "Design/Block")
+                            log("Translating models: " + cast_value(set_len(all_blocks)))
                             while (set_len(all_blocks) > 0):
                                 element_name = set_pop(all_blocks)
+                                log("Check " + element_name)
                                 mm_type_name = "PartialRuntime/" + cast_string(list_read(string_split(read_type(model, element_name), "/"), 1))
 
                                 if (set_len(allOutgoingAssociationInstances(model, element_name, "D2P_block")) == 0):
                                     // New design element, so create in partial runtime model as well
                                     new_element_name = instantiate_node(model, mm_type_name, "")
                                     instantiate_link(model, "D2P_block", "", element_name, new_element_name)
+                                    log("New element: " + mm_type_name)
 
                                 // Always update the value of attributes of PartialRuntime
                                 new_element_name = map_D2P(model, element_name)
                                 if (mm_type_name == "PartialRuntime/ConstantBlock"):
                                     instantiate_attribute(model, new_element_name, "value", read_attribute(model, element_name, "value"))
+                                    log("Copied value: " + cast_value(read_attribute(model, element_name, "value")))
                                 elif (mm_type_name == "PartialRuntime/ProbeBlock"):
                                     instantiate_attribute(model, new_element_name, "name", read_attribute(model, element_name, "name"))
 
                             all_blocks = allInstances(model, "PartialRuntime/Block")
+                            log("All created blocks: " + cast_value(set_len(all_blocks)))
                             while (set_len(all_blocks) > 0):
                                 element_name = set_pop(all_blocks)
                                 if (set_len(allIncomingAssociationInstances(model, element_name, "D2P_block")) == 0):
@@ -414,13 +422,18 @@ Composite schedule {
                             all_links = allInstances(model, "Design/Link")
                             while (set_len(all_links) > 0):
                                 element_name = set_pop(all_links)
+                                log("Recreate link " + element_name)
+                                log("Source type: " + read_type(model, readAssociationSource(model, element_name)))
                                 instantiate_link(model, "PartialRuntime/Link", "", map_D2P(model, readAssociationSource(model, element_name)), map_D2P(model, readAssociationDestination(model, element_name)))
 
                             all_links = allInstances(model, "Design/InitialCondition")
                             while (set_len(all_links) > 0):
                                 element_name = set_pop(all_links)
+                                log("Recreate initial condition " + element_name)
                                 instantiate_link(model, "PartialRuntime/InitialCondition", "", map_D2P(model, readAssociationSource(model, element_name)), map_D2P(model, readAssociationDestination(model, element_name)))
 
+                            log("Finished translation")
+
                             return!
 
                         String function map_D2P(model : Element, name : String):
@@ -441,19 +454,7 @@ Composite schedule {
 
 Initial (schedule, expand) {}
 
-OnSuccess (expand, optimize_inverse) {}
-OnSuccess (optimize_inverse, optimize_inverse) {}
-OnSuccess (optimize_negator, optimize_inverse) {}
-OnSuccess (optimize_adder, optimize_inverse) {}
-OnSuccess (optimize_multiplier, optimize_inverse) {}
-OnSuccess (optimize_constant, optimize_inverse) {}
-
-OnFailure (expand, optimize_inverse) {}
-OnFailure (optimize_inverse, optimize_negator) {}
-OnFailure (optimize_negator, optimize_adder) {}
-OnFailure (optimize_adder, optimize_multiplier) {}
-OnFailure (optimize_multiplier, optimize_constant) {}
-OnFailure (optimize_constant, remove_blocks) {}
-
-OnSuccess (remove_blocks, success) {}
-OnFailure (remove_blocks, success) {}
+OnSuccess (expand, expand) {}
+OnFailure (expand, remove_blocks) {}
+OnSuccess (remove_blocks, map_to_partial) {}
+OnSuccess (map_to_partial, success) {}

+ 4 - 1
models/dtcbd_simulate.alc

@@ -326,9 +326,10 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 
 			// Execute "block"
 			blocktype = read_type(model, block)
+			log("Execute block " + block + " : " + blocktype)
 			incoming = set_copy(inputs[block])
 			if (blocktype == "FullRuntime/ConstantBlock"):
-				signal = read_attribute(model, block, "value")
+				signal = cast_float(read_attribute(model, block, "value"))
 			elif (blocktype == "FullRuntime/AdditionBlock"):
 				signal = 0.0
 				while (set_len(incoming) > 0):
@@ -356,6 +357,7 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 					incoming = allAssociationOrigins(model, block, "FullRuntime/InitialCondition")
 					while (set_len(incoming) > 0):
 						selected = set_pop(incoming)
+						log("Reading signal from block: " + selected + " : " + read_type(model, selected))
 						signal = cast_float(read_attribute(model, selected, "signal"))
 				else:
 					signal = read_attribute(model, block, "last_in")
@@ -372,6 +374,7 @@ Float function step_simulation(model : Element, schedule : Element, time : Float
 		block = set_pop(memory_blocks)
 		// Update memory
 		incoming = set_copy(inputs[block])
+		log("Update memory of " + block)
 		while (set_len(incoming) > 0):
 			selected = set_pop(incoming)
 			instantiate_attribute(model, block, "last_in", cast_float(read_attribute(model, selected, "signal")))

+ 10 - 10
models/live_modelling.py

@@ -31,12 +31,11 @@ transformation_add_AL({}, {}, "models/DTCBD/restartSim", open("models/cbd_restar
 model_add("models/live_modelling_DTCBD", "formalisms/ProcessModel", open("models/pm_live_DTCBD.mvc", 'r').read())
 """
 
-"""
 ### live modelling CTCBD
 
 model_add("formalisms/CTCBD/Design_MM", "formalisms/SimpleClassDiagrams", open("models/ctcbd_design.mvc", 'r').read())
-#model_add("formalisms/DTCBD/PartialRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/dtcbd_partial_runtime.mvc", 'r').read())
-#model_add("formalisms/DTCBD/FullRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/dtcbd_runtime.mvc", 'r').read())
+model_add("formalisms/DTCBD/PartialRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/dtcbd_partial_runtime.mvc", 'r').read())
+model_add("formalisms/DTCBD/FullRuntime_MM", "formalisms/SimpleClassDiagrams", open("models/dtcbd_runtime.mvc", 'r').read())
 
 model_add("models/CTCBD_model", "formalisms/CTCBD/Design_MM", open("models/ctcbd_spring.mvc", 'r').read())
 
@@ -47,16 +46,16 @@ def trace_D2P(model):
 
 transformation_add_MT({"Design": "formalisms/CTCBD/Design_MM", "PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM"}, {"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM"}, "models/CTCBD/toRuntime", open("models/ctcbd_toPartialRuntime.mvc", 'r').read(), trace_D2P)
 
-#def trace_P2F(model):
-#    instantiate(model, "Association", ("PartialRuntime/Block", "FullRuntime/Block"), ID="P2F_block")
-#
-#transformation_add_AL({"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM", "FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"NewFullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/merge", open("models/dtcbd_merge.alc", 'r').read(), trace_P2F)
-#transformation_add_AL({"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/simulate", open("models/dtcbd_simulate.alc", 'r').read())
-#transformation_add_AL({}, {}, "models/DTCBD/restartSim", open("models/cbd_restartSim.alc", 'r').read())
+def trace_P2F(model):
+    instantiate(model, "Association", ("PartialRuntime/Block", "FullRuntime/Block"), ID="P2F_block")
+
+transformation_add_AL({"PartialRuntime": "formalisms/DTCBD/PartialRuntime_MM", "FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"NewFullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/merge", open("models/dtcbd_merge.alc", 'r').read(), trace_P2F)
+transformation_add_AL({"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, {"FullRuntime": "formalisms/DTCBD/FullRuntime_MM"}, "models/DTCBD/simulate", open("models/dtcbd_simulate.alc", 'r').read())
+transformation_add_AL({}, {}, "models/DTCBD/restartSim", open("models/cbd_restartSim.alc", 'r').read())
 
 model_add("models/live_modelling_CTCBD", "formalisms/ProcessModel", open("models/pm_live_CTCBD.mvc", 'r').read())
-"""
 
+"""
 ### live modelling FSA
 
 model_add("formalisms/FSA/Design_MM", "formalisms/SimpleClassDiagrams", open("models/fsa_design.mvc", 'r').read())
@@ -80,3 +79,4 @@ transformation_add_AL({"FullRuntime": "formalisms/FSA/FullRuntime_MM"}, {"FullRu
 transformation_add_AL({}, {}, "models/FSA/restartSim", open("models/cbd_restartSim.alc", 'r').read())
 
 model_add("models/live_modelling_FSA", "formalisms/ProcessModel", open("models/pm_live_FSA.mvc", 'r').read())
+"""