include "primitives.alh"
include "modelling.alh"
include "object_operations.alh"
include "conformance_scd.alh"
include "io.alh"
include "metamodels.alh"
include "mini_modify.alh"
include "library.alh"

Boolean function main(model : Element):
	String input_value
	Float start_time
	String current_state
	String old_state
	Element transitions
	String transition

	start_time = time()

	Element all_states
	String element_name
	all_states = allInstances(model, "FSA/State")
	while (set_len(all_states) > 0):
		element_name = set_pop(all_states)
		if (value_eq(read_attribute(model, element_name, "initial"), True)):
			current_state = element_name
			old_state = element_name
			break!

	while (True):
		if (has_input()):
			input_value = list_read(string_split(input(), "\n"), 0)

			if (input_value == "__EXIT__"):
				break!

			transitions = allOutgoingAssociationInstances(model, current_state, "FSA/Transition")
			while (set_len(transitions) > 0):
				transition = set_pop(transitions)
				if (cast_string(read_attribute(model, transition, "trigger")) == input_value):
					if (element_neq(read_attribute(model, transition, "raise"), read_root())):
						log(cast_value(time() - start_time) + " output " + cast_string(read_attribute(model, transition, "raise")))
						output(cast_value(time() - start_time) + " output " + cast_string(read_attribute(model, transition, "raise")))
					if (element_neq(read_attribute(model, transition, "script"), read_root())):
						Element func
						func = get_func_AL_model(import_node(read_attribute(model, transition, "script")))
						func()
					current_state = readAssociationDestination(model, transition)
					break!

		output(cast_value(time() - start_time) + " " + cast_string(read_attribute(model, current_state, "name")))
		sleep(0.2)
	return True!
