浏览代码

Add initial code to generate Python code from rules

Yentl Van Tendeloo 7 年之前
父节点
当前提交
889307388b
共有 3 个文件被更改,包括 60 次插入2 次删除
  1. 3 1
      kernel/rules/MvK_rules.mvc
  2. 56 0
      kernel/rules/to_python.alc
  3. 1 1
      wrappers/modelverse_SCCD.py

+ 3 - 1
kernel/rules/MvK_rules.mvc

@@ -970,7 +970,9 @@ Rule Call_Resolve_No_Params_N13 {
     Match N13_1 {
     Match N13_1 {
         match = True
         match = True
     }
     }
-    Match N13_2 {}
+    Match N13_2 {
+        match = True
+    }
     Match N13_3 {
     Match N13_3 {
         value = "'init'"
         value = "'init'"
         match = True
         match = True

+ 56 - 0
kernel/rules/to_python.alc

@@ -0,0 +1,56 @@
+include "primitives.alh"
+include "modelling.alh"
+include "object_operations.alh"
+
+Boolean function main(model : Element):
+	String result
+	Element nodes
+	String node
+	Element edges
+	String edge
+	String new_node
+	String source
+	String destination
+	String name
+	Element to_explore
+	Element explored
+
+	result = "root, = yield [('RR', [])]\n"
+	nodes = allInstances(model, "Rules/Root")
+	while (set_len(nodes) > 0):
+		node = set_pop(nodes)
+		source = string_replace(node, "/", "_")
+		result = result + source + " = root\n"
+
+		// Keep following outgoing edges to find matching nodes
+		to_explore = set_create()
+		explored = set_create()
+		set_add(to_explore, node)
+
+		while (set_len(to_explore) > 0):
+			// Still explore more!
+			node = set_pop(to_explore)
+			source = string_replace(node, "/", "_")
+
+			edges = allOutgoingAssociationInstances(model, node, "Rules/Edge")
+			while (set_len(edges) > 0):
+				edge = set_pop(edges)
+				new_node = readAssociationDestination(model, edge)
+				if (value_eq(read_attribute(model, new_node, "match"), True)):
+					// Is a match node, so fetch the value on the edge
+					name = read_attribute(model, edge, "value")
+					destination = string_replace(new_node, "/", "_")
+					result = result + destination + ", = yield [('RD', [" + source + ", " + name + "])]\n"
+
+					String value
+					value = read_attribute(model, new_node, "value")
+					if (element_neq(value, read_root())):
+						// Match node has a value we should compare as well!
+						// Read out the value from the Modelverse
+						result = result + destination + "_V, = yield [('RV', [" + destination + "])]\n"
+
+					set_add(to_explore, new_node)
+
+	log("Got result:")
+	log(result)
+	return True!

+ 1 - 1
wrappers/modelverse_SCCD.py

@@ -1,7 +1,7 @@
 """
 """
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 
 
-Date:   Thu Nov 23 16:19:24 2017
+Date:   Fri Nov 24 11:09:50 2017
 
 
 Model author: Yentl Van Tendeloo
 Model author: Yentl Van Tendeloo
 Model name:   MvK Server
 Model name:   MvK Server