changeset 34:4bbb832c53ac

Completed: Base functionality for simple examples. Minus button now removes all filters for simplicity. Still need to adjust how to go about restarting the querying process once a result has been selected from the display list (eg. the 'selected object is:' case). Also need to strip out a bunch of unneeded code.
author arussell
date Sat, 19 Dec 2015 23:25:23 -0500
parents 758a5313baf4
children 9f0d4fd3a412
files query_builder/querybuild.html
diffstat 1 files changed, 141 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/query_builder/querybuild.html	Sat Dec 19 09:17:45 2015 -0500
+++ b/query_builder/querybuild.html	Sat Dec 19 23:25:23 2015 -0500
@@ -55,8 +55,9 @@
                                     </select>
                                 </div>
                                 <div class="col-sm-4 col-md-4" id="select-col2">
-                                    <select class="select-object1 form-control">
-                                        <option selected>TEXT</option>
+                                    <select class="select-object1 form-control" id="startselect">
+                                        <option disable selected></option>
+                                        <option>TEXT</option>
                                         <option>WITNESS</option>
                                         <option>PERSON</option>
                                         <option>CODEX</option>
@@ -109,9 +110,10 @@
 
     // Defining initial variables and helper functions
     var sourceType = "TEXT";
+    var currentQueryData;
     var targets = [];
     var targetTypes = [];
-    var targetObj = []; // The current objects on the display box
+    var targetObj = [{}]; // The current objects on the display box
     var numFilters = 1;
     var filters = [
         { id: 0, text: 'has relation' },
@@ -183,31 +185,61 @@
             minimumResultsForSearch: Infinity
         });
     });
+    // TODO: update minus button to alter the currRel currAttr arrays
     $("#minus_filter_button").click(function() {
         console.log("minus filter");
-        $("#row"+numFilters).remove();
-        numFilters--;
+        //$("#row"+numFilters).remove();
+        //numFilters--;
+
+        // NOTE: Temporary solution for removing filters,
+        while (numFilters > 0) {
+            $("#row"+numFilters).remove();
+            numFilters--;
+        }
+        numFilters++;
+        $("#filters").append(filter_html(numFilters));
+        $(".filter-box"+numFilters).select2({
+            data: filters,
+            minimumResultsForSearch: Infinity
+        });
+        currAttrs = [];
+        currRels = [];
+        currSource = sourceType;
+
+
     });
     $("#results-container").dblclick(function() {
         var displayChoice = $("#results-container").find("option:selected").text();
-        console.log(displayChoice + ".........");
-        var selection;
-
-        _.map(targetObj, function(obj){
+        console.log(targetObj);
+        if (!targetObj[displayChoice]) {
+            // increase tolerance
+            console.log("increase listTolerance");
+            listTolerance = 100000;
+            process_display_results(currentQueryData);
+            listTolerance = 100;
+        }
+        else {
+            console.log(displayChoice + ".........");
+            var displayType = targetObj[displayChoice];
+            /*
+             _.map(targetObj, function(obj){
 
-            if (_.values(_.values(targetObj))[1] === displayChoice) {
-                selection = (_.values(_.values(obj))[0])[1];
-            }
-            else {
-                selection = (_.values(_.values(obj))[0])[0];
-            }
-        });
-        numFilters++;
-        $("#filters").append(select_html(selection, numFilters));
-        // Generate inner relation list
-        //genRelations(sourceType, ".constraint-box"+numFilters);
-        sourceType = selection;
-        console.log("add filter");
+             if (_.values(_.values(targetObj))[1] === displayChoice) {
+             selection = (_.values(_.values(obj))[0])[1];
+             }
+             else {
+             selection = (_.values(_.values(obj))[0])[0];
+             }
+             });
+             */
+            numFilters++;
+            $("#filters").append(select_html(displayChoice, numFilters));
+            // Generate inner relation list
+            //genRelations(sourceType, ".constraint-box"+numFilters);
+            sourceType = displayChoice;
+            console.log("New Target");
+            genResults("New Target", [displayType,displayChoice], "target")
+        }
     });
     $(function() {
         $("input").submit(function(event, data) {
@@ -300,28 +332,47 @@
         currentQueryType = "relation";
         ajax1(query, constraintBox);
     }
+
     function genAttributes(sourceNodeType, constraintBox) {
         console.log("GENERATE ALL ATTRIBUTES");
         var query = "match (n:"+sourceNodeType+") with keys(n) as collection unwind collection as attributes return distinct attributes";
         ajax1(query, constraintBox);
     }
+
     function genResults(sourceNodeType, selected, constraintBox) {
         // TODO: Return first the list of current values of the display box
 
         if (selected === "New Source") {
             console.log("NEW SOURCE - UPDATE RESULTS");
             var query = "match (source:"+sourceNodeType+") return source._n_label, source.type";
+            set_current_query('source', sourceNodeType+")");
+        }
+        else if (sourceNodeType === "New Target") {
+            newTarget = 1;
+            set_current_query('target', selected);
+            var query = get_current_query;
         }
         else if (currentQueryType === "attribute") {
             console.log("ATTR CHANGE - UPDATE RESULTS");
             console.log("before update currentQueryAttr is " + currentQueryAttr);
             var qAttr = currentQueryAttr;
-            var query = "match (source:"+sourceNodeType+") where "+get_result_labels()+" AND source."+qAttr+"=~\"(?i).*"+selected+".*\" return distinct source._n_label, source.type limit 5";
+            set_current_query('attribute', "."+qAttr+"=~\"(?i).*"+selected+".*\"");
+            // this might cause an issue
+            var query = get_current_query();
+        }
+            /*
+            var query = "match (source:"+sourceNodeType+") where ";
+            if (get_result_labels()) query += get_result_labels() + " AND ";
+            query += "source."+qAttr+"=~\"(?i).*"+selected+".*\" return distinct source._n_label, source.type limit 5";
+            // TODO: case where all attributes are searched
+            // if (selected === "ALL") set_current_query('attribute', "."+qAttr+"=~\"(?i).*"+selected+".*\"");
+
             console.log(query);
-        }
+            */
         else if (currentQueryType === "relation") {
             var query = "match (source:" + sourceNodeType + ")-[rel:" + selected + "]->(target) return distinct target._n_label, target.type";
             console.log("REL CHANGE - UPDATE RESULTS");
+            set_current_query('relation', selected+"]->(");
         }
         else {
             console.log("GENRESULTS IMPROPERLY CALLED");
@@ -346,6 +397,65 @@
     // to use contains
     // TODO: start n = node(*) where n.Name =~ '.*SUBSTRING.*' return n.Name, n;
 
+    var currSource = [sourceType];
+    var currRels = [];
+    var currAttrs = [];
+    var currQ = '';
+
+    function set_current_query(type, q) {
+        if (type === 'source') {
+            currSource[0] = ("match (source:"+q);
+        }
+        else if (type === 'target') {
+            currSource.push("match (target:"+q[0]+")");
+            currRels = [];
+            currAttrs = [];
+            currAttrs.push("target._n_label=\""+q[1]+"\"");
+        }
+        else if (type === 'relation') {
+            currRels.push("-[rel"+currRels.length+":"+q+"target"+currRels.length+")");
+        }
+        else if (type === 'attribute') {
+            if (Boolean(currRels.length)) currAttrs.push("target"+(currRels.length-1)+q);
+            else currAttrs.push("source"+q);
+        }
+        else if (type === 'return') {
+            return currQ
+        }
+        else {
+            console.log("improper call to change current query");
+        }
+    }
+    var newTarget = -1;
+    function get_current_query() {
+        var currQ = currSource[currSource.length-1];
+        var i;
+        var n=currRels.length;
+        var m=currAttrs.length;
+        for (i=0; i<n; i++) {
+            currQ += currRels[i];
+        }
+        currQ += " where ";
+        for (i=0; i<m; i++) {
+            if (Boolean(i)) {
+                currQ += "AND ";
+            }
+            currQ += currAttrs[i]+" ";
+        }
+        if (Boolean(currRels.length)) {
+            currQ += "return distinct target"+(currRels.length-1)+"._n_label, target"+(currRels.length-1)+".type";
+        }
+        else if (newTarget > 0) {
+            currQ += "return distinct target._n_label, target.type";
+            newTarget = -1;
+        }
+        else currQ += "return distinct source._n_label, source.type";
+        console.log(currQ);
+        return currQ;
+    }
+
+
+
 
 
     // Ajax request function
@@ -399,6 +509,7 @@
     function dataGen(dataArr, resBox) {
         var d = dataArr;
         targets = [];
+        targetObj = [];
 
         // Consider implementing localStorage to avoid reloading every time
         var queryData = [];
@@ -406,7 +517,9 @@
             var j = d[i];
             queryData.push({ id: j, text: j[0] });
             targets.push(j[1]);
+            targetObj[j[0]]=j[1];
         }
+        console.log(queryData);
         // Now that queryData array has been defined we will sort it by its label value and initialize the option.
         queryData.sort(function(a,b){
             var c = a.text.replace(/<|>/g, '');
@@ -418,6 +531,9 @@
         if (resBox === "results-container") {
             process_display_results(queryData);
         }
+        if (resBox === "target") {
+            sourceType = currSource[0];
+        }
         // Or fill any select boxes
         else {
             $(resBox).select2({
@@ -426,10 +542,10 @@
         }
     }
     function process_display_results(queryData) {
+        currentQueryData = queryData;
         var weights = [];
         var resultLength = queryData.length;
         targetTypes = [];
-        targetObj = queryData;
         set_result_labels(queryData);
         $('#results-container')
                 .find('option')