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

Boolean function main(model : Element):
	// Find all activities
	Element known_activities
	Element known_formalisms
	Element activity_mapping
	Element formalism_mapping
	Element elems
	String elem
	String type
	String new_elem

	activity_mapping = dict_create()
	formalism_mapping = dict_create()
	known_activities = dict_create()
	known_formalisms = dict_create()

	elems = allInstances(model, "PM/Exec")
	while (set_len(elems) > 0):
		elem = set_pop(elems)
		type = read_attribute(model, elem, "name")

		if dict_in(known_activities, type):
			new_elem = known_activities[type]
		else:
			new_elem = instantiate_node(model, "FTG/Activity", "")
			instantiate_attribute(model, new_elem, "name", type)
			dict_add(known_activities, type, new_elem)
		dict_add(activity_mapping, elem, new_elem)

	elems = allInstances(model, "PM/Data")
	while (set_len(elems) > 0):
		elem = set_pop(elems)
		type = read_attribute(model, elem, "type")

		if dict_in(known_formalisms, type):
			new_elem = known_formalisms[type]
		else:
			new_elem = instantiate_node(model, "FTG/Formalism", "")
			instantiate_attribute(model, new_elem, "name", type)
			dict_add(known_formalisms, type, new_elem)
		dict_add(formalism_mapping, elem, new_elem)

	elems = allInstances(model, "PM/Produces")
	while (set_len(elems) > 0):
		elem = set_pop(elems)
		new_elem = instantiate_link(model, "FTG/Produces", "", activity_mapping[readAssociationSource(model, elem)], formalism_mapping[readAssociationDestination(model, elem)])
		instantiate_attribute(model, new_elem, "name", read_attribute(model, elem, "name"))

	elems = allInstances(model, "PM/Consumes")
	while (set_len(elems) > 0):
		elem = set_pop(elems)
		new_elem = instantiate_link(model, "FTG/Consumes", "", formalism_mapping[readAssociationDestination(model, elem)], activity_mapping[readAssociationSource(model, elem)])
		instantiate_attribute(model, new_elem, "name", read_attribute(model, elem, "name"))

	return True!
