@Test public void testCircularGraph() { /* Layout * * (a)---(b)===(c)---(e) * \ / * (d) */ graph.makeEdge("a", "b"); graph.makeEdge("b", "c"); graph.makeEdge("b", "c"); graph.makeEdge("b", "d"); graph.makeEdge("c", "d"); graph.makeEdge("c", "e"); PathFinder<Path> finder = instantiatePathFinder(10); Iterable<Path> paths = finder.findAllPaths(graph.getNode("a"), graph.getNode("e")); assertPaths( paths, "a,b,c,e", "a,b,c,e", "a,b,d,c,e", "a,b,c,d,b,c,e", "a,b,c,d,b,c,e", "a,b,c,b,d,c,e", "a,b,c,b,d,c,e", "a,b,d,c,b,c,e", "a,b,d,c,b,c,e"); }
@Test public void testTripleRelationshipGraph() { /* Layout * ___ * (a)---(b)===(c)---(d) */ graph.makeEdge("a", "b"); graph.makeEdge("b", "c"); graph.makeEdge("b", "c"); graph.makeEdge("b", "c"); graph.makeEdge("c", "d"); PathFinder<Path> finder = instantiatePathFinder(10); Iterable<Path> paths = finder.findAllPaths(graph.getNode("a"), graph.getNode("d")); assertPaths( paths, "a,b,c,d", "a,b,c,d", "a,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d", "a,b,c,b,c,d"); }
@SuppressWarnings({"rawtypes", "unchecked"}) public ListRepresentation findPaths(long startId, long endId, Map<String, Object> map) { final FindParams findParams = new FindParams(startId, endId, map).invoke(); PathFinder finder = findParams.getFinder(); Node startNode = findParams.getStartNode(); Node endNode = findParams.getEndNode(); Iterable paths = finder.findAllPaths(startNode, endNode); IterableWrapper<PathRepresentation, Path> pathRepresentations = new IterableWrapper<PathRepresentation, Path>(paths) { @Override protected PathRepresentation underlyingObjectToObject(Path path) { return findParams.pathRepresentationOf(path); } }; return new ListRepresentation(RepresentationType.PATH, pathRepresentations); }
private static Iterator<Node> findPath(Node s, Node e) { Iterator<Node> it; try (Transaction tx = graphDb.beginTx()) { PathFinder<Path> finder = GraphAlgoFactory.shortestPath( PathExpanders.forTypeAndDirection(RelTypes.STD, Direction.OUTGOING), 15); Iterable<Path> paths = finder.findAllPaths(s, e); Path path = paths.iterator().next(); it = path.nodes().iterator(); if (it.hasNext()) { System.out.println(it.next().getProperty("name")); } if (it.hasNext()) { System.out.println(it.next().getProperty("name")); } if (it.hasNext()) { System.out.println(it.next().getProperty("name")); } tx.success(); } return it; }
@Override public Map<String, Set<Long>> extract( List<DocGraph> docGraphs, DocGraphFilterFunction filterFunc, Set<String> resourceClasses, Set<String> documentClasses) { // pattern-document graph associations R_D Map<String, Set<Long>> associations = new HashMap<String, Set<Long>>(); // init path finder initFinder(); // G_D(filtered) <- G_D.where({G_d|filter(G_D)}) Set<DocGraph> filteredDocGraphs = new HashSet<DocGraph>(); if (filterFunc == null) { filteredDocGraphs.addAll(docGraphs); } else { for (DocGraph docGraph : docGraphs) { if (filterFunc.filter(docGraph)) { filteredDocGraphs.add(docGraph); } } } Set<Node> relevantResources = null; Set<Node> relevantDocuments = null; String r = null; Node v_p = null; boolean validPath = true; int pCnt = 0; log.info(String.format("Starting analysis of %d document graphs", filteredDocGraphs.size())); for (DocGraph docGraph : filteredDocGraphs) { log.info( String.format( "Analyzing DocGraph[%d] (%d, %d)", docGraph.getId(), docGraph.getNodeCount(), docGraph.getEdgeCount())); pCnt = 0; // V_R <- G_d.V.where({v|tau(mu(v)) in C_R}) relevantResources = filterNodesByClass(docGraph.getNodes(), resourceClasses); // V_R <- G_d.V.where({v|tau(mu(v)) in C_D}) relevantDocuments = filterNodesByClass(docGraph.getNodes(), documentClasses); preProcessGraph(docGraph, resourceClasses); try (Transaction tx = graphDB.beginTx()) { for (Node v_r : relevantResources) { for (Node v_d : relevantDocuments) { // P <- G_d.paths(v_R,v_D) for (Path p : finder.findAllPaths(v_r, v_d)) { validPath = true; r = String.format("%s", v_r.getProperty(Constants.ID_KEY)); v_p = v_r; for (Relationship e : p.relationships()) { if (e.getStartNode().getId() == v_p.getId()) { if (getNodeSuperClass(e.getEndNode()) .equals(Constants.NODE_SUPER_CLASS_RESOURCE_VALUE)) { validPath = false; break; } r = String.format( "%s-%s->%s", r, e.getType().name(), getNodeClass(e.getEndNode())); v_p = e.getEndNode(); } else { if (getNodeSuperClass(e.getStartNode()) .equals(Constants.NODE_SUPER_CLASS_RESOURCE_VALUE)) { validPath = false; break; } r = String.format( "%s<-%s-%s", r, e.getType().name(), getNodeClass(e.getStartNode())); v_p = e.getStartNode(); } } // R_D.add(<r, G_d>) if (validPath) { associations = addPatternDocGraphAssociation(associations, r, docGraph); pCnt++; } } } } tx.success(); } log.info(String.format("%d patterns in DocGraph[%d]", pCnt, docGraph.getId())); } return associations; }