瀏覽代碼

Added some new utility functions to allow changing defined attributes

Yentl Van Tendeloo 7 年之前
父節點
當前提交
fab6176d98
共有 3 個文件被更改,包括 113 次插入1 次删除
  1. 87 1
      bootstrap/mini_modify.alc
  2. 24 0
      bootstrap/modelling.alc
  3. 2 0
      interface/HUTN/includes/modelling.alh

+ 87 - 1
bootstrap/mini_modify.alc

@@ -206,6 +206,86 @@ String function cmd_attr_del(write : Boolean, model : Element, element_name : St
 	else:
 		return "Permission denied to write"!
 
+String function cmd_attr_name(write : Boolean, model : Element, element_name : String, attr_name : String, new_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)):
+				//TODO this can be more interesting if we keep the IDs in the model, thereby preventing problems with language evolution
+				if (set_in(dict_keys(attrs), attr_name)):
+					if (bool_not(set_in(dict_keys(attrs), new_attr_name))):
+						// TODO use a cleaner way of deleting the attribute
+						Boolean optional
+						String attr_edge
+						attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
+						optional = read_attribute(model, attr_edge, "optional")
+						model_undefine_attribute(model, element_name, attr_name)
+						model_define_attribute_ID(model, element_name, attr_name, optional, attrs[attr_name], attr_edge)
+						return "Success"!
+					else:
+						return "Attribute already defined: " + new_attr_name!
+				else:
+					return "Attribute not defined: " + 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_attr_type(write : Boolean, model : Element, element_name : String, attr_name : String, new_attr_type : 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)):
+				//TODO this can be more interesting if we keep the IDs in the model, thereby preventing problems with language evolution
+				if (set_in(dict_keys(attrs), attr_name)):
+					// TODO use a cleaner way of deleting the attribute
+					Boolean optional
+					String attr_edge
+					attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
+					optional = read_attribute(model, attr_edge, "optional")
+					model_undefine_attribute(model, element_name, attr_name)
+					model_define_attribute_ID(model, element_name, attr_name, optional, new_attr_type, attr_edge)
+					return "Success"!
+				else:
+					return "Attribute not defined: " + 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_attr_optional(write : Boolean, model : Element, element_name : String, attr_name : String, optional : Boolean):
+	if (write):
+		if (dict_in(model["model"], element_name)):
+			Element attrs
+			attrs = getAttributeList(model, element_name)
+			if (set_in(dict_keys(attrs), attr_name)):
+				//TODO this can be more interesting if we keep the IDs in the model, thereby preventing problems with language evolution
+				if (set_in(dict_keys(attrs), attr_name)):
+					// TODO use a cleaner way of deleting the attribute
+					String attr_edge
+					attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][element_name], attr_name))
+					optional = read_attribute(model, attr_edge, "optional")
+					model_undefine_attribute(model, element_name, attr_name)
+					model_define_attribute_ID(model, element_name, attr_name, optional, attrs[attr_name], attr_edge)
+					return "Success"!
+				else:
+					return "Attribute not defined: " + 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)):
@@ -426,7 +506,13 @@ Element function modify(model : Element, write : Boolean):
 		elif (cmd == "attr_add_code"):
 			output(cmd_attr_add_code(write, model, single_input("Name?"), single_input("Attribute name?")))
 		elif (cmd == "attr_del"):
-			output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute_name?")))
+			output(cmd_attr_del(write, model, single_input("Name?"), single_input("Attribute name?")))
+		elif (cmd == "attr_name"):
+			output(cmd_attr_name(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("New name?")))
+		elif (cmd == "attr_type"):
+			output(cmd_attr_type(write, model, single_input("Name?"), single_input("Attribute name?"), single_input("Type name?")))
+		elif (cmd == "attr_optional"):
+			output(cmd_attr_optional(write, model, single_input("Name?"), single_input("Attribute name?"), cast_s2b(single_input("Optional?"))))
 		elif (cmd == "delete"):
 			output(cmd_delete(write, model, single_input("Name?")))
 		elif (cmd == "nice_list"):

+ 24 - 0
bootstrap/modelling.alc

@@ -376,6 +376,14 @@ Void function model_delete_element(model : Element, name : String):
 	delete_element(model["model"][name])
 	return!
 
+String function model_undefine_attribute(model : Element, elem : String, attr_name : String):
+	String attr_edge
+	attr_edge = reverseKeyLookup(model["model"], dict_read_edge(model["model"][elem], attr_name))
+	unset_attribute(model, attr_edge, "name")
+	unset_attribute(model, attr_edge, "optional")
+	model_delete_element(model, attr_edge)
+	return attr_edge!
+
 String function model_define_attribute(model : Element, elem : String, name : String, optional : Boolean, type : String):
 	// Create the necessary links to make it an attribute
 	String edge_name
@@ -392,6 +400,22 @@ String function model_define_attribute(model : Element, elem : String, name : St
 
 	return edge_name!
 
+String function model_define_attribute_ID(model : Element, elem : String, name : String, optional : Boolean, type : String, ID : String):
+	// Create the necessary links to make it an attribute
+	String edge_name
+
+	edge_name = ID
+	while (dict_in(model["model"], edge_name)):
+		// Already exists, so make random name
+		edge_name = edge_name + cast_id2s(model["model"][elem])
+		log("Name clash detected for attribute: try new name: " + edge_name)
+
+	edge_name = instantiate_link(model, "AttributeLink", edge_name, elem, type)
+	instantiate_attribute(model, edge_name, "name", name)
+	instantiate_attribute(model, edge_name, "optional", optional)
+
+	return edge_name!
+
 Element function read_attribute(model : Element, element : String, attribute : String):
 	if (dict_in(model["model"], element)):
 		Integer i

+ 2 - 0
interface/HUTN/includes/modelling.alh

@@ -21,7 +21,9 @@ Void function unset_attribute(model : Element, elem : String, name : String)
 Void function construct_model()
 Element function read_attribute(model : Element, elem : String, name : String)
 Void function model_delete_element(model : Element, name : String)
+String function model_undefine_attribute(model : Element, elem : String, name : String)
 String function model_define_attribute(model : Element, elem : String, name : String, optional : Boolean, type : String)
+String function model_define_attribute_ID(model : Element, elem : String, name : String, optional : Boolean, type : String, ID : String)
 Element function construct_model_raw(metamodel : Element)
 Element function get_func_AL_model(model : Element)
 Void function add_code_model(model : Element, export_name : String, code : Element)