首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在取向数据库遍历中使用最大深度和时间

在取向数据库遍历中使用最大深度和时间
EN

Stack Overflow用户
提问于 2015-12-08 07:08:40
回答 2查看 844关注 0票数 0

我使用的是or v2.1.4,是否可以在SQL或Java中使用with条件和maxdepth参数从目标记录执行or遍历?例如:

代码语言:javascript
复制
traverse all() from TargetRecordID maxdepth 25 while @class <> 'some_edge_class'

orientdb解析器似乎应用了最大深度条件,而忽略了while条件。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-09 07:28:29

请尝试以下查询:

代码语言:javascript
复制
traverse both() from YourClass while both('your_edge_class').size() = 0 and $depth <= 25

因此,遍历不会传递连接到您指定的边缘/边缘的顶点。

编辑:

给出下面的图表示例,并假设不需要边type1type2,您希望将节点1、2、3、4、5和8连接到边缘type 3,还是要避免与type 1type 2同时连接的所有顶点?

编辑:

您可以使用这个javascript函数的三个参数(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]);
            }
        }
    }
}

使用以下命令

代码语言:javascript
复制
select expand(result) from (select myFunction("#9:1",25,"[type1,type2]") as result)
票数 1
EN

Stack Overflow用户

发布于 2015-12-10 11:11:07

使用Java Api

代码语言:javascript
复制
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();

类导线

代码语言:javascript
复制
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);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34149973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档