123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- include "primitives.alh"
- include "modelling.alh"
- include "object_operations.alh"
- include "library.alh"
- include "conformance_scd.alh"
- include "io.alh"
- include "metamodels.alh"
- include "compilation_manager.alh"
- Element function retype_to_runtime(design_model : Element):
- Element runtime_model
- Element all_states
- Element all_links
- String mm_type_name
- String element_name
- String attr_name
- String attr_value
- String attribute
- String src
- String dst
- String time
- Element all_attributes
- runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
- all_blocks = allInstances(design_model, "State")
- while (list_len(all_states) > 0):
- element_name = set_pop(all_states)
- mm_type_name = reverseKeyLookup(design_model["metamodel"]["model"], dict_read_node(design_model["type_mapping"], design_model["model"][element_name]))
- element_name = instantiate_node(runtime_model, mm_type_name, element_name)
- instantiate_attribute(runtime_model, element_name, "name", read_attribute(design_model, element_name, "name"))
- // Don't merge this together with the block conversion, as the destination block might not exist yet!
- all_links = allInstances(design_model, "Transition")
- while (read_nr_out(all_links) > 0):
- element_name = set_pop(all_links)
- src = reverseKeyLookup(design_model["model"], read_edge_src(design_model["model"][element_name]))
- dst = reverseKeyLookup(design_model["model"], read_edge_dst(design_model["model"][element_name]))
- instantiate_link(runtime_model, "Transition", element_name, src, dst)
- instantiate_attribute(runtime_model, element_name, "event", read_attribute(design_model, element_name, "event"))
- if (element_neq(read_attribute(design_model, element_name, "raise"), read_root())):
- // There is a raise attribute
- instantiate_attribute(runtime_model, element_name, "raise", read_attribute(design_model, element_name, "raise"))
- return runtime_model!
- Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
- Element all_blocks
- Element all_links
- String element_name
- String attr_name
- String attr_value
- String attribute
- String time
- Element all_attributes
- Float current_time
- all_blocks = allInstances(new_runtime_model, "Block")
- while (list_len(all_blocks) > 0):
- element_name = set_pop(all_blocks)
- if (dict_in(old_runtime_model["model"], element_name)):
- if (is_nominal_instance(new_runtime_model, element_name, "ICBlock")):
- instantiate_attribute(new_runtime_model, element_name, "last_in", read_attribute(old_runtime_model, element_name, "last_in"))
- if (is_nominal_instance(new_runtime_model, element_name, "IntegratorBlock")):
- instantiate_attribute(new_runtime_model, element_name, "last_out", read_attribute(old_runtime_model, element_name, "last_out"))
- instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
- else:
- instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
- if (dict_in(old_runtime_model["model"], "time")):
- current_time = read_attribute(old_runtime_model, "time", "current_time")
- else:
- current_time = 0
- 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)
- return new_runtime_model!
- Void function dict_overwrite(d : Element, key : Element, value : Element):
- if (dict_in(d, key)):
- dict_delete(d, key)
- if (dict_in_node(d, key)):
- dict_delete_node(d, key)
- dict_add(d, key, value)
- return !
- Integer function min(a : Integer, b : Integer):
- if (a < b):
- return a!
- else:
- return b!
- Element function list_pop(list : Element):
- Integer top
- Element t
- top = list_len(list) - 1
- t = list_read(list, top)
- list_delete(list, top)
- return t!
- String function readType(model : Element, name : String):
- return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
- Integer function list_index_of(lst : Element, elem : Element):
- Integer i
- i = 0
- while (i < read_nr_out(lst)):
- if (value_eq(list_read(lst, i), elem)):
- return i!
- else:
- i = i + 1
- return -1!
- Void function do_transition(model : Element, start_time : Float, event : String):
- // Read out current state
- String cstate_link
- String cstate
- cstate_link = set_pop(allInstances(model, "CurrentStateLink"))
- cstate = readAssociationDestination(model, cstate_link)
- // Read out all outgoing transitions
- Element all_transitions
- all_transitions = allOutgoingAssociationInstances(model, cstate, "Transition")
- while (read_nr_out(all_transitions) > 0):
- if (value_eq(read_attribute(model, cstate, "event"), event)):
- // Found a match
- model_delete_element(model, cstate_link)
- // TODO
- // Set destination of state
- // Raise "raise" attribute of transition
- return !
- return!
- Void function execute_fsa(design_model : Element):
- String verify_result
- Element runtime_model
- Element old_runtime_model
- String cmd
- Boolean running
- String conforming
- Float simulation_time
- Float start_time
- start_time = time()
- simulation_time = 0.0
- old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
- runtime_model = retype_to_runtime(design_model)
- runtime_model = sanitize(runtime_model, old_runtime_model)
- conforming = conformance_scd(design_model)
- if (conforming == "OK"):
- output("CONFORMANCE_OK")
- else:
- output("CONFORMANCE_FAIL")
- while (True):
- cmd = input()
- // Process input
- if (cmd == "pause"):
- // Pausing merely stops a running simulation
- simulation_time = time() - start_time
- output("PAUSED")
- while (cmd != "simulate"):
- cmd = input()
- start_time = time() - simulation_time
- output("CONTINUE")
-
- elif (cmd == "event"):
- String evt
- evt = input()
- do_transition(runtime_model, start_time, evt)
-
- elif (cmd == "read_available_attributes"):
- // Returns a list of all available attributes
- Element attr_list
- Element attrs
- Element attr
- attr_list = getAttributeList(design_model, input())
- attrs = dict_keys(attr_list)
- while (0 < read_nr_out(attrs)):
- attr = set_pop(attrs)
- output("AVAILABLE_ATTR_VALUE " + cast_v2s(attr))
- output("AVAILABLE_ATTR_TYPE " + cast_v2s(dict_read(attr_list, attr)))
- output("AVAILABLE_ATTR_END")
- elif (cmd == "read_attribute"):
- // Returns the value of an attribute
- output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
- elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
- // Modify the structure
- if (cmd == "set_attribute"):
- // Setting an attribute
- String element_name
- String attribute_name
- element_name = input()
- attribute_name = input()
- // Delete it if it exists already
- if (bool_not(element_eq(read_attribute(design_model, element_name, attribute_name), read_root()))):
- unset_attribute(design_model, element_name, attribute_name)
- // And finally set it
- instantiate_attribute(design_model, element_name, attribute_name, input())
- elif (cmd == "instantiate_node"):
- // Instantiate a node
- instantiate_node(design_model, input(), input())
- elif (cmd == "instantiate_association"):
- // Instantiate an association
- instantiate_link(design_model, input(), input(), input(), input())
- elif (cmd == "delete_element"):
- // Delete the provided element
- model_delete_element(design_model, input())
- // After changes, we check whether or not the design model conforms
- if (conforming == "OK"):
- // Was correct, so store just to make sure
- simulation_time = time() - start_time
- conforming = conformance_scd(design_model)
- if (conforming == "OK"):
- // Conforming, so do the retyping and sanitization step
- runtime_model = retype_to_runtime(design_model)
- runtime_model = sanitize(runtime_model, old_runtime_model)
- schedule_init = create_schedule(runtime_model)
- schedule_run = read_root()
- old_runtime_model = runtime_model
- start_time = time() - simulation_time
- output("CONFORMANCE_OK")
- else:
- // Not conforming, so stop simulation and block for input (preferably a modify to make everything consistent again)
- output("CONFORMANCE_FAIL " + conforming)
- else:
- log("Did not understand command: " + cmd)
- Float function v2f(i : Element):
- return cast_s2f(cast_v2s(i))!
|