Преглед изворни кода

Added more tests for the simple interface; updated to allow for anonymous elements

Yentl Van Tendeloo пре 9 година
родитељ
комит
89eb245299

+ 19 - 0
integration/code/my_petrinet.mvc

@@ -0,0 +1,19 @@
+import models/PetriNets as PetriNets
+
+PetriNets my_petrinet {
+    Place p1 {
+        tokens = 1
+    }
+    Place p2 {
+        tokens = 3
+    }
+    Transition t1 {}
+    P2T (p1, t1) {
+        weight = 1
+    }
+    T2P (t1, p2) {
+        weight = 2
+    }
+}
+
+export my_petrinet to models/my_petrinet

+ 33 - 0
integration/code/my_petrinet_with_MM.mvc

@@ -0,0 +1,33 @@
+import models/SimpleClassDiagrams as SCD
+
+SCD PetriNets{
+    Class Natural {}
+    Class Place{
+        tokens : Natural
+    }
+    Class Transition{}
+    Association P2T (Place, Transition) {
+        weight : Natural
+    }
+    Association T2P (Transition, Place) {
+        weight : Natural
+    }
+}
+
+PetriNets my_petrinet {
+    Place p1 {
+        tokens = 1
+    }
+    Place p2 {
+        tokens = 3
+    }
+    Transition t1 {}
+    P2T (p1, t1) {
+        weight = 1
+    }
+    T2P (t1, p2) {
+        weight = 2
+    }
+}
+
+export my_petrinet to models/my_petrinet

+ 17 - 0
integration/code/petrinets.mvc

@@ -0,0 +1,17 @@
+import models/SimpleClassDiagrams as SCD
+
+SCD PetriNets{
+    Class Natural {}
+    Class Place{
+        tokens : Natural
+    }
+    Class Transition{}
+    Association P2T (Place, Transition) {
+        weight : Natural
+    }
+    Association T2P (Transition, Place) {
+        weight : Natural
+    }
+}
+
+export PetriNets to models/PetriNets

+ 21 - 0
integration/code/simpleclassdiagrams.mvc

@@ -0,0 +1,21 @@
+import /formalisms/SimpleClassDiagrams as SCD
+
+SCD SimpleClassDiagrams{
+    Class Any {}
+    Class Natural {}
+    Class String {}
+    Class Class{
+        lower_cardinality : Natural
+        upper_cardinality : Natural
+    }
+    Association Association (Class, Class){
+        name : String
+        source_lower_cardinality : Natural
+        source_upper_cardinality : Natural
+        target_lower_cardinality : Natural
+        target_upper_cardinality : Natural
+    }
+    Association Inheritance (Class, Class){}
+    Inheritance assoc_inh_class (Association, Class) {}
+    Inheritance class_inh_any (Class, Any) {}
+}

+ 19 - 1
integration/test_constructors_models_compiled.py

@@ -35,5 +35,23 @@ def conformance_check(node):
 
 class TestConstructorsModelsCompiled(unittest.TestCase):
     def test_constructors_petrinets(self):
-        commands = ['"model"' ,'"initialize_SCD"', '"models/SimpleClassDiagrams"', '"exit"'] + model_compile("interface/HUTN/test/modelling_language/code/petrinets.mvc") + conformance_check("models/PetriNets") + ['"return"', 'false']
+        commands = ['"model"' ,'"initialize_SCD"', '"models/SimpleClassDiagrams"', '"exit"'] + \
+                   model_compile("integration/code/petrinets.mvc") + \
+                   conformance_check("models/PetriNets") + \
+                   ['"return"', 'false']
+        self.assertTrue(run_barebone(commands, ["OK"], 1))
+
+    def test_constructors_petrinet_instance(self):
+        commands = ['"model"' ,'"initialize_SCD"', '"models/SimpleClassDiagrams"', '"exit"'] + \
+                   model_compile("integration/code/petrinets.mvc") + \
+                   model_compile("integration/code/my_petrinet.mvc") + \
+                   conformance_check("models/my_petrinet") + \
+                   ['"return"', 'false']
+        self.assertTrue(run_barebone(commands, ["OK"], 1))
+
+    def test_constructors_petrinet_full(self):
+        commands = ['"model"' ,'"initialize_SCD"', '"models/SimpleClassDiagrams"', '"exit"'] + \
+                   model_compile("integration/code/my_petrinet_with_MM.mvc") + \
+                   conformance_check("models/my_petrinet") + \
+                   ['"return"', 'false']
         self.assertTrue(run_barebone(commands, ["OK"], 1))

+ 1 - 1
interface/HUTN/grammars/modelling.g

@@ -6,7 +6,7 @@ grammar{
 
     model: MODEL_ID MODEL_ID NEWLINE? LCURLY NEWLINE? (model_element)* RCURLY;
 
-    model_element: MODEL_ID MODEL_ID inheritance? (LPAR MODEL_ID COMMA MODEL_ID RPAR)? NEWLINE? LCURLY NEWLINE? (model_attribute)* RCURLY NEWLINE?;
+    model_element: MODEL_ID MODEL_ID? inheritance? (LPAR MODEL_ID COMMA MODEL_ID RPAR)? NEWLINE? LCURLY NEWLINE? (model_attribute)* RCURLY NEWLINE?;
 
     inheritance: COLON MODEL_ID (COMMA MODEL_ID)*;
 

+ 9 - 5
interface/HUTN/hutn_compiler/model_visitor.py

@@ -54,11 +54,15 @@ class ModelVisitor(Visitor):
     def visit_model_element(self, tree):
         children = tree.get_children("MODEL_ID")
         element_type = children[0].get_text()
-        element_name = children[1].get_text()
+        if len(children) == 2 or len(children) == 4:
+            element_name = children[1].get_text()
+        else:
+            element_name = ""
 
-        if len(children) == 4:
-            source_name = children[2].get_text()
-            target_name = children[3].get_text()
+        if len(children) > 2:
+            # So we have a source and target; but aren't sure which is which, because the name is optional!
+            source_name = children[-2].get_text()
+            target_name = children[-1].get_text()
             self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
         else:
             self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
@@ -80,5 +84,5 @@ class ModelVisitor(Visitor):
         else:
             # is assign
             attr_name = children[0].get_text()
-            attr_value = children[1]
+            attr_value = tree.get_children("value")[0]
             self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])

Разлика између датотеке није приказан због своје велике величине
+ 1 - 0
interface/HUTN/test/modelling_language/expected/my_petrinet


Разлика између датотеке није приказан због своје велике величине
+ 1 - 0
interface/HUTN/test/modelling_language/expected/my_petrinet_with_MM


+ 6 - 0
interface/HUTN/test/modelling_language/test_compile.py

@@ -19,5 +19,11 @@ class TestCompile(unittest.TestCase):
     def test_PetriNets(self):
         compile_file(self, "petrinets.mvc")
 
+    def test_PetriNetsInstance(self):
+        compile_file(self, "my_petrinet.mvc")
+
     def test_SimpleClassDiagrams(self):
         compile_file(self, "simpleclassdiagrams.mvc")
+
+    def test_PetriNetsBoth(self):
+        compile_file(self, "my_petrinet_with_MM.mvc")