modelverse.py 14 KB

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