[英]Using maxdepth and while in orientdb traversal
I am using orientdb v2.1.4 Is it possible to do an orientdb traversal from a target record with a while condition and a maxdepth parameter in either SQL or the Java API? 我正在使用orientdb v2.1.4是否可以在SQL或Java API中使用while条件和maxdepth参数从目标记录中进行Orientdb遍历? For example:
例如:
traverse all() from TargetRecordID maxdepth 25 while @class <> 'some_edge_class'
The orientdb parser seems to apply the maxdepth condition and ignores the while condition. orientdb解析器似乎应用了maxdepth条件,而忽略了while条件。
I use the while condition because I have 10 different edge classes overall, but I wish to do the traversal while excluding a subset (~3) of the edge classes . 我使用while条件是因为总体上我有10个不同的边缘类,但是我希望在遍历的同时排除边缘类的子集(〜3个) 。 If you have an alternative approach that I use to ignore certain edge classes during the traversal, that would be great as well.
如果您有另一种方法可以在遍历过程中忽略某些边缘类,那也很好。
try this query: 试试这个查询:
traverse both() from YourClass while both('your_edge_class').size() = 0 and $depth <= 25
so the traverse won't pass on the vertices connected to the edge/edges that you specified 因此遍历不会在连接到您指定的一个或多个边的顶点上传递
Edit: 编辑:
Given the following graph example and assuming that you don't want the edges type1
and type2
, would you like to get the nodes 1,2,3,4,5 and 8 connected to the edge type 3
or do you want to avoid all the vertices connected also with type 1
and type 2
? 给定以下图形示例,并假设您不希望边缘
type1
和type2
,是否要使节点1,2,3,4,5和8连接到边缘type 3
还是要避免所有顶点也与type 1
和type 2
?
Edit: 编辑:
You can use this javascript function with three parameters (rid,maxDepth,excludeEdges) 您可以将此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]);
}
}
}
}
Using the following command 使用以下命令
select expand(result) from (select myFunction("#9:1",25,"[type1,type2]") as result)
With Java Api 使用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();
Class traverse 类遍历
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.