public void testAddingSelfLoops() { Graph graph = graphTest.getGraphInstance(); if (graphTest.allowsSelfLoops) { List<String> ids = generateIds(3); Vertex v1 = graph.addVertex(convertId(ids.get(0))); Vertex v2 = graph.addVertex(convertId(ids.get(1))); Vertex v3 = graph.addVertex(convertId(ids.get(2))); graph.addEdge(null, v1, v1, convertId("is_self")); graph.addEdge(null, v2, v2, convertId("is_self")); graph.addEdge(null, v3, v3, convertId("is_self")); if (graphTest.supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graphTest.supportsEdgeIteration) { assertEquals(3, count(graph.getEdges())); int counter = 0; for (Edge edge : graph.getEdges()) { counter++; assertEquals(edge.getInVertex(), edge.getOutVertex()); assertEquals(edge.getInVertex().getId(), edge.getOutVertex().getId()); } assertEquals(counter, 3); } } graph.shutdown(); }
public void testEdgeCentricRemoving() { final Graph graph = graphTest.generateGraph(); final Edge a = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); final Edge b = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); final Edge c = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); Object cId = c.getId(); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 3); a.remove(); b.remove(); if (graph.getFeatures().supportsEdgeRetrieval) assertNotNull(graph.getEdge(cId)); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 1); graph.shutdown(); }
public Set<Edge> getNonadjacencies() { Graph complete = GraphUtils.completeGraph(graph); Set<Edge> nonAdjacencies = complete.getEdges(); Graph undirected = GraphUtils.undirectedGraph(graph); nonAdjacencies.removeAll(undirected.getEdges()); return new HashSet<Edge>(nonAdjacencies); }
public void testAddingDuplicateEdges() { Graph graph = graphTest.generateGraph(); Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); graph.addEdge(null, v1, v2, convertId(graph, "knows")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); graph.addEdge(null, v2, v3, convertId(graph, "pets")); if (graph.getFeatures().supportsDuplicateEdges) { if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) assertEquals(5, count(graph.getEdges())); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(4, count(v2.getEdges(Direction.OUT))); assertEquals(4, count(v3.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); } else { if (graph.getFeatures().supportsVertexIteration) assertEquals(count(graph.getVertices()), 3); if (graph.getFeatures().supportsEdgeIteration) assertEquals(count(graph.getEdges()), 2); assertEquals(0, count(v1.getEdges(Direction.IN))); assertEquals(1, count(v1.getEdges(Direction.OUT))); assertEquals(1, count(v2.getEdges(Direction.IN))); assertEquals(1, count(v2.getEdges(Direction.OUT))); assertEquals(1, count(v3.getEdges(Direction.IN))); assertEquals(0, count(v3.getEdges(Direction.OUT))); } graph.shutdown(); }
public void testAddingVerticesAndEdges() { Graph graph = graphTest.generateGraph(); Vertex a = graph.addVertex(null); Vertex b = graph.addVertex(null); Edge edge = graph.addEdge(null, a, b, convertId(graph, "knows")); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(1, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(2, count(graph.getVertices())); } graph.removeVertex(a); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(0, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(1, count(graph.getVertices())); } try { graph.removeEdge(edge); // TODO: doesn't work with wrapper graphs assertTrue(false); } catch (Exception e) { assertTrue(true); } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(0, count(graph.getEdges())); } if (graph.getFeatures().supportsVertexIteration) { assertEquals(1, count(graph.getVertices())); } graph.shutdown(); }
/** * Transforms a maximally directed pattern (PDAG) represented in graph <code>g</code> into an * arbitrary DAG by modifying <code>g</code> itself. Based on the algorithm described in * Chickering (2002) "Optimal structure identification with greedy search" Journal of Machine * Learning Research. R. Silva, June 2004 */ public static void pdagToDag(Graph g) { Graph p = new EdgeListGraph(g); List<Edge> undirectedEdges = new ArrayList<Edge>(); for (Edge edge : g.getEdges()) { if (edge.getEndpoint1() == Endpoint.TAIL && edge.getEndpoint2() == Endpoint.TAIL && !undirectedEdges.contains(edge)) { undirectedEdges.add(edge); } } g.removeEdges(undirectedEdges); List<Node> pNodes = p.getNodes(); do { Node x = null; for (Node pNode : pNodes) { x = pNode; if (p.getChildren(x).size() > 0) { continue; } Set<Node> neighbors = new HashSet<Node>(); for (Edge edge : p.getEdges()) { if (edge.getNode1() == x || edge.getNode2() == x) { if (edge.getEndpoint1() == Endpoint.TAIL && edge.getEndpoint2() == Endpoint.TAIL) { if (edge.getNode1() == x) { neighbors.add(edge.getNode2()); } else { neighbors.add(edge.getNode1()); } } } } if (neighbors.size() > 0) { Collection<Node> parents = p.getParents(x); Set<Node> all = new HashSet<Node>(neighbors); all.addAll(parents); if (!GraphUtils.isClique(all, p)) { continue; } } for (Node neighbor : neighbors) { Node node1 = g.getNode(neighbor.getName()); Node node2 = g.getNode(x.getName()); g.addDirectedEdge(node1, node2); } p.removeNode(x); break; } pNodes.remove(x); } while (pNodes.size() > 0); }
public static <V, E> void customTraverse( final Graph<V, E> graph, final V startNode, final TraversalCallback<V, E> handler) { Set<V> traversedNodes = new HashSet<V>(); Queue<VertexHolder<V>> traversalQueue = new PriorityQueue<VertexHolder<V>>(16); int counter = 0; traversalQueue.add(new VertexHolder<V>(startNode, counter++, 0)); boolean done = false; do { boolean first = true; while (!traversalQueue.isEmpty()) { V node = traversalQueue.remove().getVertex(); VertexEdges<V, E> edges = graph.getEdges(node); if (traversedNodes.contains(node)) { continue; } Map<E, V> incomming = edges.getIncomming(); boolean hasIncomingBeenTraversed = true; for (V val : incomming.values()) { if (!traversedNodes.contains(val)) { hasIncomingBeenTraversed = false; break; } } if (!first && !hasIncomingBeenTraversed) { continue; } handler.handle(node, incomming); traversedNodes.add(node); first = false; Map<E, V> outgoing = edges.getOutgoing(); for (V next : outgoing.values()) { traversalQueue.add( new VertexHolder<V>(next, counter++, graph.getEdges(next).getIncomming().size())); } } Set<V> leftNodes = Sets.difference(graph.getVertices(), traversedNodes); if (leftNodes.isEmpty()) { done = true; } else { boolean added = false; for (V node : leftNodes) { Collection<V> incomingNodes = graph.getEdges(node).getIncomming().values(); for (V incoming : incomingNodes) { if (traversedNodes.contains(incoming)) { traversalQueue.add(new VertexHolder<V>(node, counter++, 0)); added = true; break; } } if (added) { break; } } } } while (!done); }
public String reachGraphInfo(Graph graph) { String result = "Reachability Graph was constructed. Its arcs are the following:\n\n"; for (int i = 0; i < graph.getEdges().size(); i++) { result += graph.getEdges().get(i).from().toString(); result += " ->(t" + graph.getEdges().get(i).getTrans().getNumber() + ") "; result += graph.getEdges().get(i).to().toString() + "\n"; } return result; }
public void testRemoveManyEdges() { Graph graph = graphTest.getGraphInstance(); long counter = 200000l; int edgeCount = 100; Set<Edge> edges = new HashSet<Edge>(); for (int i = 0; i < edgeCount; i++) { Vertex out = graph.addVertex(convertId("" + counter++)); Vertex in = graph.addVertex(convertId("" + counter++)); edges.add(graph.addEdge(null, out, in, convertId("a" + UUID.randomUUID().toString()))); } assertEquals(edgeCount, edges.size()); if (graphTest.supportsVertexIteration) { this.stopWatch(); assertEquals(edgeCount * 2, count(graph.getVertices())); BaseTest.printPerformance( graph.toString(), edgeCount * 2, "vertices counted", this.stopWatch()); } if (graphTest.supportsEdgeIteration) { this.stopWatch(); assertEquals(edgeCount, count(graph.getEdges())); BaseTest.printPerformance(graph.toString(), edgeCount, "edges counted", this.stopWatch()); int i = edgeCount; this.stopWatch(); for (Edge edge : edges) { graph.removeEdge(edge); i--; assertEquals(i, count(graph.getEdges())); if (graphTest.supportsVertexIteration) { int x = 0; for (Vertex vertex : graph.getVertices()) { if (count(vertex.getOutEdges()) > 0) { assertEquals(1, count(vertex.getOutEdges())); assertFalse(count(vertex.getInEdges()) > 0); } else if (count(vertex.getInEdges()) > 0) { assertEquals(1, count(vertex.getInEdges())); assertFalse(count(vertex.getOutEdges()) > 0); } else { x++; } } assertEquals((edgeCount - i) * 2, x); } } BaseTest.printPerformance( graph.toString(), edgeCount, "edges removed and graph checked", this.stopWatch()); } graph.shutdown(); }
public Set<Edge> getAdjacencies() { Set<Edge> adjacencies = new HashSet<Edge>(); for (Edge edge : graph.getEdges()) { adjacencies.add(edge); } return adjacencies; }
/** * Creates an SPQRTree. * * @param g Create the tree for this graph. */ public SPQRTree(Graph g, Pair<Integer, Integer> atEdge) { rootNode = null; nodeId2NodePointer = new HashMap<Integer, Integer>(); nodePointer2NodeId = new HashMap<Integer, Integer>(); treeNode2TreeNodePtr = new HashMap<SPQRTreeNode, Integer>(); int graphPointer = createGraph(); for (Integer v : g.getVertices()) { int nodePointer = addNode(graphPointer); nodeId2NodePointer.put(v, nodePointer); nodePointer2NodeId.put(nodePointer, v); } Integer atEdgePtr = null; for (Pair<Integer, Integer> e : g.getEdges()) { int eptr = addEdge( graphPointer, nodeId2NodePointer.get(Pair.get1(e)), nodeId2NodePointer.get(Pair.get2(e))); if (e.equals(atEdge)) { atEdgePtr = eptr; } } int spqrTreePointer = (atEdgePtr == null) ? createSPQRTree(graphPointer) : createSPQRTree(graphPointer, atEdgePtr); rootNode = new SPQRTreeNode(); int rootNodePointer = spqrRootNode(spqrTreePointer); addSPQRTreeNodeAndChildren(spqrTreePointer, rootNodePointer, rootNode); orderSNodes(rootNode); }
public void createEdges(Graph graph) { edgeArray = new ArrayList<>(); for (int i = 0; i < graph.getEdges().length; i++) { Line edgeI = new Line(); edgeI.startXProperty().bind(nodeArray.get(graph.getEdge(i)[0]).translateXProperty()); edgeI.startYProperty().bind(nodeArray.get(graph.getEdge(i)[0]).translateYProperty()); edgeI.endXProperty().bind(nodeArray.get(graph.getEdge(i)[1]).translateXProperty()); edgeI.endYProperty().bind(nodeArray.get(graph.getEdge(i)[1]).translateYProperty()); if (abs(graph.getEdge(i)[0] - graph.getEdge(i)[1]) == 1) { edgeI.setId("backbone"); } else if ((nodeArray.get(graph.getEdge(i)[0]).getId().equals("A") && nodeArray.get(graph.getEdge(i)[1]).getId().equals("U")) || nodeArray.get(graph.getEdge(i)[0]).getId().equals("U") && nodeArray.get(graph.getEdge(i)[1]).getId().equals("A")) { edgeI.setId("2-H-bond"); } else if ((nodeArray.get(graph.getEdge(i)[0]).getId().equals("G") && nodeArray.get(graph.getEdge(i)[1]).getId().equals("C")) || nodeArray.get(graph.getEdge(i)[0]).getId().equals("C") && nodeArray.get(graph.getEdge(i)[1]).getId().equals("G")) { edgeI.setId("3-H-bond"); } root.nodes.getChildren().add(edgeI); edgeArray.add(edgeI); } }
public TimeLagGraphWrapper(GraphWrapper graphWrapper) { if (graphWrapper == null) { throw new NullPointerException("No graph wrapper."); } TimeLagGraph graph = new TimeLagGraph(); Graph _graph = graphWrapper.getGraph(); for (Node node : _graph.getNodes()) { Node _node = node.like(node.getName() + ":0"); _node.setNodeType(node.getNodeType()); graph.addNode(_node); } for (Edge edge : _graph.getEdges()) { if (!Edges.isDirectedEdge(edge)) { throw new IllegalArgumentException(); } Node from = edge.getNode1(); Node to = edge.getNode2(); Node _from = graph.getNode(from.getName(), 1); Node _to = graph.getNode(to.getName(), 0); graph.addDirectedEdge(_from, _to); } this.graph = graph; }
public void testRemoveVertexWithEdges(final Graph graph) { List<String> ids = generateIds(2); Vertex v1 = graph.addVertex(convertId(ids.get(0))); Vertex v2 = graph.addVertex(convertId(ids.get(1))); graph.addEdge(null, v1, v2, convertId("knows")); if (config.supportsVertexIteration) assertEquals(2, count(graph.getVertices())); if (config.supportsEdgeIteration) assertEquals(1, count(graph.getEdges())); graph.removeVertex(v1); if (config.supportsVertexIteration) assertEquals(1, count(graph.getVertices())); if (config.supportsEdgeIteration) assertEquals(0, count(graph.getEdges())); graph.removeVertex(v2); if (config.supportsVertexIteration) assertEquals(0, count(graph.getVertices())); if (config.supportsEdgeIteration) assertEquals(0, count(graph.getEdges())); }
public Graph orient() { Graph skeleton = GraphUtils.undirectedGraph(getPattern()); Graph graph = new EdgeListGraph(skeleton.getNodes()); List<Node> nodes = skeleton.getNodes(); // Collections.shuffle(nodes); if (isR1Done()) { ruleR1(skeleton, graph, nodes); } for (Edge edge : skeleton.getEdges()) { if (!graph.isAdjacentTo(edge.getNode1(), edge.getNode2())) { graph.addUndirectedEdge(edge.getNode1(), edge.getNode2()); } } if (isR2Done()) { ruleR2(skeleton, graph); } if (isMeekDone()) { new MeekRules().orientImplied(graph); } return graph; }
public GraphIndex(Graph graph) { LOG.info("Indexing graph..."); for (String feedId : graph.getFeedIds()) { for (Agency agency : graph.getAgencies(feedId)) { Map<String, Agency> agencyForId = agenciesForFeedId.getOrDefault(feedId, new HashMap<>()); agencyForId.put(agency.getId(), agency); this.agenciesForFeedId.put(feedId, agencyForId); } } Collection<Edge> edges = graph.getEdges(); /* We will keep a separate set of all vertices in case some have the same label. * Maybe we should just guarantee unique labels. */ Set<Vertex> vertices = Sets.newHashSet(); for (Edge edge : edges) { vertices.add(edge.getFromVertex()); vertices.add(edge.getToVertex()); if (edge instanceof TablePatternEdge) { TablePatternEdge patternEdge = (TablePatternEdge) edge; TripPattern pattern = patternEdge.getPattern(); patternForId.put(pattern.code, pattern); } } for (Vertex vertex : vertices) { vertexForId.put(vertex.getLabel(), vertex); if (vertex instanceof TransitStop) { TransitStop transitStop = (TransitStop) vertex; Stop stop = transitStop.getStop(); stopForId.put(stop.getId(), stop); stopVertexForStop.put(stop, transitStop); stopsForParentStation.put(stop.getParentStation(), stop); } } for (TransitStop stopVertex : stopVertexForStop.values()) { Envelope envelope = new Envelope(stopVertex.getCoordinate()); stopSpatialIndex.insert(envelope, stopVertex); } for (TripPattern pattern : patternForId.values()) { patternsForFeedId.put(pattern.getFeedId(), pattern); patternsForRoute.put(pattern.route, pattern); for (Trip trip : pattern.getTrips()) { patternForTrip.put(trip, pattern); tripForId.put(trip.getId(), trip); } for (Stop stop : pattern.getStops()) { patternsForStop.put(stop, pattern); } } for (Route route : patternsForRoute.asMap().keySet()) { routeForId.put(route.getId(), route); } // Copy these two service indexes from the graph until we have better ones. calendarService = graph.getCalendarService(); serviceCodes = graph.serviceCodes; this.graph = graph; LOG.info("Done indexing graph."); }
/** Get a graph and direct only the unshielded colliders. */ public static void basicPattern(Graph graph) { Set<Edge> undirectedEdges = new HashSet<Edge>(); NEXT_EDGE: for (Edge edge : graph.getEdges()) { Node head = null, tail = null; if (edge.getEndpoint1() == Endpoint.ARROW && edge.getEndpoint2() == Endpoint.TAIL) { head = edge.getNode1(); tail = edge.getNode2(); } else if (edge.getEndpoint2() == Endpoint.ARROW && edge.getEndpoint1() == Endpoint.TAIL) { head = edge.getNode2(); tail = edge.getNode1(); } if (head != null) { for (Node node : graph.getParents(head)) { if (node != tail && !graph.isAdjacentTo(tail, node)) { continue NEXT_EDGE; } } undirectedEdges.add(edge); } } for (Edge nextUndirected : undirectedEdges) { Node node1 = nextUndirected.getNode1(), node2 = nextUndirected.getNode2(); graph.removeEdge(nextUndirected); graph.addUndirectedEdge(node1, node2); } }
private Graph condense(Graph mimStructure, Graph mimbuildStructure) { // System.out.println("Uncondensed: " + mimbuildStructure); Map<Node, Node> substitutions = new HashMap<Node, Node>(); for (Node node : mimbuildStructure.getNodes()) { for (Node _node : mimStructure.getNodes()) { if (node.getName().startsWith(_node.getName())) { substitutions.put(node, _node); break; } substitutions.put(node, node); } } HashSet<Node> nodes = new HashSet<Node>(substitutions.values()); Graph graph = new EdgeListGraph(new ArrayList<Node>(nodes)); for (Edge edge : mimbuildStructure.getEdges()) { Node node1 = substitutions.get(edge.getNode1()); Node node2 = substitutions.get(edge.getNode2()); if (node1 == node2) continue; if (graph.isAdjacentTo(node1, node2)) continue; graph.addEdge(new Edge(node1, node2, edge.getEndpoint1(), edge.getEndpoint2())); } // System.out.println("Condensed: " + graph); return graph; }
public void testConcurrentModification() { Graph graph = graphTest.generateGraph(); Vertex a = graph.addVertex(null); graph.addVertex(null); graph.addVertex(null); if (graph.getFeatures().supportsVertexIteration) { for (Vertex vertex : graph.getVertices()) { graph.addEdge(null, vertex, a, convertId(graph, "x")); graph.addEdge(null, vertex, a, convertId(graph, "y")); } for (Vertex vertex : graph.getVertices()) { assertEquals(BaseTest.count(vertex.getEdges(Direction.OUT)), 2); for (Edge edge : vertex.getEdges(Direction.OUT)) { graph.removeEdge(edge); } } for (Vertex vertex : graph.getVertices()) { graph.removeVertex(vertex); } } else if (graph.getFeatures().supportsEdgeIteration) { for (int i = 0; i < 10; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "test")); } for (Edge edge : graph.getEdges()) { graph.removeEdge(edge); } } graph.shutdown(); }
public void testSemanticallyCorrectIterables() { Graph graph = graphTest.generateGraph(); for (int i = 0; i < 15; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); } if (graph.getFeatures().supportsVertexIteration) { Iterable<Vertex> vertices = graph.getVertices(); assertEquals(count(vertices), 30); assertEquals(count(vertices), 30); Iterator<Vertex> itty = vertices.iterator(); int counter = 0; while (itty.hasNext()) { assertTrue(itty.hasNext()); itty.next(); counter++; } assertEquals(counter, 30); } if (graph.getFeatures().supportsEdgeIteration) { Iterable<Edge> edges = graph.getEdges(); assertEquals(count(edges), 15); assertEquals(count(edges), 15); Iterator<Edge> itty = edges.iterator(); int counter = 0; while (itty.hasNext()) { assertTrue(itty.hasNext()); itty.next(); counter++; } assertEquals(counter, 15); } graph.shutdown(); }
/** * Forward equivalence search. * * @param graph The graph in the state prior to the forward equivalence search. */ private void fes(Graph graph, List<Node> nodes) { TetradLogger.getInstance().log("info", "** FORWARD EQUIVALENCE SEARCH"); lookupArrows = new HashMap<OrderedPair, Set<Arrow>>(); initializeArrowsForward(nodes); while (!sortedArrows.isEmpty()) { Arrow arrow = sortedArrows.first(); sortedArrows.remove(arrow); Node x = arrow.getX(); Node y = arrow.getY(); clearArrow(x, y); if (graph.isAdjacentTo(x, y)) { continue; } if (!validInsert(x, y, arrow.getHOrT(), arrow.getNaYX(), graph)) { continue; } List<Node> t = arrow.getHOrT(); double bump = arrow.getBump(); Set<Edge> edges = graph.getEdges(); insert(x, y, t, graph, bump); score += bump; rebuildPattern(graph); // Try to avoid duplicating scoring calls. First clear out all of the edges that need to be // changed, // then change them, checking to see if they're already been changed. I know, roundabout, but // there's // a performance boost. for (Edge edge : graph.getEdges()) { if (!edges.contains(edge)) { reevaluateForward(graph, nodes, edge.getNode1(), edge.getNode2()); } } storeGraph(graph); } }
private void populateLists(List<Vertex> verticies, List<Edge> edges) { for (Vertex v : graph.getVertices()) { verticies.add(v); } for (Edge e : graph.getEdges()) { edges.add(e); } }
public void testGraphDataPersists() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().isPersistent) { Vertex v = graph.addVertex(null); Vertex u = graph.addVertex(null); if (graph.getFeatures().supportsVertexProperties) { v.setProperty("name", "marko"); u.setProperty("name", "pavel"); } Edge e = graph.addEdge(null, v, u, convertId(graph, "collaborator")); if (graph.getFeatures().supportsEdgeProperties) e.setProperty("location", "internet"); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices()), 2); } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges()), 1); } graph.shutdown(); this.stopWatch(); graph = graphTest.generateGraph(); printPerformance(graph.toString(), 1, "graph loaded", this.stopWatch()); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices()), 2); if (graph.getFeatures().supportsVertexProperties) { for (Vertex vertex : graph.getVertices()) { assertTrue( vertex.getProperty("name").equals("marko") || vertex.getProperty("name").equals("pavel")); } } } if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges()), 1); for (Edge edge : graph.getEdges()) { assertEquals(edge.getLabel(), convertId(graph, "collaborator")); if (graph.getFeatures().supportsEdgeProperties) assertEquals(edge.getProperty("location"), "internet"); } } } graph.shutdown(); }
public void testNoConcurrentModificationException() { Graph graph = graphTest.generateGraph(); for (int i = 0; i < 25; i++) { graph.addEdge(null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "test")); } if (graph.getFeatures().supportsVertexIteration) assertEquals(BaseTest.count(graph.getVertices()), 50); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(BaseTest.count(graph.getEdges()), 25); for (final Edge edge : graph.getEdges()) { graph.removeEdge(edge); } assertEquals(BaseTest.count(graph.getEdges()), 0); } if (graph.getFeatures().supportsVertexIteration) assertEquals(BaseTest.count(graph.getVertices()), 50); graph.shutdown(); }
private boolean isUndirected(Graph graph, Node x, Node y) { List<Edge> edges = graph.getEdges(x, y); if (edges.size() == 1) { Edge edge = graph.getEdge(x, y); return Edges.isUndirectedEdge(edge); } return false; }
private void calcStats() { // Graph resultGraph = getAlgorithmRunner().getResultGraph(); IGesRunner runner = (IGesRunner) getAlgorithmRunner(); if (runner.getTopGraphs().isEmpty()) { throw new IllegalArgumentException( "No patterns were recorded. Please adjust the number of " + "patterns to store."); } Graph resultGraph = runner.getTopGraphs().get(runner.getIndex()).getGraph(); if (getAlgorithmRunner().getDataModel() instanceof DataSet) { // resultGraph may be the output of a PC search. // Such graphs sometimes contain doubly directed edges. // /We converte such edges to directed edges here. // For the time being an orientation is arbitrarily selected. Set<Edge> allEdges = resultGraph.getEdges(); for (Edge edge : allEdges) { if (edge.getEndpoint1() == Endpoint.ARROW && edge.getEndpoint2() == Endpoint.ARROW) { // Option 1 orient it from node1 to node2 resultGraph.setEndpoint(edge.getNode1(), edge.getNode2(), Endpoint.ARROW); // Option 2 remove such edges: resultGraph.removeEdge(edge); } } Pattern pattern = new Pattern(resultGraph); PatternToDag ptd = new PatternToDag(pattern); Graph dag = ptd.patternToDagMeekRules(); DataSet dataSet = (DataSet) getAlgorithmRunner().getDataModel(); String report; if (dataSet.isContinuous()) { report = reportIfContinuous(dag, dataSet); } else if (dataSet.isDiscrete()) { report = reportIfDiscrete(dag, dataSet); } else { throw new IllegalArgumentException(""); } JScrollPane dagWorkbenchScroll = dagWorkbenchScroll(dag); modelStatsText.setLineWrap(true); modelStatsText.setWrapStyleWord(true); modelStatsText.setText(report); removeStatsTabs(); tabbedPane.addTab("DAG in pattern", dagWorkbenchScroll); tabbedPane.addTab("DAG Model Statistics", modelStatsText); } }
public void testEdgeIterator() { Graph graph = graphTest.getGraphInstance(); if (graphTest.supportsEdgeIteration) { List<String> ids = generateIds(3); Vertex v1 = graph.addVertex(convertId(ids.get(0))); Vertex v2 = graph.addVertex(convertId(ids.get(1))); Vertex v3 = graph.addVertex(convertId(ids.get(2))); Edge e1 = graph.addEdge(null, v1, v2, convertId("test")); Edge e2 = graph.addEdge(null, v2, v3, convertId("test")); Edge e3 = graph.addEdge(null, v3, v1, convertId("test")); if (graphTest.supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graphTest.supportsEdgeIteration) assertEquals(3, count(graph.getEdges())); Set<String> edgeIds = new HashSet<String>(); int count = 0; for (Edge e : graph.getEdges()) { count++; edgeIds.add(e.getId().toString()); assertEquals(convertId("test"), e.getLabel()); if (e.getId().toString().equals(e1.getId().toString())) { assertEquals(v1, e.getOutVertex()); assertEquals(v2, e.getInVertex()); } else if (e.getId().toString().equals(e2.getId().toString())) { assertEquals(v2, e.getOutVertex()); assertEquals(v3, e.getInVertex()); } else if (e.getId().toString().equals(e3.getId().toString())) { assertEquals(v3, e.getOutVertex()); assertEquals(v1, e.getInVertex()); } else { assertTrue(false); } // System.out.println(e); } assertEquals(3, count); assertEquals(3, edgeIds.size()); assertTrue(edgeIds.contains(e1.getId().toString())); assertTrue(edgeIds.contains(e2.getId().toString())); assertTrue(edgeIds.contains(e3.getId().toString())); } graph.shutdown(); }
public void testEdgeIterator() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsEdgeIteration) { Vertex v1 = graph.addVertex(convertId(graph, "1")); Vertex v2 = graph.addVertex(convertId(graph, "2")); Vertex v3 = graph.addVertex(convertId(graph, "3")); Edge e1 = graph.addEdge(null, v1, v2, convertId(graph, "test")); Edge e2 = graph.addEdge(null, v2, v3, convertId(graph, "test")); Edge e3 = graph.addEdge(null, v3, v1, convertId(graph, "test")); if (graph.getFeatures().supportsVertexIteration) assertEquals(3, count(graph.getVertices())); if (graph.getFeatures().supportsEdgeIteration) assertEquals(3, count(graph.getEdges())); Set<String> edgeIds = new HashSet<String>(); int count = 0; for (Edge e : graph.getEdges()) { count++; edgeIds.add(e.getId().toString()); assertEquals(convertId(graph, "test"), e.getLabel()); if (e.getId().toString().equals(e1.getId().toString())) { assertEquals(v1, e.getVertex(Direction.OUT)); assertEquals(v2, e.getVertex(Direction.IN)); } else if (e.getId().toString().equals(e2.getId().toString())) { assertEquals(v2, e.getVertex(Direction.OUT)); assertEquals(v3, e.getVertex(Direction.IN)); } else if (e.getId().toString().equals(e3.getId().toString())) { assertEquals(v3, e.getVertex(Direction.OUT)); assertEquals(v1, e.getVertex(Direction.IN)); } else { assertTrue(false); } // System.out.println(e); } assertEquals(3, count); assertEquals(3, edgeIds.size()); assertTrue(edgeIds.contains(e1.getId().toString())); assertTrue(edgeIds.contains(e2.getId().toString())); assertTrue(edgeIds.contains(e3.getId().toString())); } graph.shutdown(); }
public void testGettingVerticesAndEdgesWithKeyValue() { Graph graph = graphTest.generateGraph(); if (graph.getFeatures().supportsVertexProperties) { Vertex v1 = graph.addVertex(null); v1.setProperty("name", "marko"); v1.setProperty("location", "everywhere"); Vertex v2 = graph.addVertex(null); v2.setProperty("name", "stephen"); v2.setProperty("location", "everywhere"); if (graph.getFeatures().supportsVertexIteration) { assertEquals(count(graph.getVertices("location", "everywhere")), 2); assertEquals(count(graph.getVertices("name", "marko")), 1); assertEquals(count(graph.getVertices("name", "stephen")), 1); assertEquals(getOnlyElement(graph.getVertices("name", "marko")), v1); assertEquals(getOnlyElement(graph.getVertices("name", "stephen")), v2); } } if (graph.getFeatures().supportsEdgeProperties) { Edge e1 = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); e1.setProperty("name", "marko"); e1.setProperty("location", "everywhere"); Edge e2 = graph.addEdge( null, graph.addVertex(null), graph.addVertex(null), convertId(graph, "knows")); e2.setProperty("name", "stephen"); e2.setProperty("location", "everywhere"); if (graph.getFeatures().supportsEdgeIteration) { assertEquals(count(graph.getEdges("location", "everywhere")), 2); assertEquals(count(graph.getEdges("name", "marko")), 1); assertEquals(count(graph.getEdges("name", "stephen")), 1); assertEquals(graph.getEdges("name", "marko").iterator().next(), e1); assertEquals(graph.getEdges("name", "stephen").iterator().next(), e2); } } graph.shutdown(); }
/** Validate that each node has been colored and connected nodes have different coloring. */ private static <N, E> void validateColoring(Graph<N, E> graph) { for (GraphNode<N, E> node : graph.getNodes()) { assertNotNull(node.getAnnotation()); } for (GraphEdge<N, E> edge : graph.getEdges()) { Color c1 = edge.getNodeA().getAnnotation(); Color c2 = edge.getNodeB().getAnnotation(); assertNotNull(c1); assertNotNull(c2); assertThat(c1.equals(c2)).isFalse(); } }