Переглянути джерело

Rewrite core_algorithm and mini_modify for an "all at once" approach

Yentl Van Tendeloo 8 роки тому
батько
коміт
70d98776a3
3 змінених файлів з 1299 додано та 1231 видалено
  1. 969 960
      core/core_algorithm.alc
  2. 326 271
      core/mini_modify.alc
  3. 4 0
      core/mini_modify.alh

Різницю між файлами не показано, бо вона завелика
+ 969 - 960
core/core_algorithm.alc


+ 326 - 271
core/mini_modify.alc

@@ -8,6 +8,8 @@ include "metamodels.alh"
 include "modelling.alh"
 include "compilation_manager.alh"
 
+Boolean verbose = True
+
 String function pretty_print(model : Element):
 	Element keys_m
 	String type
@@ -51,15 +53,277 @@ String function pretty_print(model : Element):
 				result = result + "\n"
 	return result!
 
-Element function modify(model : Element, write : Boolean):
-	String cmd
+String function cmd_help_m(write : Boolean):
+	String result
+	result = ""
+	result = result + "Allowed operations:\n"
+	if (write):
+		result = result + " == READ/WRITE ==\n"
+		result = result + "  instantiate_node -- Create a new model element (node)\n"
+		result = result + "  instantiate_edge -- Create a new model element (edge)\n"
+		result = result + "  delete           -- Delete an existing element\n"
+		result = result + "  attr_add         -- Add an attribute to an element\n"
+		result = result + "  attr_add_code    -- Add a coded attribute to an element\n"
+		result = result + "  attr_del         -- Delete an attribute of an element\n"
+		result = result + "  attr_modify      -- Modify an attribute of an element\n"
+		result = result + "  retype           -- Change the type of an element\n"
+		result = result + "  upload           -- Upload a completely new model\n"
+	else:
+		result = result + " == READ-ONLY ==\n"
+	result = result + "  read_outgoing    -- Prints the list of outgoing links of an element\n"
+	result = result + "  read_incoming    -- Prints the list of incoming links to an element\n"
+	result = result + "  list             -- Prints the list of elements in the model\n"
+	result = result + "  list_full        -- Prints the list of all elements in the model\n"
+	result = result + "  types            -- Prints the list of elements that can be instantiated\n"
+	result = result + "  read             -- Prints the current state of a model element\n"
+	result = result + "  verify           -- Check whether the model conforms to the metamodel\n"
+	result = result + "  exit             -- Leave the modification interface\n"
+
+	return result!
+
+String function cmd_upload(write : Boolean, model : Element):
+	Element new_model
+	if (write):
+		output("Waiting for model constructors...")
+		new_model = construct_model_raw(model["metamodel"])
+		dict_overwrite(model, "model", new_model["model"])
+		dict_overwrite(model, "type_mapping", new_model["type_mapping"])
+		return "Success"!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_instantiate_node(write : Boolean, model : Element, mm_type_name : String, element_name : String):
+	if (write):
+		if (dict_in(model["metamodel"]["model"], mm_type_name)):
+			if (dict_in(model["model"], element_name)):
+				return "Element exists: " + element_name!
+			else:
+				if (is_edge(model["metamodel"]["model"][mm_type_name])):
+					return "Element is not a node but an edge: " + mm_type_name!
+
+				element_name = instantiate_node(model, mm_type_name, element_name)
+				return "Success: " + element_name!
+		else:
+			return "Element not found: " + mm_type_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_instantiate_edge(write : Boolean, model : Element, mm_type_name : String, element_name : String, source_name : String, target_name : String):
+	if (write):
+		if (dict_in(model["metamodel"]["model"], mm_type_name)):
+			if (dict_in(model["model"], element_name)):
+				return "Element exists: " + element_name!
+			else:
+				if (is_edge(model["metamodel"]["model"][mm_type_name])):
+					if (dict_in(model["model"], source_name)):
+						if (dict_in(model["model"], target_name)):
+							element_name = instantiate_link(model, mm_type_name, element_name, source_name, target_name)
+							return "Success: " + element_name!
+						else:
+							return "Element not found: " + target_name!
+					else:
+						return "Element not found: " + source_name!
+				else:
+					return "Element is a node not an edge: " + mm_type_name!
+		else:
+			return "Element not found: " + mm_type_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_attr_add(write : Boolean, model : Element, element_name : String, attr_name : String, value : Element):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			Element attrs
+			attrs = getAttributeList(model, element_name)
+			if (set_in(dict_keys(attrs), attr_name)):
+				instantiate_attribute(model, element_name, attr_name, input())
+				return "Success"!
+			else:
+				return "Attribute not found: " + attr_name!
+		else:
+			return "Element not found: " + element_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_attr_add_code(write : Boolean, model : Element, element_name : String, attr_name : String):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			Element attrs
+			attrs = getAttributeList(model, element_name)
+			if (set_in(dict_keys(attrs), attr_name)):
+				output("Waiting for code constructors...")
+				instantiate_attribute_code(model, element_name, attr_name, input())
+				return "Success"!
+			else:
+				return "Attribute not found: " + attr_name!
+		else:
+			return "Element not found: " + element_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_attr_del(write : Boolean, model : Element, element_name : String, attr_name : String):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			Element attrs
+			attrs = getAttributeList(model, element_name)
+			if (set_in(dict_keys(attrs), attr_name)):
+				unset_attribute(model, element_name, attr_name)
+				return "Success"!
+			else:
+				return "Attribute not found: " + attr_name!
+		else:
+			return "Element not found: " + element_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_delete(write : Boolean, model : Element, element_name : String):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			model_delete_element(model, element_name)
+			return "Success"!
+		else:
+			return "Element not found: " + element_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_list(model : Element):
+	Element keys_m
+	String v_m
+	String result
+	String typename
 
-	Element attr_list_pn
-	Element attr_keys_pn
-	String attr_key_pn
-	Element metamodel_element_pn
+	result = ""
+	keys_m = dict_keys(model["model"])
+	while (read_nr_out(keys_m) > 0):
+		v_m = set_pop(keys_m)
+		// Filter out anonymous objects
+		if (bool_not(string_startswith(v_m, "__"))):
+			typename = read_type(model, v_m)
+			result = (result + ((("  " + v_m) + " : ") + typename)) + "\n"
+	
+	return result!
+
+String function cmd_list_full(model : Element):
+	Element keys_m
+	String v_m
+	String result
 	String typename
 
+	result = ""
+	keys_m = dict_keys(model["model"])
+	while (read_nr_out(keys_m) > 0):
+		v_m = set_pop(keys_m)
+		// Filter out anonymous objects
+		typename = read_type(model, v_m)
+		result = (result + ((("  " + v_m) + " : ") + typename)) + "\n"
+	
+	return result!
+
+String function cmd_read_outgoing(model : Element, element_name : String, type : String):
+	String result
+	Element elems
+
+	result = ""
+	if (dict_in(model["model"], element_name)):
+		elems = allOutgoingAssociationInstances(model, element_name, type)
+		while (read_nr_out(elems) > 0):
+			result = string_join(result, set_pop(elems)) + "\n"
+		return result!
+	else:
+		return "Element not found: " + element_name!
+
+String function cmd_read_incoming(model : Element, element_name : String, type : String):
+	String result
+	Element elems
+
+	result = ""
+	if (dict_in(model["model"], element_name)):
+		elems = allIncomingAssociationInstances(model, element_name, type)
+		while (read_nr_out(elems) > 0):
+			result = string_join(result, set_pop(elems)) + "\n"
+		return result!
+	else:
+		return "Element not found: " + element_name!
+
+String function cmd_read(model : Element, element_name : String):
+	String result
+	Element attr_list
+	Element attr_keys
+	String attr_key
+
+	result = ""
+	if (dict_in(model["model"], element_name)):
+		result = ((result + "ID: ") + element_name) + "\n"
+		result = ((result + "Type: ") + read_type(model, element_name)) + "\n"
+		if (is_edge(model["model"][element_name])):
+			result = ((result + "Source: ") + reverseKeyLookup(model["model"], read_edge_src(model["model"][element_name]))) + "\n"
+			result = ((result + "Destination: ") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][element_name]))) + "\n"
+		if (has_value(model["model"][element_name])):
+			result = ((result + "Value: ") + cast_v2s(model["model"][element_name])) + "\n"
+		result = result + "Defines attributes:\n"
+		attr_list = getInstantiatableAttributes(model, element_name)
+		attr_keys = dict_keys(attr_list)
+		while (0 < read_nr_out(attr_keys)):
+			attr_key = set_pop(attr_keys)
+			result = ((((result + "  ") + attr_key) + " : ") + cast_v2s(attr_list[attr_key])) + "\n"
+		result = result + "Attributes:\n"
+		attr_list = getAttributeList(model, element_name)
+		attr_keys = dict_keys(attr_list)
+		while (0 < read_nr_out(attr_keys)):
+			attr_key = set_pop(attr_keys)
+			result = ((((((result + "  ") + cast_v2s(attr_key)) + " : ") + cast_v2s(attr_list[attr_key])) + " = ") + cast_v2s(read_attribute(model, element_name, attr_key))) + "\n"
+		return result!
+	else:
+		return "Element not found: " + element_name!
+
+String function cmd_types(model : Element):
+	Element keys_t
+	String v_t
+	String result
+
+	keys_t = dict_keys(model["metamodel"]["model"])
+	result = ""
+	while (read_nr_out(keys_t) > 0):
+		v_t = set_pop(keys_t)
+		if (bool_not(string_startswith(v_t, "__"))):
+			result = (result + string_join(("  " + v_t) + " : ", read_type(model["metamodel"], v_t))) + "\n"
+
+	return result!
+
+String function cmd_retype(write : Boolean, model : Element, element_name : String, new_type : String):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			if (dict_in(model["metamodel"]["model"], new_type)):
+				retype(model, element_name, new_type)
+				return "Success"!
+			else:
+				return "Element not found: " + new_type!
+		else:
+			return "Element not found: " + element_name!
+	else:
+		return "Permission denied to write"!
+
+String function cmd_read_association_source(write : Boolean, model : Element, element_name : String):
+	if (dict_in(model["model"], element_name)):
+		if (is_edge(model["model"][element_name])):
+			return readAssociationSource(model, element_name)!
+		else:
+			return "Not an association: " + element_name!
+	else:
+		return "Element not found: " + element_name!
+
+String function cmd_read_association_destination(write : Boolean, model : Element, element_name : String):
+	if (dict_in(model["model"], element_name)):
+		if (is_edge(model["model"][element_name])):
+			return readAssociationDestination(model, element_name)!
+		else:
+			return "Not an association: " + element_name!
+	else:
+		return "Element not found: " + element_name!
+
+Element function modify(model : Element, write : Boolean):
+	String cmd
+
 	output("Model loaded, ready for commands!")
 	if (write):
 		output("Mode: r/w")
@@ -68,293 +332,84 @@ Element function modify(model : Element, write : Boolean):
 	output("Use 'help' command for a list of possible commands")
 
 	while (True):
-		output("Please give your command.")
 		cmd = input()
 		if (cmd == "help"):
-			output("Allowed operations:")
-			if (write):
-				output(" == READ/WRITE ==")
-				output("  instantiate   -- Create a new model element")
-				output("  delete        -- Delete an existing element")
-				output("  attr_add      -- Add an attribute to an element")
-				output("  attr_add_code -- Add a coded attribute to an element")
-				output("  attr_del      -- Delete an attribute of an element")
-				output("  attr_modify   -- Modify an attribute of an element")
-				output("  retype        -- Change the type of an element")
-				output("  upload        -- Upload a completely new model")
-			else:
-				output(" == READ-ONLY ==")
-			output("  read_outgoing -- Prints the list of outgoing links of an element")
-			output("  read_incoming -- Prints the list of incoming links to an element")
-			output("  list          -- Prints the list of elements in the model")
-			output("  list_full     -- Prints the list of all 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("  exit          -- Leave the modification interface")
+			output(cmd_help_m(write))
 		elif (cmd == "exit"):
 			return model!
 		elif (cmd == "upload"):
-			Element new_model
-			output("Waiting for model constructors...")
-			new_model = construct_model_raw(model["metamodel"])
-			dict_overwrite(model, "model", new_model["model"])
-			dict_overwrite(model, "type_mapping", new_model["type_mapping"])
-		elif (cmd == "instantiate"):
-			if (write):
-				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)):
-									element_name = instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
-									output("Instantiation successful!")
-									output(element_name)
-								else:
-									output("Unknown destination; aborting")
-							else:
-								log("Unknown source!")
-								log("SRC: " + src_name)
-								log("In set: " + set_to_string(dict_keys(model["model"])))
-								output("Unknown source; aborting")
-						else:
-							element_name = instantiate_node(model, mm_type_name, element_name)
-							output("Instantiation successful!")
-							output(element_name)
-				else:
-					log("Could not find element!")
-					output("Unknown type specified; aborting")
-			else:
-				output("Permission denied")
-
+			output(cmd_upload(write, model))
+		elif (cmd == "instantiate_node"):
+			output(cmd_instantiate_node(write, model, single_input("Type?"), single_input("Name?")))
+		elif (cmd == "instantiate_edge"):
+			output(cmd_instantiate_edge(write, model, single_input("Type?"), single_input("Name?"), single_input("Source?"), single_input("Target?")))
 		elif (cmd == "attr_add"):
-			if (write):
-				String model_name
-				output("Which element 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 element!")
-			else:
-				output("Permission denied")
-
+			output(cmd_attr_add(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Value?")))
 		elif (cmd == "attr_add_code"):
-			if (write):
-				String model_name
-				output("Which element do you want to assign a coded 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("Waiting for code constructors...")
-						instantiate_attribute_code(model, model_name, attr_name, input())
-						output("Added attribute!")
-					else:
-						output("No such attribute!")
-				else:
-					output("No such element!")
-			else:
-				output("Permission denied")
-
+			output(cmd_attr_add_code(write, model, single_input("Name?"), single_input("Attribute name?")))
 		elif (cmd == "attr_del"):
-			if (write):
-				String model_name
-				output("Which element 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 element!")
-			else:
-				output("Permission denied")
-
+			output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute_name?")))
 		elif (cmd == "delete"):
-			if (write):
-				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")
-			else:
-				output("Permission denied")
-
+			output(cmd_delete(write, model, single_input("Name?")))
 		elif (cmd == "nice_list"):
-			pretty_print(model)
-
+			output(pretty_print(model))
 		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
-				if (bool_not(string_startswith(v_m, "__"))):
-					typename = read_type(model, v_m)
-					output((("  " + v_m) + " : ") + typename)
-
+			output(cmd_list(model))
 		elif (cmd == "list_full"):
-			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 = read_type(model, v_m)
-				output((("  " + v_m) + " : ") + typename)
-
+			output(cmd_list_full(model))
 		elif (cmd == "read_outgoing"):
-			Element elems
-			output("Element to read from?")
-			cmd = input()
-			if (dict_in(model["model"], cmd)):
-				String t
-				output("Type of outgoing edge (empty for all)?")
-				elems = allOutgoingAssociationInstances(model, cmd, input())
-				while (read_nr_out(elems) > 0):
-					output(set_pop(elems))
-			else:
-				output("Unknown element; aborting")
-
+			output(cmd_read_outgoing(model, single_input("Name?"), single_input("Type?")))
 		elif (cmd == "read_incoming"):
-			Element elems
-			output("Element to read from?")
-			cmd = input()
-			if (dict_in(model["model"], cmd)):
-				String t
-				output("Type of incoming edge (empty for all)?")
-				elems = allIncomingAssociationInstances(model, cmd, input())
-				while (read_nr_out(elems) > 0):
-					output(set_pop(elems))
-			else:
-				output("Unknown element; aborting")
-
+			output(cmd_read_incoming(model, single_input("Name?"), single_input("Type?")))
 		elif (cmd == "read"):
-			output("Element to read?")
-			cmd = input()
-			if (dict_in(model["model"], cmd)):
-				output("ID: " + cmd)
-				output("Type: " + read_type(model, cmd))
-				if (is_edge(model["model"][cmd])):
-					output("Source: " + reverseKeyLookup(model["model"], read_edge_src(model["model"][cmd])))
-					output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(model["model"][cmd])))
-				if (has_value(model["model"][cmd])):
-					output("Value: " + cast_v2s(model["model"][cmd]))
-				output("Defines attributes:")
-				attr_list_pn = getInstantiatableAttributes(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(((("  " + 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, cmd, attr_key_pn)))
-			else:
-				output("Unknown element; aborting")
-
+			output(cmd_read(model, single_input("Name?")))
 		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) + " : ", read_type(model["metamodel"], v_t)))
-
+			output(cmd_types(model))
 		elif (cmd == "retype"):
-			if (write):
-				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)):
-						retype(model, elementname, typename)
-						output("Retyped!")
-					else:
-						output("Unknown type; aborting")
-				else:
-					output("Unknown element; aborting")
-			else:
-				output("Permission denied")
-
+			output(cmd_retype(write, model, single_input("Name?"), single_input("New type?")))
 		elif (cmd == "read_association_source"):
-			output("Association to read source of?")
-			cmd = input()
-
-			if (dict_in(model["model"], cmd)):
-				if (is_edge(model["model"][cmd])):
-					output("Read source:")
-					output(readAssociationSource(model, cmd))
-				else:
-					output("Not an association; aborting")
-			else:
-				output("Unknown element; aborting")
-
+			output(cmd_read_association_source(write, model, single_input("Name?")))
 		elif (cmd == "read_association_destination"):
-			output("Association to read destination of?")
-			cmd = input()
-
-			if (dict_in(model["model"], cmd)):
-				if (is_edge(model["model"][cmd])):
-					output("Read destination:")
-					output(readAssociationDestination(model, cmd))
-				else:
-					output("Not an association; aborting")
-			else:
-				output("Unknown element; aborting")
-
+			output(cmd_read_association_destination(write, model, single_input("Name?")))
 		else:
 			output("Unknown command: " + cast_v2s(cmd))
 			output("Use command 'help' to get a list of available commands")
-
 	return model!
+
+String function single_input(prompt : String):
+	if (verbose):
+		output(prompt)
+	return input()!
+
+Element function set_input(prompt : String):
+	Element result
+
+	result = create_node()
+	if (verbose):
+		output(prompt)
+		output("-- Set input: empty string to terminate set")
+	set_add(result, input())
+
+	return result!
+
+Element function dict_input(prompt : String):
+	Element result
+	String key
+	
+	result = create_node()
+
+	if (verbose):
+		output(prompt)
+		output("-- Dict input: empty key to terminate dict")
+
+	key = input()
+	while (key != ""):
+		dict_add(result, key, input())
+
+	return result!
+
+Void function set_verbose(v : Boolean):
+	verbose = v
+	return!

+ 4 - 0
core/mini_modify.alh

@@ -1,2 +1,6 @@
 Element function modify(model : Element, write : Boolean)
 String function pretty_print(model : Element)
+String function single_input(prompt : String)
+Element function set_input(prompt : String)
+Element function dict_input(prompt : String)
+Void function set_verbose(v : Boolean)