|
@@ -1,17 +1,31 @@
|
|
|
import urllib
|
|
|
import urllib2
|
|
|
import json
|
|
|
+from urllib2 import URLError
|
|
|
|
|
|
-# Helper functions
|
|
|
+# Helper functions and configuration: do not use yourself!
|
|
|
taskname = "test"
|
|
|
address = "http://localhost:8001"
|
|
|
last_output = None
|
|
|
+mode = 0
|
|
|
|
|
|
def _input(value):
|
|
|
# Ugly json encoding of primitives
|
|
|
value = '"%s"' if type(value) == str else value
|
|
|
urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname})))
|
|
|
|
|
|
+def _input_raw(value, taskname):
|
|
|
+ # Ugly json encoding of primitives
|
|
|
+ urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname})))
|
|
|
+
|
|
|
+def _compile_AL(AL_file):
|
|
|
+ # Compile an action language file and send the compiled code
|
|
|
+ pass
|
|
|
+
|
|
|
+def _compile_model(model_file):
|
|
|
+ # Compile a model and send the compiled graph
|
|
|
+ pass
|
|
|
+
|
|
|
def _output():
|
|
|
try:
|
|
|
global last_output
|
|
@@ -54,90 +68,391 @@ class ModelExists(Exception):
|
|
|
class PermissionDenied(Exception):
|
|
|
pass
|
|
|
|
|
|
+class InvalidMode(Exception):
|
|
|
+ pass
|
|
|
+
|
|
|
# Main MvC operations
|
|
|
def init():
|
|
|
"""Starts up the connection to the Modelverse."""
|
|
|
- pass
|
|
|
# return None
|
|
|
# raises ConnectionError
|
|
|
# raises UnknownError
|
|
|
+ # raises InvalidMode
|
|
|
+ global mode
|
|
|
+ if mode != 0:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input_raw('"%s"' % taskname, "user_manager")
|
|
|
+ mode = 1
|
|
|
+ except URLError as e:
|
|
|
+ raise ConnectionError(e.reason)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def login(username, password):
|
|
|
"""Log in an existing user."""
|
|
|
- pass
|
|
|
# return None
|
|
|
# raises UnknownError
|
|
|
# raises PermissionDenied
|
|
|
+ global mode
|
|
|
+ if mode != 1:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input(username)
|
|
|
+ _input(password)
|
|
|
+ mode = 2
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def register(username, password):
|
|
|
"""Register a new user."""
|
|
|
- pass
|
|
|
# return None
|
|
|
# raises UnknownError
|
|
|
# raises UserExists
|
|
|
+ global mode
|
|
|
+ if mode != 1:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input(username)
|
|
|
+ _input(password)
|
|
|
+ _input(password)
|
|
|
+ mode = 2
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def model_add(model_name, metamodel_name):
|
|
|
"""Instantiate a new model."""
|
|
|
- pass
|
|
|
# return None
|
|
|
# raises UnknownModel
|
|
|
# raises ModelExists
|
|
|
# raises UnknownError
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("model_add")
|
|
|
+ _input(metamodel_name)
|
|
|
+ _input(model_name)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def model_modify(model_name):
|
|
|
"""Modify an existing model."""
|
|
|
- pass
|
|
|
# return is_write
|
|
|
# raises UnknownModel
|
|
|
# raises PermissionDenied
|
|
|
# raises UnknownError
|
|
|
+ global mode
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("model_modify")
|
|
|
+ _input(model_name)
|
|
|
+ mode = 3
|
|
|
+ _input("help")
|
|
|
+ _output()
|
|
|
+ if ("r/w" in _output()):
|
|
|
+ write = True
|
|
|
+ else:
|
|
|
+ write = False
|
|
|
+ _output()
|
|
|
+ return write
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def model_list():
|
|
|
"""List all models."""
|
|
|
- pass
|
|
|
# return [(model1, metamodel1), (model2, metamodel2), ...]
|
|
|
# raises UnknownError
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("model_list")
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ v = _last_output()
|
|
|
+ m, mm = v.split(":")
|
|
|
+ m.trim()
|
|
|
+ mm.trim()
|
|
|
+ lst.append((m, mm))
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def model_list_full():
|
|
|
"""List full information on all models."""
|
|
|
- pass
|
|
|
# return [(model1, metamodel1, owner1, group1, permissions1), (model2, metamodel2, owner2, group2, permissions2), ...]
|
|
|
# raises UnknownError
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("model_list_full")
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ v = _last_output()
|
|
|
+ m, mm = v.split(":")
|
|
|
+ m.trim()
|
|
|
+ mm.trim()
|
|
|
+ perm, own, grp, m = m.split(" ")
|
|
|
+ lst.append((m, mm, own, grp, perm))
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def model_overwrite(model_name, new_model):
|
|
|
"""Upload a new model and overwrite an existing model."""
|
|
|
- pass
|
|
|
# return None
|
|
|
# raises UnknownModel
|
|
|
# raises PermissionDenied
|
|
|
# raises CompilationError
|
|
|
# raises UnknownError
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("model_overwrite")
|
|
|
+ _input(model_name)
|
|
|
+ _compile_model(new_model)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def user_logout():
|
|
|
+ """Log out the current user. A new login will be required afterwards."""
|
|
|
+ # return None
|
|
|
+ # raises UnknownException
|
|
|
+ global mode
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("exit")
|
|
|
+ mode = 1
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def user_delete():
|
|
|
+ """Removes the current user. A new login will be required afterwards."""
|
|
|
+ # return None
|
|
|
+ # raises UnknownException
|
|
|
+ global mode
|
|
|
+ if mode != 2:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("self-destruct")
|
|
|
+ mode = 1
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
# Actual operations on the model
|
|
|
def list():
|
|
|
"""Return a list of all IDs and the type of the element"""
|
|
|
- pass
|
|
|
# return [(name1, type1), (name2, type2), ...]
|
|
|
# raises UnknownError
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("list")
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ v = _last_output()
|
|
|
+ m, mm = v.split(":")
|
|
|
+ m.trim()
|
|
|
+ mm.trim()
|
|
|
+ lst.append((m, mm))
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def types():
|
|
|
"""Return a list of all types usable in the model"""
|
|
|
- pass
|
|
|
# return [type1, type2, ...]
|
|
|
# raises UnknownError
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("types")
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ v = _last_output()
|
|
|
+ m, mm = v.split(":")
|
|
|
+ m.trim()
|
|
|
+ lst.append(m)
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def read(ID):
|
|
|
"""Return a tuple of information on the element: its type and source/target (None if not an edge)"""
|
|
|
- pass
|
|
|
# return (type, (source, target))
|
|
|
# raises UnknownError
|
|
|
# raises UnknownIdentifier
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("read")
|
|
|
+ _input(ID)
|
|
|
+ _output()
|
|
|
+ t = _output().split(":")[1].trim()
|
|
|
+ if (not _output().startswith("Source:")):
|
|
|
+ rval = (t, None)
|
|
|
+ else:
|
|
|
+ src = _last_output().split(":")[1].trim()
|
|
|
+ trg = _output().split(":")[1].trim()
|
|
|
+ rval = (t, (src, trg))
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ pass
|
|
|
+ return rval
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def read_attrs(ID):
|
|
|
"""Return a dictionary of attribute value pairs"""
|
|
|
- pass
|
|
|
# return {attr1: value1, attr2: value2, ...}
|
|
|
# raises UnknownError
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("read")
|
|
|
+ _input(ID)
|
|
|
+ rval = {}
|
|
|
+ while (_output() != "Attributes:"):
|
|
|
+ pass
|
|
|
+ while (_output() != "Ready for command..."):
|
|
|
+ r = _last_output()
|
|
|
+ key, value = r.split(":")
|
|
|
+ _, value = value.split("=")
|
|
|
+ key.trim()
|
|
|
+ value.trim()
|
|
|
+ rval[key] = value
|
|
|
+ return rval
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def instantiate(typename, edge=None, ID=""):
|
|
|
+ """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""
|
|
|
+ # return instantiated_ID
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownType
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("instantiate")
|
|
|
+ _input(typename)
|
|
|
+ _input(ID)
|
|
|
+ if (edge is not None):
|
|
|
+ _input(edge[0])
|
|
|
+ _input(edge[1])
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def delete(ID):
|
|
|
+ """Delete the element with the given ID"""
|
|
|
+ # return None
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("delete")
|
|
|
+ _input(ID)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def attr_assign(ID, attr, value):
|
|
|
+ """Assign a value to an attribute"""
|
|
|
+ # return None
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ # raises NoSuchAttribute
|
|
|
+ # raises UnsupportedValue
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("attr_modify")
|
|
|
+ _input(ID)
|
|
|
+ _input(attr)
|
|
|
+ _input(value)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def attr_assign_code(ID, attr, code):
|
|
|
+ """Assign a piece of Action Language code to the attribute"""
|
|
|
+ # return None
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ # raises NoSuchAttribute
|
|
|
+ # raises UnsupportedValue
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("attr_modify")
|
|
|
+ _input(ID)
|
|
|
+ _input(attr)
|
|
|
+ _compile_AL(code)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def upload(new_model):
|
|
|
+ """Overwrite the current model with the provided model"""
|
|
|
+ # return None
|
|
|
+ # raises UnknownError
|
|
|
+ # raises CompilationError
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("upload")
|
|
|
+ _compile_model(new_model)
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def read_outgoing(ID, typename):
|
|
|
+ """Returns a list of all outgoing associations of a specific type ("" = all)"""
|
|
|
+ # return [name1, name2, ...]
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ # raises UnknownType
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("read_outgoing")
|
|
|
+ _input(ID)
|
|
|
+ _input(typename)
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Please give your command."):
|
|
|
+ lst.append(_last_output())
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def read_incoming(ID, typename):
|
|
|
+ """Returns a list of all incoming associations of a specific type ("" = all)"""
|
|
|
+ # return [name1, name2, ...]
|
|
|
+ # raises UnknownError
|
|
|
+ # raises UnknownIdentifier
|
|
|
+ # raises UnknownType
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("read_incoming")
|
|
|
+ _input(ID)
|
|
|
+ _input(typename)
|
|
|
+ lst = []
|
|
|
+ while (_output() != "Please give your command."):
|
|
|
+ lst.append(_last_output())
|
|
|
+ return lst
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
+
|
|
|
+def model_exit():
|
|
|
+ """Leave model modify mode."""
|
|
|
+ # return None
|
|
|
+ # raises UnknownError
|
|
|
+ global mode
|
|
|
+ if mode != 3:
|
|
|
+ raise InvalidMode()
|
|
|
+ try:
|
|
|
+ _input("exit")
|
|
|
+ mode = 2
|
|
|
+ except Exception as e:
|
|
|
+ raise UnknownError(str(e))
|
|
|
|
|
|
def transformation_add_MT_language():
|
|
|
"""Create a new Model Transformation language out of a set of metamodels."""
|
|
@@ -227,70 +542,3 @@ def admin_demote():
|
|
|
"""Demote a user from admin status."""
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
-def user_logout():
|
|
|
- """Log out the current user. A new login will be required afterwards."""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownException
|
|
|
-
|
|
|
-def user_delete():
|
|
|
- """Removes the current user. A new login will be required afterwards."""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownException
|
|
|
-
|
|
|
-def instantiate(typename, edge=None, ID=""):
|
|
|
- """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""
|
|
|
- pass
|
|
|
- # return instantiated_ID
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownType
|
|
|
- # raises UnknownIdentifier
|
|
|
-
|
|
|
-def delete(ID):
|
|
|
- """Delete the element with the given ID"""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownIdentifier
|
|
|
-
|
|
|
-def attr_assign(ID, attr, value):
|
|
|
- """Assign a value to an attribute"""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownIdentifier
|
|
|
- # raises NoSuchAttribute
|
|
|
- # raises UnsupportedValue
|
|
|
-
|
|
|
-def attr_assign_code(ID, attr, code):
|
|
|
- """Assign a piece of Action Language code to the attribute"""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownIdentifier
|
|
|
- # raises NoSuchAttribute
|
|
|
- # raises UnsupportedValue
|
|
|
-
|
|
|
-def upload(new_model):
|
|
|
- """Overwrite the current model with the provided model"""
|
|
|
- pass
|
|
|
- # return None
|
|
|
- # raises UnknownError
|
|
|
- # raises CompilationError
|
|
|
-
|
|
|
-def read_outgoing(ID, typename):
|
|
|
- """Returns a list of all outgoing associations of a specific type ("" = all)"""
|
|
|
- pass
|
|
|
- # return [name1, name2, ...]
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownIdentifier
|
|
|
- # raises UnknownType
|
|
|
-
|
|
|
-def read_incoming(ID, typename):
|
|
|
- """Returns a list of all incoming associations of a specific type ("" = all)"""
|
|
|
- pass
|
|
|
- # return [name1, name2, ...]
|
|
|
- # raises UnknownError
|
|
|
- # raises UnknownIdentifier
|
|
|
- # raises UnknownType
|