Merge branch 'eugenia/asterix_sdk_stable' into eugenia/black_cherry_stable

Conflicts:
	asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
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..3afaaa4 100644
--- a/asterix-app/src/main/resources/sdk/static/example/demo.html
+++ b/asterix-app/src/main/resources/sdk/static/example/demo.html
@@ -45,14 +45,10 @@
         </pre></div>
 
         <div class="how-to-run"><pre><code class="javascript">
-    var expression0a = new FLWOGRExpression({ 
-        "dataverse" : "TinySocial",
-        "success"   : function(res) {
-                          $('#result0a').html(res["results"]);
-                      }
-        })
-        .bind( new ForClause("user", null, new AsterixExpression().set(["dataset FacebookUsers"])) )
-        .bind( new WhereClause(new BooleanExpression("$user.id = 8")));
+        var expression0a = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause(new AExpression().set("$user.id = 8")))
+            .ReturnClause("$user");
         </code></pre></div>
 
         <div class="result-output" id="result0a">
@@ -72,8 +68,15 @@
     return $user;
         </pre></div>
         
-        <div class="how-to-run"><pre>
-        </pre></div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression0b = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause().and(
+                new AExpression().set("$user.id >= 2"), 
+                new AExpression().set("$user.id <= 4")
+            ))
+            .ReturnClause("$user");        
+        </code></pre></div>
 
         <div class="result-output" id="result0b">
         </div>
@@ -93,8 +96,15 @@
     return $user;
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression1 = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause().and(
+                new AExpression().set("$user.user-since >= datetime('2010-07-22T00:00:00')"), 
+                new AExpression().set("$user.user-since <= datetime('2012-07-29T23:59:59')")
+            ))
+            .ReturnClause("$user");
+        </code></pre></div>
 
         <div class="result-output" id="result1">
         </div>
@@ -117,8 +127,16 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression2a = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new ForClause ("message", new AQLClause().set("dataset FacebookMessages")))
+            .bind( new WhereClause (new AExpression().set("$message.author-id = $user.id")))
+            .ReturnClause({
+                    "uname" : "$user.name",
+                    "message" : "$message.message"
+                });
+        </code></pre></div>
 
         <div class="result-output" id="result2a">
         </div>
@@ -141,8 +159,18 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression2b = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new ForClause ("message", new AQLClause().set("dataset FacebookMessages")))
+            .bind( new WhereClause (new AExpression().set("$message.author-id /*+ indexnl */  = $user.id")))
+            .ReturnClause(
+                {
+                    "uname" : "$user.name",
+                    "message" : "$message.message"
+                }
+            );
+        </code></pre></div>
 
         <div class="result-output" id="result2b">
         </div>
@@ -165,8 +193,19 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression3messages = new FLWOGRExpression()
+            .bind( new ForClause("message", new AExpression().set("dataset FacebookMessages")))
+            .bind( new WhereClause(new AExpression().set("$message.author-id = $user.id")))
+            .ReturnClause("$message.message");
+
+        var expression3 = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .ReturnClause({
+                "uname": "$user.name",
+                "messages" : expression3messages
+            });
+        </code></pre></div>
 
         <div class="result-output" id="result3">
         </div>
@@ -189,8 +228,19 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression4messages = new FLWOGRExpression()
+            .bind( new ForClause( "t2", new AExpression().set("dataset TweetMessages")))
+            .bind( new WhereClause( new AExpression().set("spatial-distance($t.sender-location, $t2.sender-location) <= 1")))
+            .ReturnClause({ "msgtxt" : "$t2.message-text" });
+            
+        var expression4 = new FLWOGRExpression()
+            .bind( new ForClause( "t", new AExpression().set("dataset TweetMessages") ))
+            .ReturnClause({
+                "message" : "$t.message-text",
+                "nearby-messages" : expression4messages
+            });
+        </code></pre></div>
 
         <div class="result-output" id="result4">
         </div>
@@ -221,8 +271,26 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var similarUsersExpression = new FLWOGRExpression()
+            .bind( new ForClause ("t", new AQLClause().set("dataset TweetMessages")))
+            .bind( new LetClause ("tu", new AExpression().set("$t.user")))
+            .bind( new WhereClause (new AExpression().set("$tu.name ~= $fbu.name")))
+            .ReturnClause({
+                "twitter-screenname": "$tu.screen-name",
+                "twitter-name": "$tu.name"
+            });
+            
+        var expression5 = new FLWOGRExpression()
+            .bind( new ForClause ("fbu", new AQLClause().set("dataset FacebookUsers")))
+            .ReturnClause(
+                {
+                    "id" : "$fbu.id",
+                    "name" : "$fbu.name",
+                    "similar-users" : similarUsersExpression
+                }
+            );
+        </code></pre></div>
 
         <div class="result-output" id="result5">
         </div>
@@ -241,8 +309,18 @@
     return $fbu;
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression6 = new FLWOGRExpression()
+            .bind( new ForClause ("fbu", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new WhereClause ( 
+                new QuantifiedExpression (
+                    "some" , 
+                    {"$e" : new AQLClause().set("$fbu.employment") },
+                    new AQLClause().set("is-null($e.end-date)")
+                )
+            ))
+            .ReturnClause("$fbu");
+        </code></pre></div>
 
         <div class="result-output" id="result6">
         </div>
@@ -261,8 +339,21 @@
     return $fbu;
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression7 = new FLWOGRExpression()
+        .bind( new ForClause (
+            "fbu",
+            new AQLClause().set("dataset FacebookUsers")
+        ))
+        .bind( new WhereClause ( 
+            new QuantifiedExpression (
+                "every" , 
+                {"$e" : new AQLClause().set("$fbu.employment") },
+                new AQLClause().set("not(is-null($e.end-date))")
+            )
+        ))
+        .ReturnClause("$fbu");
+        </code></pre></div>
 
         <div class="result-output" id="result7">
         </div>
@@ -280,18 +371,12 @@
         </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 FLWOGRExpression()
+                .bind( new ForClause("fbu", new AQLClause().set("dataset FacebookUsers")))
+                .ReturnClause("$fbu")
+        );
         </code></pre></div>
 
         <div class="result-output" id="result8">
@@ -314,8 +399,17 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression9a = new FLWOGRExpression()
+            .bind( new ForClause("t", new AExpression().set("dataset TweetMessages")))
+            .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+            .ReturnClause(
+                {
+                    "user" : "$uid",
+                    "count" : new FunctionExpression("count", new AExpression().set("$t"))
+                }
+            );
+        </code></pre></div>
 
         <div class="result-output" id="result9a">
         </div>
@@ -338,8 +432,18 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression9b = new FLWOGRExpression()
+            .bind( new ForClause("t", new AExpression().set("dataset TweetMessages"))) 
+            .bind( new AQLClause().set("/*+ hash*/"))  
+            .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+            .ReturnClause(
+                {
+                    "user" : "$uid",
+                    "count" : new FunctionExpression("count", new AExpression().set("$t"))
+                }
+            );
+        </code></pre></div>
 
         <div class="result-output" id="result9b">
         </div>
@@ -364,8 +468,23 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression10 = new FLWOGRExpression()
+        .bind( new ForClause("t", new AExpression().set("dataset TweetMessages")))
+        .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+        .bind( new LetClause(
+            "c", 
+            new FunctionExpression("count", new AExpression().set("$t"))
+        ))
+        .bind( new OrderbyClause( new AExpression().set("$c"), "desc" ) )
+        .bind( new LimitClause(new AExpression().set("3")) )
+        .ReturnClause(
+            {
+                "user" : "$uid",
+                "count" : "$c"
+            }
+        );
+        </code></pre></div>
 
         <div class="result-output" id="result10">
         </div>
@@ -392,8 +511,20 @@
     };
         </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre><code class="javascript">
+        var expression11 = new FLWOGRExpression()
+        .bind( new ForClause( "t", new AExpression().set("dataset TweetMessages") ))
+        .ReturnClause({
+            "tweet"         : new AExpression().set("$t"),       
+            "similar-tweets": new FLWOGRExpression()
+                                .bind( new ForClause( "t2", new AExpression().set("dataset TweetMessages") ))
+                                .bind( new WhereClause().and(
+                                    new AExpression().set("$t2.referred-topics ~= $t.referred-topics"), 
+                                    new AExpression().set("$t2.tweetid != $t.tweetid")
+                                 ))
+                                .ReturnClause("$t2.referred-topics")
+        }); 
+        </code></pre></div>
 
         <div class="result-output" id="result11">
         </div>
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..3310b30 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
@@ -1,37 +1,40 @@
 $(document).ready(function() {
+    
+    var A = new AsterixDBConnection().dataverse("TinySocial");
 
     // 0A - Exact-Match Lookup
     $('#run0a').click(function () {
         $('#result0a').html('');
-        var expression0a = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            $('#result0a').html(res["results"]);
-                          } 
-            })
-            .bind( new ForClause("user", null, new AExpression().set("dataset FacebookUsers")) )
-            .bind( new WhereClause(new BooleanExpression("$user.id = 8")) )
-            .bind({ "return" : new AExpression().set("$user") });
         
-        expression0a.run();
+        var expression0a = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause(new AExpression().set("$user.id = 8")))
+            .ReturnClause("$user");
+        
+        var success0a = function(res) {
+            $('#result0a').html(res["results"]);
+        };
+        
+        A.run(expression0a.val(), success0a);
     });
 
     // 0B - Range Scan
     $("#run0b").click(function() {
         $('#result0b').html('');
 
-        var expression0b = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                              alert(JSON.stringify(res));
-                              $('#result0b').html(res["results"]);
-                          },
-            })
-            .bind( new ForClause("user", null, new AExpression().set("dataset FacebookUsers")) )
-            .bind( new WhereClause( new BooleanExpression("AND", new BooleanExpression(">=", "$user.id", 2), new BooleanExpression("<=", "$user.id", 4)) ) )
-            .bind( new ReturnClause("$user") );
-        alert(expression0b.val());
-        expression0b.run();
+        var expression0b = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause().and(
+                new AExpression().set("$user.id >= 2"), 
+                new AExpression().set("$user.id <= 4")
+            ))
+            .ReturnClause("$user");
+            
+        var success0b = function(res) {
+            $('#result0b').html(res["results"]);
+        };
+        
+        A.run(expression0b.val(), success0b);
 
     });
 
@@ -39,127 +42,169 @@
     $("#run1").click(function() {
         $('#result1').html('');
 
-        var expression1 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result1').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 1 " + expression1.val());
+        var expression1 = new FLWOGRExpression()
+            .bind( new ForClause("user", new AExpression().set("dataset FacebookUsers")))
+            .bind( new WhereClause().and(
+                new AExpression().set("$user.user-since >= datetime('2010-07-22T00:00:00')"), 
+                new AExpression().set("$user.user-since <= datetime('2012-07-29T23:59:59')")
+            ))
+            .ReturnClause("$user");
+        
+        var success1 = function(res) {
+            $('#result1').html(res["results"]);
+        };
+        A.run(expression1.val(), success1);
     });
-
+        
     // 2A - Equijoin
     $("#run2a").click(function() {
         $('#result2a').html('');
 
-        var expression2a = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result2a').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 2a " + expression2a.val());
+        var expression2a = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new ForClause ("message", new AQLClause().set("dataset FacebookMessages")))
+            .bind( new WhereClause (new AExpression().set("$message.author-id = $user.id")))
+            .ReturnClause({
+                    "uname" : "$user.name",
+                    "message" : "$message.message"
+                });
+        
+        var success2a = function(res) {
+            $('#result2a').html(res["results"]);
+        };
+        A.run(expression2a.val(), success2a);
     });
 
     // 2B - Index Join
     $("#run2b").click(function() {
         $('#result2b').html('');
 
-        var expression2b = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result2b').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 2b " + expression2b.val());
+        var expression2b = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new ForClause ("message", new AQLClause().set("dataset FacebookMessages")))
+            .bind( new WhereClause (new AExpression().set("$message.author-id /*+ indexnl */  = $user.id")))
+            .ReturnClause(
+                {
+                    "uname" : "$user.name",
+                    "message" : "$message.message"
+                }
+            );
+        
+        var success2b = function(res) {
+            $('#result2b').html(res["results"]);
+        };
+        A.run(expression2b.val(), success2b);
     });
 
     // 3 - Nested Outer Join
     $("#run3").click(function() {
         $('#result3').html('');
 
-        var expression3 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result3').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 3 " + expression3.val());
+        var expression3messages = new FLWOGRExpression()
+            .bind( new ForClause("message", new AExpression().set("dataset FacebookMessages")))
+            .bind( new WhereClause(new AExpression().set("$message.author-id = $user.id")))
+            .ReturnClause("$message.message");
+
+        var expression3 = new FLWOGRExpression()
+            .bind( new ForClause ("user", new AQLClause().set("dataset FacebookUsers")))
+            .ReturnClause({
+                "uname": "$user.name",
+                "messages" : expression3messages
+            });
+            
+        var success3 = function(res) {
+            $('#result3').html(res["results"]);
+        };
+        A.run(expression3.val(), success3);
     });
     
     // 4 - Theta Join
     $("#run4").click(function() {
         $('#result4').html('');
 
-        var expression4 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result4').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 4 " + expression4.val());
+        var expression4messages = new FLWOGRExpression()
+            .bind( new ForClause( "t2", new AExpression().set("dataset TweetMessages")))
+            .bind( new WhereClause( new AExpression().set("spatial-distance($t.sender-location, $t2.sender-location) <= 1")))
+            .ReturnClause({ "msgtxt" : "$t2.message-text" });
+            
+        var expression4 = new FLWOGRExpression()
+            .bind( new ForClause( "t", new AExpression().set("dataset TweetMessages") ))
+            .ReturnClause({
+                "message" : "$t.message-text",
+                "nearby-messages" : expression4messages
+            });
+        
+        var success4 = function(res) {
+            $('#result4').html(res["results"]);
+        };
+        A.run(expression4.val(), success4);
     });
 
     // 5 - Fuzzy Join
     $("#run5").click(function() {
         $('#result5').html('');
 
-        var expression5 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result5').html(res["results"]);
-                          }
-        });
-        alert("EXPRESSION 5 " + expression5.val());
+        var similarUsersExpression = new FLWOGRExpression()
+            .bind( new ForClause ("t", new AQLClause().set("dataset TweetMessages")))
+            .bind( new LetClause ("tu", new AExpression().set("$t.user")))
+            .bind( new WhereClause (new AExpression().set("$tu.name ~= $fbu.name")))
+            .ReturnClause({
+                "twitter-screenname": "$tu.screen-name",
+                "twitter-name": "$tu.name"
+            });
+        
+        var expression5 = new FLWOGRExpression()
+            .bind( new ForClause ("fbu", new AQLClause().set("dataset FacebookUsers")))
+            .ReturnClause(
+                {
+                    "id" : "$fbu.id",
+                    "name" : "$fbu.name",
+                    "similar-users" : similarUsersExpression
+                }
+            );
+        
+        var success5 = function (res) {
+            $('#result5').html(res["results"]);
+        };
+        
+        var simfunction = new SetStatement( "simfunction", "edit-distance" );
+        var simthreshold = new SetStatement( "simthreshold", "3");
+
+        A.run(
+            [ simfunction.val() , simthreshold.val() , expression5.val() ], 
+            success5
+        );
     });
 
     // 6 - Existential Quantification
     $("#run6").click(function() {
         $('#result6').html('');
 
-        var expression6 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result6').html(res["results"]);
-                          }
-        })
-        .bind( new ForClause (
-            "fbu", 
-            null, 
-            new AQLClause().set("dataset FacebookUsers")
-        ))
-        .bind( new WhereClause ( 
-            new QuantifiedExpression (
-                "some" , 
-                {"$e" : new AQLClause().set("$fbu.employment") },
-                new AQLClause().set("is-null($e.end-date)")
-            )
-        ))
-        .bind( new ReturnClause( new AQLClause().set("$fbu") ));
-        alert("EXPRESSION 6 " + expression6.val());
+        var expression6 = new FLWOGRExpression()
+            .bind( new ForClause ("fbu", new AQLClause().set("dataset FacebookUsers")))
+            .bind( new WhereClause ( 
+                new QuantifiedExpression (
+                    "some" , 
+                    {"$e" : new AQLClause().set("$fbu.employment") },
+                    new AQLClause().set("is-null($e.end-date)")
+                )
+            ))
+            .ReturnClause("$fbu");
+        
+        var success6 = function(res) {
+            $('#result6').html(res["results"]);
+        };
+        
+        A.run(expression6.val(), success6);
     });
 
     // 7 - Universal Quantification
     $("#run7").click(function() {
         $('#result7').html('');
 
-        var expression7 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            alert(JSON.stringify(res));
-                            $('#result7').html(res["results"]);
-                          }
-        })
+        var expression7 = new FLWOGRExpression()
         .bind( new ForClause (
-            "fbu", 
-            null, 
+            "fbu",
             new AQLClause().set("dataset FacebookUsers")
         ))
         .bind( new WhereClause ( 
@@ -169,153 +214,143 @@
                 new AQLClause().set("not(is-null($e.end-date))")
             )
         ))
-        .bind(new ReturnClause( new AQLClause().set("$fbu") ));
-        alert("EXPRESSION 7 " + expression7.val());
+        .ReturnClause("$fbu");
+        
+        var success7 = function(res) {
+            $('#result7').html(res["results"]);
+        };
+        A.run(expression7.val(), success7);
     });
 
     // 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();
+
+        var expression8 = new FunctionExpression(
+            "count",
+            new FLWOGRExpression()
+                .bind( new ForClause("fbu", new AQLClause().set("dataset FacebookUsers")))
+                .ReturnClause("$fbu")
+        );
+        
+        var success8 = function(res) {
+            $('#result8').html(res["results"]);
+        };
+        A.run(expression8.val(), 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", new AExpression().set("dataset TweetMessages")))
+            .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+            .ReturnClause(
+                {
+                    "user" : "$uid",
+                    "count" : new FunctionExpression("count", new AExpression().set("$t"))
+                }
+            );
 
-        expression9a.run();
+        var success9a = function(res) {
+            $('#result9a').html(res["results"]);
+        };
+        A.run(expression9a.val(), 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", new AExpression().set("dataset TweetMessages"))) 
+            .bind( new AQLClause().set("/*+ hash*/"))  
+            .bind( new GroupClause("uid", new AExpression().set("$t.user.screen-name"), "with", "t") )
+            .ReturnClause(
+                {
+                    "user" : "$uid",
+                    "count" : new FunctionExpression("count", new AExpression().set("$t"))
+                }
+            );
         
-        expression9b.run();
+        var success9b = function(res) {
+            $('#result9b').html(res["results"]);
+        };
+        A.run(expression9b.val(), success9b);
     });
     
     // 10 - Grouping and Limits
     $("#run10").click(function() {
         $('#result10').html('');
 
-        var expression10 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            $('#result10').html(res["results"]);
-                          }
-        })
-        .bind( new ForClause("t", null, new AExpression().set("dataset TweetMessages")))
+        var expression10 = new FLWOGRExpression()
+        .bind( new ForClause("t", 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")) )
-        .bind( new ReturnClause(
+        .ReturnClause(
             {
                 "user" : "$uid",
                 "count" : "$c"
             }
-        ));
+        );
 
-        expression10.run();
+        var success10 = function(res) {
+            $('#result10').html(res["results"]);
+        };
+        A.run(expression10.val(), success10);
     });
 
     // 11 - Left Outer Fuzzy Join
     $("#run11").click(function() {
         $('#result11').html('');
 
-        var expression11 = new FLWOGRExpression({
-            "dataverse" : "TinySocial",
-            "success"   : function(res) {
-                            $('#result11').html(res["results"]);
-                          }
-        })
-        .bind( new SetStatement( "simfunction", "jaccard" ))
-        .bind( new SetStatement( "simthreshold", "0.3"))
-        .bind( new ForClause( "t", null, new AExpression().set("dataset TweetMessages") ))
-        .bind( new ReturnClause({
+    var expression11 = new FLWOGRExpression()
+        .bind( new ForClause( "t", new AExpression().set("dataset TweetMessages") ))
+        .ReturnClause({
             "tweet"         : new AExpression().set("$t"),       
             "similar-tweets": new FLWOGRExpression()
-                                .bind( new ForClause( "t2", null, new AExpression().set("dataset TweetMessages") ))
-                                .bind( new AQLClause().set("where $t2.referred-topics ~= $t.referred-topics and $t2.tweetid != $t.tweetid") )
-                                .bind( new ReturnClause(new AQLClause().set("$t2.referred-topics")))
-        })); 
+                                .bind( new ForClause( "t2", new AExpression().set("dataset TweetMessages") ))
+                                .bind( new WhereClause().and(
+                                    new AExpression().set("$t2.referred-topics ~= $t.referred-topics"), 
+                                    new AExpression().set("$t2.tweetid != $t.tweetid")
+                                 ))
+                                .ReturnClause("$t2.referred-topics")
+        }); 
         
-        expression11.run();
+        var success11 = function(res) {
+            $('#result11').html(res["results"]);
+        };
+        
+        var simfunction = new SetStatement( "simfunction", "jaccard" );
+        var simthreshold = new SetStatement( "simthreshold", "0.3");
+        A.run(
+            [ simfunction.val(), simthreshold.val(), expression11.val()], 
+            success11
+        );
+        
     });
 
-    //$('#run0a').trigger('click');
-    //$('#run0b').trigger('click');
-    //$('#run1').trigger('click');
-    //$('#run2a').trigger('click');
-    //$('#run2b').trigger('click');
-    //$('#run3').trigger('click');
-    //$('#run4').trigger('click');
-    //$('#run5').trigger('click');
-    //$('#run6').trigger('click');
-    //$('#run7').trigger('click');
-    //$('#run8').trigger('click');
+    $('#run0a').trigger('click');
+    $('#run0b').trigger('click');
+    $('#run1').trigger('click');
+    $('#run2a').trigger('click');
+    $('#run2b').trigger('click');
+    $('#run3').trigger('click');
+    $('#run4').trigger('click');
+    $('#run5').trigger('click');
+    $('#run6').trigger('click');
+    $('#run7').trigger('click');
+    $('#run8').trigger('click');
     $('#run9a').trigger('click');
     $('#run9b').trigger('click');
     $('#run10').trigger('click');
     $('#run11').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 078c37c..dedc76e 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
@@ -1,4 +1,57 @@
-// Temporary AsterixExpression Placeholder
+function AsterixDBConnection(configuration) {
+    this._properties = {};
+    this._properties["dataverse"] = "";
+    this._properties["mode"] = "synchronous";
+    
+    var configuration = arguments || {};
+    
+    for (var key in configuration) {
+        this._properties[key] = configuration[key];
+    }
+    
+    return this;
+}
+
+
+AsterixDBConnection.prototype.dataverse = function(dataverseName) {
+    this._properties["dataverse"] = dataverseName;
+    
+    return this;
+};
+
+
+AsterixDBConnection.prototype.run = function(statements, successFn) {
+
+    var success_fn = successFn;
+   
+    if ( typeof statements === 'string') {
+        statements = [ statements ];
+    }
+    
+    var query = "use dataverse " + this._properties["dataverse"] + "\n;" + statements.join("\n");
+    var mode = this._properties["mode"];
+    
+    $.ajax({
+        type : 'GET',
+        url : "http://localhost:19002/query",
+        data : {
+            "query" : query,
+            "mode" : mode
+        },
+        dataType : "json",
+        success : function(data) {     
+            success_fn(data);
+        },
+        error: function(r) {
+            //alert("AsterixSDK ERROR\n" + JSON.stringify(r));
+        }
+    });
+
+    return this;
+};
+
+
+// Asterix Expressions
 function AExpression () {
     this._properties = {};
     this._success = function() {};
@@ -10,10 +63,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,33 +73,19 @@
 };
 
 
-AExpression.prototype.run = function(endpoint, payload, callbacks, extras) {
+AExpression.prototype.run = function(successFn) {
+    var success_fn = successFn;
 
-    this._extras = extras;
-    this._callbacks = callbacks;
-    myThis = this;
-
-    $.getJSON( endpoint, payload, function (response) {
-        //alert("DEBUG: Run Response: " + JSON.stringify(response));
-    
-        if (response && response["error-code"]) {
-           
-            alert("Error [Code" + response["error-code"][0] + "]: " + response["error-code"][1]);
-            
-        } else if (response && response["results"]) {
-            
-            var fn_callback = myThis._callbacks["sync"];
-            fn_callback(response, myThis._extras);
-            
-        } else if (response["handle"]) {
-            
-            var fn_callback = myThis._callbacks["async"];
-            fn_callback(response, myThis._extras);
-            
-        } else if (response["status"]) {
-                
-            var fn_callback = myThis._callbacks["sync"];
-            fn_callback(response, myThis._extras);
+    $.ajax({
+        type : 'GET',
+        url : "http://localhost:19002/query",
+        data : {"query" : "use dataverse TinySocial;\n" + this.val()},
+        dataType : "json",
+        success : function(data) {     
+            success_fn(data);
+        },
+        error: function(r) {
+            //alert(JSON.stringify(r));
         }
     });
 
@@ -115,24 +150,28 @@
 };
 
 
-AExpression.prototype.error = function(msg) {
-    return "Asterix FunctionExpression Error: " + msg;
-};
-
-
 // FunctionExpression
 // Parent: AsterixExpression
 // 
 // @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;
@@ -143,39 +182,30 @@
 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() + ")";
 };
 
 
-// FLWOGR         ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
-// Clause         ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
-// 
-// WhereClause    ::= "where" Expression
-// OrderbyClause  ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
-//
-// GroupClause    ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
-// LimitClause    ::= "limit" Expression ( "offset" Expression )?
-// DistinctClause ::= "distinct" "by" Expression ( "," Expression )*
-
-
 // FLWOGRExpression
 //
 // FLWOGRExpression ::= ( ForClause | LetClause ) ( Clause )* "return" Expression
@@ -233,6 +263,10 @@
 };
 
 
+FLWOGRExpression.prototype.ReturnClause = function(expression) {
+    return this.bind(new ReturnClause(expression));
+};
+
 // AQLClause
 //
 // Base Clause  ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
@@ -243,19 +277,14 @@
 
 AQLClause.prototype.val = function() {
     var value = this._properties["clause"];
-
-    if (this._properties.hasOwnProperty("return")) {
-        value += " return " + this._properties["return"].val();
-    }
  
     return value;
 };
 
 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;
@@ -275,20 +304,18 @@
 // @param for_variable [String], REQUIRED, first variable in clause 
 // @param at_variable [String], NOT REQUIRED, first variable in clause
 // @param expression [AsterixExpression], REQUIRED, expression to evaluate
-//
-// TODO Error Checking
 function ForClause(for_variable, at_variable, expression) {
     AQLClause.call(this);
   
-    // at_variable is optional, check if defined
-    var at = typeof at_variable ? at_variable : null;
-
-    // Prepare clause
-    this._properties["clause"] = "for " + for_variable;
-    if (at != null) {
-        this._properties["clause"] += " at " + at_variable;
+    this._properties["clause"] = "for $" + arguments[0];
+    
+    if (arguments.length == 3) {
+        this._properties["clause"] += " at $" + arguments[1];
+        this._properties["clause"] += " in " + arguments[2].val();
+    } else if (arguments.length == 2) {
+        this._properties["clause"] += " in " + arguments[1].val();
     }
-    this._properties["clause"] += " in " + expression.val();
+    
     return this;
 }
 
@@ -326,9 +353,13 @@
     AQLClause.call(this);
 
     this._properties["clause"] = "return ";
+    
     if (expression instanceof AExpression || expression instanceof AQLClause) {
         this._properties["clause"] += expression.val();
-    } else if ( Object.getPrototypeOf( expression ) === Object.prototype ) {
+    
+    } else if ( typeof expression == "object" && Object.getPrototypeOf( expression ) === Object.prototype ) {
+        
+        // TODO Null object check
         
         this._properties["clause"] += "{";
         var returnStatements = [];
@@ -341,22 +372,19 @@
             }
         }
         this._properties["clause"] += returnStatements.join(",\n");
-        this._properties["clause"] += "}";  
+        this._properties["clause"] += "\n}";  
     
     } else {
-        this._properties["clause"] += new AExpression().set(expression).val();
+        this._properties["clause"] += new AQLClause().set(expression).val();
     }
 
     return this;
 }
 
+
 ReturnClause.prototype = Object.create(AQLClause.prototype);
 ReturnClause.prototype.constructor = ReturnClause;
 
-ReturnClause.prototype.val = function () {
-    return this._properties["clause"];  
-};
-
 
 // WhereClause
 //
@@ -399,6 +427,40 @@
 };
 
 
+WhereClause.prototype.and = function() {
+    
+    var andClauses = [];  
+    for (var expression in arguments) {
+        
+        if (arguments[expression] instanceof AExpression) {
+            andClauses.push(arguments[expression].val());
+        }
+    }
+    
+    if (andClauses.length > 0) {
+        this._properties["stack"].push(new AExpression().set(andClauses.join(" and ")));
+    }
+    
+    return this;
+};
+
+
+WhereClause.prototype.or = function() {
+    var orClauses = [];  
+    for (var expression in arguments) {
+        
+        if (arguments[expression] instanceof AExpression) {
+            orClauses.push(arguments[expression].val());
+        }
+    }
+    
+    if (andClauses.length > 0) {
+        this._properties["stack"].push(new AExpression().set(orClauses.join(" and ")));
+    }
+    
+    return this;
+};
+
 // LimitClause
 // Grammar:
 // LimitClause    ::= "limit" Expression ( "offset" Expression )?
@@ -437,6 +499,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;
@@ -491,7 +554,7 @@
     var expressions = [];
     var variableRefs = [];
     var isDecor = false;
-
+    
     while (expc < arguments.length) {
 
         if (arguments[expc] instanceof AExpression) {
@@ -539,18 +602,6 @@
 GroupClause.prototype = Object.create(AQLClause.prototype);
 GroupClause.prototype.constructor = GroupClause;
 
-// BooleanExpression
-// 
-// TODO
-function BooleanExpression(expression) {
-    this.value = expression;
-    alert("Debugging Bool: " + arguments.length + " " + expression);
-} 
-
-BooleanExpression.prototype.val = function() {
-    return this.value;
-}
-
 
 // SetStatement
 //
@@ -601,41 +652,3 @@
     var value = AExpression.prototype.val.call(this);
     return "(" + value + ")";    
 };
-
-
-// Functions that can be used to call core expressions/clauses more cleanly
-function AFLWOGR () {
-
-}
-
-function AClause () {
-
-}
-
-function ALetClause () {
-
-}
-
-function AWhereClause () {
-
-}
-
-function AOrderbyClause () {
-
-}
-
-function AGroupClause () {
-
-}
-
-function ALimitClause () {
-
-}
-
-function ADistinctClause () {
-
-}
-
-function AVariable () {
-
-}