Search code examples
graphorientdbdijkstra

OrientDB: How to query a graph using dijkstra function while ignoring some edges


I am trying to query data from orientdb while ignoring some edges.

My query has the form:

select expand(dijkstra(#12:15,#12:20,'property','both'))

but as mentioned I want to ignore some edges of the graph.

Are there any suggestions?

Edit


Here is my graph structure . Station as Vertex Image Click

Path as Edge Image Click

Thank you @Ivan Mainetti so much for answer i have try the testing main()

Here is my main()

 String nomeDb = "Demo2";
    try {
        System.out.println("Before connect OServerAdmin");
        OServerAdmin serverAdmin = new OServerAdmin("remote:128.199.xxx.xxx/"+nomeDb).connect("admin","password");
        System.out.println("After connect");
        if(serverAdmin.existsDatabase()){  // il db esiste
            System.out.println("in if");
            //connessione a db
            OrientGraph g = new OrientGraph("remote:128.199.xxx.xxx/"+nomeDb);
            DijkstraExcl d = new DijkstraExcl(g, "Path", "distance");
            Set<String> ex =new HashSet<String>();

            //------------------------------------------------
            Vertex start = g.getVertex("#12:6");
            Vertex end = g.getVertex("#12:11");
            ex.add("#13:0");
            Direction direction = Direction.OUT;
            System.out.println(d.getPath(start,end,direction,ex));
            System.out.println(d.getPathString(start,end,direction,ex));
            System.out.println(d.getWeight(start,end,direction,ex));
            //------------------------------------------------
            //chiude db
            g.shutdown();
        }
        else{
            System.out.println("Il database '"+ nomeDb + "' non esiste");
        }
        serverAdmin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

and the result after run the main() is


null

null

2147483647


The correct answer after ignore [#13:0] should be

[#12:6,#12:8,#12:10,#12:11]


Solution

  • Try the following JS function that has as parameters ridFrom, ridTo, property, direction and excludeEdges. With Studio you can try it with this command:

    select expand(result) from (select myFunction("12:6","12:11","distance","out","[#13:0]") as result)
    

    The edges "edge1" and "edge2" are ignored.

    var g=orient.getGraph();
    var listEdges=excludeEdges.substring(1,excludeEdges.length-1).split(",");
    var S=[], T=[] , id_weigth=[] , from , to , infinity = Number.MAX_VALUE;
    step1();
    step2();
    return getPath();
    
    // Initialization
    function step1() {
        var selectFrom=g.command("sql","select from V where @rid ="+ ridFrom);
        var selectTo=g.command("sql","select from V where @rid ="+ ridTo);
        if(selectFrom.length>0 && selectTo.length>0){
            from=selectFrom[0]; 
            to=selectTo[0];     
            S.push(from);       
            var selectAll=g.command("sql","select from V"); 
            for (i=0;i<selectAll.length;i++) {
                if (selectAll[i].getId()!=from.getId())
                    T.push(selectAll[i]);
            }
            var index=1;
            for (i=0;i<selectAll.length;i++) {
                var id = selectAll[i].getId();
                if (selectAll[i].getId()!= from.getId()) {
                    id_weigth[index] = {id:id,weigth:infinity};
                    index++;
                } 
                else  
                    id_weigth[0] = {id:id,weigth:0};
            }
            setWeigth_Direction(from);
        }
    }
    
    // Assignment permanent label
    function step2(){
        var stop = true;
        do {
            stop = true;
            for (i=0;i<T.length;i++) {
                var id = T[i].getId();
                for (j=0;j<id_weigth.length;j++) {
                    if (id_weigth[j].id==id) {
                      if (id_weigth[j].weigth != infinity){
                            stop = false;
                      }
                    }
                }
            }
            if (stop == true)
                break;
            else { 
                var index2 = 0; minWeigth = 0; j = null;
                for (i=0;i<T.length;i++) {
                    var id = T[i].getId();
                    for (m=0;m<id_weigth.length;m++) {
                        if (id_weigth[m].id==id) {
                            if (index2 == 0) {
                                minWeigth = id_weigth[m].weigth;
                                index2++;
                                j = T[i];
                            }    
                            else if (id_weigth[m].weigth < minWeigth) {
                                minWeigth = id_weigth[m].weigth;
                                j = T[i];
                            }
                        }
                    }
                }
                T.splice(getPositionInT(j.getId()),1);
                S.push(j);
                if (T.length == 0)
                    break;
                else
                    step3(j);
            }
        } while (stop == false);
    }
    
    // Assignment temporary label
    function step3(j) {
        setWeigth_Direction(j);
    }
    
    function setWeigth(vertex,direction1,direction2) {
        var edges=g.command("sql","select expand(" + direction1+"E()) from "+ vertex.getId());
        for(m=0;m<edges.length;m++){
            var myEdge=edges[m];;
            var idEdge = myEdge.getId().toString();
            var validEdge=true;
            for (s=0;s<listEdges.length;s++) {
                if(listEdges[s]==idEdge)
                    validEdge=false;
            }
            if(validEdge==true){
                var myWeigth = myEdge.getProperty(property);
                var myVertex=g.command("sql","select expand("+ direction2 + ") from " +myEdge.getId());
                var id = myVertex[0].getId();
                if(vertex!=from){
                    for (p=0;p<T.length;p++) {
                        if (T[p].getId()==id) {
                            var id_weight_i = getId_Weigth(id);
                            var id_weight_j = getId_Weigth(j.getId());
                            var weigthi = id_weight_i.weigth;
                            var weigthj = id_weight_j.weigth;
                            if (weigthi > weigthj + myWeigth) {
                                id_weight_i.weigth=weigthj + myWeigth;
                                id_weight_i.previous=vertex;
                            }
                        }
                    }
                }
                else{
                    for (q=0;q<id_weigth.length;q++) {
                        if (id_weigth[q].id==id) {
                            id_weigth[q].weigth=myWeigth;
                            id_weigth[q].previous=vertex;
                        }
                    }
                }
            }
        }
    }
    
    function getId_Weigth(id) {
        for (l=0;l<id_weigth.length;l++) {
            if (id_weigth[l].id==id) 
                return id_weigth[l];
        }
        return null;
    }
    
    function getPath(){
        var validPath = true, temp = [], path = [];
        temp.push(to);
        var npm = getId_Weigth(to.getId());
        var v = npm.previous;
        while (v != from) {
            temp.push(v);
            if (v == null) {
                validPath = false;
                break;
            }
            npm = getId_Weigth(v.getId());
            v = npm.previous;
        }
        if (validPath == true) {
            temp.push(from);
            for (i = temp.length - 1; i >= 0; i--)
                path.push(temp[i]);
        } 
        return path;
    }
    
    function setWeigth_Direction(vertex){
        if (direction == "both"){
            setWeigth(vertex,"in","out");
            setWeigth(vertex,"out","in");
        }
        else if (direction == "in")
            setWeigth(vertex,"in","out");
        else
            setWeigth(vertex,"out","in");
    }
    
    function getPositionInT(id){
        for (l=0;l<T.length;l++) {
            if(T[l].getId()==id)
                return l;
        }
        return null;
    }