Browse Source

Make a nicer version of the transformation engine, which calls an
external function to get all possible bindings

Yentl Van Tendeloo 8 years ago
parent
commit
f7ee380b16

BIN
bootstrap/bootstrap.m.gz


+ 15 - 0
bootstrap/primitives.alc

@@ -194,3 +194,18 @@ Element function set_copy(a : Element):
 		i = i + 1
 
 	return b!
+
+Element function set_to_string(s : Element):
+	String result
+	Integer i
+
+	result = "{"
+	i = 0
+	while (i < read_nr_out(s)):
+		result = result + cast_v2s(read_edge_dst(read_out(s, i)))
+		result = result + ", "
+		i = i + 1
+	
+	result = result + "}"
+
+	return result!

+ 55 - 10
bootstrap/transform.alc

@@ -55,18 +55,53 @@ Element function make_matching_schedule(LHS_model : Element):
 
 	return schedule!
 
+Element function get_possible_bindings(host_model : Element, LHS_model : Element, current_element : String, map : Element):
+	Element options
+
+	output("FIND BINDING")
+	options = create_node()
+
+	return options!
+
 Element function match(host_model : Element, LHS_model : Element):
-	Element schedule
+	// Match the LHS_model to the host_model, returning all possible mappings from LHS_model elements to host_model elements
 
+	// Make the schedule first
+	Element schedule
 	schedule = make_matching_schedule(LHS_model)
-	output("Got schedule")
-	while (read_nr_out(schedule) > 0):
-		output(list_pop(schedule, 0))
-
-	Element mapping
-	mapping = create_node()
 
-	return mapping!
+	// Now follow the schedule, incrementally building all mappings
+	Element mappings
+	Element new_mappings
+	Element new_map
+	String current_element
+	Element map
+	String option
+	Element options
+
+	mappings = create_node()
+	set_add(mappings, create_node())
+	while (bool_and(read_nr_out(schedule) > 0, read_nr_out(mappings) > 0)):
+		current_element = list_pop(schedule, 0)
+		log("Finding options for " + current_element)
+		new_mappings = create_node()
+
+		while (read_nr_out(mappings) > 0):
+			map = set_pop(mappings)
+			log("In context " + set_to_string(map))
+			options = get_possible_bindings(host_model, LHS_model, current_element, map)
+			log("Found options " + set_to_string(options))
+
+			while (read_nr_out(options) > 0):
+				option = set_pop(options)
+				new_map = set_copy(map)
+				dict_add(new_map, current_element, option)
+				log(" --> adding mapping " + set_to_string(new_map))
+				set_add(new_mappings, new_map)
+
+		mappings = new_mappings
+
+	return mappings!
 
 Void function rewrite(host_model : Element, RHS_model : Element, mapping : Element):
 	output("TODO: rewrite!")
@@ -75,7 +110,17 @@ Void function rewrite(host_model : Element, RHS_model : Element, mapping : Eleme
 
 Void function transform(host_model : Element, LHS_model : Element, RHS_model : Element):
 	Element mapping
-	mapping = match(host_model, LHS_model)
-	rewrite(host_model, RHS_model, mapping)
+	Element mappings
+
+	// Get all possible mappings
+	mappings = match(host_model, LHS_model)
+
+	// Select one such mapping and rewrite it
+	if (read_nr_out(mappings) > 0):
+		// Mapping found, so can rewrite it
+		mapping = set_pop(mappings)
+		rewrite(host_model, RHS_model, mapping)
+	else:
+		output("No mapping found!")
 
 	return!

+ 1 - 0
interface/HUTN/includes/primitives.alh

@@ -97,3 +97,4 @@ Element function resolve(var_name : String)
 Boolean function has_input()
 Element function list_pop(lst : Element, index : Integer)
 Element function set_copy(set : Element)
+String function set_to_string(set : Element)