1
votes

I would like to traverse from a vertex, only following edges that match a particular expression (example: friendsWith.type="bestFriend"). How do I do this?

Furthermore, I have to run this operation for a bunch of vertices. Is it possible to keep things organized by the "head" of the traversals? Ideally the return would be:

[[start1, [list of all the vertices during traversal], [start2, [...]],...]

2
Could you explain better what you want to get ? What should be start2 ? Could you provide an example ? - Alessandro Rota
Could you accept a javascript function ? - Alessandro Rota
@AlessandroRota actually a javascript function would be very helpful. - cgreen

2 Answers

1
votes

I tried with this graph

enter image description here

I used this javascript function with the parameter rid

var g=orient.getGraph();
var nodes = [];
var previous=[];
var currently=[];
var b=g.command("sql","select from " + rid);
if(b.length>0){
    var vertex=b[0];
    previous.push(vertex);
    nodes.push(vertex);
    do{
        for(i=0;i<previous.length;i++){
            var vertexOut=previous[i];
            var edges=g.command("sql","select expand(outE('friendsWith')) from "+ vertexOut.getId());
            for(s=0;s<edges.length;s++){ 
                var edge=edges[s];
                var type= edge.getProperty("type");
                if(type=="bestFriend"){
                    var vertices=g.command("sql","select expand(inV('friendsWith')) from "+ edge.getId());
                    var vertex=vertices[0];
                    var found=false;          
                    for(k=0;k<nodes.length;k++){
                        var id=nodes[i].getId();
                        var id_v=vertex.getId()
                        if(id==id_v){
                            found=true;
                            break;
                        }
                    }
                    if(found==false){
                        currently.push(vertex);
                        nodes.push(vertex);
                    }
                }
            }
        }
        change();
    }while(previous.length>0);
    return nodes;
}

function change(){
    previous=[];
    for (indice=0;indice<currently.length;indice++)
        previous.push(currently[indice]);
    currently=[];
}

and with select expand(node) from (select myFunction("#9:0") as node) I got

enter image description here

Hope it helps.

0
votes

try this:

select outV().name as start, inV().name as end from(traverse * from friendsWith while type = "bestFriend")

you can do it even without traverse.

Hope it helps.

Regards.