PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/javascript/irs-api.js

http://github.com/kmi/irs
JavaScript | 278 lines | 211 code | 39 blank | 28 comment | 7 complexity | 83db1c1923cc626d78b9d4d75d61d84b MD5 | raw file
Possible License(s): CC0-1.0
  1. // Copyright Š 2007,2008 The Open University
  2. // This is an API to the JavaScript support on the IRS server.
  3. /*
  4. The httpget function has now been moved outside of the 'irs' function.
  5. Also, I added an 'asynchronous' parameter because there are times when we want to make the request to the server
  6. in a synchronous rather than asynchronous manner (e.g. when receiving the data in bulk for an ontology).
  7. (But honestly, I'm not sure what is peculiar about getting all the ontology data that means we have to change
  8. from asynchronous to synchronous. I just know we have to, otherwise it won't work)
  9. Note also, the 'onload' Event Listener for 'request' has been removed because this is only supported by Mozilla.
  10. It has been replaced with the 'onreadystatechange' Event Listener
  11. */
  12. function httpget(url,callback, asynchronous, multipart) {
  13. //In case the calling function didn't bother to say whether GET should be asynchronous or synchronous, then make it asynchronous by default.
  14. asynchronous = typeof(asynchronous) !== 'undefined' ? asynchronous : true;
  15. var request = new XMLHttpRequest();
  16. request.multipart = multipart;
  17. request.open("GET", url, true); //asynchronous
  18. request.onreadystatechange = function () {
  19. if(request.readyState == 4 || request.readyState == 0) {
  20. if (request.status == 200) {
  21. callback(request.responseText);
  22. }
  23. }
  24. }
  25. request.send(null);
  26. }
  27. var irs = function() {
  28. function baseUri () {
  29. return document.getElementById("baseid").href;
  30. }
  31. // The absolute IRS URI, computed from the relUri and the base href.
  32. function irsUri (relUri) {
  33. return baseUri() + relUri;
  34. }
  35. // CALLBACK is a function of one argument, an array of services.
  36. function listServices (callback) {
  37. httpget(irsUri("/api-javascript/events/services"),
  38. function (response) {callback(eval(response))});
  39. };
  40. // Call CALLBACK with one argument, an array of events. CALLBACK will
  41. // be called repeatedly, every IRS.WEB::*KEEP-ALIVE-INTERVAL* seconds.
  42. function listEvents (callback) {
  43. httpget(irsUri("/api-javascript/events/events-info"),
  44. function (response) {callback(eval(response))},
  45. true, true);
  46. };
  47. function listGoalSlots (callback, ontology, goal) {
  48. url = irsUri("/api-javascript/goal/slots?ontology=")
  49. + ontology + "&concept=" + goal;
  50. httpget(url, function (response) {callback(eval(response))});
  51. };
  52. // Achieve a goal
  53. function achieveGoal (callback, ontology, goal, slotValuePairs) {
  54. httpget(achieveGoalURL(ontology, goal, slotValuePairs), callback);
  55. };
  56. function achieveGoalURL (ontology, goal, slotValuePairs) {
  57. var slotArgs = "";
  58. for (var s=0; s<slotValuePairs.length; s++) {
  59. slotArgs += "&" + encodeURIComponent(slotValuePairs[s].name)
  60. + "=" + encodeURIComponent(slotValuePairs[s].value);
  61. }
  62. return irsUri("/api-rest/achieve-goal") + "?ontology=" + ontology
  63. + "&goal=" + goal + slotArgs;
  64. };
  65. function ocmlQuery (callback,ontology, query) {
  66. httpget(irsUri("/api-rest/query?o=") + ontology + "&q=" + encodeURIComponent(query),
  67. callback);
  68. }
  69. function getClassTree(callback, ontology, filters) {
  70. httpget(irsUri("/api-javascript/class-tree?")
  71. + "ontology=" + ontology
  72. + "&filters=" + encodeURIComponent(filters),
  73. function (response) {callback(eval(response))});
  74. };
  75. function getWsmoEntitiesTree(ontology, filters, callback) {
  76. httpget(irsUri("/api-javascript/wsmo-entities-tree?")
  77. + "ontology=" + ontology
  78. + "&filters=" + encodeURIComponent(filters),
  79. function (response) {callback(eval(response)[0]);});
  80. };
  81. function getFunctionList(callback, ontology, filters) {
  82. httpget(irsUri("/api-javascript/function-list?ontology=") + ontology
  83. + "&filters=" + encodeURIComponent(filters),
  84. function (response) {callback(eval(response));});
  85. };
  86. function getConceptGraph(callback,ontology,concept) {
  87. httpget(irsUri("/api-javascript/graph/data?ontology=") + ontology
  88. + "&concept=" + encodeURIComponent(concept),
  89. function (response) {callback(eval(response));});
  90. }
  91. function getInstanceList(callback,ontology,filters) {
  92. httpget(irsUri("/api-javascript/instance-list?ontology=") + ontology
  93. + "&filters=" + encodeURIComponent(filters),
  94. function (response) {callback(eval(response));});
  95. }
  96. function getOntologyList(callback) {
  97. httpget(irsUri("/api-javascript/ontology-list"),
  98. function (response) {callback(eval(response));});
  99. };
  100. function getRelationList(callback, ontology, filters) {
  101. httpget(irsUri("/api-javascript/relation-list?ontology=") + ontology
  102. + "&filters=" + encodeURIComponent(filters),
  103. function (response) {callback(eval(response));});
  104. }
  105. function getRuleList(callback, ontology, filters) {
  106. httpget(irsUri("/api-javascript/rule-list?ontology=") + ontology
  107. + "&filters=" + encodeURIComponent(filters),
  108. function (response) {callback(eval(response));});
  109. }
  110. /* XXX For some reason, Firefox doesn't like eval'ing singleton
  111. JSON objects. So, we'll just wrap it in an array, and read
  112. that. */
  113. function evalSingletonObject(str) {
  114. str = "[" + str + "]";
  115. val = eval(str);
  116. return val[0];
  117. }
  118. function getClass(callback, ontology, classname) {
  119. httpget(irsUri("/api-javascript/class?ontology=") + ontology
  120. + "&class=" + encodeURIComponent(classname),
  121. function (response) {callback(evalSingletonObject(response));});
  122. };
  123. function getFunction(callback, ontology, func) {
  124. httpget(irsUri("/api-javascript/function?ontology=") + ontology
  125. + "&function=" + encodeURIComponent(func),
  126. function (response) {callback(evalSingletonObject(response));});
  127. };
  128. function getInstance(callback, ontology, instance) {
  129. httpget(irsUri("/api-javascript/instance?ontology=") + ontology
  130. + "&instance=" + encodeURIComponent(instance),
  131. function (response) {callback(evalSingletonObject(response));});
  132. };
  133. function getOntology(callback, ontology) {
  134. httpget(irsUri("/api-javascript/ontology?ontology=") + ontology,
  135. function (response) {callback(evalSingletonObject(response));});
  136. };
  137. function getRelation(callback, ontology, relation) {
  138. httpget(irsUri("/api-javascript/relation?ontology=") + ontology
  139. + "&relation=" + encodeURIComponent(relation),
  140. function (response) {callback(evalSingletonObject(response));});
  141. };
  142. function getRule(callback, ontology, rule) {
  143. httpget(irsUri("/api-javascript/rule?ontology=") + ontology
  144. + "&rule=" + encodeURIComponent(rule),
  145. function (response) {callback(evalSingletonObject(response));});
  146. };
  147. function updateServer(klass, ontology, type, callback) {
  148. httpget(irsUri("/irsb-update-server?class=") + klass
  149. + "&onto=" + ontology + "&typeclass=" + encodeURIComponent(type),
  150. callback);
  151. };
  152. // These are the exported functions:
  153. return {listServices: listServices,
  154. listEvents: listEvents,
  155. listGoalSlots: listGoalSlots,
  156. achieveGoal: achieveGoal,
  157. ocmlQuery: ocmlQuery,
  158. getWsmoEntitiesTree: getWsmoEntitiesTree,
  159. getClass: getClass,
  160. getOntology: getOntology,
  161. updateServer: updateServer,
  162. getClassTree: getClassTree,
  163. getConceptGraph: getConceptGraph,
  164. getFunction: getFunction,
  165. getFunctionList: getFunctionList,
  166. getRelation: getRelation,
  167. getInstanceList: getInstanceList,
  168. getInstance: getInstance,
  169. getOntologyList: getOntologyList,
  170. getRelationList: getRelationList,
  171. getRule: getRule,
  172. getRuleList: getRuleList,
  173. irsUri: irsUri
  174. };
  175. } ();
  176. // {{{ Lisp/JavaScript identifier munging
  177. // Translate NAME to a JavaScript compatible name.
  178. function lispNameToJs (name) {
  179. var cleanHyphen = name.replace(/-/g, "_");
  180. var cleanColon = cleanHyphen.replace(/:/g, "$");
  181. return cleanColon;
  182. }
  183. function JsNameToLisp (name) {
  184. var hypenated = name.replace(/_/g, "-");
  185. var coloned = hypenated.replace(/$/g, ":");
  186. return coloned;
  187. }
  188. // }}}
  189. // {{{ Utilities
  190. // XXX Not strictly part of the API, but we'll leave them here for the
  191. // moment.
  192. function replaceAll(string, oldString, newString) {
  193. return string.replace(eval("/"+oldString+"/g"), newString);
  194. }
  195. function xmlEscape(string) {
  196. return replaceAll(replaceAll(replaceAll(replaceAll(replaceAll(string,'&','&amp;'),'<','&lt;'), '>','&gt;'),'\'','&apos;'),'"','&quot;');
  197. }
  198. // }}}
  199. // {{{ Gensym functionality
  200. // For generatig sort-of unique identifiers.
  201. function genGen() {
  202. var number = 1;
  203. return function () {
  204. var n = number++;
  205. return "gensym" + n;
  206. };
  207. }
  208. var gensym = genGen();
  209. // }}}
  210. // {{{ OCML
  211. var ocml = function() {
  212. // {{{ OCML identifier handling
  213. // These are wrong! At some point, when we enable the use of
  214. // namespaced identifiers, we will need to stop case-fiddling.
  215. // But if you use these functions instead of doing it by hand, it
  216. // should be easier to switch later.
  217. function internSymbol(str) {
  218. return str;
  219. }
  220. function externSymbol(str) {
  221. return str;
  222. }
  223. // }}}
  224. return {internSymbol: internSymbol,
  225. externSymbol: externSymbol};
  226. }();
  227. // }}}