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

Element function ramify(model : Element):
	// Create new model structure
	Element new_model
	new_model = instantiate_model(model["metamodel"])

	// 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, "SimpleAttribute", "Natural")
	instantiate_attribute(new_model, "Natural", "constraint", constraint_Natural)

	instantiate_node(new_model, "SimpleAttribute", "String")
	instantiate_attribute(new_model, "String", "constraint", constraint_String)

	instantiate_node(new_model, "SimpleAttribute", "Boolean")
	instantiate_attribute(new_model, "Boolean", "constraint", constraint_Boolean)

	instantiate_node(new_model, "SimpleAttribute", "Location")
	instantiate_attribute(new_model, "Location", "constraint", constraint_Location)

	instantiate_node(new_model, "ActionLanguage", "ActionCode")

	// Add some default elements
	//	Class LHS_Root {
	//		constraint? : ActionCode
	//		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)
	model_define_attribute(new_model, "LHS_Root", "constraint", True, "ActionCode")

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

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

	//	Class PreElement {
	//		label : String
	//		constraint? : ActionCode
	//	}
	instantiate_node(new_model, "Class", "PreElement")
	model_define_attribute(new_model, "PreElement", "label", False, "String")
	model_define_attribute(new_model, "PreElement", "constraint", True, "ActionCode")

	// Association LHS_contains (LHS_Root, PreElement) {}
	instantiate_link(new_model, "Association", "LHS_contains", "LHS_Root", "PreElement")

	//	Class RHS {
	//		action? : ActionCode
	//		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_attribute(new_model, "RHS", "name", "RHS")
	model_define_attribute(new_model, "RHS", "action", True, "ActionCode")

	//	Class PostElement {
	//		label : String
	//		action? : ActionCode
	//	}
	instantiate_node(new_model, "Class", "PostElement")
	model_define_attribute(new_model, "PostElement", "label", False, "String")
	model_define_attribute(new_model, "PostElement", "action", True, "ActionCode")
	model_define_attribute(new_model, "PostElement", "__id__", True, "ActionCode")

	// Association RHS_contains (RHS, PostElement) {}
	instantiate_link(new_model, "Association", "RHS_contains", "RHS", "PostElement")

	// 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 = set_to_list(dict_keys(old_m))
	String type_name
	String old_source
	String old_target
	String attr_name
	Element masked_attributes
	Element copied_attributes
	Element local_copied_attributes

	masked_attributes = set_create()
	set_add(masked_attributes, "constraint")
	set_add(masked_attributes, "lower_cardinality")
	set_add(masked_attributes, "source_lower_cardinality")
	set_add(masked_attributes, "target_lower_cardinality")

	copied_attributes = set_create()
	set_add(copied_attributes, "upper_cardinality")
	set_add(copied_attributes, "source_upper_cardinality")
	set_add(copied_attributes, "target_upper_cardinality")
	set_add(copied_attributes, "name")

	while (list_len(keys) > 0):
		key = list_pop(keys, 0)
		entry = old_m[key]

		type_name = read_type(model, key)
		if (type_name == "Class"):
			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, "PreElement")
			instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")

			local_copied_attributes = set_copy(copied_attributes)
			while (set_len(local_copied_attributes) > 0):
				attr_name = set_pop(local_copied_attributes)
				if (element_neq(read_attribute(model, key, attr_name), read_root())):
					// Attribute was defined, so reassign in both LHS and RHS
					if (attr_name == "name"):
						instantiate_attribute(new_model, "Pre_" + key, attr_name, string_join("Pre_", read_attribute(model, key, attr_name)))
						instantiate_attribute(new_model, "Post_" + key, attr_name, string_join("Post_", read_attribute(model, key, attr_name)))
					else:
						instantiate_attribute(new_model, "Pre_" + key, attr_name, read_attribute(model, key, attr_name))
						instantiate_attribute(new_model, "Post_" + key, attr_name, read_attribute(model, key, attr_name))

		elif (type_name == "AttributeLink"):
			// Got an attribute, so find out the source and name
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			attr_name = read_attribute(model, key, "name")

			// TODO distinguish between Simple and Complex Attributes

			if (dict_in(new_model["model"], "Pre_" + old_source)):
				// Only if it is a class that it originates from is a class, should we add this
				if (is_nominal_instance(model, old_source, "Element")):
					// If the attribute is one used in the conformance check, we should mask it
					// Add the attribute in the new model, altering its name and making it optional
					model_define_attribute(new_model, "Pre_" + old_source, "constraint_" + attr_name, True, "ActionCode")
					model_define_attribute(new_model, "Post_" + old_source, "value_" + attr_name, True, "ActionCode")
			else:
				// Queue for later
				list_append(keys, key)
				continue!

		elif (type_name == "Association"):
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))

			if (bool_and(dict_in(new_model["model"], "Pre_" + old_source), dict_in(new_model["model"], "Pre_" + old_target))):
				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)

				// Make it inherit from the Root Element (PreElement or PostElement)
				instantiate_link(new_model, "Inheritance", "", "Pre_" + key, "PreElement")
				instantiate_link(new_model, "Inheritance", "", "Post_" + key, "PostElement")
			else:
				// Queue for later
				list_append(keys, key)
				continue!
				
			local_copied_attributes = set_copy(copied_attributes)
			while (set_len(local_copied_attributes) > 0):
				attr_name = set_pop(local_copied_attributes)
				if (element_neq(read_attribute(model, key, attr_name), read_root())):
					// Attribute was defined, so reassign in both LHS and RHS
					if (attr_name == "name"):
						instantiate_attribute(new_model, "Pre_" + key, attr_name, string_join("Pre_", read_attribute(model, key, attr_name)))
						instantiate_attribute(new_model, "Post_" + key, attr_name, string_join("Post_", read_attribute(model, key, attr_name)))
					else:
						instantiate_attribute(new_model, "Pre_" + key, attr_name, read_attribute(model, key, attr_name))
						instantiate_attribute(new_model, "Post_" + key, attr_name, read_attribute(model, key, attr_name))

		elif (type_name == "Inheritance"):
			old_source = reverseKeyLookup(model["model"], read_edge_src(entry))
			old_target = reverseKeyLookup(model["model"], read_edge_dst(entry))

			if (bool_and(dict_in(new_model["model"], "Pre_" + old_source), dict_in(new_model["model"], "Pre_" + old_target))):
				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)
			else:
				// Queue for later
				list_append(keys, key)
				continue!

	// 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")
	instantiate_attribute(new_model, "Success", "name", "Success")

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

	//  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")
	instantiate_attribute(new_model, "Query", "name", "Query")

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

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

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

	//	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!
