Skip to content

Instantly share code, notes, and snippets.

@rameshvarun
Last active December 19, 2015 17:09
Show Gist options
  • Save rameshvarun/5989339 to your computer and use it in GitHub Desktop.
Save rameshvarun/5989339 to your computer and use it in GitHub Desktop.
var db = require('./../db')
function getPaths(startnode, endnode,
numpaths, maxdepth,
graphs,
allowcycles,
onresult, onfinish)
{
var results = [];
var depth = 1;
function buildQuery()
{
if(results.length < numpaths && depth <= maxdepth)
{
//START portion
var query = "START a = node( {a} ), b = node( {b} )";
//MATCH portion
query += " MATCH p=(a)-->";
for( var i = 1; i < depth; ++i )
{
query += "()-->";
}
query += "(b)";
//WHERE Clause
var where = [];
//If cycles should be limited, filter paths
if(allowcycles == false){
where.push(" ALL(n in nodes(p) where 1=length(filter(m in nodes(p) : m=n)))");
}
if(graphs.length > 0){
var comparison = ""
for(var i = 0; i < graphs.length; ++i){
if(i != 0) { comparison += " OR "}
comparison += "n.graphid! = " + graphs[i];
}
where.push( "ALL(n in nodes(p) where " + comparison + ")" )
}
if( where.length > 0 )
{
query += " WHERE"
query += where.join(" AND ")
}
//RETURN portion
query += " RETURN p"
//Limit
query += " LIMIT " + (numpaths - results.length)
//Params
params = {}
params.a = startnode
params.b = endnode
console.log(query);
db.graphdb.query( query, params, function(err, result){
depth += 1;
results = results.concat(result);
if(onresult != null && result != null && result.length > 0)
{
result.forEach( function(item, i, array)
{
onresult(item);
});
}
buildQuery();
});
}
else
{
if(onfinish != null)
{
onfinish(results);
}
}
}
buildQuery();
}
exports.getPaths = getPaths;
/* Example
getPaths(13077, //Starting node id
35038, //Ending node id
1000, //Amount of pathways to return
10, //Maximum depth to explore
[1], //Array of ids for each graph that can be traversed
false, //Don't allow cycles
function(result)
{
//Print pathways as they are streamed in
//console.log(result);
},
function(results)
{
//Print total number of pathways returned
console.log(results.length + " pathways returned.");
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment