modelverse.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import urllib
  2. import urllib2
  3. import json
  4. # Helper functions
  5. taskname = "test"
  6. address = "http://localhost:8001"
  7. last_output = None
  8. def _input(value):
  9. # Ugly json encoding of primitives
  10. value = '"%s"' if type(value) == str else value
  11. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname})))
  12. def _output():
  13. try:
  14. global last_output
  15. last_output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname})))
  16. return last_output
  17. except:
  18. raise UnknownError()
  19. def _output_last():
  20. return last_output
  21. # Exceptions
  22. class UnknownError(Exception):
  23. pass
  24. class UnknownIdentifier(Exception):
  25. pass
  26. class UnknownType(Exception):
  27. pass
  28. class UnsupportedValue(Exception):
  29. pass
  30. class CompilationError(Exception):
  31. pass
  32. class NoSuchAttribute(Exception):
  33. pass
  34. # Actual functions to use for the wrapper
  35. def list():
  36. """Return a list of all IDs and the type of the element"""
  37. pass
  38. # return [(name1, type1), (name2, type2), ...]
  39. # raises UnknownError
  40. def types():
  41. """Return a list of all types usable in the model"""
  42. pass
  43. # return [type1, type2, ...]
  44. # raises UnknownError
  45. def read(ID):
  46. """Return a tuple of information on the element: its type and source/target (None if not an edge)"""
  47. pass
  48. # return (type, (source, target))
  49. # raises UnknownError
  50. # raises UnknownIdentifier
  51. def read_attrs(ID):
  52. """Return a dictionary of attribute value pairs"""
  53. pass
  54. # return {attr1: value1, attr2: value2, ...}
  55. # raises UnknownError
  56. def verify():
  57. """Return the result of conformance checking (OK = conforms, otherwise error message)"""
  58. pass
  59. # return "OK"
  60. # raises UnknownError
  61. def instantiate(typename, edge=None, ID=""):
  62. """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""
  63. pass
  64. # return instantiated_ID
  65. # raises UnknownError
  66. # raises UnknownType
  67. # raises UnknownIdentifier
  68. def delete(ID):
  69. """Delete the element with the given ID"""
  70. pass
  71. # return None
  72. # raises UnknownError
  73. # raises UnknownIdentifier
  74. def attr_assign(ID, attr, value):
  75. """Assign a value to an attribute"""
  76. pass
  77. # return None
  78. # raises UnknownError
  79. # raises UnknownIdentifier
  80. # raises NoSuchAttribute
  81. # raises UnsupportedValue
  82. def attr_assign_code(ID, attr, code):
  83. """Assign a piece of Action Language code to the attribute"""
  84. pass
  85. # return None
  86. # raises UnknownError
  87. # raises UnknownIdentifier
  88. # raises NoSuchAttribute
  89. # raises UnsupportedValue
  90. def upload(new_model):
  91. """Overwrite the current model with the provided model"""
  92. pass
  93. # return None
  94. # raises UnknownError
  95. # raises CompilationError
  96. def read_outgoing(ID, typename):
  97. """Returns a list of all outgoing associations of a specific type ("" = all)"""
  98. pass
  99. # return [name1, name2, ...]
  100. # raises UnknownError
  101. # raises UnknownIdentifier
  102. # raises UnknownType
  103. def read_incoming(ID, typename):
  104. """Returns a list of all incoming associations of a specific type ("" = all)"""
  105. pass
  106. # return [name1, name2, ...]
  107. # raises UnknownError
  108. # raises UnknownIdentifier
  109. # raises UnknownType