Browse Source

More changes to CBD semantics: added something resembling basic
semantics and added times to runtime

Yentl Van Tendeloo 8 years ago
parent
commit
1defe76f46
2 changed files with 126 additions and 19 deletions
  1. 33 5
      integration/code/cbd_runtime.mvc
  2. 93 14
      integration/code/cbd_semantics.alc

+ 33 - 5
integration/code/cbd_runtime.mvc

@@ -20,7 +20,24 @@ SCD CausalBlockDiagrams_Runtime{
         $
     }
 
-    Class Block{}
+    Class Natural {
+        $
+            if (bool_not(is_physical_int(self))):
+                return "Natural has no integer value at " + name!
+            elif (integer_lt(self, 0)):
+                return "Natural does not have a positive or zero value at " + name!
+            else:
+                return "OK"!
+        $
+    }
+
+
+    Class Block{
+        signal : Float {
+            target_lower_cardinality = 1
+            target_upper_cardinality = 1
+        }
+    }
 
     Class ConstantBlock{
         value : Float {
@@ -40,6 +57,21 @@ SCD CausalBlockDiagrams_Runtime{
         }
     }
 
+    Class Time{
+        lower_cardinality = 1
+        upper_cardinality = 1
+
+        current_time : Natural {
+            target_lower_cardinality = 1
+            target_upper_cardinality = 1
+        }
+
+        termination_time : Natural {
+            target_lower_cardinality = 1
+            target_upper_cardinality = 1
+        }
+    }
+
     Class Schedule {
         lower_cardinality = 1
         active : Boolean {
@@ -59,10 +91,6 @@ SCD CausalBlockDiagrams_Runtime{
     }
 
     Association Link(Block, Block){
-        signal : Float {
-            target_lower_cardinality = 0
-            target_upper_cardinality = 1
-        }
     }
     Association InitialCondition(Block, DelayBlock){
         source_lower_cardinality = 0

+ 93 - 14
integration/code/cbd_semantics.alc

@@ -35,6 +35,7 @@ Element function translate_to_runtime(design_model : Element):
 	String attribute
 	String src
 	String dst
+	String time
 	Element all_attributes
 
 	log("Translating to runtime model!")
@@ -48,6 +49,7 @@ Element function translate_to_runtime(design_model : Element):
 		element_name = set_pop(all_blocks)
 		mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
 		instantiate_node(runtime_model, mm_type_name, element_name)
+		instantiate_attribute(runtime_model, element_name, "signal", 0.0)
 		all_attributes = getAttributeList(design_model, element_name)
 		while (read_nr_out(all_attributes) > 0):
 			attr_name = set_pop(all_attributes)
@@ -82,6 +84,11 @@ Element function translate_to_runtime(design_model : Element):
 
 	log("Solving loops (TODO)")
 
+	log("Creating simulation time")
+	time = instantiate_node(runtime_model, "Time", "time")
+	instantiate_attribute(runtime_model, time, "current_time", 0)
+	instantiate_attribute(runtime_model, time, "termination_time", 100)
+
 	log("DONE!")
 	return runtime_model!
 
@@ -184,29 +191,101 @@ Void function list_CBD(model : Element):
 
 	return !
 
-Void function start(model : Element):
-	// Do a simulation loop, until we receive pause
+Void function step_simulation(model : Element):
+	String time
+	String schedule
+	Float value
+	Element incoming
+	String selected
+
+	time = "time"
+	if (readAttribute(model, time, "current_time") == 0):
+		schedule = "schedule_init"
+	else:
+		schedule = "schedule_run"
+
+	while (read_nr_out(allOutgoingAssociationInstances(model, schedule, "LinkedBlock")) > 0):
+		block = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "LinkedBlock")))
+		schedule = readAssociationDestination(model, set_pop(allOutgoingAssociationInstances(model, elem, "NextSchedule")))
+
+		// Execute "block"
+		blocktype = readType(model, block)
+		if (blocktype == "ConstantBlock"):
+			unset_attribute(model, block, "signal")
+			instantiate_attribute(model, block, "signal", read_attribute(model, block, "value"))
+		elif (blocktype == "AdditionBlock"):
+			unset_attribute(model, block, "signal")
+			value = 0.0
+			incoming = allIncomingAssociationInstances(model, schedule, "Link")
+			while (read_nr_out(incoming) > 0):
+				selected = readAssociationSource(model, set_pop(incoming))
+				value = value + cast_v2f(read_attribute(model, selected, "signal"))
+			instantiate_attribute(model, block, "signal", value)
+
+	// Increase simulation time
+	Integer new_time
+	new_time = cast_v2i(read_attribute(model, time, "current_time")) + 1
+	unset_attribute(model, time, "current_time")
+	instantiate_attribute(model, time, "current_time", new_time)
+
+	return !
+
+Void function set_termination_time(model : Element):
+	unset_attribute(model, "time", "termination_time")
+	instantiate_attribute(model, "time", "termination_time", input())
+	return !
 
 Void function execute_cbd(model : Element):
 	model = translate_to_runtime(model)
 
 	String cmd
+	Boolean running
+	running = False
+
 	while (True):
 		output("Which operation do you want to execute?")
-		cmd = input()
+		if (running):
+			if (has_input()):
+				cmd = input()
+			else:
+				cmd = "start"
+		else:
+			cmd = input()
 
 		if (cmd == "help"):
 			output("Supported operations:")
-			output("  start   -- start simulation")
-			output("  pause   -- pause simulation")
-			output("  modify  -- live modelling: modify model structure")
-			output("  list    -- list blocks and connections")
+			if (bool_not(running)):
+				output("  step    -- do one simulation step")
+				output("  start   -- start simulation")
+				//output("  modify  -- live modelling: modify model structure")
+				output("  list    -- list blocks and connections")
+				output("  exit    -- select another model")
+			else:
+				output("  pause   -- pause simulation")
 			output("  help    -- this information")
-			output("  exit    -- select another model")
-		elif (cmd == "list"):
-			list_CBD(model)
-		elif (cmd == "exit"):
-			return!
+			output("  term    -- set termination time")
+		elif (cmd == "step"):
+			// Do a simulation step
+			step_simulation(model)
+		elif (cmd == "term"):
+			set_termination_time(model)
+		elif (running):
+			if (cmd == "pause"):
+				running = False
+			else:
+				output("Did not understand command!")
+				output("Use 'help' for a list of available options")
 		else:
-			output("Did not understand command!")
-			output("Use 'help' for a list of available options")
+			// Not running
+			if (cmd == "list"):
+				list_CBD(model)
+			elif (cmd == "start"):
+				running = True
+			elif (cmd == "exit"):
+				return!
+			else:
+				output("Did not understand command!")
+				output("Use 'help' for a list of available options")
+
+		if (cast_v2i(read_attribute(model, "time", "current_time")) > cast_v2i(read_attribute(model, "time", "termination_time"))):
+			running = False