ソースを参照

Added initial code for an activity that prints a model

Yentl Van Tendeloo 7 年 前
コミット
e3ee8ca1da
4 ファイル変更136 行追加1 行削除
  1. 119 0
      models/AL_to_py.alc
  2. 6 0
      models/String.mvc
  3. 10 0
      test_printer.py
  4. 1 1
      wrappers/modelverse_SCCD.py

+ 119 - 0
models/AL_to_py.alc

@@ -0,0 +1,119 @@
+include "primitives.alh"
+include "modelling.alh"
+include "object_operations.alh"
+
+//    def print_instruction(self, inst, indent=0):
+//        intrinsics = {"integer_addition": (lambda x, y: "(%s + %s)" % (x, y)),
+//                      #"string_join": (lambda x, y: "(str(%s) + str(%s))" % (x, y)),
+//                      #"dict_read": (lambda x, y: "(%s[%s])" % (x, y)),
+//                      #"dict_overwrite": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
+//                      #"dict_add_fast": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
+//                      #"dict_add_fast": (lambda x, y, z: "(%s[%s] = %s)" % (x, y, z)),
+//                      }
+//
+//        inst_type, = yield [("RV", [inst])]
+//        instruction = "(no_printer_for_%s)" % inst_type["value"]
+//
+//        if inst_type["value"] == "if":
+//            cond, true, false = yield [("RD", [inst, "cond"]),
+//                                       ("RD", [inst, "then"]),
+//                                       ("RD", [inst, "else"])]
+//            cond, = yield [("CALL_ARGS", [self.print_instruction, (cond, 0)])]
+//            true, = yield [("CALL_ARGS", [self.print_instruction, (true, indent+1)])]
+//            if false:
+//                false, = yield [("CALL_ARGS", [self.print_instruction, (false, indent+1)])]
+//                false = ("  " * indent + "else:\n%s\n") % false
+//            else:
+//                false = ""
+//
+//            instruction = "  " * indent + "if (" + cond + "):\n" + true + "\n" + false
+//            #TODO fix for inline calls
+//        elif inst_type["value"] == "constant":
+//            node, = yield [("RD", [inst, "node"])]
+//            node, = yield [("RV", [node])]
+//            if isinstance(node, str):
+//                instruction = "  " * indent + '"%s"' % node
+//            else:
+//                instruction = "  " * indent + str(node)
+//        elif inst_type["value"] == "return":
+//            value, = yield [("RD", [inst, "value"])]
+//            if value:
+//                value, = yield [("CALL_ARGS", [self.print_instruction, (value, 0)])]
+//                instruction = "  " * indent + "return %s\n" % value
+//            else:
+//                instruction = "  " * indent + "return\n"
+//            #TODO fix for inline calls
+//        elif inst_type["value"] == "declare":
+//            instruction = ""
+//        elif inst_type["value"] == "resolve":
+//            value, = yield [("RD", [inst, "var"])]
+//            str_value, = yield [("RV", [value])]
+//            if str_value:
+//                instruction = str_value
+//            else:
+//                instruction = "var_%s" % value
+//        elif inst_type["value"] == "assign":
+//            var, val = yield [("RD", [inst, "var"]),
+//                              ("RD", [inst, "value"])]
+//            var, val = yield [("CALL_ARGS", [self.print_instruction, (var, 0)]),
+//                              ("CALL_ARGS", [self.print_instruction, (val, 0)])]
+//            instruction = "  " * indent + var + " = " + val + "\n"
+//            #TODO fix for inline calls
+//        elif inst_type["value"] == "call":
+//            func_name, = yield [("RD", [inst, "func"])]
+//            func_name, = yield [("CALL_ARGS", [self.print_instruction, (func_name, 0)])]
+//            param_list = []
+//
+//            param, = yield [("RD", [inst, "params"])]
+//            while param:
+//                value, = yield [("RD", [param, "value"])]
+//                res, param = yield [("CALL_ARGS", [self.print_instruction, (value, 0)]),
+//                                    ("RD", [param, "next_param"])]
+//                param_list.append(res)
+//
+//            if func_name in intrinsics:
+//                instruction = "  " * indent + intrinsics[func_name](*param_list)
+//            else:
+//                instruction = "  " * indent + func_name + "(" + ", ".join(param_list) + ")"
+//                """
+//                if indent == 0:
+//                    #TODO fix for inline calls
+//                    instruction = 'result, = yield [("CALL_ARGS", (%s, (' % func_name + ",".join(param_list) + ',)))]'
+//                else:
+//                    instruction = "  " * indent + 'yield [("CALL_ARGS", (%s, (' % func_name + ",".join(param_list) + ',)))]'
+//                """
+//            if indent:
+//                instruction += "\n"
+//        elif inst_type["value"] == "access":
+//            value, = yield [("RD", [inst, "var"])]
+//            instruction, = yield [("CALL_ARGS", [self.print_instruction, (value, 0)])]
+//        elif inst_type["value"] == "while":
+//            cond, body = yield [("RD", [inst, "cond"]),
+//                                ("RD", [inst, "body"])]
+//            cond, body = yield [("CALL_ARGS", [self.print_instruction, (cond, 0)]),
+//                                ("CALL_ARGS", [self.print_instruction, (body, indent+1)])]
+//            instruction = "  " * indent + "while (%s):\n" % cond + body
+//            #TODO fix for inline calls
+//
+//        next_inst, = yield [("RD", [inst, "next"])]
+//        if next_inst:
+//            next_inst, = yield [("CALL_ARGS", [self.print_instruction, (next_inst, indent)])]
+//        else:
+//            next_inst = ""
+//
+//        raise primitive_functions.PrimitiveFinished(instruction + next_inst)
+
+Boolean function main(model : Element):
+	// Read out the main function
+	String initial_function
+	String al_node
+
+	initial_function = set_pop(allInstances(model, "AL/Initial"))
+	initial_function = set_pop(allAssociationDestinations(model, initial_function))
+	al_node = cast_value(initial_function)
+	log("Read out initial node: " + initial_function)
+	log("Value: " + al_node)
+
+	// Ready, now branch out
+	// TODO
+	return True!

+ 6 - 0
models/String.mvc

@@ -0,0 +1,6 @@
+SimpleAttribute Str{}
+Class String {
+    value : Str
+    lower_cardinality = 1
+    upper_cardinality = 1
+}

+ 10 - 0
test_printer.py

@@ -0,0 +1,10 @@
+import sys
+sys.path.append("wrappers/")
+from modelverse import *
+
+init()
+login("admin", "admin")
+
+model_add("formalisms/String", "formalisms/SimpleClassDiagrams", open("models/String.mvc", 'r').read())
+transformation_add_AL({"AL": "formalisms/ActionLanguage"}, {"String": "formalisms/String"}, "models/AL_to_Py", open("models/AL_to_py.alc", 'r').read())
+transformation_execute_AL("models/AL_to_PY", {"AL": "models/AL_model"}, {"String": "models/printed_string"})

+ 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)
 
-Date:   Sat Mar 24 11:30:43 2018
+Date:   Tue Mar 27 15:57:35 2018
 
 Model author: Yentl Van Tendeloo
 Model name:   MvK Server