我正在使用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)时进行遍历。如果您在遍历期间使用替代方法忽略某些边缘类,那么这也很好。
答案 0 :(得分:0)
尝试此查询:
traverse both() from YourClass while both('your_edge_class').size() = 0 and $depth <= 25
所以遍历不会传递连接到指定边/边的顶点
编辑:
鉴于以下图表示例并假设您不想要边type1
和type2
,您是否希望得到节点1,2,3,4,5和8已连接到边type 3
,或者您是否要避免与type 1
和type 2
连接的所有顶点?
编辑:
你可以使用这个带有三个参数的javascript函数(rid,maxDepth,excludeEdges)
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)
答案 1 :(得分:0)
使用Java Api
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();
类遍历
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);
}
}
}
}