modelverse.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. import urllib
  2. import urllib2
  3. import json
  4. from urllib2 import URLError
  5. # Helper functions and configuration: do not use yourself!
  6. taskname = "test"
  7. address = "http://localhost:8001"
  8. last_output = None
  9. mode = 0
  10. def _input(value):
  11. # Ugly json encoding of primitives
  12. value = '"%s"' if type(value) == str else value
  13. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname})))
  14. def _input_raw(value, taskname):
  15. # Ugly json encoding of primitives
  16. urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname})))
  17. def _compile_AL(AL_file):
  18. # Compile an action language file and send the compiled code
  19. pass
  20. def _compile_model(model_file):
  21. # Compile a model and send the compiled graph
  22. pass
  23. def _output():
  24. try:
  25. global last_output
  26. last_output = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": taskname})))
  27. return last_output
  28. except:
  29. raise UnknownError()
  30. def _output_last():
  31. return last_output
  32. # Exceptions
  33. class UnknownError(Exception):
  34. pass
  35. class UnknownIdentifier(Exception):
  36. pass
  37. class UnknownType(Exception):
  38. pass
  39. class UnsupportedValue(Exception):
  40. pass
  41. class CompilationError(Exception):
  42. pass
  43. class NoSuchAttribute(Exception):
  44. pass
  45. class UnknownModel(Exception):
  46. pass
  47. class ConnectionError(Exception):
  48. pass
  49. class ModelExists(Exception):
  50. pass
  51. class PermissionDenied(Exception):
  52. pass
  53. class InvalidMode(Exception):
  54. pass
  55. # Main MvC operations
  56. def init():
  57. """Starts up the connection to the Modelverse."""
  58. # return None
  59. # raises ConnectionError
  60. # raises UnknownError
  61. # raises InvalidMode
  62. global mode
  63. if mode != 0:
  64. raise InvalidMode()
  65. try:
  66. _input_raw('"%s"' % taskname, "user_manager")
  67. mode = 1
  68. except URLError as e:
  69. raise ConnectionError(e.reason)
  70. except Exception as e:
  71. raise UnknownError(str(e))
  72. def login(username, password):
  73. """Log in an existing user."""
  74. # return None
  75. # raises UnknownError
  76. # raises PermissionDenied
  77. global mode
  78. if mode != 1:
  79. raise InvalidMode()
  80. try:
  81. _input(username)
  82. _input(password)
  83. mode = 2
  84. except Exception as e:
  85. raise UnknownError(str(e))
  86. def register(username, password):
  87. """Register a new user."""
  88. # return None
  89. # raises UnknownError
  90. # raises UserExists
  91. global mode
  92. if mode != 1:
  93. raise InvalidMode()
  94. try:
  95. _input(username)
  96. _input(password)
  97. _input(password)
  98. mode = 2
  99. except Exception as e:
  100. raise UnknownError(str(e))
  101. def model_add(model_name, metamodel_name):
  102. """Instantiate a new model."""
  103. # return None
  104. # raises UnknownModel
  105. # raises ModelExists
  106. # raises UnknownError
  107. if mode != 2:
  108. raise InvalidMode()
  109. try:
  110. _input("model_add")
  111. _input(metamodel_name)
  112. _input(model_name)
  113. except Exception as e:
  114. raise UnknownError(str(e))
  115. def model_modify(model_name):
  116. """Modify an existing model."""
  117. # return is_write
  118. # raises UnknownModel
  119. # raises PermissionDenied
  120. # raises UnknownError
  121. global mode
  122. if mode != 2:
  123. raise InvalidMode()
  124. try:
  125. _input("model_modify")
  126. _input(model_name)
  127. mode = 3
  128. _input("help")
  129. _output()
  130. if ("r/w" in _output()):
  131. write = True
  132. else:
  133. write = False
  134. _output()
  135. return write
  136. except Exception as e:
  137. raise UnknownError(str(e))
  138. def model_list():
  139. """List all models."""
  140. # return [(model1, metamodel1), (model2, metamodel2), ...]
  141. # raises UnknownError
  142. if mode != 2:
  143. raise InvalidMode()
  144. try:
  145. _input("model_list")
  146. lst = []
  147. while (_output() != "Ready for command..."):
  148. v = _last_output()
  149. m, mm = v.split(":")
  150. m.trim()
  151. mm.trim()
  152. lst.append((m, mm))
  153. return lst
  154. except Exception as e:
  155. raise UnknownError(str(e))
  156. def model_list_full():
  157. """List full information on all models."""
  158. # return [(model1, metamodel1, owner1, group1, permissions1), (model2, metamodel2, owner2, group2, permissions2), ...]
  159. # raises UnknownError
  160. if mode != 2:
  161. raise InvalidMode()
  162. try:
  163. _input("model_list_full")
  164. lst = []
  165. while (_output() != "Ready for command..."):
  166. v = _last_output()
  167. m, mm = v.split(":")
  168. m.trim()
  169. mm.trim()
  170. perm, own, grp, m = m.split(" ")
  171. lst.append((m, mm, own, grp, perm))
  172. return lst
  173. except Exception as e:
  174. raise UnknownError(str(e))
  175. def model_overwrite(model_name, new_model):
  176. """Upload a new model and overwrite an existing model."""
  177. # return None
  178. # raises UnknownModel
  179. # raises PermissionDenied
  180. # raises CompilationError
  181. # raises UnknownError
  182. if mode != 2:
  183. raise InvalidMode()
  184. try:
  185. _input("model_overwrite")
  186. _input(model_name)
  187. _compile_model(new_model)
  188. except Exception as e:
  189. raise UnknownError(str(e))
  190. def user_logout():
  191. """Log out the current user. A new login will be required afterwards."""
  192. # return None
  193. # raises UnknownException
  194. global mode
  195. if mode != 2:
  196. raise InvalidMode()
  197. try:
  198. _input("exit")
  199. mode = 1
  200. except Exception as e:
  201. raise UnknownError(str(e))
  202. def user_delete():
  203. """Removes the current user. A new login will be required afterwards."""
  204. # return None
  205. # raises UnknownException
  206. global mode
  207. if mode != 2:
  208. raise InvalidMode()
  209. try:
  210. _input("self-destruct")
  211. mode = 1
  212. except Exception as e:
  213. raise UnknownError(str(e))
  214. # Actual operations on the model
  215. def list():
  216. """Return a list of all IDs and the type of the element"""
  217. # return [(name1, type1), (name2, type2), ...]
  218. # raises UnknownError
  219. if mode != 3:
  220. raise InvalidMode()
  221. try:
  222. _input("list")
  223. lst = []
  224. while (_output() != "Ready for command..."):
  225. v = _last_output()
  226. m, mm = v.split(":")
  227. m.trim()
  228. mm.trim()
  229. lst.append((m, mm))
  230. return lst
  231. except Exception as e:
  232. raise UnknownError(str(e))
  233. def types():
  234. """Return a list of all types usable in the model"""
  235. # return [type1, type2, ...]
  236. # raises UnknownError
  237. if mode != 3:
  238. raise InvalidMode()
  239. try:
  240. _input("types")
  241. lst = []
  242. while (_output() != "Ready for command..."):
  243. v = _last_output()
  244. m, mm = v.split(":")
  245. m.trim()
  246. lst.append(m)
  247. return lst
  248. except Exception as e:
  249. raise UnknownError(str(e))
  250. def read(ID):
  251. """Return a tuple of information on the element: its type and source/target (None if not an edge)"""
  252. # return (type, (source, target))
  253. # raises UnknownError
  254. # raises UnknownIdentifier
  255. if mode != 3:
  256. raise InvalidMode()
  257. try:
  258. _input("read")
  259. _input(ID)
  260. _output()
  261. t = _output().split(":")[1].trim()
  262. if (not _output().startswith("Source:")):
  263. rval = (t, None)
  264. else:
  265. src = _last_output().split(":")[1].trim()
  266. trg = _output().split(":")[1].trim()
  267. rval = (t, (src, trg))
  268. while (_output() != "Ready for command..."):
  269. pass
  270. return rval
  271. except Exception as e:
  272. raise UnknownError(str(e))
  273. def read_attrs(ID):
  274. """Return a dictionary of attribute value pairs"""
  275. # return {attr1: value1, attr2: value2, ...}
  276. # raises UnknownError
  277. if mode != 3:
  278. raise InvalidMode()
  279. try:
  280. _input("read")
  281. _input(ID)
  282. rval = {}
  283. while (_output() != "Attributes:"):
  284. pass
  285. while (_output() != "Ready for command..."):
  286. r = _last_output()
  287. key, value = r.split(":")
  288. _, value = value.split("=")
  289. key.trim()
  290. value.trim()
  291. rval[key] = value
  292. return rval
  293. except Exception as e:
  294. raise UnknownError(str(e))
  295. def instantiate(typename, edge=None, ID=""):
  296. """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""
  297. # return instantiated_ID
  298. # raises UnknownError
  299. # raises UnknownType
  300. # raises UnknownIdentifier
  301. if mode != 3:
  302. raise InvalidMode()
  303. try:
  304. _input("instantiate")
  305. _input(typename)
  306. _input(ID)
  307. if (edge is not None):
  308. _input(edge[0])
  309. _input(edge[1])
  310. except Exception as e:
  311. raise UnknownError(str(e))
  312. def delete(ID):
  313. """Delete the element with the given ID"""
  314. # return None
  315. # raises UnknownError
  316. # raises UnknownIdentifier
  317. if mode != 3:
  318. raise InvalidMode()
  319. try:
  320. _input("delete")
  321. _input(ID)
  322. except Exception as e:
  323. raise UnknownError(str(e))
  324. def attr_assign(ID, attr, value):
  325. """Assign a value to an attribute"""
  326. # return None
  327. # raises UnknownError
  328. # raises UnknownIdentifier
  329. # raises NoSuchAttribute
  330. # raises UnsupportedValue
  331. if mode != 3:
  332. raise InvalidMode()
  333. try:
  334. _input("attr_modify")
  335. _input(ID)
  336. _input(attr)
  337. _input(value)
  338. except Exception as e:
  339. raise UnknownError(str(e))
  340. def attr_assign_code(ID, attr, code):
  341. """Assign a piece of Action Language code to the attribute"""
  342. # return None
  343. # raises UnknownError
  344. # raises UnknownIdentifier
  345. # raises NoSuchAttribute
  346. # raises UnsupportedValue
  347. if mode != 3:
  348. raise InvalidMode()
  349. try:
  350. _input("attr_modify")
  351. _input(ID)
  352. _input(attr)
  353. _compile_AL(code)
  354. except Exception as e:
  355. raise UnknownError(str(e))
  356. def upload(new_model):
  357. """Overwrite the current model with the provided model"""
  358. # return None
  359. # raises UnknownError
  360. # raises CompilationError
  361. if mode != 3:
  362. raise InvalidMode()
  363. try:
  364. _input("upload")
  365. _compile_model(new_model)
  366. except Exception as e:
  367. raise UnknownError(str(e))
  368. def read_outgoing(ID, typename):
  369. """Returns a list of all outgoing associations of a specific type ("" = all)"""
  370. # return [name1, name2, ...]
  371. # raises UnknownError
  372. # raises UnknownIdentifier
  373. # raises UnknownType
  374. if mode != 3:
  375. raise InvalidMode()
  376. try:
  377. _input("read_outgoing")
  378. _input(ID)
  379. _input(typename)
  380. lst = []
  381. while (_output() != "Please give your command."):
  382. lst.append(_last_output())
  383. return lst
  384. except Exception as e:
  385. raise UnknownError(str(e))
  386. def read_incoming(ID, typename):
  387. """Returns a list of all incoming associations of a specific type ("" = all)"""
  388. # return [name1, name2, ...]
  389. # raises UnknownError
  390. # raises UnknownIdentifier
  391. # raises UnknownType
  392. if mode != 3:
  393. raise InvalidMode()
  394. try:
  395. _input("read_incoming")
  396. _input(ID)
  397. _input(typename)
  398. lst = []
  399. while (_output() != "Please give your command."):
  400. lst.append(_last_output())
  401. return lst
  402. except Exception as e:
  403. raise UnknownError(str(e))
  404. def model_exit():
  405. """Leave model modify mode."""
  406. # return None
  407. # raises UnknownError
  408. global mode
  409. if mode != 3:
  410. raise InvalidMode()
  411. try:
  412. _input("exit")
  413. mode = 2
  414. except Exception as e:
  415. raise UnknownError(str(e))
  416. def transformation_add_MT_language():
  417. """Create a new Model Transformation language out of a set of metamodels."""
  418. raise NotImplementedError()
  419. def transformation_add_MT():
  420. """Create a new model transformation."""
  421. raise NotImplementedError()
  422. def transformation_add_AL():
  423. """Create a new action language fragment."""
  424. raise NotImplementedError()
  425. def transformation_add_MANUAL():
  426. """Create a new manual model operation."""
  427. raise NotImplementedError()
  428. def transformation_execute():
  429. """Execute an existing model operation."""
  430. raise NotImplementedError()
  431. def transformation_list():
  432. """List existing model operations."""
  433. raise NotImplementedError()
  434. def transformation_list_full():
  435. """List detailed information on model operations."""
  436. raise NotImplementedError()
  437. def transformation_detail():
  438. """List full details of a a model operation."""
  439. raise NotImplementedError()
  440. def transformation_RAMify():
  441. """Ramify an existing metamodel."""
  442. raise NotImplementedError()
  443. def process_execute():
  444. """Execute a process model."""
  445. raise NotImplementedError()
  446. def permission_modify():
  447. """Modify permissions of a model."""
  448. raise NotImplementedError()
  449. def permission_owner():
  450. """Modify the owning user of a model."""
  451. raise NotImplementedError()
  452. def permission_group():
  453. """Modify the owning group of a model."""
  454. raise NotImplementedError()
  455. def group_create():
  456. """Create a new group."""
  457. raise NotImplementedError()
  458. def group_delete():
  459. """Delete a group of which you are an owner."""
  460. raise NotImplementedError()
  461. def group_owner_add():
  462. """Add a new owning user to a group you own."""
  463. raise NotImplementedError()
  464. def group_owner_delete():
  465. """Delete an owning user to a group you own."""
  466. raise NotImplementedError()
  467. def group_join():
  468. """Add a new user to a group you own."""
  469. raise NotImplementedError()
  470. def group_kick():
  471. """Delete a user from a group you own."""
  472. raise NotImplementedError()
  473. def group_list():
  474. """List existing groups."""
  475. raise NotImplementedError()
  476. def admin_promote():
  477. """Promote a user to admin status."""
  478. raise NotImplementedError()
  479. def admin_demote():
  480. """Demote a user from admin status."""
  481. raise NotImplementedError()