且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Orientdb通过边属性遍历时从每个路径获取最后一个顶点

更新时间:2023-11-05 21:25:04

我尝试用一​​个例子来构建下图

javascript 函数myFunction"有三个参数,分别是ridVertex、property 和value

var g=orient.getGraph();前一个变量=[];当前变量=[];无功节点=[];var b=g.command("sql","select from v where @rid="+ridVertex);if(b.length>0){上一个.push(b[0]);node.push(b[0]);做{for(i=0;i0){设置路径();}}改变();}while(previous.length>0);}返回节点;函数 setPaths(){for (m = 0; m < node.length; m++) {var lastId=node[m].getId().toString();var idOut=previous[i].getId().toString();如果(lastId==idOut){for(r=0;r

使用以下命令

select expand(result) from (select myFunction("#9:0","dependence","yes") as result)

路径是 A -> D 和 A -> B -> C -> G 然后将返回顶点 D 和 G

I need to traverse all vertices that are connected by edges where the property 'dependence' is 'true'

This is what I have so far:

SELECT
FROM (TRAVERSE * 
      FROM (SELECT outE() FROM 9:5) 
      WHILE (@class = 'E' AND dependence = 'yes') OR @class = 'V')
WHERE @class = 'V' 

Although im not sure if is the best way to do it, this seems to work fine following only the paths where edges have 'dependence' = 'yes'.

Now, There could be more than one path generated, and I need to get the last vertex from each path/branch.

traverserdVertex(-1) should return the last one, but im guessing that is from the whole traversal so is no good. (and it looks like there's a bug because it retrieves more than one)

The outer SELECT returns the whole bag of vertices so I'm thinking that maybe finding the ones that doesn't have an outgoing edge with dependence='yes' might solve it, although I'm not sure how to do it nicely.

SOLUTION:

SELECT
    FROM (TRAVERSE * 
          FROM (SELECT outE() FROM 9:5) 
          WHILE (@class = 'E' AND dependence = 'yes') OR @class = 'V')
    WHERE @class = 'V' AND NOT (outE() contains (dependence='yes'))

This effectively returns the last vertex from each branch. I'm open to any other option, I'm wondering if it could be improved.

I tried with an example by building the following graph

The javascript function "myFunction" has three parameters which are ridVertex, property and value

var g=orient.getGraph();
var previous=[];
var currently=[];
var node=[];
var b=g.command("sql","select from v where @rid =" + ridVertex);
if(b.length>0){
   previous.push(b[0]);
   node.push(b[0]);
   do{
      for(i=0;i<previous.length;i++){
         var edges=g.command("sql","select expand(outE()) from V   where @rid = "+ previous[i].getId());
         var myVertex=[];   
         for(j=0;j<edges.length;j++){ 
            var edge=edges[j];
            var dependence=edge.getProperty(property);
            if(dependence==value){
               var vIn=edge.getProperty("in");
               myVertex.push(vIn);
            }
          }
          if(myVertex.length>0){
             setPaths();
          }
       }
       change();
    }while(previous.length>0);  
}
return node;

function setPaths(){
   for (m = 0; m < node.length; m++) {
      var lastId=node[m].getId().toString();
      var idOut=previous[i].getId().toString();
      if (lastId==idOut) {
         for(r=0;r<myVertex.length;r++){
            var vertex=myVertex[r];
            node.push(vertex);
            currently.push(vertex);
        }
        node.splice(m,1);
        break;
      }
   }
}

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

Using the following command

select expand(result) from (select myFunction("#9:0","dependence","yes") as result)

the paths are A -> D and A -> B -> C -> G and then will be returned the verteces D and G