include "primitives.alh" include "constructors.alh" include "object_operations.alh" include "library.alh" include "conformance_scd.alh" include "io.alh" include "metamodels.alh" include "modelling.alh" Element function pn_operations(): Element ops ops = create_node() dict_add(ops, "fire", petrinet_fire) dict_add(ops, "enabled", petrinet_enabled) return ops Element function petrinet_enabled(model : Element): Element set_enabled set_enabled = petrinet_enabled_set(model) output("Enabled transitions:") while (0 < read_nr_out(set_enabled)): output(set_pop(set_enabled)) return model Element function petrinet_enabled_set(model : Element): Element all_transitions Element enabled_transitions String under_study Element in_arcs String arc_under_study Boolean enabled all_transitions = allInstances(model, "Transition") enabled_transitions = create_node() while (0 < read_nr_out(all_transitions)): under_study = set_pop(all_transitions) enabled = True // Find all incoming transitions in_arcs = allIncomingAssociationInstances(model, under_study, "P2T") while (0 < read_nr_out(in_arcs)): arc_under_study = set_pop(in_arcs) Integer present_tokens Integer required_tokens required_tokens = read_attribute(model, arc_under_study, "weight") log("Weight: " + cast_i2s(required_tokens)) present_tokens = read_attribute(model, reverseKeyLookup(model["model"], read_edge_src(model["model"][arc_under_study])), "tokens") log("Tokens: " + cast_i2s(present_tokens)) if (present_tokens < required_tokens): // Less tokens than required, so disable the transition completely enabled = False if (enabled): set_add(enabled_transitions, under_study) log("Got all enabled transitions!") return enabled_transitions Element function petrinet_fire(model : Element): output("Transition to fire?") String transition transition = input() if (dict_in(model["model"], transition)): if (set_in(petrinet_enabled_set(model), transition)): Element workset String working_place String working_arc Integer new_value // Consume tokens workset = allIncomingAssociationInstances(model, transition, "P2T") while (0 < read_nr_out(workset)): working_arc = set_pop(workset) working_place = reverseKeyLookup(model["model"], read_edge_src(model["model"][working_arc])) new_value = integer_subtraction(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight")) unset_attribute(model, working_place, "tokens") instantiate_attribute(model, working_place, "tokens", new_value) output(((" " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens"))) // Add tokens workset = allOutgoingAssociationInstances(model, transition, "T2P") while (0 < read_nr_out(workset)): working_arc = set_pop(workset) working_place = reverseKeyLookup(model["model"], read_edge_dst(model["model"][working_arc])) new_value = integer_addition(read_attribute(model, working_place, "tokens"), read_attribute(model, working_arc, "weight")) unset_attribute(model, working_place, "tokens") instantiate_attribute(model, working_place, "tokens", new_value) output(((" " + working_place) + ": ") + cast_i2s(read_attribute(model, working_place, "tokens"))) output("Transition fired!") else: output("Cannot fire if not enabled; aborting") else: output("Unknown transition; aborting") return model Element function model_loaded(model : Element): String cmd Element attr_list_pn Element attr_keys_pn String attr_key_pn Element metamodel_element_pn String typename Boolean bottom Element other_metamodel bottom = False other_metamodel = create_node() dict_add(other_metamodel, "model", model["model"]) dict_add(other_metamodel, "type_mapping", create_node()) dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom")) dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"]) output("Model loaded, ready for commands!") output("Use 'help' command for a list of possible commands") while (True): output("Please give your command.") cmd = input() if (cmd == "help"): output("Generic model operations:") output(" instantiate -- Create a new model element") output(" delete -- Delete an existing element") output(" attr_add -- Add an attribute to an element") output(" attr_del -- Delete an attribute of an element") output(" rename -- Rename an existing element") output(" modify -- Modify the attributes of an element") output(" list -- Prints the list of elements in the model") output(" types -- Prints the list of elements that can be instantiated") output(" read -- Prints the current state of a model element") output(" verify -- Check whether the model conforms to the metamodel") output(" retype -- Change the type of an element") output(" switch -- Switch between conformance bottom and the linguistic metamodel") output(" exit -- Unload the model and go back to the loading prompt") if (bool_not(bottom)): output("Model-specific operations:") Element specific_ops specific_ops = dict_keys(pn_operations()) String specific_op while (0 < dict_len(specific_ops)): specific_op = set_pop(specific_ops) output(" " + specific_op) elif (cmd == "exit"): return model elif (cmd == "instantiate"): String mm_type_name output("Type to instantiate?") mm_type_name = input() if (dict_in(model["metamodel"]["model"], mm_type_name)): String element_name output("Name of new element?") element_name = input() if (dict_in(model["model"], element_name)): output("Element already exists; aborting") else: if (is_edge(model["metamodel"]["model"][mm_type_name])): output("Source name?") String src_name src_name = input() if (dict_in(model["model"], src_name)): output("Destination name?") String dst_name dst_name = input() if (dict_in(model["model"], dst_name)): instantiate_link(model, mm_type_name, element_name, src_name, dst_name) if (dict_in(model["model"], element_name)): output("Instantiation successful!") else: output("Instantiation error!") else: output("Unknown destination; aborting") else: output("Unknown source; aborting") else: instantiate_node(model, mm_type_name, element_name) if (dict_in(model["model"], element_name)): output("Instantiation successful!") else: output("Instantiation error!") else: output("Unknown type specified; aborting") elif (cmd == "modify"): String model_name output("Element to modify?") model_name = input() if (dict_in(model["model"], model_name)): Element attrs attrs = getAttributeList(model, model_name) String attr_name output("Attribute to modify?") attr_name = input() if (set_in(dict_keys(attrs), attr_name)): output("New value?") unset_attribute(model, model_name, attr_name) instantiate_attribute(model, model_name, attr_name, input()) output("Modified!") else: output("No such attribute!") else: output("No such model!") elif (cmd == "attr_add"): String model_name output("Which model do you want to assign an attribute to?") model_name = input() if (dict_in(model["model"], model_name)): Element attrs attrs = getAttributeList(model, model_name) String attr_name output("Which attribute do you wish to assign?") attr_name = input() if (set_in(dict_keys(attrs), attr_name)): output("Value of attribute?") instantiate_attribute(model, model_name, attr_name, input()) output("Added attribute!") else: output("No such attribute!") else: output("No such model!") elif (cmd == "attr_del"): String model_name output("Which model do you want to remove an attribute of?") model_name = input() if (dict_in(model["model"], model_name)): Element attrs attrs = getAttributeList(model, model_name) String attr_name output("Which attribute do you want to delete?") attr_name = input() if (set_in(dict_keys(attrs), attr_name)): unset_attribute(model, model_name, attr_name) output("Attribute deleted!") else: output("No such attribute!") else: output("No such model!") elif (cmd == "delete"): output("What is the name of the element you want to delete?") cmd = input() if (dict_in(model["model"], cmd)): model_delete_element(model, cmd) output("Deleted!") else: output("No such element; aborting") elif (cmd == "rename"): output("Old name?") String old_name_e old_name_e = input() if (dict_in(model["model"], old_name_e)): output("New name?") String new_name_e new_name_e = input() if (dict_in(model["model"], new_name_e)): output("New name already used; aborting") else: dict_add(model["model"], new_name_e, model["model"][old_name_e]) dict_delete(model["model"], old_name_e) output("Rename complete!") else: output("Unknown element; aborting") elif (cmd == "list"): Element keys_m keys_m = dict_keys(model["model"]) output("List of all elements:") String v_m while (read_nr_out(keys_m) > 0): v_m = set_pop(keys_m) // Filter out anonymous objects typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m])) if (bool_not(string_startswith(v_m, "__"))): output(((" " + v_m) + " : ") + typename) elif (cmd == "read"): output("Element to read?") cmd = input() if (dict_in(model["model"], cmd)): Element read_elem read_elem = model["model"][cmd] metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem) output("Name: " + cmd) output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn)) if (is_edge(read_elem)): output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem))) output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem))) if (cast_v2s(read_elem) != "None"): output("Value: " + cast_v2s(read_elem)) output("Defines attributes:") attr_list_pn = getInstantiatableAttributes(model, read_elem) attr_keys_pn = dict_keys(attr_list_pn) while (0 < read_nr_out(attr_keys_pn)): attr_key_pn = set_pop(attr_keys_pn) output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn]))) output("Attributes:") attr_list_pn = getAttributeList(model, cmd) attr_keys_pn = dict_keys(attr_list_pn) while (0 < read_nr_out(attr_keys_pn)): attr_key_pn = set_pop(attr_keys_pn) output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, reverseKeyLookup(model["model"], read_elem), attr_key_pn))) else: output("Unknown element; aborting") elif (cmd == "verify"): output(conformance_scd(model)) elif (cmd == "types"): Element keys_t keys_t = dict_keys(model["metamodel"]["model"]) output("List of types:") String v_t while (read_nr_out(keys_t) > 0): v_t = set_pop(keys_t) if (bool_not(string_startswith(v_t, "__"))): output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t])))) elif (cmd == "retype"): output("Element to retype?") String elementname elementname = input() if (dict_in(model["model"], elementname)): output("New type") typename = input() if (dict_in(model["metamodel"]["model"], typename)): // OK, do the retyping // First try removing the previous type if it exists dict_delete(model["type_mapping"], model["model"][elementname]) // Now add the new type dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename]) output("Retyped!") else: output("Unknown type; aborting") else: output("Unknown element; aborting") elif (cmd == "switch"): bottom = bool_not(bottom) Element tmp_model tmp_model = model model = other_metamodel other_metamodel = tmp_model if (bottom): // The type mapping we are using is probably not complete for our model // so we completely recreate it from the model we have. output("Switching to conformance bottom mode!") generate_bottom_type_mapping(model) else: // We already switched the models and such, so we are already done! output("Switching to linguistic metamodel!") elif (bool_and(dict_in(pn_operations(), cmd), bool_not(bottom))): // A model-specific operation, so execute that one Element specific_op specific_op = dict_read(pn_operations(), cmd) specific_op(model) else: output("Unknown command; aborting") output("Use command 'help' to get a list of available commands") Element function initial_prompt(): output("Welcome to the Model Management Interface, running live on the Modelverse!") output("Use 'help' command for a list of possible commands") String command Element root Element metamodel String name Element my_model String mm_name root = create_metamodels() while (True): output("Please give your command.") command = input() if (command == "help"): output("Currently no model is loaded, so your operations are limited to:") output(" new -- Create a new model and save it for future use") output(" load -- Load a previously made model") output(" rename -- Rename a previously made model") output(" delete -- Delete a previously made model") output(" list -- Show a list of all stored models") output(" help -- Show a list of possible commands") elif (command == "new"): output("Metamodel to instantiate?") mm_name = input() if (dict_in(root, mm_name)): output("Name of model?") name = input() if (dict_in(root, name)): output("Model exists; aborting") else: my_model = instantiate_model(root[mm_name]) dict_add(root, name, my_model) model_loaded(my_model) else: output("Unknown metamodel; aborting") elif (command == "load"): output("Model to load?") name = input() if (dict_in(root, name)): my_model = root[name] model_loaded(my_model) else: output("Model not found; aborting") elif (command == "list"): Element keys String m_menu_list keys = dict_keys(root) output("Found models:") while (read_nr_out(keys) > 0): m_menu_list = set_pop(keys) output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"])) elif (command == "delete"): output("Model to delete?") name = input() if (dict_in(root, name)): dict_delete(root, name) output("Deleted!") else: output("Model not found; aborting") elif (command == "rename"): output("Old name?") String old_name old_name = input() if (dict_in(root, old_name)): output("New name?") String new_name new_name = input() if (dict_in(root, new_name)): output("Model exists; aborting") else: dict_add(root, new_name, root[old_name]) dict_delete(root, old_name) output("Rename complete!") else: output("Model not found; aborting") else: output("Command not recognized, use 'help' for a list of possible commands") Void function main(): initial_prompt()