Orientdb通过边属性遍历时从每个路径获取最后一个顶点 [英] Orientdb get last vertex from each path when Traversing by edge property

查看:52
本文介绍了Orientdb通过边属性遍历时从每个路径获取最后一个顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历由属性依赖"为真"的边连接的所有顶点

这是我目前所拥有的:

SELECT从(遍历 *从 (SELECT outE() FROM 9:5)WHILE (@class = 'E' AND dependency = 'yes') OR @class = 'V')哪里@class = 'V'

尽管我不确定这是否是最好的方法,但这似乎仅在边缘具有 'dependence' = 'yes' 的路径之后工作正常.

现在,生成的路径可能不止一个,我需要从每个路径/分支中获取最后一个顶点.

traverserdVertex(-1) 应该返回最后一个,但我猜这是从整个遍历中得出的,所以不好.(看起来有一个错误,因为它检索了多个)

外部 SELECT 返回整个顶点包,所以我想也许可以用 dependence='yes' 找到没有出边的那些可能会解决它,虽然我不知道如何做得很好.

解决方案:

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

这有效地返回了每个分支的最后一个顶点.我对任何其他选择持开放态度,我想知道是否可以改进.

解决方案

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

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;i

0){设置路径();}}改变();}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

这篇关于Orientdb通过边属性遍历时从每个路径获取最后一个顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆