include "primitives.alh"
include "object_operations.alh"
include "library.alh"
include "conformance_scd.alh"
include "modelling.alh"
include "typing.alh"

String function constraint_Natural(model : Element, name : String):
	Element self
	self = model["model"][name]
	if (is_physical_int(self)):
		if (integer_gte(self, 0)):
			return "OK"!
		else:
			return "Natural number not larger than or equal to zero"!
	else:
		return "Natural number not larger than or equal to zero"!

Element function constraint_String(model : Element, name : String):
	Element self
	self = model["model"][name]
	if (is_physical_string(self)):
		return "OK"!
	else:
		return "String has non-string instance"!

Element function constraint_Boolean(model : Element, name : String):
	Element self
	self = model["model"][name]
	if (is_physical_boolean(self)):
		return "OK"!
	else:
		return "Boolean has non-boolean instance"!

Element function constraint_Location(model : Element, name : String):
	Element self
	self = model["model"][name]
	if (is_physical_string(self)):
		if (element_neq(import_node(self), read_root())):
			return "OK"!
		else:
			return "Location references non-existing element!"!
	else:
		return "Location has non-string instance"!

Element function constraint_ActionLanguage(model : Element, name : String):
	Element self
	self = model["model"][name]
	if (is_physical_string(self)):
		if (element_neq(import_node(self), read_root())):
			return "OK"!
		else:
			return "ActionLanguage references non-existing element!"!
	else:
		return "ActionLanguage has non-string value!"!

Element function initialize_SCD(location : String):
	if (element_neq(import_node(location), read_root())):
		return import_node(location)!

	Element scd
	String al_location
	al_location = "models/ActionLanguage"

	scd = instantiate_bottom()

	model_add_node(scd, "Element")
	model_add_node(scd, "Class")
	model_add_node(scd, "Attribute")
	model_add_node(scd, "SimpleAttribute")
	model_add_node(scd, "String")
	model_add_value(scd, "name_value", "name")
	model_add_edge(scd, "Association", "Class", "Class")
	model_add_edge(scd, "Inheritance", "Class", "Class")
	model_add_edge(scd, "AttributeLink", "Class", "Attribute")
	model_add_edge(scd, "attr_name", "AttributeLink", "String")
	model_add_edge(scd, "attr_name_name", "attr_name", "name_value")
	model_add_edge(scd, "class_inh_element", "Class", "Element")
	model_add_edge(scd, "attribute_inh_element", "Attribute", "Element")
	model_add_edge(scd, "simple_inh_attribute", "SimpleAttribute", "Attribute")
	model_add_edge(scd, "association_inh_element", "Association", "Class")
	model_add_edge(scd, "attributelink_inh_element", "AttributeLink", "Element")

	// Retype to self
	retype_model(scd, scd)
	retype(scd, "Element", "Class")
	retype(scd, "Class", "Class")
	retype(scd, "Attribute", "Class")
	retype(scd, "SimpleAttribute", "Class")
	retype(scd, "String", "SimpleAttribute")
	retype(scd, "name_value", "String")
	retype(scd, "Association", "Association")
	retype(scd, "Inheritance", "Association")
	retype(scd, "AttributeLink", "Association")
	retype(scd, "attr_name", "AttributeLink")
	retype(scd, "attr_name_name", "attr_name")
	retype(scd, "class_inh_element", "Inheritance")
	retype(scd, "attribute_inh_element", "Inheritance")
	retype(scd, "simple_inh_attribute", "Inheritance")
	retype(scd, "association_inh_element", "Inheritance")
	retype(scd, "attributelink_inh_element", "Inheritance")

	// Add some attributes, now that it is an ordinary model
	instantiate_node(scd, "SimpleAttribute", "Location")
	instantiate_attribute(scd, "Location", "name", "Location")
	instantiate_node(scd, "SimpleAttribute", "Natural")
	instantiate_attribute(scd, "Natural", "name", "Natural")
	instantiate_node(scd, "SimpleAttribute", "Boolean")
	instantiate_attribute(scd, "Boolean", "name", "Boolean")
	instantiate_link(scd, "AttributeLink", "attr_optional", "AttributeLink", "Boolean")
	instantiate_attribute(scd, "attr_optional", "name", "optional")
	instantiate_attribute(scd, "attr_optional", "optional", False)
	instantiate_attribute(scd, "attr_name", "optional", False)

	instantiate_node(scd, "Class", "ActionLanguage")
	instantiate_link(scd, "Inheritance", "", "ActionLanguage", "Attribute")

	model_define_attribute(scd, "Class", "lower_cardinality", True, "Natural")
	model_define_attribute(scd, "Class", "upper_cardinality", True, "Natural")
	model_define_attribute(scd, "Association", "source_lower_cardinality", True, "Natural")
	model_define_attribute(scd, "Association", "target_lower_cardinality", True, "Natural")
	model_define_attribute(scd, "Association", "source_upper_cardinality", True, "Natural")
	model_define_attribute(scd, "Association", "target_upper_cardinality", True, "Natural")

	model_define_attribute(scd, "Class", "name", False, "String")
	model_define_attribute(scd, "Class", "abstract", True, "Boolean")
	model_define_attribute(scd, "SimpleAttribute", "name", False, "String")
	instantiate_attribute(scd, "String", "name", "String")
	instantiate_attribute(scd, "Element", "name", "Element")
	instantiate_attribute(scd, "Element", "abstract", True)
	instantiate_attribute(scd, "Attribute", "name", "Attribute")
	instantiate_attribute(scd, "Attribute", "abstract", True)
	instantiate_attribute(scd, "Class", "name", "Class")
	instantiate_attribute(scd, "Association", "name", "Association")
	instantiate_attribute(scd, "SimpleAttribute", "name", "SimpleAttribute")
	instantiate_attribute(scd, "Location", "name", "Location")
	instantiate_attribute(scd, "Natural", "name", "Natural")
	instantiate_attribute(scd, "Boolean", "name", "Boolean")
	instantiate_attribute(scd, "Inheritance", "name", "Inheritance")
	instantiate_attribute(scd, "AttributeLink", "name", "AttributeLink")

	// Export already, to allow AL to pick it up
	export_node(location, scd)
	// Add in the Action Language metamodel
	initialize_AL(location, al_location)

	// Define additional attributes that define functions
	instantiate_node(scd, "ActionLanguage", "ActionCode")
	instantiate_attribute(scd, "ActionLanguage", "name", "ActionLanguage")
	model_define_attribute(scd, "Element", "constraint", True, "ActionCode")

	// Define some constraints
	//instantiate_attribute_code(scd, "Natural", "constraint", constraint_Natural)
	//instantiate_attribute_code(scd, "String", "constraint", constraint_String)
	//instantiate_attribute_code(scd, "Boolean", "constraint", constraint_Boolean)
	//instantiate_attribute_code(scd, "Location", "constraint", constraint_Location)
	//instantiate_attribute_code(scd, "ActionLanguage", "constraint", constraint_ActionLanguage)

	instantiate_node(scd, "Class", "GlobalConstraint")
	model_define_attribute(scd, "GlobalConstraint", "global_constraint", False, "ActionCode")
	instantiate_attribute(scd, "GlobalConstraint", "name", "GlobalConstraint")

	dict_overwrite(scd, "types", get_type_mapping(scd))

	return scd!

Void function initialize_AL(scd_location : String, export_location : String):
	// TODO this should be written in a file-based model and not created like this in the bootstrap
	Element model
	Element scd_model

	if (element_neq(import_node(export_location), read_root())):
		return!

	scd_model = import_node(scd_location)
	model = instantiate_model(scd_model)

	instantiate_node(model, "Class", "Element")
	instantiate_node(model, "Class", "Action")
	instantiate_node(model, "Class", "Statement")
	instantiate_node(model, "Class", "Expression")
	instantiate_node(model, "Class", "funcdef")
	instantiate_node(model, "Class", "param")
	instantiate_node(model, "Class", "param_dict")
	instantiate_node(model, "Class", "if")
	instantiate_node(model, "Class", "break")
	instantiate_node(model, "Class", "while")
	instantiate_node(model, "Class", "continue")
	instantiate_node(model, "Class", "assign")
	instantiate_node(model, "Class", "return")
	instantiate_node(model, "Class", "output")
	instantiate_node(model, "Class", "declare")
	instantiate_node(model, "Class", "global")
	instantiate_node(model, "Class", "access")
	instantiate_node(model, "Class", "constant")
	instantiate_node(model, "Class", "input")
	instantiate_node(model, "Class", "resolve")
	instantiate_node(model, "Class", "call")
	instantiate_node(model, "Class", "String")
	instantiate_node(model, "SimpleAttribute", "StringAttr")
	instantiate_node(model, "Class", "Initial")

	instantiate_attribute(model, "Initial", "lower_cardinality", 1)
	instantiate_attribute(model, "Initial", "upper_cardinality", 1)
	instantiate_link(model, "Association", "initial_funcdef", "Initial", "Action")
	instantiate_attribute(model, "initial_funcdef", "target_lower_cardinality", 1)
	instantiate_attribute(model, "initial_funcdef", "target_upper_cardinality", 1)

	instantiate_link(model, "Inheritance", "", "Action", "Element")
	instantiate_link(model, "Inheritance", "", "funcdef", "Action")
	instantiate_link(model, "Inheritance", "", "param", "Action")
	instantiate_link(model, "Inheritance", "", "param_dict", "Element")
	instantiate_link(model, "Inheritance", "", "Statement", "Action")
	instantiate_link(model, "Inheritance", "", "Expression", "Action")
	instantiate_link(model, "Inheritance", "", "resolve", "Statement")
	instantiate_link(model, "Inheritance", "", "if", "Statement")
	instantiate_link(model, "Inheritance", "", "break", "Statement")
	instantiate_link(model, "Inheritance", "", "continue", "Statement")
	instantiate_link(model, "Inheritance", "", "global", "Statement")
	instantiate_link(model, "Inheritance", "", "while", "Statement")
	instantiate_link(model, "Inheritance", "", "assign", "Statement")
	instantiate_link(model, "Inheritance", "", "return", "Statement")
	instantiate_link(model, "Inheritance", "", "call", "Statement")
	instantiate_link(model, "Inheritance", "", "declare", "Statement")
	instantiate_link(model, "Inheritance", "", "output", "Statement")
	instantiate_link(model, "Inheritance", "", "call", "Expression")
	instantiate_link(model, "Inheritance", "", "access", "Expression")
	instantiate_link(model, "Inheritance", "", "constant", "Expression")
	instantiate_link(model, "Inheritance", "", "input", "Expression")
	instantiate_link(model, "Inheritance", "", "String", "Element")

	instantiate_link(model, "Association", "dict_link", "Action", "Element")
	model_define_attribute(model, "dict_link", "name", False, "StringAttr")

	instantiate_link(model, "Association", "Statement_next", "Statement", "Statement")
	instantiate_link(model, "Association", "if_cond", "if", "Expression")
	instantiate_link(model, "Association", "if_then", "if", "Statement")
	instantiate_link(model, "Association", "if_else", "if", "Statement")
	instantiate_link(model, "Association", "while_cond", "while", "Expression")
	instantiate_link(model, "Association", "while_body", "while", "Statement")
	instantiate_link(model, "Association", "assign_var", "assign", "resolve")
	instantiate_link(model, "Association", "assign_value", "assign", "Expression")
	instantiate_link(model, "Association", "break_while", "break", "while")
	instantiate_link(model, "Association", "continue_while", "continue", "while")
	instantiate_link(model, "Association", "return_value", "return", "Expression")
	instantiate_link(model, "Association", "resolve_var", "resolve", "Element")
	instantiate_link(model, "Association", "access_var", "access", "resolve")
	instantiate_link(model, "Association", "constant_node", "constant", "Element")
	instantiate_link(model, "Association", "output_node", "output", "Expression")
	instantiate_link(model, "Association", "global_var", "global", "String")
	instantiate_link(model, "Association", "declare_var", "declare", "Element")
	instantiate_link(model, "Association", "param_name", "param", "String")
	instantiate_link(model, "Association", "param_value", "param", "Expression")
	instantiate_link(model, "Association", "param_next_param", "param", "param")
	instantiate_link(model, "Association", "funcdef_body", "funcdef", "Statement")
	instantiate_link(model, "Association", "funcdef_params", "funcdef", "param_dict")
	instantiate_link(model, "Association", "funcdef_mutable", "funcdef", "Element")
	instantiate_link(model, "Association", "call_func", "call", "Expression")
	instantiate_link(model, "Association", "call_params", "call", "param")
	instantiate_link(model, "Association", "call_last_param", "call", "param")
	instantiate_link(model, "Association", "param_dict_link", "param_dict", "Element")
	instantiate_link(model, "Inheritance", "", "Statement_next", "dict_link")
	instantiate_link(model, "Inheritance", "", "if_cond", "dict_link")
	instantiate_link(model, "Inheritance", "", "if_then", "dict_link")
	instantiate_link(model, "Inheritance", "", "if_else", "dict_link")
	instantiate_link(model, "Inheritance", "", "while_cond", "dict_link")
	instantiate_link(model, "Inheritance", "", "while_body", "dict_link")
	instantiate_link(model, "Inheritance", "", "assign_var", "dict_link")
	instantiate_link(model, "Inheritance", "", "assign_value", "dict_link")
	instantiate_link(model, "Inheritance", "", "break_while", "dict_link")
	instantiate_link(model, "Inheritance", "", "continue_while", "dict_link")
	instantiate_link(model, "Inheritance", "", "return_value", "dict_link")
	instantiate_link(model, "Inheritance", "", "resolve_var", "dict_link")
	instantiate_link(model, "Inheritance", "", "access_var", "dict_link")
	instantiate_link(model, "Inheritance", "", "constant_node", "dict_link")
	instantiate_link(model, "Inheritance", "", "output_node", "dict_link")
	instantiate_link(model, "Inheritance", "", "global_var", "dict_link")
	instantiate_link(model, "Inheritance", "", "declare_var", "dict_link")
	instantiate_link(model, "Inheritance", "", "param_name", "dict_link")
	instantiate_link(model, "Inheritance", "", "param_value", "dict_link")
	instantiate_link(model, "Inheritance", "", "param_next_param", "dict_link")
	instantiate_link(model, "Inheritance", "", "funcdef_body", "dict_link")
	instantiate_link(model, "Inheritance", "", "funcdef_params", "dict_link")
	instantiate_link(model, "Inheritance", "", "funcdef_mutable", "dict_link")
	instantiate_link(model, "Inheritance", "", "call_func", "dict_link")
	instantiate_link(model, "Inheritance", "", "call_params", "dict_link")
	instantiate_link(model, "Inheritance", "", "call_last_param", "dict_link")
	instantiate_link(model, "Inheritance", "", "param_dict_link", "dict_link")

	instantiate_attribute(model, "if_cond", "target_lower_cardinality", 1)
	instantiate_attribute(model, "if_then", "target_lower_cardinality", 1)
	instantiate_attribute(model, "while_cond", "target_lower_cardinality", 1)
	instantiate_attribute(model, "while_body", "target_lower_cardinality", 1)
	instantiate_attribute(model, "assign_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "assign_value", "target_lower_cardinality", 1)
	instantiate_attribute(model, "break_while", "target_lower_cardinality", 1)
	instantiate_attribute(model, "continue_while", "target_lower_cardinality", 1)
	instantiate_attribute(model, "resolve_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "access_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "constant_node", "target_lower_cardinality", 1)
	instantiate_attribute(model, "output_node", "target_lower_cardinality", 1)
	instantiate_attribute(model, "global_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "declare_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "param_name", "target_lower_cardinality", 1)
	instantiate_attribute(model, "param_value", "target_lower_cardinality", 1)
	instantiate_attribute(model, "funcdef_body", "target_lower_cardinality", 1)
	instantiate_attribute(model, "funcdef_params", "target_lower_cardinality", 1)
	instantiate_attribute(model, "call_func", "target_lower_cardinality", 1)

	instantiate_attribute(model, "Statement_next", "target_upper_cardinality", 1)
	instantiate_attribute(model, "if_cond", "target_upper_cardinality", 1)
	instantiate_attribute(model, "if_then", "target_upper_cardinality", 1)
	instantiate_attribute(model, "if_else", "target_upper_cardinality", 1)
	instantiate_attribute(model, "while_cond", "target_upper_cardinality", 1)
	instantiate_attribute(model, "while_body", "target_upper_cardinality", 1)
	instantiate_attribute(model, "assign_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "assign_value", "target_upper_cardinality", 1)
	instantiate_attribute(model, "break_while", "target_upper_cardinality", 1)
	instantiate_attribute(model, "continue_while", "target_upper_cardinality", 1)
	instantiate_attribute(model, "return_value", "target_upper_cardinality", 1)
	instantiate_attribute(model, "resolve_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "access_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "constant_node", "target_upper_cardinality", 1)
	instantiate_attribute(model, "output_node", "target_upper_cardinality", 1)
	instantiate_attribute(model, "global_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "declare_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "param_name", "target_upper_cardinality", 1)
	instantiate_attribute(model, "param_value", "target_upper_cardinality", 1)
	instantiate_attribute(model, "param_next_param", "target_upper_cardinality", 1)
	instantiate_attribute(model, "funcdef_body", "target_upper_cardinality", 1)
	instantiate_attribute(model, "funcdef_params", "target_upper_cardinality", 1)
	instantiate_attribute(model, "funcdef_mutable", "target_upper_cardinality", 1)
	instantiate_attribute(model, "call_func", "target_upper_cardinality", 1)
	instantiate_attribute(model, "call_params", "target_upper_cardinality", 1)
	instantiate_attribute(model, "call_last_param", "target_upper_cardinality", 1)

	dict_overwrite(model, "types", get_type_mapping(model))

	export_node(export_location, model)
	return !
