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

Element function constraint_natural(model : Element, name : String):
	if (is_physical_int(model["model"][name])):
		if (integer_gte(model["model"][name], 0)):
			return "OK"!
		else:
			return "Natural number not larger than or equal to zero"!
	else:
		return "Natural has non-integer instance"!

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

Element function constraint_if(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "if"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_while(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "while"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_break(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "break"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_continue(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "continue"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_assign(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "assign"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_return(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "return"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_output(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "output"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_input(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "input"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_declare(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "declare"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_global(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "global"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_access(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "access"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_constant(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "constant"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_resolve(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "resolve"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function constraint_call(model : Element, name : String):
	if (is_physical_action(model["model"][name])):
		if (cast_a2s(model["model"][name]) == "call"):
			return "OK"!
		else:
			return "Got wrong action primitive: " + cast_a2s(model["model"][name])!
	else:
		return "Expected physical action value"!

Element function initialize_SCD(location : String):
	Element scd
	scd = instantiate_bottom()

	// Initial model, typed using LTM_bottom
	model_add_node(scd, "Class")
	model_add_node(scd, "Any")
	model_add_node(scd, "String")
	model_add_value(scd, "name", "name")
	model_add_edge(scd, "Association", "Class", "Any")
	model_add_edge(scd, "Inheritance", "Class", "Class")
	model_add_edge(scd, "Association_attribute", "Association", "String")
	model_add_edge(scd, "Association_name", "Association_attribute", "name")
	model_add_edge(scd, "assoc_inh_class", "Association", "Class")
	model_add_edge(scd, "class_inh_any", "Class", "Any")
	model_add_edge(scd, "string_inh_any", "String", "Any")

	// Retype to a "real" LTM, which happens to be itself
	retype_model(scd, scd)
	define_inheritance(scd, "Inheritance")
	retype(scd, "Class", "Class")
	retype(scd, "Any", "Class")
	retype(scd, "String", "Class")
	retype(scd, "name", "String")
	retype(scd, "Association", "Association")
	retype(scd, "Inheritance", "Association")
	retype(scd, "Association_attribute", "Association")
	retype(scd, "Association_name", "Association_attribute")
	retype(scd, "assoc_inh_class", "Inheritance")
	retype(scd, "class_inh_any", "Inheritance")
	retype(scd, "string_inh_any", "Inheritance")

	// Add some attributes, now that it is an ordinary model
	instantiate_node(scd, "Class", "Natural")
	instantiate_link(scd, "Association", "lc", "Class", "Natural")
	instantiate_attribute(scd, "lc", "name", "lower_cardinality")
	instantiate_link(scd, "Association", "uc", "Class", "Natural")
	instantiate_attribute(scd, "uc", "name", "upper_cardinality")
	instantiate_link(scd, "Association", "slc", "Association", "Natural")
	instantiate_attribute(scd, "slc", "name", "source_lower_cardinality")
	instantiate_link(scd, "Association", "suc", "Association", "Natural")
	instantiate_attribute(scd, "suc", "name", "source_upper_cardinality")
	instantiate_link(scd, "Association", "tlc", "Association", "Natural")
	instantiate_attribute(scd, "tlc", "name", "target_lower_cardinality")
	instantiate_link(scd, "Association", "tuc", "Association", "Natural")
	instantiate_attribute(scd, "tuc", "name", "target_upper_cardinality")

	// Add in the Action Language metamodel
	add_AL_to_MM(scd)

	// And add some, to enforce correct physical types
	add_constraint(scd, "Natural", constraint_natural)
	add_constraint(scd, "String", constraint_string)

	// Finally done, so export!
	export_node(location, scd)
	return scd!

Element function initialize_PN(location_SCD : String, location_PN : String):
	Element pn
	Element scd

	scd = import_node(location_SCD)

	pn = instantiate_model(scd)
	define_inheritance(pn, "Inheritance")
	instantiate_node(pn, "Class", "Place")
	instantiate_node(pn, "Class", "Transition")
	instantiate_node(pn, "Class", "Natural")
	instantiate_link(pn, "Association", "P2T", "Place", "Transition")
	instantiate_link(pn, "Association", "T2P", "Transition", "Place")
	instantiate_link(pn, "Association", "Place_tokens", "Place", "Natural")
	instantiate_attribute(pn, "Place_tokens", "name", "tokens")
	instantiate_attribute(pn, "Place_tokens", "target_lower_cardinality", 1)
	instantiate_attribute(pn, "Place_tokens", "target_upper_cardinality", 1)
	instantiate_link(pn, "Association", "P2T_weight", "P2T", "Natural")
	instantiate_attribute(pn, "P2T_weight", "name", "weight")
	instantiate_attribute(pn, "P2T_weight", "target_lower_cardinality", 1)
	instantiate_attribute(pn, "P2T_weight", "target_upper_cardinality", 1)
	instantiate_link(pn, "Association", "T2P_weight", "T2P", "Natural")
	instantiate_attribute(pn, "T2P_weight", "name", "weight")
	instantiate_attribute(pn, "T2P_weight", "target_lower_cardinality", 1)
	instantiate_attribute(pn, "T2P_weight", "target_upper_cardinality", 1)

	// Add constraint on the Natural
	//TODO temporarily done for RAMification to test
	//add_constraint(pn, "Natural", constraint_natural)

	// Add global constraints
	//set_model_constraints(pn, petrinet_constraints)
	export_node(location_PN, pn)

	return pn!

Element function initialize_bottom(location_bottom : String):
	Element ltm_bottom
	ltm_bottom = instantiate_bottom()
	model_add_node(ltm_bottom, "Node")
	model_add_edge(ltm_bottom, "Edge", "Node", "Node")
	model_add_edge(ltm_bottom, "inheritance", "Node", "Node")
	model_add_edge(ltm_bottom, "__inh", "Edge", "Node")

	retype_model(ltm_bottom, ltm_bottom)
	define_inheritance(ltm_bottom, "inheritance")
	retype(ltm_bottom, "Node", "Node")
	retype(ltm_bottom, "Edge", "Edge")
	retype(ltm_bottom, "inheritance", "Edge")
	retype(ltm_bottom, "__inh", "inheritance")

	export_node(location_bottom, ltm_bottom)

	return ltm_bottom!

Element function create_metamodels():
	String location_SCD
	String location_PN
	String location_bottom

	location_SCD = "models/SimpleClassDiagrams"
	location_PN = "models/PetriNets"
	location_bottom = "models/LTM_bottom"

	if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "SimpleClassDiagrams"))):
		initialize_SCD(location_SCD)
	if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "PetriNets"))):
		initialize_PN(location_SCD, location_PN)
	if (bool_not(dict_in(dict_read(dict_read(read_root(), "__hierarchy"), "models"), "LTM_bottom"))):
		initialize_bottom(location_bottom)

	return dict_read(dict_read(read_root(), "__hierarchy"), "models")!

Void function add_AL_to_MM(model : 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", "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_link(model, "Association", "dict_link", "Action", "Any")
	instantiate_link(model, "Association", "to_str", "dict_link", "String")
	instantiate_attribute(model, "to_str", "name", "name")
	instantiate_link(model, "Inheritance", "", "Action", "Any")
	instantiate_link(model, "Inheritance", "", "funcdef", "Action")
	instantiate_link(model, "Inheritance", "", "param", "Action")
	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", "", "call", "Expression")
	instantiate_link(model, "Inheritance", "", "access", "Expression")
	instantiate_link(model, "Inheritance", "", "constant", "Expression")
	instantiate_link(model, "Inheritance", "", "input", "Expression")
	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", "Any")
	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", "Any")
	instantiate_link(model, "Association", "access_var", "access", "Any")
	instantiate_link(model, "Association", "constant_node", "constant", "Any")
	instantiate_link(model, "Association", "output_node", "output", "Expression")
	instantiate_link(model, "Association", "global_var", "global", "String")
	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", "call_func", "call", "Expression")
	instantiate_link(model, "Association", "call_params", "call", "param")
	instantiate_link(model, "Association", "call_last_param", "call", "param")
	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", "", "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", "", "call_func", "dict_link")
	instantiate_link(model, "Inheritance", "", "call_params", "dict_link")
	instantiate_link(model, "Inheritance", "", "call_last_param", "dict_link")

	// Add cardinalities on how many connections are allowed: one of each
	instantiate_attribute(model, "statement_next", "target_upper_cardinality", 1)
	instantiate_attribute(model, "if_cond", "target_lower_cardinality", 1)
	instantiate_attribute(model, "if_cond", "target_upper_cardinality", 1)
	instantiate_attribute(model, "if_then", "target_lower_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_lower_cardinality", 1)
	instantiate_attribute(model, "while_cond", "target_upper_cardinality", 1)
	instantiate_attribute(model, "while_body", "target_lower_cardinality", 1)
	instantiate_attribute(model, "while_body", "target_upper_cardinality", 1)
	instantiate_attribute(model, "assign_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "assign_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "assign_value", "target_lower_cardinality", 1)
	instantiate_attribute(model, "assign_value", "target_upper_cardinality", 1)
	instantiate_attribute(model, "break_while", "target_lower_cardinality", 1)
	instantiate_attribute(model, "break_while", "target_upper_cardinality", 1)
	instantiate_attribute(model, "continue_while", "target_lower_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_lower_cardinality", 1)
	instantiate_attribute(model, "resolve_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "access_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "access_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "constant_node", "target_lower_cardinality", 1)
	instantiate_attribute(model, "constant_node", "target_upper_cardinality", 1)
	instantiate_attribute(model, "output_node", "target_lower_cardinality", 1)
	instantiate_attribute(model, "output_node", "target_upper_cardinality", 1)
	instantiate_attribute(model, "global_var", "target_lower_cardinality", 1)
	instantiate_attribute(model, "global_var", "target_upper_cardinality", 1)
	instantiate_attribute(model, "param_name", "target_lower_cardinality", 1)
	instantiate_attribute(model, "param_name", "target_upper_cardinality", 1)
	instantiate_attribute(model, "param_value", "target_lower_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_lower_cardinality", 1)
	instantiate_attribute(model, "funcdef_body", "target_upper_cardinality", 1)
	instantiate_attribute(model, "call_func", "target_lower_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)

	// Now still allow for constraints on classes
	instantiate_link(model, "Association", "constraint", "Class", "funcdef")
	instantiate_attribute(model, "constraint", "name", "constraint")

	// Add constraints to all primitive classes
	//add_constraint(model, "if", constraint_if)
	//add_constraint(model, "while", constraint_while)
	//add_constraint(model, "break", constraint_break)
	//add_constraint(model, "continue", constraint_continue)
	//add_constraint(model, "assign", constraint_assign)
	//add_constraint(model, "return", constraint_return)
	//add_constraint(model, "output", constraint_output)
	//add_constraint(model, "input", constraint_input)
	//add_constraint(model, "declare", constraint_declare)
	//add_constraint(model, "global", constraint_global)
	//add_constraint(model, "access", constraint_access)
	//add_constraint(model, "constant", constraint_constant)
	//add_constraint(model, "resolve", constraint_resolve)
	//add_constraint(model, "call", constraint_call)
	return !
