|
@@ -0,0 +1,127 @@
|
|
|
+include "primitives.alh"
|
|
|
+include "modelling.alh"
|
|
|
+include "object_operations.alh"
|
|
|
+include "library.alh"
|
|
|
+include "conformance_scd.alh"
|
|
|
+
|
|
|
+Void function main():
|
|
|
+ Element model
|
|
|
+ String verify_result
|
|
|
+
|
|
|
+ while (True):
|
|
|
+ output("Which model do you want to execute with CBD semantics?")
|
|
|
+ model = import_node(input())
|
|
|
+
|
|
|
+ if (element_eq(model, read_root())):
|
|
|
+ output("Could not find model; aborting")
|
|
|
+ elif (element_neq(model["metamodel"], import_node("models/CausalBlockDiagrams_Design"))):
|
|
|
+ output("Not a CBD design model; aborting")
|
|
|
+ else:
|
|
|
+ verify_result = conformance_scd(model)
|
|
|
+ if (verify_result == "OK"):
|
|
|
+ output("Model OK!")
|
|
|
+ execute_cbd(model)
|
|
|
+ else:
|
|
|
+ output("Non-conforming model: " + verify_result)
|
|
|
+
|
|
|
+Void function translate_to_runtime(design_model : Element):
|
|
|
+ Element runtime_model
|
|
|
+ Element all_blocks
|
|
|
+ Element all_links
|
|
|
+ String mm_type_name
|
|
|
+ String element_name
|
|
|
+ String attr_name
|
|
|
+ String attr_value
|
|
|
+ String attribute
|
|
|
+ String src
|
|
|
+ String dst
|
|
|
+
|
|
|
+ output("Translating to runtime model!")
|
|
|
+
|
|
|
+ output("Creating empty runtime model")
|
|
|
+ runtime_model = instantiate_model("models/CausalBlockDiagrams_Runtime")
|
|
|
+
|
|
|
+ output("Converting blocks")
|
|
|
+ all_blocks = allInstances(design_model, "Block")
|
|
|
+ while (list_len(all_blocks) > 0):
|
|
|
+ element_name = set_pop(all_blocks)
|
|
|
+ mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][element_name]))
|
|
|
+ instantiate_node(runtime_model, mm_type_name, element_name)
|
|
|
+ all_attributes = getAttributeList(design_model, element_name)
|
|
|
+ while (set_len(all_attributes) > 0):
|
|
|
+ attr_name = set_pop(all_attributes)
|
|
|
+ if (bool_not(set_in(runtime_model["model"], read_attribute(design_model, element_name, attr_name)))):
|
|
|
+ instantiate_attribute(runtime_model, element_name, attr_name, read_attribute(design_model, element_name, attr_name))
|
|
|
+ output(" Converted block " + element_name)
|
|
|
+
|
|
|
+ // Don't merge this together with the block conversion, as the destination block might not exist yet!
|
|
|
+ output("Relinking blocks")
|
|
|
+ all_links = allInstances(design_model, "Link")
|
|
|
+ while (set_len(all_links) > 0):
|
|
|
+ element_name = set_pop(all_links)
|
|
|
+ src = reverseKeyLookup(design_model["model"], read_edge_src(model["model"][element_name]))
|
|
|
+ dst = reverseKeyLookup(design_model["model"], read_edge_dst(model["model"][element_name]))
|
|
|
+ instantiate_link(runtime_model, "Link", element_name, src, dst)
|
|
|
+ output(((" Relinked blocks " + src) + " and ") + dst)
|
|
|
+
|
|
|
+ output("Resetting initial conditions")
|
|
|
+ all_links = allInstances(design_model, "InitialCondition")
|
|
|
+ while (set_len(all_links) > 0):
|
|
|
+ element_name = set_pop(all_links)
|
|
|
+ src = reverseKeyLookup(design_model["model"], read_edge_src(model["model"][element_name]))
|
|
|
+ dst = reverseKeyLookup(design_model["model"], read_edge_dst(model["model"][element_name]))
|
|
|
+ instantiate_link(runtime_model, "InitialCondition", element_name, src, dst)
|
|
|
+ output(" Reset IC of " + dst)
|
|
|
+
|
|
|
+ output("Creating schedule at time = 0")
|
|
|
+ create_schedule(runtime_model, True)
|
|
|
+
|
|
|
+ output("Creating schedule at time > 0")
|
|
|
+ create_schedule(runtime_model, False)
|
|
|
+
|
|
|
+ output("Solving loops (TODO)")
|
|
|
+
|
|
|
+ output("DONE!")
|
|
|
+ return !
|
|
|
+
|
|
|
+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):
|
|
|
+ schedule = instantiate_node(model, "Schedule", "schedule_init")
|
|
|
+ else:
|
|
|
+ schedule = instantiate_node(model, "Schedule", "schedule_running")
|
|
|
+
|
|
|
+ while (set_len(all_blocks) > 0):
|
|
|
+ element_name = set_pop(all_blocks)
|
|
|
+ if (bool_not(set_in(visited, element_name))):
|
|
|
+ list_append(to_visit, element_name)
|
|
|
+
|
|
|
+ while (list_len(to_visit) > 0):
|
|
|
+ element_name = list_read(to_visit, list_len(to_visit) - 1)
|
|
|
+ incoming_links = allIncomingAssociationInstances(model, element_name, "Link")
|
|
|
+
|
|
|
+ return !
|
|
|
+
|
|
|
+Void function execute_cbd(model : Element):
|
|
|
+ translate_to_runtime(model)
|
|
|
+
|
|
|
+ String cmd
|
|
|
+ while (True):
|
|
|
+ output("Which operation do you want to execute?")
|
|
|
+ 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")
|
|
|
+ output(" help -- this information")
|
|
|
+ output(" exit -- select another model")
|
|
|
+ elif (cmd == "exit"):
|
|
|
+ return!
|
|
|
+ else:
|
|
|
+ output("Did not understand command!")
|
|
|
+ output("Use 'help' for a list of available options")
|