/** * Returns an empty graph (immutable) initialized with all properties queryable from {@code graph}. * * @param graph The graph to use as template for the created graph * @param <N> The class of the nodes * @return an empty graph * @see GraphBuilder#from(Graph) */ public static <N> ImmutableGraph<N> emptyGraph(Graph<N> graph) { return ImmutableGraph.copyOf(GraphBuilder.from(graph).build()); }
/** Returns an immutable copy of {@code graph}. */ public static <N> ImmutableGraph<N> copyOf(Graph<N> graph) { return (graph instanceof ImmutableGraph) ? (ImmutableGraph<N>) graph : new ImmutableGraph<N>( new ConfigurableValueGraph<N, Presence>( GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size())); }
/** * Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph * that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges} * from {@code graph} for which both nodes are contained by {@code nodes}. * * @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph */ public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) { MutableGraph<N> subgraph = (nodes instanceof Collection) ? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build() : GraphBuilder.from(graph).build(); for (N node : nodes) { subgraph.addNode(node); } for (N node : subgraph.nodes()) { for (N successorNode : graph.successors(node)) { if (subgraph.nodes().contains(successorNode)) { subgraph.putEdge(node, successorNode); } } } return subgraph; }
private MutableGraph<EntityDescriptor> resolveDependencyGraph(Graph<EntityDescriptor> dependencyGraph, Set<EntityDescriptor> resolvedEntities) { final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.from(dependencyGraph).build(); Graphs.merge(mutableGraph, dependencyGraph); for (EntityDescriptor entityDescriptor : dependencyGraph.nodes()) { LOG.debug("Resolving entity {}", entityDescriptor); if (resolvedEntities.contains(entityDescriptor)) { LOG.debug("Entity {} already resolved, skipping.", entityDescriptor); continue; } final EntityFacade<?> facade = entityFacades.getOrDefault(entityDescriptor.type(), UnsupportedEntityFacade.INSTANCE); final Graph<EntityDescriptor> graph = facade.resolveNativeEntity(entityDescriptor); LOG.trace("Dependencies of entity {}: {}", entityDescriptor, graph); Graphs.merge(mutableGraph, graph); LOG.trace("New dependency graph: {}", mutableGraph); resolvedEntities.add(entityDescriptor); final Graph<EntityDescriptor> result = resolveDependencyGraph(mutableGraph, resolvedEntities); Graphs.merge(mutableGraph, result); } return mutableGraph; }
/** * Returns an immutable graph, containing only the specified node. * * @param graph The graph to use as template for the created graph * @param node The single node in the returned graph * @param <N> The class of the nodes * @return an immutable graph with a single node * @see GraphBuilder#from(Graph) */ public static <N> ImmutableGraph<N> singletonGraph(Graph<N> graph, N node) { final MutableGraph<N> mutableGraph = GraphBuilder.from(graph).build(); mutableGraph.addNode(node); return ImmutableGraph.copyOf(mutableGraph); }
/** Returns an immutable copy of {@code graph}. */ public static <N> ImmutableGraph<N> copyOf(Graph<N> graph) { return (graph instanceof ImmutableGraph) ? (ImmutableGraph<N>) graph : new ImmutableGraph<N>( new ConfigurableValueGraph<N, Presence>( GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size())); }
/** Creates a mutable copy of {@code graph} with the same nodes and edges. */ public static <N> MutableGraph<N> copyOf(Graph<N> graph) { MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdge(edge.nodeU(), edge.nodeV()); } return copy; }
/** Returns an immutable copy of {@code graph}. */ public static <N> ImmutableGraph<N> copyOf(Graph<N> graph) { return (graph instanceof ImmutableGraph) ? (ImmutableGraph<N>) graph : new ImmutableGraph<N>( new ConfigurableValueGraph<N, Presence>( GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size())); }
/** * Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph * that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges} * from {@code graph} for which both nodes are contained by {@code nodes}. * * @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph */ public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) { MutableGraph<N> subgraph = (nodes instanceof Collection) ? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build() : GraphBuilder.from(graph).build(); for (N node : nodes) { subgraph.addNode(node); } for (N node : subgraph.nodes()) { for (N successorNode : graph.successors(node)) { if (subgraph.nodes().contains(successorNode)) { subgraph.putEdge(node, successorNode); } } } return subgraph; }
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
/** * Returns the subgraph of {@code graph} induced by {@code nodes}. This subgraph is a new graph * that contains all of the nodes in {@code nodes}, and all of the {@link Graph#edges() edges} * from {@code graph} for which both nodes are contained by {@code nodes}. * * @throws IllegalArgumentException if any element in {@code nodes} is not a node in the graph */ public static <N> MutableGraph<N> inducedSubgraph(Graph<N> graph, Iterable<? extends N> nodes) { MutableGraph<N> subgraph = (nodes instanceof Collection) ? GraphBuilder.from(graph).expectedNodeCount(((Collection) nodes).size()).build() : GraphBuilder.from(graph).build(); for (N node : nodes) { subgraph.addNode(node); } for (N node : subgraph.nodes()) { for (N successorNode : graph.successors(node)) { if (subgraph.nodes().contains(successorNode)) { subgraph.putEdge(node, successorNode); } } } return subgraph; }
@Test public void equivalent_propertiesDiffer() { graph.putEdge(N1, N2); MutableGraph<Integer> g2 = GraphBuilder.from(graph).allowsSelfLoops(!graph.allowsSelfLoops()).build(); g2.putEdge(N1, N2); assertThat(graph).isEqualTo(g2); }
/** Creates a mutable copy of {@code graph} with the same nodes and edges. */ public static <N> MutableGraph<N> copyOf(Graph<N> graph) { MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdge(edge.nodeU(), edge.nodeV()); } return copy; }
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
/** Creates a mutable copy of {@code graph} with the same nodes, edges, and edge values. */ public static <N, V> MutableValueGraph<N, V> copyOf(ValueGraph<N, V> graph) { MutableValueGraph<N, V> copy = ValueGraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdgeValue( edge.nodeU(), edge.nodeV(), graph.edgeValueOrDefault(edge.nodeU(), edge.nodeV(), null)); } return copy; }
@Test public void equivalent_edgeAddOrdersDiffer() { GraphBuilder<Integer> builder = GraphBuilder.from(graph); MutableGraph<Integer> g1 = builder.build(); MutableGraph<Integer> g2 = builder.build(); // for g1, add 1->2 first, then 3->1 g1.putEdge(N1, N2); g1.putEdge(N3, N1); // for g2, add 3->1 first, then 1->2 g2.putEdge(N3, N1); g2.putEdge(N1, N2); assertThat(g1).isEqualTo(g2); }
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
/** Returns an immutable copy of {@code graph}. */ public static <N> ImmutableGraph<N> copyOf(Graph<N> graph) { return (graph instanceof ImmutableGraph) ? (ImmutableGraph<N>) graph : new ImmutableGraph<N>( new ConfigurableValueGraph<N, Presence>( GraphBuilder.from(graph), getNodeConnections(graph), graph.edges().size())); }
/** Creates a mutable copy of {@code graph} with the same nodes and edges. */ public static <N> MutableGraph<N> copyOf(Graph<N> graph) { MutableGraph<N> copy = GraphBuilder.from(graph).expectedNodeCount(graph.nodes().size()).build(); for (N node : graph.nodes()) { copy.addNode(node); } for (EndpointPair<N> edge : graph.edges()) { copy.putEdge(edge.nodeU(), edge.nodeV()); } return copy; }
@Test public void equivalent_propertiesDiffer() { graph.putEdge(N1, N2); MutableGraph<Integer> g2 = GraphBuilder.from(graph).allowsSelfLoops(!graph.allowsSelfLoops()).build(); g2.putEdge(N1, N2); assertThat(graph).isEqualTo(g2); }