简体   繁体   English

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

[英]Orientdb get last vertex from each path when Traversing by edge property

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' .尽管我不确定这是否是最好的方法,但这似乎仅在边缘具有'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. traversrdVertex(-1)应该返回最后一个,但我猜这是来自整个遍历所以不好。 (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.外部SELECT返回整个顶点包,所以我想也许找到那些没有带有dependency='yes'的输出边的顶点可能会解决它,尽管我不确定如何很好地做到这一点。

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 javascript 函数“myFunction”有三个参数,分别是ridVertex、property 和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路径是 A -> D 和 A -> B -> C -> G 然后将返回顶点 D 和 G

在此处输入图片说明

The following is a slight simplification of @sebastian's solution, using Allesandro's graph (with dependentOn.value being 0 or 1):以下是@sebastian 的解决方案的轻微简化,使用 Allesandro 的图(dependentOn.value 为 0 或 1):

select from
  (TRAVERSE * FROM (select from Circle where name="A")
   while (@CLASS="dependentOn" and value=1) OR @CLASS="Circle")
where @CLASS='Circle' AND NOT (outE().value contains 1)

----+-----+------+----+--------------
#   |@RID |@CLASS|name|in_dependentOn
----+-----+------+----+--------------
0   |#11:6|Circle|G   |[#12:4]       
1   |#11:3|Circle|D   |[#12:1]       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM