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

Element function ramify(model : Element):
	// 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, "source", model)
	dict_add(new_model, "target", model)

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

	// Add in some primitives
	instantiate_node(new_model, "Class", "Natural")
	instantiate_node(new_model, "Class", "String")
	instantiate_node(new_model, "Class", "Any")

	// Add in the complete AL metamodel
	add_AL_to_MM(new_model)

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

	//  Class LHS : LHS_Root {}
	instantiate_node(new_model, "Class", "LHS")
	instantiate_link(new_model, "Inheritance", "", "LHS", "LHS_Root")

	//  Class NAC : LHS_Root {}
	instantiate_node(new_model, "Class", "NAC")
	instantiate_link(new_model, "Inheritance", "", "NAC", "LHS_Root")

	//	Class Pre_Element {
	//		label : String {
	//			target_lower_cardinality = 1
	//			target_upper_cardinality = 1
	//		}
	//		constraint : funcdef {
	//			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", "funcdef")
	instantiate_attribute(new_model, "Pre_Element_constraint", "name", "constraint")
	instantiate_attribute(new_model, "Pre_Element_constraint", "target_upper_cardinality", 1)

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

	//	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 : funcdef {
	//			target_upper_cardinality = 1
	//		}
	//		action : funcdef {
	//			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", "funcdef")
	instantiate_attribute(new_model, "Post_Element_value", "name", "value")
	instantiate_attribute(new_model, "Post_Element_value", "target_upper_cardinality", 1)
	instantiate_link(new_model, "Association", "Post_Element_action", "Post_Element", "funcdef")
	instantiate_attribute(new_model, "Post_Element_action", "name", "action")
	instantiate_attribute(new_model, "Post_Element_action", "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")

	// Basics are added, now we need to add each node and transition, but slightly modified
	Element keys
	String key
	Element new_entry
	Element entry

	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"):
			log("Added Pre_" + key)
			instantiate_node(new_model, type_name, "Pre_" + key)
			instantiate_node(new_model, type_name, "Post_" + key)

			// Also make it inherit from the "root element"
			instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "Pre_Element")
			instantiate_link(new_model, "Inheritance", "", "Post_" + key, "Post_Element")

		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, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
			instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + old_target)

			// 1) Make it inherit from the Root Element (Pre_Element or Post_Element)
			instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "Pre_Element")
			instantiate_link(new_model, "Inheritance", "", "Post_" + key, "Post_Element")
			
		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, "Pre_" + key, "Pre_" + old_source, "Pre_" + old_target)
			instantiate_link(new_model, type_name, "Post_" + key, "Post_" + old_source, "Post_" + 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, "Pre_" + old_source, name, read_edge_dst(entry))
				instantiate_attribute(new_model, "Post_" + old_source, name, read_edge_dst(entry))
			else:
				// Rename the attributes as well
				instantiate_attribute(new_model, "Pre_" + old_source, "Pre_" + name, read_edge_dst(entry))
				instantiate_attribute(new_model, "Post_" + old_source, "Post_" + name, read_edge_dst(entry))

	// Define schedule over these elements

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

	//	Class Success : Entry {}
	instantiate_node(new_model, "Class", "Success")
	instantiate_link(new_model, "Inheritance", "", "Success", "Entry")

	//	Class Failure : Entry {}
	instantiate_node(new_model, "Class", "Failure")
	instantiate_link(new_model, "Inheritance", "", "Failure", "Entry")

	//  Class Rule : Entry {}
	instantiate_node(new_model, "Class", "Rule")
	instantiate_link(new_model, "Inheritance", "", "Rule", "Entry")

	//  Association OnSuccess(Rule, Entry){
	//		target_lower_cardinality = 1
	//		target_upper_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "OnSuccess", "Rule", "Entry")
	instantiate_attribute(new_model, "OnSuccess", "target_lower_cardinality", 1)
	instantiate_attribute(new_model, "OnSuccess", "target_upper_cardinality", 1)

	//  Association OnFailure(Rule, Entry){
	//		target_lower_cardinality = 1
	//		target_upper_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "OnFailure", "Rule", "Entry")
	instantiate_attribute(new_model, "OnFailure", "target_lower_cardinality", 1)
	instantiate_attribute(new_model, "OnFailure", "target_upper_cardinality", 1)

	// Class LHSRule : Rule {}
	instantiate_node(new_model, "Class", "LHSRule")
	instantiate_link(new_model, "Inheritance", "", "LHSRule", "Rule")

	// 	Association LHSLink (LHSRule, LHS) {
	//		target_lower_cardinality = 1
	//		target_upper_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "LHSLink", "LHSRule", "LHS")
	instantiate_attribute(new_model, "LHSLink", "target_lower_cardinality", 1)
	instantiate_attribute(new_model, "LHSLink", "target_upper_cardinality", 1)

	// 	Association NACLink (LHSRule, NAC) {}
	instantiate_link(new_model, "Association", "NACLink", "LHSRule", "NAC")

	//  Class RHSRule : Rule {}
	instantiate_node(new_model, "Class", "RHSRule")
	instantiate_link(new_model, "Inheritance", "", "RHSRule", "Rule")

	// 	Association RHSLink (RHSRule, RHS) {
	//		target_lower_cardinality = 1
	//		target_upper_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "RHSLink", "RHSRule", "RHS")
	instantiate_attribute(new_model, "RHSLink", "target_lower_cardinality", 1)
	instantiate_attribute(new_model, "RHSLink", "target_upper_cardinality", 1)

	// 	Class Query : LHSRule {}
	instantiate_node(new_model, "Class", "Query")
	instantiate_link(new_model, "Inheritance", "", "Query", "LHSRule")

	//	Class Atomic : LHSRule, RHSRule {}
	instantiate_node(new_model, "Class", "Atomic")
	instantiate_link(new_model, "Inheritance", "", "Atomic", "LHSRule")
	instantiate_link(new_model, "Inheritance", "", "Atomic", "RHSRule")

	// 	Class ForAll : LHSRule, RHSRule {}
	instantiate_node(new_model, "Class", "ForAll")
	instantiate_link(new_model, "Inheritance", "", "ForAll", "LHSRule")
	instantiate_link(new_model, "Inheritance", "", "ForAll", "RHSRule")

	//	Class Composite : Rule {}
	instantiate_node(new_model, "Class", "Composite")
	instantiate_link(new_model, "Inheritance", "", "Composite", "Rule")

	//	Association Initial(Composite, Entry){
	//		target_lower_cardinality = 1
	//		target_upper_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "Initial", "Composite", "Entry")
	instantiate_attribute(new_model, "Initial", "target_lower_cardinality", 1)
	instantiate_attribute(new_model, "Initial", "target_upper_cardinality", 1)

	//	Association Contains(Composite, Entry){
	//		source_upper_cardinality = 1
	//		target_lower_cardinality = 1
	//	}
	instantiate_link(new_model, "Association", "Contains", "Composite", "Entry")
	instantiate_attribute(new_model, "Contains", "source_upper_cardinality", 1)
	instantiate_attribute(new_model, "Contains", "target_lower_cardinality", 1)

	return new_model!
