فهرست منبع

More fixes to Python wrapper

Yentl Van Tendeloo 8 سال پیش
والد
کامیت
10bbc00a37
3فایلهای تغییر یافته به همراه75 افزوده شده و 16 حذف شده
  1. 1 1
      core/mini_modify.alc
  2. 51 9
      wrappers/modelverse.py
  3. 23 6
      wrappers/test_modelverse.py

+ 1 - 1
core/mini_modify.alc

@@ -90,7 +90,7 @@ Element function modify(model : Element, write : Boolean):
 							output(element_name)
 							output("Instantiation successful!")
 				else:
-					log("Could not find element in " + set_to_string(dict_keys(model["metamodel"]["model"])))
+					log("Could not find element!")
 					output("Unknown type specified; aborting")
 			else:
 				output("Permission denied")

+ 51 - 9
wrappers/modelverse.py

@@ -80,7 +80,7 @@ def _consume_to_end():
 class ModelverseException(Exception):
     pass
 
-class UnknownError(ModelverseException):
+class UnknownException(ModelverseException):
     pass
 
 class UnknownIdentifier(ModelverseException):
@@ -158,7 +158,7 @@ def register(username, password):
     _consume_to_end()
     mode = 2
 
-def model_add(model_name, metamodel_name, model_code):
+def model_add(model_name, metamodel_name, model_code=None):
     """Instantiate a new model."""
     # return None
     # raises UnknownModel
@@ -176,7 +176,10 @@ def model_add(model_name, metamodel_name, model_code):
         _input(model_name)
         if _output() == "Waiting for model constructors...":
             try:
-                _compile_model(model_code)
+                if model_code == None:
+                    _input("exit")
+                else:
+                    _compile_model(model_code)
             except Exception:
                 raise CompilationError()
             _consume_to_end()
@@ -369,7 +372,7 @@ def read_attrs(ID):
             r = _last_output()
             key, value = r.split(":")
             _, value = value.split("=")
-            key = key.strip()
+            key = json.loads(key.strip())
             value = value.strip()
             value = None if value == "None" else json.loads(value)
             rval[key] = value
@@ -381,14 +384,52 @@ def instantiate(typename, edge=None, ID=""):
     # raises UnknownError
     # raises UnknownType
     # raises UnknownIdentifier
+    # raises NotAnEdge
     if mode != 3:
         raise InvalidMode()
     _input("instantiate")
-    _input(typename)
-    _input(ID)
-    if (edge is not None):
-        _input(edge[0])
-        _input(edge[1])
+    if (_output() == "Permission denied"):
+        _consume_to_end()
+        raise PermissionDenied()
+    else:
+        _input(typename)
+        if (_output() == "Name of new element?"):
+            _input(ID)
+            if (_output() == "Element already exists; aborting"):
+                _consume_to_end()
+                raise ElementExists()
+
+            if (edge is not None):
+                if (_last_output() == "Source name?"):
+                    _input(edge[0])
+                    if (_output() != "Destination name?"):
+                        _consume_to_end()
+                        raise UnknownIdentifier()
+                    _input(edge[1])
+                    ID = _output()
+                    if (ID == "Unknown destination; aborting"):
+                        _consume_to_end()
+                        raise UnknownIdentifier()
+                    else:
+                        _consume_to_end()
+                        return ID
+                else:
+                    _consume_to_end()
+                    raise NotAnEdge()
+            else:
+                ID = _last_output()
+                _consume_to_end()
+                return ID
+
+        elif (_last_output() == "Permission denied"):
+            _consume_to_end()
+            raise PermissionDenied()
+        elif (_last_output() == "Unknown type specified; aborting"):
+            _consume_to_end()
+            raise UnknownType()
+        else:
+            print(9)
+            print(_last_output())
 
 def delete(ID):
     """Delete the element with the given ID"""
@@ -478,6 +519,7 @@ def model_exit():
     if mode != 3:
         raise InvalidMode()
     _input("exit")
+    _consume_to_end()
     mode = 2
 
 def transformation_add_MT_language():

+ 23 - 6
wrappers/test_modelverse.py

@@ -3,19 +3,17 @@ from random import random
 
 username = str(random())
 password = str(random())
+model_name = str(random())
+model_name2 = str(random())
 
 init()
 register(username, password)
 
-try:
-    model_add("PetriNets", "SimpleClassDiagrams", open("../models/petrinet_ports.mvc", "r").read())
-except ModelExists:
-    pass
-
+model_add(model_name, "SimpleClassDiagrams", open("../models/petrinet_ports.mvc", "r").read())
 model_modify("SimpleClassDiagrams")
 print(element_list())
 try:
-    model_list()
+    print(model_list())
 except InvalidMode:
     print(types())
     try:
@@ -26,3 +24,22 @@ except InvalidMode:
             print(read_attrs("Place"))
         except UnknownIdentifier:
             print(read_attrs("Class"))
+            try:
+                print(instantiate("Class", None, "abc"))
+            except PermissionDenied:
+                print(("abc", "Class") not in element_list())
+                model_exit()
+                model_modify(model_name)
+                print(element_list())
+                print(instantiate("Class", None, "abc"))
+                print(("abc", "Class") in element_list())
+                model_exit()
+                model_add(model_name2, model_name)
+                model_modify(model_name2)
+                print(instantiate("Place", None, "p1"))
+                print(instantiate("Place", None, "p2"))
+                print(instantiate("Transition", None, "t1"))
+                print(instantiate("P2T", ("p1", "t1")))
+                print(instantiate("T2P", ("t1", "p2")))
+                print(element_list())
+                print(read_attrs("p1"))