|
@@ -3,6 +3,7 @@ include "modelling.alh"
|
|
|
include "object_operations.alh"
|
|
|
include "library.alh"
|
|
|
include "conformance_scd.alh"
|
|
|
+include "mini_modify.alh"
|
|
|
|
|
|
Void function main():
|
|
|
Element model
|
|
@@ -20,11 +21,12 @@ Void function main():
|
|
|
verify_result = conformance_scd(model)
|
|
|
if (verify_result == "OK"):
|
|
|
output("Model OK!")
|
|
|
+ log("Model conforms!")
|
|
|
execute_cbd(model)
|
|
|
else:
|
|
|
output("Non-conforming model: " + verify_result)
|
|
|
|
|
|
-Element function translate_to_runtime(design_model : Element):
|
|
|
+Element function retype_to_runtime(design_model : Element):
|
|
|
Element runtime_model
|
|
|
Element all_blocks
|
|
|
Element all_links
|
|
@@ -47,9 +49,6 @@ Element function translate_to_runtime(design_model : Element):
|
|
|
element_name = instantiate_node(runtime_model, mm_type_name, element_name)
|
|
|
if (mm_type_name == "ConstantBlock"):
|
|
|
instantiate_attribute(runtime_model, element_name, "value", read_attribute(design_model, element_name, "value"))
|
|
|
- elif (mm_type_name == "DelayBlock"):
|
|
|
- instantiate_attribute(runtime_model, element_name, "memory", 0.0)
|
|
|
- instantiate_attribute(runtime_model, element_name, "signal", 0.0)
|
|
|
|
|
|
// Don't merge this together with the block conversion, as the destination block might not exist yet!
|
|
|
all_links = allInstances(design_model, "Link")
|
|
@@ -66,17 +65,52 @@ Element function translate_to_runtime(design_model : Element):
|
|
|
dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
|
|
|
instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
|
|
|
|
|
|
- create_schedule(runtime_model, True)
|
|
|
+ return runtime_model!
|
|
|
|
|
|
- create_schedule(runtime_model, False)
|
|
|
+Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
|
|
|
+ Element all_blocks
|
|
|
+ Element all_links
|
|
|
+ String mm_type_name
|
|
|
+ String element_name
|
|
|
+ String attr_name
|
|
|
+ String attr_value
|
|
|
+ String attribute
|
|
|
+ String time
|
|
|
+ Element all_attributes
|
|
|
+ Integer current_time
|
|
|
+ Integer termination_time
|
|
|
|
|
|
- time = instantiate_node(runtime_model, "Time", "time")
|
|
|
- instantiate_attribute(runtime_model, time, "current_time", 0)
|
|
|
- instantiate_attribute(runtime_model, time, "termination_time", 100)
|
|
|
+ all_blocks = allInstances(new_runtime_model, "Block")
|
|
|
+ while (list_len(all_blocks) > 0):
|
|
|
+ element_name = set_pop(all_blocks)
|
|
|
+ mm_type_name = reverseKeyLookup(new_runtime_model["metamodel"]["model"], dict_read_node(new_runtime_model["type_mapping"], new_runtime_model["model"][element_name]))
|
|
|
+ if (dict_in(old_runtime_model["model"], element_name)):
|
|
|
+ if (mm_type_name == "DelayBlock"):
|
|
|
+ instantiate_attribute(new_runtime_model, element_name, "memory", read_attribute(old_runtime_model, element_name, "memory"))
|
|
|
+ instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
|
|
|
+ else:
|
|
|
+ if (mm_type_name == "DelayBlock"):
|
|
|
+ instantiate_attribute(new_runtime_model, element_name, "memory", 0.0)
|
|
|
+ instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
|
|
|
|
|
|
- return runtime_model!
|
|
|
+ if (dict_in(old_runtime_model["model"], "time")):
|
|
|
+ current_time = read_attribute(old_runtime_model, "time", "current_time")
|
|
|
+ termination_time = read_attribute(old_runtime_model, "time", "termination_time")
|
|
|
+ else:
|
|
|
+ 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)
|
|
|
+ instantiate_attribute(new_runtime_model, time, "termination_time", termination_time)
|
|
|
+
|
|
|
+ return new_runtime_model!
|
|
|
|
|
|
-Void function create_schedule(model : Element, is_time_zero : Boolean):
|
|
|
+Void function create_schedule(model : Element, old_model : Element, start_time : Integer):
|
|
|
Element all_blocks
|
|
|
Element visited
|
|
|
Element to_visit
|
|
@@ -91,7 +125,7 @@ Void function create_schedule(model : Element, is_time_zero : Boolean):
|
|
|
all_blocks = allInstances(model, "Block")
|
|
|
visited = create_node()
|
|
|
to_visit = create_node()
|
|
|
- if (is_time_zero):
|
|
|
+ if (start_time != -1):
|
|
|
schedule = instantiate_node(model, "Schedule", "schedule_init")
|
|
|
else:
|
|
|
schedule = instantiate_node(model, "Schedule", "schedule_run")
|
|
@@ -104,10 +138,10 @@ Void function create_schedule(model : Element, is_time_zero : Boolean):
|
|
|
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 (is_time_zero):
|
|
|
- incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
|
|
|
- else:
|
|
|
+ if (dict_in(old_model["model"], element_name)):
|
|
|
incoming_links = create_node()
|
|
|
+ else:
|
|
|
+ incoming_links = allIncomingAssociationInstances(model, element_name, "InitialCondition")
|
|
|
else:
|
|
|
incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
|
|
|
ready = True
|
|
@@ -155,14 +189,14 @@ 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 (== 0):")
|
|
|
+ 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 (> 0):")
|
|
|
+ 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")))
|
|
@@ -184,7 +218,7 @@ Void function step_simulation(model : Element):
|
|
|
Element delay_blocks
|
|
|
|
|
|
time = "time"
|
|
|
- if (cast_s2i(cast_v2s(read_attribute(model, time, "current_time"))) == 0):
|
|
|
+ if (read_attribute(model, time, "current_time") == read_attribute(model, time, "start_time")):
|
|
|
schedule = "schedule_init"
|
|
|
else:
|
|
|
schedule = "schedule_run"
|
|
@@ -206,7 +240,7 @@ Void function step_simulation(model : Element):
|
|
|
selected = readAssociationSource(model, set_pop(incoming))
|
|
|
signal = signal + cast_s2f(cast_v2s(read_attribute(model, selected, "signal")))
|
|
|
elif (blocktype == "MultiplyBlock"):
|
|
|
- signal = 0.0
|
|
|
+ signal = 1.0
|
|
|
incoming = allIncomingAssociationInstances(model, block, "Link")
|
|
|
while (read_nr_out(incoming) > 0):
|
|
|
selected = readAssociationSource(model, set_pop(incoming))
|
|
@@ -259,15 +293,21 @@ Void function set_termination_time(model : Element):
|
|
|
instantiate_attribute(model, "time", "termination_time", input())
|
|
|
return !
|
|
|
|
|
|
-Void function execute_cbd(model : Element):
|
|
|
+Void function execute_cbd(design_model : Element):
|
|
|
String verify_result
|
|
|
- model = translate_to_runtime(model)
|
|
|
- output("Runtime model constructed OK!")
|
|
|
-
|
|
|
+ Element runtime_model
|
|
|
+ Element old_runtime_model
|
|
|
String cmd
|
|
|
Boolean running
|
|
|
- running = False
|
|
|
|
|
|
+ log("New model")
|
|
|
+ old_runtime_model = instantiate_model(import_node("models/CausalBlockDiagrams_Runtime"))
|
|
|
+ log("Retype")
|
|
|
+ runtime_model = retype_to_runtime(design_model)
|
|
|
+ log("Sanitize")
|
|
|
+ runtime_model = sanitize(runtime_model, old_runtime_model)
|
|
|
+ log("Finished")
|
|
|
+ running = False
|
|
|
while (True):
|
|
|
if (running):
|
|
|
if (has_input()):
|
|
@@ -283,7 +323,7 @@ Void function execute_cbd(model : Element):
|
|
|
if (bool_not(running)):
|
|
|
output(" step -- do one simulation step")
|
|
|
output(" start -- start simulation")
|
|
|
- //output(" modify -- live modelling: modify model structure")
|
|
|
+ output(" modify -- live modelling: modify model structure")
|
|
|
output(" list -- list blocks and connections")
|
|
|
output(" exit -- select another model")
|
|
|
output(" verify -- verify the runtime model")
|
|
@@ -293,9 +333,9 @@ Void function execute_cbd(model : Element):
|
|
|
output(" term -- set termination time")
|
|
|
elif (cmd == "step"):
|
|
|
// Do a simulation step
|
|
|
- step_simulation(model)
|
|
|
+ step_simulation(runtime_model)
|
|
|
elif (cmd == "term"):
|
|
|
- set_termination_time(model)
|
|
|
+ set_termination_time(runtime_model)
|
|
|
elif (running):
|
|
|
if (cmd == "pause"):
|
|
|
running = False
|
|
@@ -305,13 +345,25 @@ Void function execute_cbd(model : Element):
|
|
|
else:
|
|
|
// Not running
|
|
|
if (cmd == "list"):
|
|
|
- list_CBD(model)
|
|
|
+ list_CBD(runtime_model)
|
|
|
elif (cmd == "start"):
|
|
|
running = True
|
|
|
elif (cmd == "exit"):
|
|
|
return!
|
|
|
+ elif (cmd == "modify"):
|
|
|
+ verify_result = "init"
|
|
|
+ while (verify_result != "OK"):
|
|
|
+ modify(design_model)
|
|
|
+ verify_result = conformance_scd(design_model)
|
|
|
+ if (verify_result != "OK"):
|
|
|
+ output("Error in design model: " + verify_result)
|
|
|
+ output("Successfully made modifications to design model!")
|
|
|
+
|
|
|
+ old_runtime_model = runtime_model
|
|
|
+ runtime_model = retype_to_runtime(design_model)
|
|
|
+ runtime_model = sanitize(runtime_model, old_runtime_model)
|
|
|
elif (cmd == "verify"):
|
|
|
- verify_result = conformance_scd(model)
|
|
|
+ verify_result = conformance_scd(runtime_model)
|
|
|
if (verify_result != "OK"):
|
|
|
output("Error in runtime model: " + verify_result)
|
|
|
else:
|
|
@@ -320,5 +372,5 @@ Void function execute_cbd(model : Element):
|
|
|
output("Did not understand command!")
|
|
|
output("Use 'help' for a list of available options")
|
|
|
|
|
|
- if (cast_s2i(cast_v2s(read_attribute(model, "time", "current_time"))) > cast_s2i(cast_v2s(read_attribute(model, "time", "termination_time")))):
|
|
|
+ if (cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "current_time"))) > cast_s2i(cast_v2s(read_attribute(runtime_model, "time", "termination_time")))):
|
|
|
running = False
|