genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 1 | function AsterixSDK() { |
| 2 | |
| 3 | // Asterix SDK => send |
| 4 | // Posts a message containing an API endpoint, json data, |
| 5 | // and a UI callback function. |
| 6 | // |
| 7 | // @param handler [Asterix REST Controller], a handler object |
| 8 | // that provides REST request information. |
| 9 | // |
| 10 | // Anticipated Usage: |
| 11 | // |
| 12 | // var a = AsterixSDK(); |
| 13 | // var e = Expression; |
| 14 | // var h = AsterixRestController.bind(e); |
| 15 | // a.send(h); |
| 16 | myThis = this; |
| 17 | this.callbacks = { |
| 18 | "sync" : function() { alert("default sync"); }, |
| 19 | "async" : function() {} |
| 20 | }; |
| 21 | this.send = function(handler, cb) { |
| 22 | myThis.callbacks = cb; |
| 23 | this.handler = handler; |
| 24 | this.extras = handler["extras"]; |
| 25 | this.xhr.post( |
| 26 | handler["endpoint"], |
| 27 | handler["apiData"], |
| 28 | this.branch |
| 29 | ); |
| 30 | }; |
| 31 | |
| 32 | this.branch = function(response) { |
| 33 | if (response && response["error-code"]) { |
| 34 | |
| 35 | alert("Error [Code" + response["error-code"][0] + "]: " + response["error-code"][1]); |
| 36 | |
| 37 | } else if (response && response["results"]) { |
| 38 | var fn_callback = myThis.callbacks["sync"]; |
| 39 | fn_callback(response, myThis.extras); |
| 40 | |
| 41 | } else if (response["handle"]) { |
| 42 | |
| 43 | var fn_callback = this.callbacks["async"]; |
| 44 | fn_callback(response, extra); |
| 45 | |
| 46 | } else if (response["status"]) { |
| 47 | |
| 48 | var fn_callback = this.callbacks["sync"]; |
| 49 | fn_callback(response, extra); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Asterix SDK => bindingHandler |
| 54 | // AsterixExpression form handler where a new REST API point is bound. Takes as input any |
| 55 | // AsterixExpression, each of which is bindable. |
| 56 | this.bindingHandler = new AsterixExpression(); |
| 57 | this.bind = this.bindingHandler.bind; |
| 58 | } |
| 59 | |
| 60 | function AsterixExpression() { |
| 61 | this.init(); |
| 62 | return this; |
| 63 | } |
| 64 | |
| 65 | AsterixExpression.prototype.init = function () { |
| 66 | this.dataverse = ""; // TODO This shouldn't make it to send |
| 67 | this.boundTo = {}; |
| 68 | this.clauses = []; |
| 69 | this.ui_callback_on_success = function() {}; |
| 70 | this.ui_callback_on_success_async = function() {}; |
| 71 | }; |
| 72 | |
| 73 | AsterixExpression.prototype.bind = function(expression) { |
| 74 | // If expression is an AsterixExpression, it becomes base |
| 75 | if (expression instanceof AsterixExpression) { |
| 76 | this.boundTo = expression; |
| 77 | } else if (expression instanceof AsterixClause) { |
| 78 | this.clauses.push(expression.val()); |
| 79 | } |
| 80 | return this; |
| 81 | }; |
| 82 | |
| 83 | AsterixExpression.prototype.send = function(arc) { |
| 84 | // Hackiest of hacks |
| 85 | var g = new AsterixSDK(); |
| 86 | g.send(arc, arc["callback"]); |
| 87 | }; |
| 88 | |
| 89 | AsterixExpression.prototype.clear = function() { |
| 90 | this.clauses.length = 0; |
| 91 | return this; |
| 92 | }; |
| 93 | |
| 94 | AsterixExpression.prototype.val = function() { |
| 95 | return this.clauses.join("\n"); |
| 96 | }; |
| 97 | |
| 98 | AsterixExpression.prototype.success = function(fn, isSync) { |
| 99 | if (isSync) { |
| 100 | this.ui_callback_on_success = fn; |
| 101 | } else { |
| 102 | this.ui_callback_on_success_async = fn; |
| 103 | } |
| 104 | return this; |
| 105 | }; |
| 106 | |
| 107 | AsterixExpression.prototype.set = function(statements_arr) { |
| 108 | for (var i = 0; i < statements_arr.length; i++) { |
| 109 | this.clauses.push(statements_arr[i]); |
| 110 | } |
| 111 | return this; |
| 112 | }; |
| 113 | |
| 114 | AsterixExpression.prototype.use_dataverse = function(dv) { |
| 115 | this.dataverse = dv; |
| 116 | this.clauses.push("use dataverse " + dv + ";"); |
| 117 | return this; |
| 118 | }; |
| 119 | |
| 120 | AsterixExpression.prototype.return = function(return_object) { |
| 121 | var components = []; |
| 122 | for (var key in return_object) { |
| 123 | components.push('"' + key + '" : ' + return_object[key]); |
| 124 | } |
| 125 | |
| 126 | var return_expression = 'return { ' + components.join(', ') + ' }'; |
| 127 | this.clauses.push(return_expression); |
| 128 | return this; |
| 129 | }; |
| 130 | |
genia.likes.science@gmail.com | 512454d | 2013-05-28 03:32:20 -0700 | [diff] [blame] | 131 | |
| 132 | // FunctionExpression |
| 133 | // Parent: AsterixExpression |
| 134 | // |
| 135 | // @param bindables [Various], |
| 136 | // @key fn [String], a function to be applid to the expression |
| 137 | // @param expr [AsterixExpression or AsterixClause] an AsterixExpression/Clause to which the fn will be applied |
| 138 | function FunctionExpression(options) { |
| 139 | |
| 140 | // Initialize own properties to be null |
| 141 | this._properties = {}; |
| 142 | this._success = function() {}; |
| 143 | |
| 144 | // Possible to initialize a function epxression without inputs, or with them |
| 145 | this.bind(options); |
| 146 | |
| 147 | // Return object |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | FunctionExpression.prototype.bind = function(options) { |
| 152 | var options = options || {}; |
| 153 | |
| 154 | if (options.hasOwnProperty("function")) { |
| 155 | this._properties["function"] = options["function"]; |
| 156 | } |
| 157 | |
| 158 | if (options.hasOwnProperty("expression")) { |
| 159 | this._properties["expression"] = options["expression"]; |
| 160 | } |
| 161 | |
| 162 | if (options.hasOwnProperty("dataverse")) { |
| 163 | this._properties["dataverse"] = options["dataverse"]; |
| 164 | } |
| 165 | |
| 166 | if (options.hasOwnProperty("success")) { |
| 167 | this._success = options["success"]; |
| 168 | } |
| 169 | |
| 170 | return this; |
| 171 | }; |
| 172 | |
| 173 | FunctionExpression.prototype.val = function () { |
| 174 | |
| 175 | var value = ""; |
| 176 | |
| 177 | // If there is a dataverse defined, provide it. TODO Can be overriden |
| 178 | if (this._properties.hasOwnProperty("dataverse")) { |
| 179 | value += "use dataverse " + this._properties["dataverse"] + ";\n"; |
| 180 | } |
| 181 | |
| 182 | return value + this._properties["function"] + "(" + this._properties["expression"].val() + ");"; |
| 183 | }; |
| 184 | |
| 185 | FunctionExpression.prototype.error = function(msg) { |
| 186 | return "Asterix FunctionExpression Error: " + msg; |
| 187 | }; |
| 188 | |
| 189 | FunctionExpression.prototype.run = function() { |
| 190 | var success_fn = this._success; |
| 191 | |
| 192 | $.ajax({ |
| 193 | type : 'GET', |
| 194 | url : "http://localhost:19101/query", |
| 195 | data : {"query" : this.val()}, |
| 196 | dataType : "json", |
| 197 | success : function(data) { |
| 198 | success_fn(data); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | return this; |
| 203 | } |
genia.likes.science@gmail.com | 73dcc70 | 2013-05-28 04:21:28 -0700 | [diff] [blame^] | 204 | |
| 205 | |
| 206 | // FLOWGR Expression |
| 207 | // FLWOGR ::= ( ForClause | LetClause ) ( Clause )* "return" Expression |
| 208 | // Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause |
| 209 | // ForClause ::= "for" Variable ( "at" Variable )? "in" ( Expression ) |
| 210 | // LetClause ::= "let" Variable ":=" Expression |
| 211 | // WhereClause ::= "where" Expression |
| 212 | // OrderbyClause ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )* |
| 213 | // GroupClause ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )* |
| 214 | // LimitClause ::= "limit" Expression ( "offset" Expression )? |
| 215 | // DistinctClause ::= "distinct" "by" Expression ( "," Expression )* |
| 216 | // Variable ::= <VARIABLE> |
| 217 | |
| 218 | |
| 219 | // AQLClause |
| 220 | // |
| 221 | // Base Clause ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause |
| 222 | function AQLClause() { |
| 223 | this._properties = {}; |
| 224 | this._properties["clause"] = ""; |
| 225 | } |
| 226 | |
| 227 | AQLClause.prototype.val = function() { |
| 228 | var value = this._properties["clause"]; |
| 229 | |
| 230 | if (this._properties.hasOwnProperty("return")) { |
| 231 | value += " return " + this._properties["return"].val(); |
| 232 | } |
| 233 | |
| 234 | return value; |
| 235 | }; |
| 236 | |
| 237 | AQLClause.prototype.bind = function(options) { |
| 238 | var options = options || {}; |
| 239 | |
| 240 | if (options.hasOwnProperty("return")) { |
| 241 | this._properties["return"] = options["return"]; |
| 242 | } |
| 243 | |
| 244 | return this; |
| 245 | }; |
| 246 | |
| 247 | |
| 248 | // ForClause |
| 249 | // |
| 250 | // Grammar: |
| 251 | // "for" Variable ( "at" Variable )? "in" ( Expression ) |
| 252 | // |
| 253 | // @param for_variable [String], REQUIRED, first variable in clause |
| 254 | // @param at_variable [String], NOT REQUIRED, first variable in clause |
| 255 | // @param expression [AsterixExpression], REQUIRED, expression to evaluate |
| 256 | // |
| 257 | // TODO Error Checking |
| 258 | function ForClause(for_variable, at_variable, expression) { |
| 259 | AQLClause.call(this); |
| 260 | |
| 261 | // at_variable is optional, check if defined |
| 262 | var at = typeof at_variable ? at_variable : null; |
| 263 | |
| 264 | // Prepare clause |
| 265 | this._properties["clause"] = "for $" + for_variable; |
| 266 | if (at != null) { |
| 267 | this._properties["clause"] += " at $" + at_variable; |
| 268 | } |
| 269 | this._properties["clause"] += " in " + expression.val(); |
| 270 | return this; |
| 271 | } |
| 272 | |
| 273 | ForClause.prototype = Object.create(AQLClause.prototype); |
| 274 | ForClause.prototype.constructor = ForClause; |