Merge branch 'eugenia/asterix_sdk_stable' into eugenia/asterix_beta_examples
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 d9c21a0..4eaff8a 100644
--- a/asterix-app/src/main/resources/sdk/static/example/demo.html
+++ b/asterix-app/src/main/resources/sdk/static/example/demo.html
@@ -6,23 +6,23 @@
 
     <style>
         .pretty-printed {
-            background-color: #eeeeee;
-            margin-bottom: 1em;
+    background-color: #eeeeee;
+    margin-bottom: 1em;
         }
 
         .how-to-run {
-            background-color: #c8c8c8;
-            margin-bottom: 1em;
+    background-color: #c8c8c8;
+    margin-bottom: 1em;
         }
 
         .result-output {
-            background-color: #BED8E5;
-            margin-bottom: 1em;
+    background-color: #BED8E5;
+    margin-bottom: 1em;
         }
 
         body {
-            font-family : "Helvetica";
-            margin-bottom: 1em;
+    font-family : "Helvetica";
+    margin-bottom: 1em;
         }
     </style>
 
@@ -36,17 +36,24 @@
     <h2>Query 0-A - Exact-Match Lookup</h2>
     <div class="sample-query">
 
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            where $user.id = 8<br/>
-            return $user;<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    where $user.id = 8
+    return $user;
+        </pre></div>
 
-        <div class="how-to-run">
-            javascript here
-        </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")));
+        </code></pre></div>
 
         <div class="result-output" id="result0a">
         </div>
@@ -57,16 +64,16 @@
     <h2>Query 0-B - Range Scan</h2>
     <div class="sample-query">
         
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            where $user.id >= 2 and $user.id <= 4<br/>
-            return $user;<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    where $user.id >= 2 and $user.id <= 4
+    return $user;
+        </pre></div>
         
-        <div class="how-to-run">
-        </div>
+        <div class="how-to-run"><pre>
+        </pre></div>
 
         <div class="result-output" id="result0b">
         </div>
@@ -77,14 +84,14 @@
 
     <h2>Query 1 - Other Query Filters</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            where $user.user-since >= datetime('2010-07-22T00:00:00')<br/>
-            and $user.user-since <= datetime('2012-07-29T23:59:59')<br/>
-            return $user;<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    where $user.user-since >= datetime('2010-07-22T00:00:00')
+    and $user.user-since <= datetime('2012-07-29T23:59:59')
+    return $user;
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -98,17 +105,17 @@
 
     <h2>Query 2-A - Equijoin</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            for $message in dataset FacebookMessages<br/>
-            where $message.author-id = $user.id<br/> 
-            return {<br/>
-            "uname": $user.name,<br/>
-            "message": $message.message<br/>
-            };<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    for $message in dataset FacebookMessages
+    where $message.author-id = $user.id 
+    return {
+        "uname": $user.name,
+        "message": $message.message
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -122,17 +129,17 @@
 
     <h2>Query 2-B - Index join</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            for $message in dataset FacebookMessages<br/>
-            where $message.author-id /*+ indexnl */  = $user.id<br/>
-            return {<br/>
-            "uname": $user.name,<br/>
-            "message": $message.message<br/>
-            };<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    for $message in dataset FacebookMessages
+    where $message.author-id /*+ indexnl */  = $user.id
+    return {
+        "uname": $user.name,
+        "message": $message.message
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -146,17 +153,17 @@
 
     <h2>Query 3 - Nested Outer Join</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $user in dataset FacebookUsers<br/>
-            return {<br/>
-            "uname": $user.name,<br/>
-            "messages": for $message in dataset FacebookMessages<br/>
-            where $message.author-id = $user.id<br/>
-            return $message.message<br/>
-            };<br/>
-        </div>
+    for $user in dataset FacebookUsers
+    return {
+        "uname": $user.name,
+        "messages": for $message in dataset FacebookMessages
+                    where $message.author-id = $user.id
+                    return $message.message
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -170,17 +177,17 @@
 
     <h2>Query 4 - Theta Join</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $t in dataset TweetMessages<br/>
-            return {<br/>
-            "message": $t.message-text,<br/>
-            "nearby-messages": for $t2 in dataset TweetMessages<br/>
-            where spatial-distance($t.sender-location, $t2.sender-location) <= 1<br/>
-            return { "msgtxt":$t2.message-text}<br/>
-            };<br/>
-        </div>
+    for $t in dataset TweetMessages
+    return {
+        "message": $t.message-text,
+        "nearby-messages": for $t2 in dataset TweetMessages
+                           where spatial-distance($t.sender-location, $t2.sender-location) <= 1
+                           return { "msgtxt":$t2.message-text}
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -194,25 +201,25 @@
 
     <h2>Query 5 - Fuzzy Join</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            set simfunction "edit-distance";<br/>
-            set simthreshold "3";<br/><br/>
+    set simfunction "edit-distance";
+    set simthreshold "3";
 
-            for $fbu in dataset FacebookUsers<br/>
-            return {<br/>
-                "id": $fbu.id,<br/>
-                "name": $fbu.name,<br/>
-                "similar-users": for $t in dataset TweetMessages<br/>
-                        let $tu := $t.user<br/>
-                        where $tu.name ~= $fbu.name<br/>
-                        return {<br/>
-                        "twitter-screenname": $tu.screen-name,<br/>
-                        "twitter-name": $tu.name<br/>
-                        }<br/>
-            };<br/>
-        </div>
+    for $fbu in dataset FacebookUsers
+    return {
+        "id": $fbu.id,
+        "name": $fbu.name,
+        "similar-users": for $t in dataset TweetMessages
+                         let $tu := $t.user
+                         where $tu.name ~= $fbu.name
+                        return {
+                            "twitter-screenname": $tu.screen-name,
+                            "twitter-name": $tu.name
+                        }
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -226,13 +233,13 @@
 
     <h2>Query 6 - Existential Quantification</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $fbu in dataset FacebookUsers<br/>
-            where (some $e in $fbu.employment satisfies is-null($e.end-date))<br/> 
-            return $fbu;<br/>
-        </div>
+    for $fbu in dataset FacebookUsers
+    where (some $e in $fbu.employment satisfies is-null($e.end-date)) 
+    return $fbu;
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -246,13 +253,13 @@
 
     <h2>Query 7 - Universal Quantification</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre><pre>
+    use dataverse TinySocial;
 
-            for $fbu in dataset FacebookUsers<br/>
-            where (every $e in $fbu.employment satisfies not(is-null($e.end-date))) <br/>
-            return $fbu;<br/>
-        </div>
+    for $fbu in dataset FacebookUsers
+    where (every $e in $fbu.employment satisfies not(is-null($e.end-date))) 
+    return $fbu;
+        </pre></div>
         
         <div class="how-to-run">
         </div>
@@ -266,26 +273,26 @@
 
     <h2>Query 8 - Simple Aggregation</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            count(for $fbu in dataset FacebookUsers return $fbu);<br/>
-        </div>
+    count(for $fbu in dataset FacebookUsers return $fbu);
+        </pre></div>
         
-        <div class="how-to-run">
-        var expression8 = new FunctionExpression({<br/>
-            "function"      : "count",<br/>
-            "expression"    : new ForClause(<br/>
-                                "fbu", null, new AsterixExpression().set(["dataset FacebookUsers"])<br/>
-                              ).bind(<br/>
-                                {"return" : new AsterixExpression().set(["$fbu"])}<br/>
-                              ),<br/>
-            "dataverse"     : "TinySocial",<br/>
-            "success"       : function(res) {<br/>
-                                $('#result8').html(res["results"]);<br/>
-                              }<br/>
-        }).run();<br/>
-        </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"]);
+                          }
+    });
+        </code></pre></div>
 
         <div class="result-output" id="result8">
         </div>
@@ -296,21 +303,21 @@
 
     <h2>Query 9-A - Grouping and Aggregation</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $t in dataset TweetMessages<br/>
-            group by $uid := $t.user.screen-name with $t<br/>
-            return {<br/>
-            "user": $uid,<br/>
-            "count": count($t)<br/>
-            };<br/>
-        </div>
+    for $t in dataset TweetMessages
+    group by $uid := $t.user.screen-name with $t
+    return {
+        "user": $uid,
+        "count": count($t)
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
 
-        <div class="result-output" id="result9">
+        <div class="result-output" id="result9a">
         </div>
 
         <button id="run9a">Run #9-A</button>
@@ -319,22 +326,22 @@
 
     <h2>Query 9-B - (Hash-Based) Grouping and Aggregation</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-           use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $t in dataset TweetMessages<br/>
-            /*+ hash*/<br/>
-            group by $uid := $t.user.screen-name with $t<br/>
-            return {<br/>
-            "user": $uid,<br/>
-            "count": count($t)<br/>
-            };<br/>
-        </div>
+    for $t in dataset TweetMessages
+    /*+ hash*/
+    group by $uid := $t.user.screen-name with $t
+    return {
+        "user": $uid,
+        "count": count($t)
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
 
-        <div class="result-output" id="result10">
+        <div class="result-output" id="result9b">
         </div>
 
         <button id="run9b">Run #9-B</button>
@@ -343,24 +350,24 @@
 
     <h2>Query 10 - Grouping and Limits</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            for $t in dataset TweetMessages<br/>
-            group by $uid := $t.user.screen-name with $t<br/>
-            let $c := count($t)<br/>
-            order by $c desc<br/>
-            limit 3<br/>
-            return {<br/>
-                    "user": $uid,<br/>
-                    "count": $c<br/>
-            };<br/>
-        </div>
+    for $t in dataset TweetMessages
+    group by $uid := $t.user.screen-name with $t
+    let $c := count($t)
+    order by $c desc
+    limit 3
+    return {
+        "user": $uid,
+        "count": $c
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </div>
 
-        <div class="result-output" id="result11">
+        <div class="result-output" id="result10">
         </div>
 
         <button id="run10">Run #10</button>
@@ -369,21 +376,21 @@
 
     <h2>Query 11 - Left Outer Fuzzy Join</h2>
     <div class="sample-query">
-        <div class="pretty-printed">
-            use dataverse TinySocial;<br/><br/>
+        <div class="pretty-printed"><pre>
+    use dataverse TinySocial;
 
-            set simfunction "jaccard";<br/>
-            set simthreshold "0.3";<br/><br/>
+    set simfunction "jaccard";
+    set simthreshold "0.3";
 
-            for $t in dataset TweetMessages<br/>
-            return {<br/>                     
-                "tweet": $t,<br/>               
-                "similar-tweets": for $t2 in dataset TweetMessages<br/>
-                        where  $t2.referred-topics ~= $t.referred-topics<br/>
-                        and $t2.tweetid != $t.tweetid<br/>
-                        return $t2.referred-topics<br/>
-            };<br/>
-        </div>
+    for $t in dataset TweetMessages
+    return {             
+        "tweet": $t,       
+        "similar-tweets": for $t2 in dataset TweetMessages
+        where  $t2.referred-topics ~= $t.referred-topics
+        and $t2.tweetid != $t.tweetid
+        return $t2.referred-topics
+    };
+        </pre></div>
         
         <div class="how-to-run">
         </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 ba827b4..0cca153 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
@@ -6,17 +6,173 @@
         var expression0a = new FLWOGRExpression({
             "dataverse" : "TinySocial",
             "success"   : function(res) {
-                            alert(JSON.stringify(res));
                             $('#result0a').html(res["results"]);
                           } 
             })
-            .bind( new ForClause("user", null, new AsterixExpression().set(["dataset FacebookUsers"])) )
+            .bind( new ForClause("user", null, new AExpression().set("dataset FacebookUsers")) )
             .bind( new WhereClause(new BooleanExpression("$user.id = 8")) )
-            .bind({ "return" : new AsterixExpression().set(["$user"]) });
-        alert(expression0a.val());
+            .bind({ "return" : new AExpression().set("$user") });
+        
         expression0a.run();
     });
 
+    // 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();
+
+    });
+
+    // 1 - Other Query Filters
+    $("#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());
+    });
+
+    // 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());
+    });
+
+    // 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());
+    });
+
+    // 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());
+    });
+    
+    // 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());
+    });
+
+    // 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());
+    });
+
+    // 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());
+    });
+
+    // 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"]);
+                          }
+        })
+        .bind( new ForClause (
+            "fbu", 
+            null, 
+            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))")
+            )
+        ))
+        .bind(new ReturnClause( new AQLClause().set("$fbu") ));
+        alert("EXPRESSION 7 " + expression7.val());
+    });
+
     // 8 - Simple Aggregation
     $('#run8').click(function () {
 
@@ -24,17 +180,142 @@
         $('#result8').html('');   
         var expression8 = new FunctionExpression({
             "function"      : "count",
-            "expression"    : new ForClause(
-                                "fbu", null, new AsterixExpression().set(["dataset FacebookUsers"])
-                              ).bind(
-                                {"return" : new AsterixExpression().set(["$fbu"])}
-                              ),
+            "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"]);
                               }
         });
-        expression8.run();
+        alert(expression8.val());
+        // expression8.run();
+    });
+
+    // 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")
+                            }
+                )
+            }
+        ));
+
+        expression9a.run();
+    });
+
+    // 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")
+                            }
+                )
+            }
+        ));
+        
+        expression9b.run();
     });
     
+    // 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")))
+        .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")
+                }
+            )
+        ))
+        .bind( new OrderbyClause( new AExpression().set("$c"), "desc" ) )
+        .bind( new LimitClause(new AExpression().set("3")) )
+        .bind( new ReturnClause(
+            {
+                "user" : "$uid",
+                "count" : "$c"
+            }
+        ));
+
+        expression10.run();
+    });
+
+    // 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({
+            "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")))
+        })); 
+        
+        expression11.run();
+    });
+
+    //$('#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 c5ef91c..7989a9e 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,142 +1,3 @@
-function AsterixSDK() {
-
-    // Asterix SDK => send
-    // Posts a message containing an API endpoint, json data,
-    // and a UI callback function.
-    //
-    // @param handler [Asterix REST Controller], a handler object
-    // that provides REST request information. 
-    //
-    // Anticipated Usage:
-    //
-    // var a = AsterixSDK();
-    // var e = Expression;
-    // var h = AsterixRestController.bind(e);
-    // a.send(h);
-    myThis = this;
-    this.callbacks = {
-        "sync" : function() { alert("default sync"); },
-        "async" : function() {}
-    };
-    this.send = function(handler, cb) {
-        myThis.callbacks = cb;
-        this.handler = handler;
-        this.extras = handler["extras"];
-        this.xhr.post(
-            handler["endpoint"],
-            handler["apiData"],
-            this.branch          
-        );
-    };
-
-    this.branch = function(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 = this.callbacks["async"];
-            fn_callback(response, extra);
-            
-        } else if (response["status"]) {
-                
-            var fn_callback = this.callbacks["sync"];
-            fn_callback(response, extra);
-        }
-    }
-
-    // Asterix SDK => bindingHandler
-    // AsterixExpression form handler where a new REST API point is bound. Takes as input any
-    // AsterixExpression, each of which is bindable.
-    this.bindingHandler = new AsterixExpression();
-    this.bind = this.bindingHandler.bind;
-}
-
-function AsterixExpression() {
-    this.init();
-    return this;
-}
-
-AsterixExpression.prototype.init = function () {
-    this.dataverse = ""; // TODO This shouldn't make it to send
-    this.boundTo = {};
-    this.clauses = [];
-    this.ui_callback_on_success = function() {};
-    this.ui_callback_on_success_async = function() {};
-};
-
-AsterixExpression.prototype.bind = function(expression) {
-    // If expression is an AsterixExpression, it becomes base
-    if (expression instanceof AsterixExpression) {
-        this.boundTo = expression;
-    } else if (expression instanceof AsterixClause) {
-        this.clauses.push(expression.val());
-    }
-    return this;
-};
-
-AsterixExpression.prototype.send = function(arc) {
-    // Hackiest of hacks
-    var g = new AsterixSDK();
-    g.send(arc, arc["callback"]);
-};
-
-AsterixExpression.prototype.clear = function() {
-    this.clauses.length = 0;
-    return this;
-};
-
-AsterixExpression.prototype.val = function() {
-    return this.clauses.join("\n"); 
-};
-
-AsterixExpression.prototype.success = function(fn, isSync) {
-    if (isSync) {
-        this.ui_callback_on_success = fn;
-    } else { 
-        this.ui_callback_on_success_async = fn;
-    }
-    return this;
-};
-
-AsterixExpression.prototype.set = function(statements_arr) {
-    for (var i = 0; i < statements_arr.length; i++) {
-        this.clauses.push(statements_arr[i]);
-    }
-    return this;
-};
-
-AsterixExpression.prototype.use_dataverse = function(dv) {
-    this.dataverse = dv;
-    this.clauses.push("use dataverse " + dv + ";");
-    return this; 
-};
-
-AsterixExpression.prototype.return = function(return_object) {
-    var components = [];
-    for (var key in return_object) {
-        components.push('"' + key + '" : ' + return_object[key]);
-    }
-    
-    var return_expression = 'return { ' + components.join(', ') + ' }'; 
-    this.clauses.push(return_expression);
-    return this;
-};
-
-
-
-
-
-
-
-
-
-
 // Temporary AsterixExpression Placeholder
 function AExpression () {
     this._properties = {};
@@ -182,23 +43,24 @@
 
 AExpression.prototype.val = function() { 
 
+    var value = "";
+
     // If there is a dataverse defined, provide it.
     if (this._properties.hasOwnProperty("dataverse")) {
-        return "use dataverse " + this._properties["dataverse"] + ";\n";
-    } else {
-        return this.error("Missing dataverse.");
+        value += "use dataverse " + this._properties["dataverse"] + ";\n";
+    };
+
+    if (this._properties.hasOwnProperty("value")) {
+        value += this._properties["value"];
     }
+
+    return value;
 };
 
-
-AExpression.prototype.onReturn = function() {
-    var ret = "";    
-
-    if (this._properties.hasOwnProperty("return")) {
-        ret += this._properties["return"] + ";";
-    }
-
-    return ret;
+// @param expressionValue [String]
+AExpression.prototype.set = function(expressionValue) {
+    this._properties["value"] = expressionValue; 
+    return this;
 };
 
 
@@ -248,10 +110,7 @@
 };
 
 FunctionExpression.prototype.val = function () { 
-
-    var value = AExpression.prototype.val.call(this);
-
-    return value + this._properties["function"] + "(" + this._properties["expression"].val() + ");" + AExpression.prototype.onReturn.call(this); 
+    return this._properties["function"] + "(" + this._properties["expression"].val() + ")";
 };
 
 
@@ -260,6 +119,7 @@
 // 
 // 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 )*
@@ -273,6 +133,7 @@
     AExpression.call(this);
 
     this._properties["clauses"] = [];
+    this._properties["minSize"] = 0;
 
     // Bind options and return
     this.bind(options);
@@ -289,7 +150,12 @@
 
     var options = options || {};
 
-    if (this._properties["clauses"].length == 0) {
+    if (options instanceof SetStatement) {
+         this._properties["clauses"].push(options);
+         this._properties["minSize"] += 1;
+    }
+
+    if (this._properties["clauses"].length <= this._properties["minSize"]) {
         // Needs to start with for or let clause
         if (options instanceof ForClause || options instanceof LetClause) {
             this._properties["clauses"].push(options);
@@ -307,13 +173,15 @@
 FLWOGRExpression.prototype.val = function() {
     var value = AExpression.prototype.val.call(this);
 
+    var clauseValues = [];
     for (var c in this._properties["clauses"]) {
-        value += this._properties["clauses"][c].val() + " ";
+        clauseValues.push(this._properties["clauses"][c].val());
     }
 
-    return value + AExpression.prototype.onReturn.call(this);
+    return value + clauseValues.join("\n");// + ";";
 };
 
+
 // AQLClause
 //
 // Base Clause  ::= ForClause | LetClause | WhereClause | OrderbyClause | GroupClause | LimitClause | DistinctClause
@@ -342,6 +210,11 @@
     return this;
 };
 
+AQLClause.prototype.set = function(value) {
+    this._properties["clause"] = value;
+    return this;
+};
+
 
 // ForClause
 //
@@ -394,14 +267,52 @@
 LetClause.prototype.constructor = LetClause;
 
 
+// ReturnClause
+//
+// Grammar:
+// return [AQLExpression]
+function ReturnClause(expression) {
+    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 ) {
+        
+        this._properties["clause"] += "{";
+        var returnStatements = [];
+        for (returnValue in expression) {
+           
+            if (expression[returnValue] instanceof AExpression) { 
+                returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue].val());            
+            } else if (typeof expression[returnValue] == "string") {          
+                returnStatements.push('"' + returnValue + '" ' + " : " + expression[returnValue]);   
+            }
+        }
+        this._properties["clause"] += returnStatements.join(",\n");
+        this._properties["clause"] += "}";  
+    
+    } else {
+        this._properties["clause"] += new AExpression().set(expression).val();
+    }
+
+    return this;
+}
+
+ReturnClause.prototype = Object.create(AQLClause.prototype);
+ReturnClause.prototype.constructor = ReturnClause;
+
+ReturnClause.prototype.val = function () {
+    return this._properties["clause"];  
+};
+
+
 // WhereClause
 //
 // Grammar: 
 // ::= "where" Expression
 // 
 // @param expression [BooleanExpression], pushes this expression onto the stack
-//
-// TODO Error fixing
 function WhereClause(expression) {
     AQLClause.call(this);
 
@@ -418,7 +329,7 @@
 
 
 WhereClause.prototype.bind = function(expression) {
-    if (expression instanceof BooleanExpression) {
+    if (expression instanceof AExpression) {
         this._properties["stack"].push(expression);
     }
 };
@@ -434,16 +345,246 @@
     }
     
     return value;
+};
+
+
+// LimitClause
+// Grammar:
+// LimitClause    ::= "limit" Expression ( "offset" Expression )?
+// 
+// @param   limitExpression [REQUIRED, AQLExpression]
+// @param   offsetExpression [OPTIONAL, AQLExpression]
+function LimitClause(limitExpression, offsetExpression) {
+
+    AQLClause.call(this);
+  
+    // limitExpression required
+    this._properties["clause"] = "limit " + limitExpression.val();
+
+    // Optional: Offset
+    var offset = typeof offsetExpression ? offsetExpression : null;
+    if (offset != null) {
+        this._properties["clause"] += " offset " + offsetExpression.val();
+    }
+
+    return this;
 }
 
+LimitClause.prototype = Object.create(AQLClause.prototype);
+LimitClause.prototype.constructor = LimitClause;
+
+
+// OrderbyClause
+//
+// Grammar:
+// OrderbyClause  ::= "order" "by" Expression ( ( "asc" ) | ( "desc" ) )? ( "," Expression ( ( "asc" ) | ( "desc" ) )? )*
+//
+// @params AQLExpressions and asc/desc strings, in any quantity. At least one required. 
+function OrderbyClause() {
+    
+    AQLClause.call(this);
+
+    // 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;
+        return this;    
+    } 
+
+    var expc = 0;
+    var expressions = [];    
+
+    while (expc < arguments.length) {
+      
+        var expression = "";
+
+        if (arguments[expc] instanceof AExpression) {
+            expression += arguments[expc].val();
+        }
+
+        var next = expc + 1;
+        if (next < arguments.length && (arguments[next] == "asc" || arguments[next] == "desc")) {
+            expc++;
+            expression += " " + arguments[expc];
+        }
+        
+        expressions.push(expression);
+      
+        expc++;
+    }
+
+    this._properties["clause"] = "order by " + expressions.join(", ");
+    return this;
+}
+
+OrderbyClause.prototype = Object.create(AQLClause.prototype);
+OrderbyClause.prototype.constructor = OrderbyClause;
+
+
+// GroupClause
+//
+// Grammar:
+// GroupClause    ::= "group" "by" ( Variable ":=" )? Expression ( "," ( Variable ":=" )? Expression )* ( "decor" Variable ":=" Expression ( "," "decor" Variable ":=" Expression )* )? "with" VariableRef ( "," VariableRef )*
+function GroupClause() {
+    AQLClause.call(this);
+
+    if (arguments.length == 0) {
+        // TODO Not sure which error to throw for an empty GroupBy but this should fail.
+        alert("Group Error");
+        this._properties["clause"] = null;
+        return this;    
+    } 
+
+    var expc = 0;
+    var expressions = [];
+    var variableRefs = [];
+    var isDecor = false;
+
+    while (expc < arguments.length) {
+
+        if (arguments[expc] instanceof AExpression) {
+
+            isDecor = false;
+            expressions.push(arguments[expc].val());
+
+        } else if (typeof arguments[expc] == "string") {       
+            
+            // Special keywords, decor & with
+            if (arguments[expc] == "decor") {
+                isDecor = true;
+            } else if (arguments[expc] == "with") {
+                isDecor = false;
+                expc++;
+                while (expc < arguments.length) {
+                    variableRefs.push("$" + arguments[expc]);
+                    expc++;
+                }
+            
+            // Variables and variable refs
+            } else {
+                
+                var nextc = expc + 1;
+                var expression = "";
+            
+                if (isDecor) {
+                    expression += "decor "; 
+                    isDecor = false;
+                }
+
+                expression += "$" + arguments[expc] + " := " + arguments[nextc].val();
+                expressions.push(expression);
+                expc++;
+            }
+        }
+
+        expc++;
+    }
+
+    this._properties["clause"] = "group by " + expressions.join(", ") + " with " + variableRefs.join(", ");
+    return this;
+}
+
+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
+//
+// Grammar
+// "set" Identifier StringLiteral
+function SetStatement (identifier, stringLiteral) {
+    AExpression.call(this);
+
+    var statement = "set " + identifier + ' "' + stringLiteral + '";';
+
+    AExpression.prototype.set.call(this, statement);
+
+    return this;
+}
+
+SetStatement.prototype = Object.create(AExpression.prototype);
+SetStatement.prototype.constructor = SetStatement;
+
+
+// Quantified Expression
+// 
+// Grammar
+// QuantifiedExpression ::= ( ( "some" ) | ( "every" ) ) Variable "in" Expression ( "," Variable "in" Expression )* "satisfies" Expression
+// 
+// @param String some/every
+// @param [AExpression]
+// @param [Aexpression] satisfiesExpression
+function QuantifiedExpression (keyword, expressions, satisfiesExpression) {
+    AExpression.call(this);
+
+    var expression = keyword + " ";
+    var varsInExpressions = [];
+
+    for (var varInExpression in expressions) {
+        varsInExpressions.push(varInExpression + " in " + expressions[varInExpression].val()); 
+    } 
+    expression += varsInExpressions.join(", ") + " satisfies " + satisfiesExpression.val();
+    
+    AExpression.prototype.set.call(this, expression);
+
+    return this;
+}
+
+QuantifiedExpression.prototype = Object.create(AExpression.prototype);
+QuantifiedExpression.prototype.constructor = QuantifiedExpression;
+
+QuantifiedExpression.prototype.val = function() {
+    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 () {
+
+}