Bläddra i källkod

Make the CBD schedule implicit

Yentl Van Tendeloo 8 år sedan
förälder
incheckning
e12538ef5e
2 ändrade filer med 30 tillägg och 58 borttagningar
  1. 0 14
      integration/code/cbd_runtime.mvc
  2. 30 44
      integration/code/cbd_semantics.alc

+ 0 - 14
integration/code/cbd_runtime.mvc

@@ -65,20 +65,6 @@ SCD CausalBlockDiagrams_Runtime{
         }
     }
 
-    Class Schedule {
-        lower_cardinality = 1
-    }
-
-    Association LinkedBlock(Schedule, Block){
-        target_lower_cardinality = 0
-        target_upper_cardinality = 1
-    }
-
-    Association NextSchedule(Schedule, Schedule){
-        target_lower_cardinality = 0
-        target_upper_cardinality = 1
-    }
-
     Association Link(Block, Block){
     }
     Association InitialCondition(Block, DelayBlock){

+ 30 - 44
integration/code/cbd_semantics.alc

@@ -98,9 +98,6 @@ Element function sanitize(new_runtime_model : Element, old_runtime_model : Eleme
 		current_time = 0
 		termination_time = 10
 
-	create_schedule(new_runtime_model, old_runtime_model, current_time)
-	create_schedule(new_runtime_model, old_runtime_model, -1)
-
 	time = instantiate_node(new_runtime_model, "Time", "time")
 	instantiate_attribute(new_runtime_model, time, "start_time", current_time)
 	instantiate_attribute(new_runtime_model, time, "current_time", current_time)
@@ -108,27 +105,24 @@ Element function sanitize(new_runtime_model : Element, old_runtime_model : Eleme
 
 	return new_runtime_model!
 
-Void function create_schedule(model : Element, old_model : Element, start_time : Integer):
+Element function create_schedule(model : Element, start_time : Integer):
 	Element all_blocks
 	Element visited
 	Element to_visit
 	Element incoming_links
-	String schedule
 	String element_name
 	String link
 	String source
 	String new_schedule
 	Boolean ready
+	Element schedule
 
 	// TODO add algebraic loop detection (not solution...)
 
+	schedule = create_node()
 	all_blocks = allInstances(model, "Block")
 	visited = create_node()
 	to_visit = create_node()
-	if (start_time != -1):
-		schedule = instantiate_node(model, "Schedule", "schedule_init")
-	else:
-		schedule = instantiate_node(model, "Schedule", "schedule_run")
 
 	while (read_nr_out(all_blocks) > 0):
 		element_name = set_pop(all_blocks)
@@ -138,10 +132,10 @@ Void function create_schedule(model : Element, old_model : Element, start_time :
 		while (list_len(to_visit) > 0):
 			element_name = list_read(to_visit, list_len(to_visit) - 1)
 			if (reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][element_name])) == "DelayBlock"):
-				if (dict_in(old_model["model"], element_name)):
-					incoming_links = create_node()
-				else:
+				if (element_eq(read_attribute(model, element_name, "memory"), read_root())):
 					incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
+				else:
+					incoming_links = create_node()
 			else:
 				incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
 			ready = True
@@ -154,17 +148,12 @@ Void function create_schedule(model : Element, old_model : Element, start_time :
 					ready = False
 
 			if (ready):
-				new_schedule = instantiate_node(model, "Schedule", "")
-				instantiate_link(model, "LinkedBlock", "", schedule, element_name)
-				instantiate_link(model, "NextSchedule", "", schedule, new_schedule)
-				schedule = new_schedule
+				list_append(schedule, element_name)
 				list_delete(to_visit, list_len(to_visit) - 1)
 				set_add(visited, element_name)
-				log("Scheduled " + element_name)
-			else:
-				log("Delay " + element_name)
 
-	return !
+	log("Schedule length: " + cast_v2s(read_nr_out(schedule)))
+	return schedule!
 
 String function readType(model : Element, name : String):
 	return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
@@ -192,25 +181,10 @@ Void function list_CBD(model : Element):
 		elem = set_pop(all_elements)
 		output((("    " + reverseKeyLookup(model["model"], read_edge_src(model["model"][elem]))) + " --> ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][elem])))
 
-	output(("Schedule (t = " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
-	elem = "schedule_init"
-	while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
-		block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
-		output("    " + block)
-		elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
-
-	output(("Schedule (t > " + cast_v2s(read_attribute(model, "time", "start_time"))) + "):")
-	elem = "schedule_run"
-	while (read_nr_out(allOutgoingAssociationInstances(model, elem, "LinkedBlock")) > 0):
-		block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
-		output("    " + block)
-		elem = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
-
 	return !
 
-Void function step_simulation(model : Element):
+Void function step_simulation(model : Element, schedule : Element):
 	String time
-	String schedule
 	Float signal
 	Element incoming
 	String selected
@@ -219,18 +193,16 @@ Void function step_simulation(model : Element):
 	String blocktype
 	String output_string
 	Element delay_blocks
+	Integer i
 
 	time = "time"
-	if (read_attribute(model, time, "current_time") == read_attribute(model, time, "start_time")):
-		schedule = "schedule_init"
-	else:
-		schedule = "schedule_run"
 
 	delay_blocks = create_node()
 	output_string = "("
-	while (read_nr_out(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")) > 0):
-		block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")))
-		schedule = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, schedule, "NextSchedule")))
+	i = 0
+	while (i < read_nr_out(schedule)):
+		block = list_read(schedule, i)
+		i = i + 1
 
 		// Execute "block"
 		blocktype = readType(model, block)
@@ -303,11 +275,18 @@ Void function execute_cbd(design_model : Element):
 	Element old_runtime_model
 	String cmd
 	Boolean running
+	Element schedule_init
+	Element schedule_run
+	Element schedule
 
 	old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
 	runtime_model = retype_to_runtime(design_model)
 	runtime_model = sanitize(runtime_model, old_runtime_model)
 	running = False
+
+	schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
+	schedule_run = create_schedule(runtime_model, -1)
+
 	while (True):
 		if (running):
 			if (has_input()):
@@ -333,7 +312,11 @@ Void function execute_cbd(design_model : Element):
 			output("  term    -- set termination time")
 		elif (cmd == "step"):
 			// Do a simulation step
-			step_simulation(runtime_model)
+			if (read_attribute(runtime_model, "time", "start_time") == read_attribute(runtime_model, "time", "current_time")):
+				schedule = schedule_init
+			else:
+				schedule = schedule_run
+			step_simulation(runtime_model, schedule)
 		elif (cmd == "term"):
 			set_termination_time(runtime_model)
 		elif (running):
@@ -364,6 +347,9 @@ Void function execute_cbd(design_model : Element):
 				runtime_model = retype_to_runtime(design_model)
 				log("Sanitize")
 				runtime_model = sanitize(runtime_model, old_runtime_model)
+				log("Recreate schedules")
+				schedule_init = create_schedule(runtime_model, read_attribute(runtime_model, "time", "start_time"))
+				schedule_run = create_schedule(runtime_model, -1)
 				log("All done!")
 			elif (cmd == "verify"):
 				verify_result = conformance_scd(runtime_model)