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

Boolean function lola_format(model : Element):
	Element all_places
	String place
	String name
	Integer tokens
	String transition
	Element all_transitions
	Element associations
	Element association
	String weight
	String lola_net
	String query
	String place_output
	String marking_output
	String transition_output

	all_places = allInstances(model, "PN/Place")
	log("ALL PLACES")
	log(cast_i2s(list_len(all_places)))
	place_output = "PLACE "
	marking_output = "MARKING "
	while (set_len(all_places) > 0):
		place = set_pop(all_places)
		name = read_attribute(model, place, "name")
		tokens = read_attribute(model, place, "tokens")

		place_output = string_join(place_output, name)

		if tokens > 0:
			if marking_output != "MARKING ":
				marking_output = string_join(marking_output, ", ")
			marking_output = string_join(marking_output, name)
			marking_output = string_join(marking_output, ": ")
			marking_output = string_join(marking_output, cast_i2s(tokens))
	
		if list_len(all_places) == 0:
			place_output = string_join(place_output, ";")
			marking_output = string_join(marking_output, ";")
		else:
			place_output = string_join(place_output, ", ")

	lola_net = string_join(place_output, marking_output)

	all_transitions = allInstances(model, "PN/Transition")
	transition_output = ""
	while (set_len(all_transitions) > 0):
		transition = set_pop(all_transitions)
		name = read_attribute(model, transition, "name")

		transition_output = string_join(transition_output, " TRANSITION ")
		transition_output = string_join(transition_output, name)
		transition_output = string_join(transition_output, " CONSUME ")

		associations = allIncomingAssociationInstances(model, transition, "PN/P2T")
		while (set_len(associations) > 0):
			association = set_pop(associations)
			place = readAssociationSource(model, association)
			weight = read_attribute(model, association, "weight")
			if cast_s2i(weight) > 0:
				transition_output = string_join(transition_output, read_attribute(model, place, "name"))
				transition_output = string_join(transition_output, ": ")
				transition_output = string_join(transition_output, weight)

			if list_len(associations) == 0:
				transition_output = string_join(transition_output, ";")
			else:
				transition_output = string_join(transition_output, ", ")

		transition_output = string_join(transition_output, " PRODUCE ")

		associations = allOutgoingAssociationInstances(model, transition, "PN/T2P")
		while (set_len(associations) > 0):
			association = set_pop(associations)
			place = readAssociationDestination(model, association)
			weight = read_attribute(model, association, "weight")
			if cast_s2i(weight) > 0:
				transition_output = string_join(transition_output, read_attribute(model, place, "name"))
				transition_output = string_join(transition_output, ": ")
				transition_output = string_join(transition_output, weight)

			if list_len(associations) == 0:
				transition_output = string_join(transition_output, ";")
			else:
				transition_output = string_join(transition_output, ", ")

	lola_net = string_join(lola_net, transition_output)

	all_places = allInstances(model, "Query/Place")
	if set_len(all_places) > 0:
		place = set_pop(all_places)
		name = read_attribute(model, place, "name")
		tokens = read_attribute(model, place, "tokens")
		query = string_join(name, ' = ')
		query = string_join(query, cast_i2s(tokens))

	log("PETRINET OUTPUT")
	log(lola_net)
	log("\n QUERY OUTPUT")
	log(query)
	output = string_join("{\"petrinet\":\"", lola_net)
	output = string_join(output, "\" , \"query\":\"")
	output = string_join(output, query)
	output = string_join(output, "\"}")

	log("\n JSON OUTPUT")
	log(output)

	String port
	port = comm_connect("lola")
	comm_set(port, output)

	String result
	String path
	result = comm_get(port)
	path = comm_get(port)
	log("\nLola safety query analysis result: " + result)
	log("\nLola path: "+path)
	comm_close(port)

	return True!
