Browse Source

Make a new getAttributeList that works nicer (but doesn't go to superclasses yet)

Yentl Van Tendeloo 9 years ago
parent
commit
26874e9632

+ 35 - 29
bootstrap/object_operations.alc

@@ -101,38 +101,29 @@ Element function deleteAttribute(model : Element, source : Element, attr_name :
 
 	return edge
 
-Element function getAttributeList(model : Element, mm : Element):
+Element function getAttributeList(model : Element, element : String):
 	Element result
+	Element keys
+	Element type
+	Element attr_name
+	String attr_type
+
 	result = create_node()
-	// Get all outgoing "dictionary" links
-	Element set_own
-	set_own = dict_keys(mm)
+	type = dict_read_node(model["type_mapping"], model["model"][element])
+	keys = dict_keys(type)
 
-	// Filter them
-	Element e
-	while (0 < read_nr_out(set_own)):
-		e = set_pop(set_own)
-		if (is_physical_string(e)):
-			if (has_value(mm[e])):
-				// This is a primitive type, so add to the list
-				dict_add(result, e, mm[e])
+	// Add our own attributes
+	while (0 < list_len(keys)):
+		attr_name = set_pop(keys)
+		log("Test for " + cast_e2s(attr_name))
+		if (is_physical_string(attr_name)):
+			log("Found attribute " + cast_v2s(attr_name))
 
-	// And go up to the possible supertypes
-	Element found_elements
-	found_elements = allOutgoingAssociationInstances(model, mm, model["inheritance"])
-	Element dict_super
-	Integer i
-	Integer max
-	Element set_super
-	Element found_key
-	while (0 < read_nr_out(found_elements)):
-		// Now find the destination of these associations, which are the superclasses of this model
-		dict_super = getAttributeList(model, read_edge_dst(set_pop(found_elements)))
-		set_super = dict_keys(dict_super)
-		// Merge the two
-		while (0 < read_nr_out(set_super)):
-			found_key = set_pop(set_super)
-			dict_add(result, found_key, dict_super[found_key])
+			attr_type = getName(model["metamodel"], type[attr_name])
+			dict_add(result, attr_name, attr_type)
+
+	// Go on to the metalevel
+	// TODO
 
 	return result
 
@@ -147,7 +138,7 @@ Element function getInstantiatableAttributes(model : Element, element : Element)
 	Element e
 	while (0 < read_nr_out(set_own)):
 		e = set_pop(set_own)
-		if (bool_and(is_physical_string(e), has_value(element[e]))):
+		if (is_physical_string(e)):
 			// This is a primitive type, so add to the list
 			dict_add(result, e, element[e])
 
@@ -177,3 +168,18 @@ String function reverseNameLookup(s : Element, e : Element):
 		if (element_eq(dict_read_node(s, key), e)):
 			return key
 	return string_join(string_join("(unknown: ", cast_e2s(e)), " )")
+
+String function print_dict(dict : Element):
+	Element keys
+	Element key
+	String result
+
+	keys = dict_keys(dict)
+	result = ""
+	while (0 < list_len(keys)):
+		key = set_pop(keys)
+		result = result + cast_v2s(key)
+		result = result + ": "
+		result = result + cast_v2s(dict[key])
+		result = result + "\n"
+	return result

+ 32 - 51
integration/code/pn_interface.alc

@@ -146,52 +146,58 @@ Element function petrinet_loaded(model : Element):
 			output("Type to instantiate?")
 			mm_type_name = input()
 			if (dict_in(model["metamodel"]["model"], mm_type_name)):
-				metamodel_element_pn = 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:
-					Boolean cnt
-					cnt = True
-					Element additional_opts
-					additional_opts = create_node()
 					if (is_edge(metamodel_element_pn)):
 						output("Source name?")
 						String src_name
 						src_name = input()
 						if (dict_in(model["model"], src_name)):
-							list_append(additional_opts, model["model"][src_name])
 							output("Destination name?")
 							String dst_name
 							dst_name = input()
 							if (dict_in(model["model"], dst_name)):
-								list_append(additional_opts, model["model"][dst_name])
+								instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
+								output("Instantiation successful!")
+								if (dict_in(model["model"], element_name)):
+									output("Instantiation successful!")
+								else:
+									output("Instantiation error!")
 							else:
 								output("Unknown destination; aborting")
-								cnt = False
 						else:
 							output("Unknown source; aborting")
-							cnt = False
-					if (has_value(metamodel_element_pn)):
-						// It is a value
-						output("Value?")
-						list_append(additional_opts, input())
-					if (cnt):
-						//attr_list_pn = getAttributeList(model, metamodel_element_pn)
-						//attr_keys_pn = dict_keys(attr_list_pn)
-						//Element attrs
-						//attrs = create_node()
-						//while (0 < read_nr_out(attr_keys_pn)):
-						//	attr_key_pn = set_pop(attr_keys_pn)
-						//	output(((attr_key_pn + " : ") + cast_e2s(attr_list_pn[attr_key_pn])) + "?")
-						//	dict_add(attrs, attr_key_pn, input())
-						//Element resulting_element
-						//resulting_element = instantiate_model_lib(model, metamodel_element_pn, element_name, additional_opts, create_node(), attrs)
-						output("Instantiation successful!")
+					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 == "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)
+				output(print_dict(attrs))
+				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 == "delete"):
 			output("What is the name of the element you want to delete?")
 			cmd = input()
@@ -200,31 +206,6 @@ Element function petrinet_loaded(model : Element):
 				output("Deleted!")
 			else:
 				output("No such element; aborting")
-		elif (cmd == "modify"):
-			output("Element to modify?")
-			cmd = input()
-			if (dict_in(model["model"], cmd)):
-				Element mod
-				mod = model["model"][cmd]
-				metamodel_element_pn = dict_read_node(model["type_mapping"], mod)
-
-				attr_list_pn = getAttributeList(model, metamodel_element_pn)
-				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_e2s(attr_list_pn[attr_key_pn]))
-
-				output("Attribute to modify?")
-				String attr_name
-				attr_name = input()
-				if (set_in(dict_keys(getAttributeList(model, metamodel_element_pn)), attr_name)):
-					output("New value?")
-					//setAttribute(model, mod, attr_name, input())
-					output("Modified!")
-				else:
-					output("Unknown attribute; aborting")
-			else:
-				output("Element does not exist; aborting")
 		elif (cmd == "rename"):
 			output("Old name?")
 			String old_name_e
@@ -274,7 +255,7 @@ Element function petrinet_loaded(model : Element):
 					attr_key_pn = set_pop(attr_keys_pn)
 					output(((("   " + attr_key_pn) + " : ") + cast_e2s(attr_list_pn[attr_key_pn])))
 				output("Attributes:")
-				attr_list_pn = getAttributeList(model, metamodel_element_pn)
+				attr_list_pn = getAttributeList(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)

+ 1 - 0
interface/HUTN/includes/object_operations.alh

@@ -9,3 +9,4 @@ Element function getAttributeList(a: Element, b: Element)
 Element function getInstantiatableAttributes(a: Element, b: Element)
 String function getName(a: Element, b: Element)
 String function reverseNameLookup(a: Element, b: Element)
+String function print_dict(dict : Element)