使用maxdepth和while in orientdb遍历

Using maxdepth and while in orientdb traversal

我正在使用orientdb v2.1.4 是否可以在 SQL 或 Java API 中使用 while 条件和 maxdepth 参数从目标记录进行 orientdb 遍历?例如:

traverse all() from TargetRecordID maxdepth 25 while @class <> 'some_edge_class'

orientdb 解析器似乎应用了 maxdepth 条件并忽略了 while 条件。

我使用 while 条件是因为我总共有 10 条不同的边 类,但我希望 进行遍历时排除边的一个子集 (~3) 类。如果你有我用来在遍历过程中忽略某些边 类 的替代方法,那也很好。

试试这个查询:

traverse both() from YourClass while both('your_edge_class').size() = 0 and $depth <= 25

因此遍历不会传递连接到您指定的 edge/edges 的顶点

编辑:

给定以下图形示例并假设您不想要边 type1type2,您是否想要获取节点 1、2、3、4、5 和 8连接到边 type 3 还是要避免所有也与 type 1type 2 连接的顶点?

编辑:

您可以使用带有三个参数(rid、maxDepth、excludeEdges)的此 javascript 函数

var g=orient.getGraph();
var result=[];
var current=[];
var next=[];
var listEdges=excludeEdges.substring(1,excludeEdges.length-1).split(",");
var root=g.command('sql','select from '+rid);
current.push(root[0]);
var step=1;
while(current.length>0 && step<=maxDepth){
    for(i=0;i<current.length;i++){
        getVertex(current[i],"OUT");
        getVertex(current[i],"IN");
    }
    change();
    step++;
}
return result;

function change(){
    current=[];
    for (index=0;index<next.length;index++)
        current.push(next[index]);
    next=[];
}

function getVertex(start,direction){
    var edgeDir="outE()";
    var reverseDirection="in";
    if(direction=="IN"){
        edgeDir="inE()";
        reverseDirection="out";
    }
    var edges=g.command("sql","select expand("+edgeDir +") from "+start.getId());
    for(h=0;h<edges.length;h++){
        var found=false;
        for(m=0;m<listEdges.length;m++){
            if(edges[h].getProperty("@class")==listEdges[m]){
                found=true;
                break;
            }
        }
        if(found==false){
            var vertex=g.command("sql","select expand("+ reverseDirection + ") from " +edges[h].getId());
            for(j=0;j<result.length;j++){
                if(result[j].getId().toString().equals(vertex[0].getId().toString()) ||
                    vertex[0].getId().toString().equals(rid)){
                    found=true;
                    break;
                }
            }
            if(found==false){
                result.push(vertex[0]);
                next.push(vertex[0]);
            }
        }
    }
}

使用以下命令

select expand(result) from (select myFunction("#9:1",25,"[type1,type2]") as result)

与JavaApi

OrientGraph g=new OrientGraph("remote:localhost/yourDb");
Traverse traverse = new Traverse(g);

List<String> listExcludeEdges=new ArrayList<String>();
listExcludeEdges.add("type1");
listExcludeEdges.add("type2");
int maxDepth=3;
String id="#9:1";

Set<Vertex> vertex=traverse.get(id,maxDepth,listExcludeEdges);
for(Vertex v:vertex){
    System.out.println(v.getId());
}
g.shutdown();

Class遍历

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;

public class Traverse {

    private Set<Vertex> listVertex = new LinkedHashSet<Vertex>(); 
    private OrientGraph g;
    private int maxDepth;
    private List<String> listExcludeEdges;
    private Vertex root;

    public Traverse(OrientGraph g) {
        this.g = g;
    }

    public Set<Vertex> get(String id,int maxDepth,List<String> listExcludeEdges) {
        this.maxDepth=maxDepth;
        this.listExcludeEdges=listExcludeEdges;
        if(checkClassOrRid(id)){
            if (getRoot(id)) 
                getChildren(1,this.root);
        }
        return this.listVertex;
    }

    private boolean checkClassOrRid(String id){
        Pattern pattern = Pattern.compile("^#[0-9]+:[0-9]+");
        Matcher matcher = pattern.matcher(id);
        if (matcher.matches()) {
            for (Vertex v : g.getVertices()) {
                if (v.getId().toString().equals(id))
                    return true;
            }   
        }
        return false;
    }

    protected boolean getRoot(String id) {      
        this.root =  g.getVertex(id);
        if(this.root!=null)
            return true;
        return false;
    }

    protected void getChildren(int step,Vertex from) {
        if(step<=maxDepth){
            getVertex(Direction.OUT,from,step);
            getVertex(Direction.IN,from,step);
        }
    }

    private void getVertex(Direction direction,Vertex from,int step){
        Iterable<Edge> edges = from.getEdges(direction);
        for(Edge e:edges){
            boolean found=false;
            for(String s:this.listExcludeEdges){
                if(e.getProperty("@class").equals(s))
                    found=true;
            }
            if(found==false){
                Vertex vertex=null;
                if(direction==Direction.OUT)
                    vertex=e.getVertex(Direction.IN);
                else
                    vertex=e.getVertex(Direction.OUT);
                this.listVertex.add(vertex);
                getChildren(step+1,vertex);
            }
        }
    }
}