Browse Source

Added Modelverse wrapper code for Python; still need to implement apart from interface

Yentl Van Tendeloo 8 years ago
parent
commit
a8fd80d69f
2 changed files with 163 additions and 2 deletions
  1. 32 2
      core/mini_modify.alc
  2. 131 0
      wrappers/modelverse.py

+ 32 - 2
core/mini_modify.alc

@@ -37,6 +37,8 @@ Element function modify(model : Element, write : Boolean):
 				output("  upload        -- Upload a completely new model")
 			else:
 				output(" == READ-ONLY ==")
+			output("  read_outgoing -- Prints the list of outgoing links of an element")
+			output("  read_incoming -- Prints the list of incoming links to an element")
 			output("  list          -- Prints the list of elements in the model")
 			output("  list_full     -- Prints the list of all elements in the model")
 			output("  types         -- Prints the list of elements that can be instantiated")
@@ -72,14 +74,16 @@ Element function modify(model : Element, write : Boolean):
 								String dst_name
 								dst_name = input()
 								if (dict_in(model["model"], dst_name)):
-									instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
+									element_name = instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
+									output(element_name)
 									output("Instantiation successful!")
 								else:
 									output("Unknown destination; aborting")
 							else:
 								output("Unknown source; aborting")
 						else:
-							instantiate_node(model, mm_type_name, element_name)
+							element_name = instantiate_node(model, mm_type_name, element_name)
+							output(element_name)
 							output("Instantiation successful!")
 				else:
 					log("Could not find element in " + set_to_string(dict_keys(model["metamodel"]["model"])))
@@ -247,6 +251,32 @@ Element function modify(model : Element, write : Boolean):
 				typename = read_type(model, v_m)
 				output((("  " + v_m) + " : ") + typename)
 
+		elif (cmd == "read_outgoing"):
+			Element elems
+			output("Element to read from?")
+			cmd = input()
+			if (dict_in(model["model"], cmd)):
+				String t
+				output("Type of outgoing edge (empty for all)?")
+				elems = readOutgoingAssociationInstances(model, cmd, input())
+				while (read_nr_out(elems) > 0):
+					output(set_pop(elems))
+			else:
+				output("Unknown element; aborting")
+
+		elif (cmd == "read_incoming"):
+			Element elems
+			output("Element to read from?")
+			cmd = input()
+			if (dict_in(model["model"], cmd)):
+				String t
+				output("Type of incoming edge (empty for all)?")
+				elems = readIncomingAssociationInstances(model, cmd, input())
+				while (read_nr_out(elems) > 0):
+					output(set_pop(elems))
+			else:
+				output("Unknown element; aborting")
+
 		elif (cmd == "read"):
 			output("Element to read?")
 			cmd = input()

+ 131 - 0
wrappers/modelverse.py

@@ -0,0 +1,131 @@
+import urllib
+import urllib2
+import json
+
+# Helper functions
+taskname = "test"
+address = "http://localhost:8001"
+last_output = None
+
+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 _output():
+    try:
+        global last_output
+        last_output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname})))
+        return last_output
+    except:
+        raise UnknownError()
+
+def _output_last():
+    return last_output
+
+# Exceptions
+class UnknownError(Exception):
+    pass
+
+class UnknownIdentifier(Exception):
+    pass
+
+class UnknownType(Exception):
+    pass
+
+class UnsupportedValue(Exception):
+    pass
+
+class CompilationError(Exception):
+    pass
+
+class NoSuchAttribute(Exception):
+    pass
+
+# Actual functions to use for the wrapper
+def list():
+    """Return a list of all IDs and the type of the element"""
+    pass
+    # return [(name1, type1), (name2, type2), ...]
+    # raises UnknownError
+
+def types():
+    """Return a list of all types usable in the model"""
+    pass
+    # return [type1, type2, ...]
+    # raises UnknownError
+
+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
+
+def read_attrs(ID):
+    """Return a dictionary of attribute value pairs"""
+    pass
+    # return {attr1: value1, attr2: value2, ...}
+    # raises UnknownError
+
+def verify():
+    """Return the result of conformance checking (OK = conforms, otherwise error message)"""
+    pass
+    # return "OK"
+    # raises UnknownError
+
+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