Generate fullquery represenation
authorJakub Skoczen <jakub@indexdata.dk>
Wed, 17 Oct 2012 13:30:39 +0000 (15:30 +0200)
committerJakub Skoczen <jakub@indexdata.dk>
Wed, 17 Oct 2012 13:30:39 +0000 (15:30 +0200)
FQ representation is lossy and used only for rendering a subset of
CQL

cql.js
index.html

diff --git a/cql.js b/cql.js
index 3e4f6d1..ebd878e 100644 (file)
--- a/cql.js
+++ b/cql.js
@@ -23,8 +23,17 @@ CQLModifier.prototype = {
                 + "<value>" + this.value +"</value>\n";
         s = s + indent(n+1) + "</modifier>\n";
         return s;
                 + "<value>" + this.value +"</value>\n";
         s = s + indent(n+1) + "</modifier>\n";
         return s;
+    },
+
+    toFQ: function () {
+        //we ignore modifier relation symbol, for value-less modifiers
+        //we assume 'true'
+        var value = this.value.length > 0 ? this.value : "true";
+        var s = '"'+this.name+'": "'+value+'"';
+        return s;
     }
 }
     }
 }
+
 // CQLSearchClause
 var CQLSearchClause = function (field, fielduri, relation, relationuri, 
                                 modifiers, term) {
 // CQLSearchClause
 var CQLSearchClause = function (field, fielduri, relation, relationuri, 
                                 modifiers, term) {
@@ -65,7 +74,38 @@ CQLSearchClause.prototype = {
         s = s + indent(n+1) + "<term>" + this.term + "</term>\n";
         s = s + indent(n) + "</searchClause>\n";
         return s;
         s = s + indent(n+1) + "<term>" + this.term + "</term>\n";
         s = s + indent(n) + "</searchClause>\n";
         return s;
+    },
+
+    toFQ: function () {
+        var s = '{ "term": "'+this.term+'"';
+        if (this.field.length > 0 && this.field != 'cql.serverChoice')
+          s+= ', "field": "'+this.field+'"';
+        if (this.relation.length > 0 && this.relation != 'scr')
+          s+= ', "relation": "'+this._mapRelation(this.relation)+'"';
+        for (var i = 0; i < this.modifiers.length; i++) {
+          //since modifiers are mapped to keys, ignore the reserved ones
+          if (this.modifiers[i].name == "term"
+            ||this.modifiers[i].name == "field"
+            ||this.modifiers[i].name == "relation")
+            continue;
+          s += ', ' + this.modifiers[i].toFQ();
+        }
+        s += ' }';
+        return s;
+    },
+
+    _mapRelation: function (rel) {
+      switch(rel) {
+        case "<" : return "lt";
+        case ">" : return "gt";
+        case "=" : return "eq";
+        case "<>" : return "ne";
+        case ">=" : return "ge";
+        case "<=" : return "le";
+        default: return rel;
+      }
     }
     }
+
 }
 // CQLBoolean
 var CQLBoolean = function() {
 }
 // CQLBoolean
 var CQLBoolean = function() {
@@ -94,7 +134,19 @@ CQLBoolean.prototype = {
             this.right.toXCQL(n+2) + indent(n+1) + "</rightOperand>\n";
         s = s + indent(n) + "</triple>\n";
         return s;
             this.right.toXCQL(n+2) + indent(n+1) + "</rightOperand>\n";
         s = s + indent(n) + "</triple>\n";
         return s;
+    },
+
+    toFQ: function () {
+      var s = ' { "op": "'+this.op+'"';
+      //proximity modifiers
+      for (var i = 0; i < this.modifiers.length; i++)
+        s += ', ' + this.modifiers[i].toFQ();
+      s += ', "s1": '+this.left.toFQ();
+      s += ', "s2": '+this.right.toFQ();
+      s += ' }'
+      return s;
     }
     }
+
 }
 // CQLParser
 var CQLParser = function () {
 }
 // CQLParser
 var CQLParser = function () {
@@ -124,6 +176,9 @@ CQLParser.prototype = {
     toXCQL: function () {
         return this.tree.toXCQL();
     },
     toXCQL: function () {
         return this.tree.toXCQL();
     },
+    toFQ: function () {
+        return this.tree.toFQ();
+    },
     _parseQuery: function(field, relation, modifiers) {
         var left = this._parseSearchClause(field, relation, modifiers);
         while (this.look == "s" && (
     _parseQuery: function(field, relation, modifiers) {
         var left = this._parseSearchClause(field, relation, modifiers);
         while (this.look == "s" && (
index 0e2e883..1024e5b 100644 (file)
@@ -8,8 +8,11 @@
             try {
                 cp.parse(query);
                 document.getElementById("output").value = cp.toXCQL();
             try {
                 cp.parse(query);
                 document.getElementById("output").value = cp.toXCQL();
+                document.getElementById("output2").value = cp.toFQ();
             } catch (e) {
                 document.getElementById("output").value = e.message;
             } catch (e) {
                 document.getElementById("output").value = e.message;
+                document.getElementById("output2").value = e.message;
+                throw e;
             }
         }
     </script>
             }
         }
     </script>
@@ -26,6 +29,7 @@
         <br>
       </form>
       <textarea id="output" rows="80" cols="80"></textarea>
         <br>
       </form>
       <textarea id="output" rows="80" cols="80"></textarea>
+      <textarea id="output2" rows="80" cols="80"></textarea>
     </div>
   </body>
 </html>
     </div>
   </body>
 </html>