2015-03-17 2 views
1

У меня есть что-то вроде следующего графика:Сортировать по длине пути в Java

g = new TinkerGraph() 
v1 = g.addVertex([name: 'one']) 
v2 = g.addVertex([name: 'two']) 
v3 = g.addVertex([name: 'three']) 
v4 = g.addVertex([name: 'four']) 
v5 = g.addVertex([name: 'five']) 
v1.addEdge('contains', v2) 
v2.addEdge('contains', v3) 
v3.addEdge('contains', v4) 
v4.addEdge('contains', v5) 

Теперь я хочу, чтобы удалить v1 и все его «дети»

v1.out('contains').loop(1){it.loops < 10}{true}.order{-it.a.path.toList().unique{a, b -> a <=> b}.size <=> it.b.path.toList().unique{a, b -> a <=> b}.size}.remove() 
v1.remove() 

(он должен мне удалить из листья)

Не могли бы вы помочь мне переписать этот Groovy-запрос на Java? У нас есть проблема с частью порядка {...}. Не знаете, как получить path от Pair<Vertex, Vertex>.

ответ

3

Не уверен, что порядок важен, если вам просто нужно удалить родительский узел и все его дочерние элементы. Я написал тест junit, который, как я думаю, решает вашу проблему.

import com.tinkerpop.blueprints.Direction; 
import com.tinkerpop.blueprints.Graph; 
import com.tinkerpop.blueprints.Vertex; 
import com.tinkerpop.blueprints.impls.tg.TinkerGraph; 
import org.junit.Assert; 
import org.junit.Test; 
import java.util.Iterator; 

public class SOTest { 

    private static final String EDGE_LABEL = "contains"; 

    @Test 
    public void testSomething(){ 
     Graph g = new TinkerGraph(); 

     Vertex v1 = createVertex(g, "name", "one"); 
     Vertex v2 = createVertex(g, "name", "two"); 
     Vertex v3 = createVertex(g, "name", "three"); 
     Vertex v4 = createVertex(g, "name", "four"); 
     Vertex v5 = createVertex(g, "name", "five"); 

     v1.addEdge(EDGE_LABEL, v2); 
     v2.addEdge(EDGE_LABEL, v3); 
     v3.addEdge(EDGE_LABEL, v4); 
     v4.addEdge(EDGE_LABEL, v5); 

     removeParentAndChildren(v1); 

     Assert.assertEquals(g.getVertices().iterator().hasNext(), false); 

    } 

    private static void removeParentAndChildren(Vertex vertex) { 
     final Iterator<Vertex> vertexIterator = vertex.getVertices(Direction.OUT, EDGE_LABEL).iterator(); 
     if(vertexIterator.hasNext()) { 
      vertex.getVertices(Direction.OUT, EDGE_LABEL).forEach(SOTest::removeParentAndChildren); 
     } 
     vertex.remove(); 
    } 

    private Vertex createVertex(final Graph g, final String key, final String value) { 
     Vertex v = g.addVertex(null); 
     v.setProperty(key, value); 
     return v; 
    } 

} 
0

мы в конечном итоге с помощью этого:

new HawkularPipeline<>(v1) 
    .as("start") 
    .out(contains) 
    .loop("start", (x) -> true, (x) -> true) 
    .toList() 
    .forEach { 
    c -> c.remove(); 
    } 
Смежные вопросы