include "primitives.alh"
include "object_operations.alh"
include "modelling.alh"
include "metamodels.alh"

Element function ramify_func(model : Element, prepost : String):
	// Create new model structure
	Element new_model
	new_model = create_node()
	dict_add(new_model, "model", create_node())
	dict_add(new_model, "type_mapping", create_node())
	dict_add(new_model, "metamodel", model["metamodel"])
	dict_add(new_model, "inheritance", model["inheritance"])

	// Get local variables for parts
	Element old_m
	Element new_m
	Element mm
	String inheritor
	new_m = new_model["model"]
	old_m = model["model"]
	mm = new_model["metamodel"]["model"]

	Boolean is_pre
	is_pre = (prepost == "pre")

	// Add in Natural and String
	instantiate_node(new_model, "Class", "Natural")
	instantiate_node(new_model, "Class", "String")

	// Add in the complete AL metamodel
	//TODO this is broken right now because of the use of Any in the AL MM
	//add_AL_to_MM(new_model)
	//TODO temporarily add them like this
	instantiate_node(new_model, "Class", "Expression")
	instantiate_node(new_model, "Class", "FuncDef")

	// Add some default elements
	if (is_pre):
		//	Class LHS {
		//		constraint : Expression {
		//			target_upper_cardinality = 1
		//		}
		//		upper_cardinality = 1
		//		lower_cardinality = 1
		//	}
		instantiate_node(new_model, "Class", "LHS")
		instantiate_attribute(new_model, "LHS", "lower_cardinality", 1)
		instantiate_attribute(new_model, "LHS", "upper_cardinality", 1)
		instantiate_link(new_model, "Association", "LHS_constraint", "LHS", "Expression")
		instantiate_attribute(new_model, "LHS_constraint", "name", "constraint")
		instantiate_attribute(new_model, "LHS_constraint", "target_upper_cardinality", 1)

		//	Class NAC {}
		instantiate_node(new_model, "Class", "NAC")

		//	Class Pre_Element {
		//		label : String {
		//			target_lower_cardinality = 1
		//			target_upper_cardinality = 1
		//		}
		//		constraint : Expression {
		//			target_upper_cardinality = 1
		//		}
		//	}
		instantiate_node(new_model, "Class", "Pre_Element")
		instantiate_link(new_model, "Association", "Pre_Element_label", "Pre_Element", "String")
		instantiate_attribute(new_model, "Pre_Element_label", "name", "label")
		instantiate_attribute(new_model, "Pre_Element_label", "target_lower_cardinality", 1)
		instantiate_attribute(new_model, "Pre_Element_label", "target_upper_cardinality", 1)
		instantiate_link(new_model, "Association", "Pre_Element_constraint", "Pre_Element", "Expression")
		instantiate_attribute(new_model, "Pre_Element_constraint", "name", "constraint")
		instantiate_attribute(new_model, "Pre_Element_constraint", "target_upper_cardinality", 1)

		// Association LHS_contains (LHS, Pre_Element) {}
		instantiate_link(new_model, "Association", "LHS_contains", "LHS", "Pre_Element")
		instantiate_attribute(new_model, "LHS_contains", "name", "contains")

		// Association NAC_contains (NAC, Pre_Element) {}
		instantiate_link(new_model, "Association", "NAC_contains", "NAC", "Pre_Element")
		instantiate_attribute(new_model, "NAC_contains", "name", "contains")

		inheritor = "Pre_Element"

	else:
		//	Class RHS {
		//		action : FuncDef {
		//			target_upper_cardinality = 1
		//		}
		//		upper_cardinality = 1
		//		lower_cardinality = 1
		//	}
		instantiate_node(new_model, "Class", "RHS")
		instantiate_attribute(new_model, "RHS", "lower_cardinality", 1)
		instantiate_attribute(new_model, "RHS", "upper_cardinality", 1)
		instantiate_link(new_model, "Association", "RHS_action", "RHS", "FuncDef")
		instantiate_attribute(new_model, "RHS_action", "name", "action")
		instantiate_attribute(new_model, "RHS_action", "target_upper_cardinality", 1)

		//	Class Post_Element {
		//		label : String {
		//			target_lower_cardinality = 1
		//			target_upper_cardinality = 1
		//		}
		//		value : Expression {
		//			target_upper_cardinality = 1
		//		}
		//	}
		instantiate_node(new_model, "Class", "Post_Element")
		instantiate_link(new_model, "Association", "Post_Element_label", "Post_Element", "String")
		instantiate_attribute(new_model, "Post_Element_label", "name", "label")
		instantiate_attribute(new_model, "Post_Element_label", "target_lower_cardinality", 1)
		instantiate_attribute(new_model, "Post_Element_label", "target_upper_cardinality", 1)
		instantiate_link(new_model, "Association", "Post_Element_value", "Post_Element", "Expression")
		instantiate_attribute(new_model, "Post_Element_value", "name", "value")
		instantiate_attribute(new_model, "Post_Element_value", "target_upper_cardinality", 1)

		// Association RHS_contains (RHS, Post_Element) {}
		instantiate_link(new_model, "Association", "RHS_contains", "RHS", "Post_Element")
		instantiate_attribute(new_model, "RHS_contains", "name", "contains")

		inheritor = "Post_Element"

	// Basics are added, now we need to add each node and transition, but slightly modified
	Element keys
	String key
	Element new_entry
	Element entry
	String append
	if (is_pre):
		append = "Pre_"
	else:
		append = "Post_"

	keys = dict_keys(old_m)
	String type_name
	String old_source
	String old_target
	Element to_link
	to_link = create_node()

	while (read_nr_out(keys) > 0):
		key = set_pop(keys)
		entry = old_m[key]

		type_name = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], entry))
		if (type_name == "Class"):
			instantiate_node(new_model, type_name, append + key)

			// Also make it inherit from the "root element"
			instantiate_link(new_model, "Inheritance", "", append + key, inheritor)

		elif (type_name == "Association"):
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
			instantiate_link(new_model, type_name, append + key, append + old_source, append + old_target)

			// 1) Make it inherit from the Root Element (Pre_Element or Post_Element)
			instantiate_link(new_model, "Inheritance", "", append + key, inheritor)
			
		elif (type_name == "Inheritance"):
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))
			instantiate_link(new_model, type_name, append + key, append + old_source, append + old_target)

		elif (bool_not(has_value(entry))):
			create_edge(to_link, entry)

	while (read_nr_out(to_link) > 0):
		entry = set_pop(to_link)
		type_name = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], entry))
		// Primitive values themselves are not copied, so skip that here

		// An instance link, so just copy over (don't make element of Root Element), and don't even copy if it is lower cardinality
		// It is probably an attribute at the meta-language level, so check which one it is
		String name
		name = read_attribute(model["metamodel"], type_name, "name")
		if (bool_not(bool_or(bool_or(name == "lower_cardinality", name == "source_lower_cardinality"), bool_or(name == "constraint", name == "target_lower_cardinality")))):
			// Not a lower cardinality or constraint, so copy
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			if (bool_or(bool_or(name == "name", name == "upper_cardinality"), bool_or(name == "source_upper_cardinality", name == "target_upper_cardinality"))):
				// Keep the old name for the physical attributes!
				instantiate_attribute(new_model, append + old_source, name, read_edge_dst(entry))
			else:
				// Rename the attributes as well
				instantiate_attribute(new_model, append + old_source, append + name, read_edge_dst(entry))

	return new_model!

Element function ramify(model : Element):
	Element rv
	rv = create_node()

	// Create Pre part
	dict_add(rv, "pre", ramify_func(model, "pre"))

	// Create Post part
	dict_add(rv, "post", ramify_func(model, "post"))

	return rv!
