瀏覽代碼

Minor optimizations

Yentl Van Tendeloo 8 年之前
父節點
當前提交
eee6f1ce47
共有 7 個文件被更改,包括 46 次插入224 次删除
  1. 1 1
      bootstrap/model_management.alc
  2. 10 16
      bootstrap/modelling.alc
  3. 5 5
      bootstrap/ramify.alc
  4. 12 12
      bootstrap/task_manager.alc
  5. 4 4
      bootstrap/transform.alc
  6. 0 186
      models/environment_to_EPN.alc
  7. 14 0
      sum_times.py

+ 1 - 1
bootstrap/model_management.alc

@@ -210,12 +210,12 @@ Element function model_split(src_model : Element, target_metamodel : Element, re
 	mapping = create_node()
 	length = string_len(retyping_key)
 
-	keys = dict_keys(src_model["model"])
 	second_keys = create_node()
 
 	Element reverse
 	reverse = make_reverse_dictionary(src_model["model"])
 
+	keys = dict_keys(src_model["model"])
 	while (read_nr_out(keys) > 0):
 		name = set_pop(keys)
 		elem = src_model["model"][name]

+ 10 - 16
bootstrap/modelling.alc

@@ -154,7 +154,6 @@ Element function get_subclasses(model : Element, name : String):
 	Element edge
 	String elem
 	Element nodes
-	Element inheritance
 
 	nodes = create_node()
 	set_add(nodes, name)
@@ -186,11 +185,9 @@ Element function get_superclasses(model : Element, name : String):
 	Element edge
 	String elem
 	Element nodes
-	Element inheritance
 
 	nodes = create_node()
 	set_add(nodes, name)
-	inheritance = "Inheritance"
 
 	// Initialize empty set
 	result = create_node()
@@ -205,7 +202,7 @@ Element function get_superclasses(model : Element, name : String):
 			j = 0
 			while (j < num_edges):
 				edge = read_out(model["model"][elem], j)
-				if (value_eq(model["type_mapping"][reverseKeyLookup(model["model"], edge)], inheritance)):
+				if (value_eq(model["type_mapping"][reverseKeyLookup(model["model"], edge)], "Inheritance")):
 					set_add(nodes, reverseKeyLookup(model["model"], read_edge_dst(edge)))
 				j = j + 1
 
@@ -375,15 +372,15 @@ String function model_define_attribute(model : Element, elem : String, name : St
 	return edge_name!
 
 Element function read_attribute(model : Element, element : String, attribute : String):
-	Integer i
-	Integer count
-	Element edge
-	Element edge_type
-	Element elem
-	Element typing
-	Element name
-
 	if (dict_in(model["model"], element)):
+		Integer i
+		Integer count
+		Element edge
+		Element edge_type
+		Element elem
+		Element typing
+		Element name
+
 		elem = model["model"][element]
 		typing = model["type_mapping"]
 		count = read_nr_out(elem)
@@ -465,10 +462,7 @@ String function add_AL(model : Element, element : Element):
 	String elem_name
 
 	todo = create_node()
-	node = create_node()
-	list_append(node, element)
-	list_append(node, "funcdef")
-	set_add(todo, node)
+	set_add(todo, create_tuple(element, "funcdef"))
 
 	while (0 < dict_len(todo)):
 		work_node = set_pop(todo)

+ 5 - 5
bootstrap/ramify.alc

@@ -9,12 +9,12 @@ Element function ramify(model : Element):
 	// Create new model structure
 	Element new_model
 	new_model = create_node()
-	dict_add(new_model, "model", create_node())
-	dict_add(new_model, "type_mapping", create_node())
-	dict_add(new_model, "metamodel", model["metamodel"])
+	dict_add_fast(new_model, "model", create_node())
+	dict_add_fast(new_model, "type_mapping", create_node())
+	dict_add_fast(new_model, "metamodel", model["metamodel"])
 
-	dict_add(new_model, "source", model)
-	dict_add(new_model, "target", model)
+	dict_add_fast(new_model, "source", model)
+	dict_add_fast(new_model, "target", model)
 
 	// Get local variables for parts
 	Element old_m

+ 12 - 12
bootstrap/task_manager.alc

@@ -19,18 +19,18 @@ Void function task_management():
 			task_frame = create_node()
 			output_value = create_node()
 			input_value = create_node()
-			dict_add(task_root, "frame", task_frame)
-			dict_add(task_root, "globals", create_node())
-			dict_add(task_root, "output", output_value)
-			dict_add(task_root, "last_output", output_value)
-			dict_add(task_root, "input", input_value)
-			dict_add(task_root, "last_input", input_value)
-			dict_add(task_frame, "evalstack", create_node())
-			dict_add(task_frame, "returnvalue", create_node())
-			dict_add(task_frame, "phase", "init")
-			dict_add(task_frame, "IP", dict_read(dict_read(read_root(), "__hierarchy"), "__IP"))
-			dict_add(task_frame, "symbols", create_node())
+			dict_add_fast(task_root, "frame", task_frame)
+			dict_add_fast(task_root, "globals", create_node())
+			dict_add_fast(task_root, "output", output_value)
+			dict_add_fast(task_root, "last_output", output_value)
+			dict_add_fast(task_root, "input", input_value)
+			dict_add_fast(task_root, "last_input", input_value)
+			dict_add_fast(task_frame, "evalstack", create_node())
+			dict_add_fast(task_frame, "returnvalue", create_node())
+			dict_add_fast(task_frame, "phase", "init")
+			dict_add_fast(task_frame, "IP", dict_read(dict_read(read_root(), "__hierarchy"), "__IP"))
+			dict_add_fast(task_frame, "symbols", create_node())
 
 			//Add this only at the end, as otherwise the task will already be detected
-			dict_add(read_root(), taskname, task_root)
+			dict_add_fast(read_root(), taskname, task_root)
 	return!

+ 4 - 4
bootstrap/transform.alc

@@ -290,7 +290,7 @@ Element function match(host_model : Element, schedule_model : Element, LHS : Str
 	set_add(mappings, initial_mapping)
 	while (read_nr_out(schedule) > 0):
 		current_element = list_pop(schedule, read_nr_out(schedule) - 1)
-		log("Binding element with label " + cast_v2s(read_attribute(schedule_model, current_element, "label")))
+		//log("Binding element with label " + cast_v2s(read_attribute(schedule_model, current_element, "label")))
 		new_mappings = create_node()
 
 		while (read_nr_out(mappings) > 0):
@@ -304,7 +304,7 @@ Element function match(host_model : Element, schedule_model : Element, LHS : Str
 				set_add(new_mappings, new_map)
 
 		mappings = new_mappings
-		log("Remaining options: " + cast_v2s(read_nr_out(mappings)))
+		//log("Remaining options: " + cast_v2s(read_nr_out(mappings)))
 
 		if (read_nr_out(mappings) == 0):
 			// Stop because we have no more options remaining!
@@ -487,7 +487,7 @@ Boolean function transform_composite(host_model : Element, schedule_model : Elem
 
 	current = set_pop(allAssociationDestinations(schedule_model, composite, "Initial"))
 	while (is_nominal_instance(schedule_model, current, "Rule")):
-		log("Executing " + current)
+		//log("Executing " + current)
 		// Still a rule that we must execute
 		typename = read_type(schedule_model, current)
 		if (typename == "Atomic"):
@@ -540,7 +540,7 @@ Boolean function transform_forall(host_model : Element, schedule_model : Element
 	else:
 		result = False
 
-	log("Matches in forall: " + cast_v2s(read_nr_out(mappings)))
+	//log("Matches in forall: " + cast_v2s(read_nr_out(mappings)))
 	while (read_nr_out(mappings) > 0):
 		mapping = set_pop(mappings)
 		// TODO check if there are actually no deletions happening in the meantime of other matched elements...

+ 0 - 186
models/environment_to_EPN.alc

@@ -1,186 +0,0 @@
-// Pseudo-code
-//    TODO: add link to the ports of the boundary
-//    TODO: update MM to reflect the new changes to the structure (everything is an Activity with a Next link, and everything has a duration)
-
-//  init_place = new_place()
-//	branches = [(init_place, [(0, topmost_model)]]
-//	while branches:
-//		prev, options = branches.pop()
-//		nr = find_min_time(options)
-//		time, cur = options.pop(nr)
-//
-//		if type(cur) == "Event":
-//			// Just add the current node and augment the duration for the next one
-//			prev_model = new_element(cur)
-//			if (cur.next != None):
-//				options.append((time + cur.next.duration, cur.next))
-//				branches.append((prev_model, options))
-//			else:
-//				// recurse upwards until we can follow a link
-//				elem = containee(cur)
-//				while elem.next_activity == None and containee(elem) != None:
-//					elem = containee(elem)
-//				if containee(elem) == None:
-//					// finished this branch
-//					continue!
-//				else:
-//					cur = elem.next_activity
-//					options.append((time + elem.duration, cur.next))
-//					branches.append((prev_model, options))
-//		elif type(cur) == "Sequence":
-//			options.append((time + first.duration, cur.first))
-//			branches.append((prev_model, options))
-//		elif type(cur) == "Parallel":
-//			// Add all starts of the parallel as potential next one
-//			// But keep the previous source, as we only expanded
-//			for next in cur.start_nodes:
-//				options.append((time + next.duration, next))
-//			branches.append((prev, options))
-//		elif type(cur) == "Alternative":
-//			// Expand into new branches, but keep the options as is (add the individual options though)
-//			for next in cur.start_nodes:
-//				options.append((time + next.duration, next))
-//				// Don't rewrite the source, as we effectively want to branch out from this node
-//				branches.append(prev, options)
-
-include "primitives.alh"
-include "modelling.alh"
-include "object_operations.alh"
-
-Element function env_to_EPN(params : Element, output_mms : Element):
-	Element result
-	Element out_model
-	Element in_model
-	String init_place
-	String current_activity
-	Element branches
-	Element options
-	Element branch
-	String previous_place
-	Integer i
-	Integer cnt
-	Integer min_time
-	Integer index_min_time
-	Element option
-	Integer current_time
-	String new_transition
-	String new_model
-	Element containers
-	Element new_options
-	Element inner_elements
-	Element entry
-	String type
-	String prev_model
-	String element
-
-	result = create_node()
-	out_model = instantiate_model(output_mms["Encapsulated_PetriNet"])
-	in_model = params["environment"]
-
-	// Create the initial place
-	init_place = instantiate_node(out_model, "Place", "")
-	instantiate_attribute(out_model, init_place, "tokens", 1)
-
-	// Set current element to the TopActivity, which will be expanded
-	current_activity = set_pop(allInstances(in_model, "TopActivity"))
-
-	// Initialize the data structure with the current element and initial place
-	branches = create_node()
-	options = create_node()
-	list_append(options, create_tuple(0, current_activity))
-	set_add(branches, create_tuple(init_place, options))
-
-	// Keep going as long as there are branches to resolve
-	while (read_nr_out(branches) > 0):
-		// Still a branch, so pick one at random
-		branch = set_pop(branches)
-		previous_place = branch[0]
-		options = branch[1]
-
-		// Find the index of the option with the lowest time (first element of tuple)
-		i = 0
-		cnt = list_len(options)
-		min_time = 9999999
-		index_min_time = -1
-		while (i < cnt):
-			entry = list_read(options, i)
-			if (integer_lt(entry[0], min_time)):
-				min_time = entry[0]
-				index_min_time = i
-			i = i + 1
-
-		// Pop the minimal option
-		option = list_pop(options, index_min_time)
-		current_time = option[0]
-		current_activity = option[1]
-
-		// Figure out the type
-		type = read_type(in_model, current_activity)
-
-		// Now branch based on the type
-		if (type == "Event"):
-			// Process an event: update the PN and go to the next activity
-			new_transition = instantiate_node(out_model, "Transition", "")
-			new_model = instantiate_node(out_model, "Place", "")
-			instantiate_link(out_model, "P2T", "", prev_model, new_transition)
-			instantiate_link(out_model, "T2P", "", new_transition, new_model)
-
-			// Check if there is a Next to this Event, meaning another event
-			if (read_nr_out(allOutgoingAssociationInstances(in_model, current_activity, "Next")) > 0):
-				// We have a Next, so just push that next event on the options
-				current_activity = set_pop(allAssociationDestinations(in_model, current_activity, "Next"))
-				list_append(options, create_tuple(integer_addition(current_time, read_attribute(in_model, current_activity, "duration")), current_activity))
-				set_add(branches, create_tuple(new_model, options))
-			else:
-				// No Next in this node, so we recurse up until one of these elements does have a next (or we reach the top)
-				while (read_nr_out(allOutgoingAssociationInstances(in_model, current_activity, "Next")) == 0):
-					// Recurse up
-					containers = allAssociationOrigins(in_model, current_activity, "Contains")
-					if (read_nr_out(containers) == 1):
-						current_activity = set_pop(containers)
-					elif (read_nr_out(containers) == 0):
-						// No more containers, so at top element
-						break!
-				if (read_nr_out(containers) == 0):
-					// Nothing left to do, so clear up this branch, but continue with the others
-					continue!
-				else:
-					// Found a node with a Next link: we follow it
-					current_activity = set_pop(allAssociationDestinations(in_model, current_activity, "Next"))
-					list_append(options, create_tuple(integer_addition(current_time, read_attribute(in_model, current_activity, "duration")), current_activity))
-					set_add(branches, create_tuple(new_model, options))
-
-		elif (type == "Sequence"):
-			// Process a sequence: just move the current option to the enclosing activity
-			inner_elements = allAssociationDestinations(in_model, current_activity, "Contains")
-			while (read_nr_out(inner_elements) > 0):
-				element = set_pop(inner_elements)
-				if (read_nr_out(allIncomingAssociationInstances(in_model, element, "Next")) == 0):
-					current_activity = element
-					break!
-			// current_activity now contains the inner element to execute
-			list_append(options, create_tuple(integer_addition(current_time, read_attribute(in_model, current_activity, "duration")), current_activity))
-			// keep the current branch alive, as everything is updated by reference
-			set_add(branches, branch)
-
-		elif (type == "Parallel"):
-			// Process a parallel: create new options for each containing element
-			inner_elements = allAssociationDestinations(in_model, current_activity, "Contains")
-			while (read_nr_out(inner_elements) > 0):
-				current_activity = set_pop(inner_elements)
-				// current_activity now contains the inner element to execute in parallel (an option)
-				list_append(options, create_tuple(integer_addition(current_time, read_attribute(in_model, current_activity, "duration")), current_activity))
-			// keep the current branch alive, as everything is updated by reference
-			set_add(branches, branch)
-
-		elif (type == "Alternative"):
-			inner_elements = allAssociationDestinations(in_model, current_activity, "Contains")
-			while (read_nr_out(inner_elements) > 0):
-				current_activity = set_pop(inner_elements)
-				// current_activity now contains the inner element to execute in alternative branches (a branch)
-				new_options = set_copy(options)
-				list_append(options, create_tuple(integer_addition(current_time, read_attribute(in_model, current_activity, "duration")), current_activity))
-				set_add(branches, create_tuple(previous_place, new_options))
-
-	dict_add(result, "Encapsulated_PetriNet", out_model)
-	return result!

+ 14 - 0
sum_times.py

@@ -0,0 +1,14 @@
+import sys
+
+total = 0.0
+
+for l in open(sys.argv[1], "r"):
+    if sys.argv[2] in l:
+        _, t = l.rsplit(" ", 1)
+        try:
+            t = float(t)
+            total += t
+        except:
+            pass
+
+print("Total for " + sys.argv[2] + " = " + str(total))