Rewrites FunctionExpression SDK syntax and example
diff --git a/asterix-app/src/main/resources/sdk/static/example/demo.html b/asterix-app/src/main/resources/sdk/static/example/demo.html
index 4eaff8a..46fb89f 100644
--- a/asterix-app/src/main/resources/sdk/static/example/demo.html
+++ b/asterix-app/src/main/resources/sdk/static/example/demo.html
@@ -280,18 +280,20 @@
</pre></div>
<div class="how-to-run"><pre><code class="javascript">
- var expression8 = new FunctionExpression({
- "function" : "count",
- "expression" : new ForClause(
- "fbu", null, new AsterixExpression().set(["dataset FacebookUsers"])
- ).bind(
- {"return" : new AsterixExpression().set(["$fbu"])}
- ),
- "dataverse" : "TinySocial",
- "success" : function(res) {
- $('#result8').html(res["results"]);
- }
- });
+ var expression8 = new FunctionExpression(
+ "count",
+ new ForClause("fbu", null, new AQLClause().set("dataset FacebookUsers"))
+ .bind( new ReturnClause( new AQLClause().set("$fbu") ))
+ );
+
+ OR
+
+ var expression8 = new FunctionExpression()
+ .fn("count")
+ .expression(
+ new ForClause("fbu", null, new AQLClause().set("dataset FacebookUsers"))
+ .bind( new ReturnClause( new AQLClause().set("$fbu") ))
+ );
</code></pre></div>
<div class="result-output" id="result8">
diff --git a/asterix-app/src/main/resources/sdk/static/example/js/demo.js b/asterix-app/src/main/resources/sdk/static/example/js/demo.js
index 0cca153..4724d25 100644
--- a/asterix-app/src/main/resources/sdk/static/example/js/demo.js
+++ b/asterix-app/src/main/resources/sdk/static/example/js/demo.js
@@ -4,7 +4,6 @@
$('#run0a').click(function () {
$('#result0a').html('');
var expression0a = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
$('#result0a').html(res["results"]);
}
@@ -21,7 +20,6 @@
$('#result0b').html('');
var expression0b = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result0b').html(res["results"]);
@@ -40,7 +38,6 @@
$('#result1').html('');
var expression1 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result1').html(res["results"]);
@@ -54,7 +51,6 @@
$('#result2a').html('');
var expression2a = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result2a').html(res["results"]);
@@ -68,7 +64,6 @@
$('#result2b').html('');
var expression2b = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result2b').html(res["results"]);
@@ -82,7 +77,6 @@
$('#result3').html('');
var expression3 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result3').html(res["results"]);
@@ -96,7 +90,6 @@
$('#result4').html('');
var expression4 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result4').html(res["results"]);
@@ -110,7 +103,6 @@
$('#result5').html('');
var expression5 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result5').html(res["results"]);
@@ -124,7 +116,6 @@
$('#result6').html('');
var expression6 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result6').html(res["results"]);
@@ -151,7 +142,6 @@
$('#result7').html('');
var expression7 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
alert(JSON.stringify(res));
$('#result7').html(res["results"]);
@@ -175,97 +165,73 @@
// 8 - Simple Aggregation
$('#run8').click(function () {
-
- // Option 1: Simple, Object Syntax
+
$('#result8').html('');
- var expression8 = new FunctionExpression({
- "function" : "count",
- "expression" : new ForClause("fbu", null, new AExpression().set("dataset FacebookUsers"))
- .bind( new ReturnClause( new AExpression().set("$fbu") )),
- "dataverse" : "TinySocial",
- "success" : function(res) {
- $('#result8').html(res["results"]);
- }
- });
- alert(expression8.val());
- // expression8.run();
+
+ // Version 1
+ var expression8 = new FunctionExpression(
+ "count",
+ new ForClause("fbu", null, new AQLClause().set("dataset FacebookUsers"))
+ .bind( new ReturnClause( new AQLClause().set("$fbu") ))
+ );
+
+ var success8 = function(res) {
+ $('#result8').html(res["results"]);
+ };
+ expression8.run(success8);
});
// 9a - Grouping & Aggregation
$("#run9a").click(function() {
$('#result9a').html('');
- var expression9a = new FLWOGRExpression({
- "dataverse" : "TinySocial",
- "success" : function(res) {
- $('#result9a').html(res["results"]);
- }
- })
- .bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
- .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
- .bind( new ReturnClause(
- {
- "user" : "$uid",
- "count" : new FunctionExpression(
- {
- "function" : "count",
- "expression" : new AExpression().set("$t")
- }
- )
- }
- ));
+ var expression9a = new FLWOGRExpression()
+ .bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
+ .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+ .bind( new ReturnClause(
+ {
+ "user" : "$uid",
+ "count" : new FunctionExpression("count", new AExpression().set("$t"))
+ }
+ ));
- expression9a.run();
+ var success9a = function(res) {
+ $('#result9a').html(res["results"]);
+ };
+ expression9a.run(success9a);
});
// 9b - Hash-based Grouping & Aggregation
$("#run9b").click(function() {
$('#result9b').html('');
- var expression9b = new FLWOGRExpression({
- "dataverse" : "TinySocial",
- "success" : function(res) {
- $('#result9b').html(res["results"]);
- }
- })
- .bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
- .bind( new AQLClause().set("/*+ hash*/"))
- .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
- .bind( new ReturnClause(
- {
- "user" : "$uid",
- "count" : new FunctionExpression(
- {
- "function" : "count",
- "expression" : new AExpression().set("$t")
- }
- )
- }
- ));
+ var expression9b = new FLWOGRExpression()
+ .bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
+ .bind( new AQLClause().set("/*+ hash*/"))
+ .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+ .bind( new ReturnClause(
+ {
+ "user" : "$uid",
+ "count" : new FunctionExpression("count", new AExpression().set("$t"))
+ }
+ ));
- expression9b.run();
+ var success9b = function(res) {
+ $('#result9b').html(res["results"]);
+ };
+ expression9b.run(success9b);
});
// 10 - Grouping and Limits
$("#run10").click(function() {
$('#result10').html('');
- var expression10 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
- "success" : function(res) {
- $('#result10').html(res["results"]);
- }
- })
+ var expression10 = new FLWOGRExpression()
.bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
.bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
.bind( new LetClause(
"c",
- new FunctionExpression(
- {
- "function" : "count",
- "expression" : new AExpression().set("$t")
- }
- )
+ new FunctionExpression("count", new AExpression().set("$t"))
))
.bind( new OrderbyClause( new AExpression().set("$c"), "desc" ) )
.bind( new LimitClause(new AExpression().set("3")) )
@@ -276,7 +242,10 @@
}
));
- expression10.run();
+ var success10 = function(res) {
+ $('#result10').html(res["results"]);
+ };
+ expression10.run(success10);
});
// 11 - Left Outer Fuzzy Join
@@ -284,7 +253,6 @@
$('#result11').html('');
var expression11 = new FLWOGRExpression({
- "dataverse" : "TinySocial",
"success" : function(res) {
$('#result11').html(res["results"]);
}
@@ -300,7 +268,10 @@
.bind( new ReturnClause(new AQLClause().set("$t2.referred-topics")))
}));
- expression11.run();
+ var success11 = function(res) {
+ $('#result11').html(res["results"]);
+ };
+ expression11.run(success11);
});
//$('#run0a').trigger('click');
@@ -313,7 +284,7 @@
//$('#run5').trigger('click');
//$('#run6').trigger('click');
//$('#run7').trigger('click');
- //$('#run8').trigger('click');
+ $('#run8').trigger('click');
$('#run9a').trigger('click');
$('#run9b').trigger('click');
$('#run10').trigger('click');
diff --git a/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js b/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
index 33b8e3c..1b4f754 100644
--- a/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
+++ b/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
@@ -10,10 +10,6 @@
AExpression.prototype.bind = function(options) {
var options = options || {};
- if (options.hasOwnProperty("dataverse")) {
- this._properties["dataverse"] = options["dataverse"];
- }
-
if (options.hasOwnProperty("success")) {
this._success = options["success"];
}
@@ -24,13 +20,13 @@
};
-AExpression.prototype.run = function() {
- var success_fn = this._success;
+AExpression.prototype.run = function(successFn) {
+ var success_fn = successFn;
$.ajax({
type : 'GET',
url : "http://localhost:19002/query",
- data : {"query" : this.val()},
+ data : {"query" : "use dataverse TinySocial;\n" + this.val()},
dataType : "json",
success : function(data) {
success_fn(data);
@@ -65,7 +61,7 @@
AExpression.prototype.error = function(msg) {
- return "Asterix FunctionExpression Error: " + msg;
+ //return "Asterix FunctionExpression Error: " + msg;
};
@@ -74,14 +70,23 @@
//
// @param options [Various],
// @key function [String], a function to be applid to the expression
-// @key expression [AsterixExpression or AsterixClause] an AsterixExpression/Clause to which the fn will be applied
-function FunctionExpression(options) {
+// @key expression [AsterixExpression or AQLClause] an AsterixExpression/Clause to which the fn will be applied
+function FunctionExpression() {
// Initialize superclass
AExpression.call(this);
+
+ this._properties["function"] = "";
+ this._properties["expression"] = new AExpression().set("");
- // Possible to initialize a function epxression without inputs, or with them
- this.bind(options);
+ // Check for fn/expression input
+ if (arguments.length == 2 && typeof arguments[0] == "string" &&
+ (arguments[1] instanceof AExpression || arguments[1] instanceof AQLClause)) {
+
+ this._properties["function"] = arguments[0];
+ this._properties["expression"] = arguments[1];
+
+ }
// Return object
return this;
@@ -92,23 +97,25 @@
FunctionExpression.prototype.constructor = FunctionExpression;
-FunctionExpression.prototype.bind = function(options) {
+FunctionExpression.prototype.fn = function(fnName) {
- AExpression.prototype.bind.call(this, options);
+ if (typeof fnName == "string") {
+ this._properties["function"] = fnName;
+ }
- var options = options || {};
-
- if (options.hasOwnProperty("function")) {
- this._properties["function"] = options["function"];
- }
-
- if (options.hasOwnProperty("expression")) {
- this._properties["expression"] = options["expression"];
- }
-
return this;
};
+
+FunctionExpression.prototype.expression = function(expression) {
+ if (expression instanceof AExpression || expression instanceof AQLClause) {
+ this._properties["expression"] = expression;
+ }
+
+ return this;
+};
+
+
FunctionExpression.prototype.val = function () {
return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
};
@@ -201,10 +208,9 @@
};
AQLClause.prototype.bind = function(options) {
- var options = options || {};
- if (options.hasOwnProperty("return")) {
- this._properties["return"] = options["return"];
+ if (options instanceof AQLClause) {
+ this._properties["clause"] += " " + options.val();
}
return this;
@@ -386,6 +392,7 @@
// At least one argument expression is required, and first should be expression
if (arguments.length == 0 || !(arguments[0] instanceof AExpression)) {
+
// TODO Not sure which error to throw for an empty OrderBy but this should fail.
alert("Order By Error");
this._properties["clause"] = null;