浏览代码

Added RPGame metamodel and allowed model constructor to use inheritance

Yentl Van Tendeloo 9 年之前
父节点
当前提交
443bcc8773
共有 2 个文件被更改,包括 137 次插入2 次删除
  1. 127 0
      integration/code/rpgame.mvc
  2. 10 2
      interface/HUTN/hutn_compiler/model_visitor.py

+ 127 - 0
integration/code/rpgame.mvc

@@ -0,0 +1,127 @@
+import models/SimpleClassDiagrams as SCD
+include "primitives.alh"
+include "object_operations.alh"
+
+SCD RPGame{
+    Class Tile {
+        $
+        Element associations
+        Element back_associations
+        Element association
+        String destination
+        associations = allOutgoingAssociationInstances(model, name, "tile_left")
+        while (0 < list_len(associations)):
+            association = set_pop(associations)
+            destination = readAssociationDestination(model, association)
+            back_associations = allOutgoingAssociationInstances(model, destination, "tile_right")
+            if (list_len(back_associations) < 1):
+                return "Left link does not have a right link back"
+            else:
+                association = set_pop(back_associations)
+                destination = readAssociationDestination(model, association)
+                if (destination != name):
+                    return "Right link does not have a left link back to the same tile"
+        associations = allOutgoingAssociationInstances(model, name, "tile_right")
+        while (0 < list_len(associations)):
+            association = set_pop(associations)
+            destination = readAssociationDestination(model, association)
+            back_associations = allOutgoingAssociationInstances(model, destination, "tile_left")
+            if (list_len(back_associations) < 1):
+                return "Right link does not have a left link back"
+            else:
+                association = set_pop(back_associations)
+                destination = readAssociationDestination(model, association)
+                if (destination != name):
+                    return "Right link does not have a left link back to the same tile"
+        associations = allOutgoingAssociationInstances(model, name, "tile_top")
+        while (0 < list_len(associations)):
+            association = set_pop(associations)
+            destination = readAssociationDestination(model, association)
+            back_associations = allOutgoingAssociationInstances(model, destination, "tile_bottom")
+            if (list_len(back_associations) < 1):
+                return "Top link does not have a bottom link back"
+            else:
+                association = set_pop(back_associations)
+                destination = readAssociationDestination(model, association)
+                if (destination != name):
+                    return "Top link does not have a bottom link back to the same tile"
+        associations = allOutgoingAssociationInstances(model, name, "tile_bottom")
+        while (0 < list_len(associations)):
+            association = set_pop(associations)
+            destination = readAssociationDestination(model, association)
+            back_associations = allOutgoingAssociationInstances(model, destination, "tile_top")
+            if (list_len(back_associations) < 1):
+                return "Bottom link does not have a top link back"
+            else:
+                association = set_pop(back_associations)
+                destination = readAssociationDestination(model, association)
+                if (destination != name):
+                    return "Bottom link does not have a top link back to the same tile"
+        return "OK"
+        $
+    }
+    Association tile_left (Tile, Tile){
+        source_upper_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Association tile_right (Tile, Tile){
+        source_upper_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Association tile_top (Tile, Tile){
+        source_upper_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Association tile_bottom (Tile, Tile){
+        source_upper_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Class Scene {}
+    Association Scene_has_tiles (Scene, Tile){
+        source_lower_cardinality = 1
+        source_upper_cardinality = 1
+        target_lower_cardinality = 1
+    }
+    Class Item {}
+    Association Item_on_tile (Item, Tile){
+        source_upper_cardinality = 1
+        target_lower_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Class Goal : Item {}
+    Class Character {}
+    Association Character_on_tile (Character, Tile){
+        source_upper_cardinality = 1
+        target_lower_cardinality = 1
+        target_upper_cardinality = 1
+    }
+    Class Hero : Character {}
+}
+
+export RPGame to models/RPGame
+
+RPGame my_game {
+    Scene scene {}
+    Hero link {}
+    Goal goal {}
+    Tile tile_00 {}
+    Tile tile_01 {}
+    Tile tile_10 {}
+    Tile tile_11 {}
+    Scene_has_tiles (scene, tile_00) {}
+    Scene_has_tiles (scene, tile_01) {}
+    Scene_has_tiles (scene, tile_10) {}
+    Scene_has_tiles (scene, tile_11) {}
+    Character_on_tile (link, tile_00) {}
+    Item_on_tile (goal, tile_11) {}
+    tile_left (tile_01, tile_00) {}
+    tile_right (tile_00, tile_01) {}
+    tile_left (tile_11, tile_10) {}
+    tile_right (tile_10, tile_11) {}
+    tile_top (tile_10, tile_00) {}
+    tile_bottom (tile_00, tile_10) {}
+    tile_top (tile_11, tile_01) {}
+    tile_bottom (tile_01, tile_11) {}
+}
+
+export my_game to models/my_game

+ 10 - 2
interface/HUTN/hutn_compiler/model_visitor.py

@@ -71,10 +71,18 @@ class ModelVisitor(Visitor):
         else:
             self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
         self.current_element = element_name
+
+        if tree.get_children("inheritance"):
+            self.visit(tree.get_children("inheritance")[0])
             
         for attr in tree.get_children("model_attribute"):
             self.visit(attr)
 
+    def visit_inheritance(self, tree):
+        for token in tree.get_children("MODEL_ID"):
+            superclass = token.get_text()
+            self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr("Inheritance"), jsonstr("%s_inherits_from_%s" % (self.current_element, superclass)), jsonstr(self.current_element), jsonstr(superclass)])
+
     def visit_model_attribute(self, tree):
         children = tree.get_children("MODEL_ID")
         is_definition = bool(tree.get_children("COLON"))
@@ -101,9 +109,9 @@ class ModelVisitor(Visitor):
             whitespaces = len(constraint) - len(constraint.lstrip())
             constraint = "\n".join(["\t" + line[whitespaces-1:].replace("    ", "\t") for line in constraint.split("\n") if len(line.strip()) != 0])
             constraint = "".join(["include %s\n" % i for i in self.includes]) + \
-                         "String function constraint(model : Element, element : String):\n" + \
+                         "String function constraint(model : Element, name : String):\n" + \
                          "\tElement self\n" + \
-                         '\tself = model["model"][element]\n' + \
+                         '\tself = model["model"][name]\n' + \
                          constraint + "\n"
             with open(".constraint.alc", 'w') as f:
                 f.write(constraint)