public Set<EntityDescriptor> resolveEntities(Collection<EntityDescriptor> unresolvedEntities) { final MutableGraph<EntityDescriptor> dependencyGraph = GraphBuilder.directed() .allowsSelfLoops(false) .nodeOrder(ElementOrder.insertion()) .build(); unresolvedEntities.forEach(dependencyGraph::addNode); final HashSet<EntityDescriptor> resolvedEntities = new HashSet<>(); final MutableGraph<EntityDescriptor> finalDependencyGraph = resolveDependencyGraph(dependencyGraph, resolvedEntities); LOG.debug("Final dependency graph: {}", finalDependencyGraph); return finalDependencyGraph.nodes(); }
/** * 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; }
/** * Returns a {@link GraphBuilder} initialized with all properties queryable from {@code graph}. * * <p>The "queryable" properties are those that are exposed through the {@link Graph} interface, * such as {@link Graph#isDirected()}. Other properties, such as {@link #expectedNodeCount(int)}, * are not set in the new builder. */ public static <N> GraphBuilder<N> from(Graph<N> graph) { return new GraphBuilder<N>(graph.isDirected()) .allowsSelfLoops(graph.allowsSelfLoops()) .nodeOrder(graph.nodeOrder()); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(true).build(); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.undirected().allowsSelfLoops(true).build(); }
/** * Create the dependency graph of a native entity described by the given entity descriptor. * * @param entityDescriptor the descriptor of the native entity to resolve dependencies for * @return A directed graph of the native entity with entity descriptors as nodes. * @see Graph */ default Graph<EntityDescriptor> resolveNativeEntity(EntityDescriptor entityDescriptor) { final MutableGraph<EntityDescriptor> mutableGraph = GraphBuilder.directed().build(); mutableGraph.addNode(entityDescriptor); return ImmutableGraph.copyOf(mutableGraph); }
MutableGraph<N> transitiveClosure = GraphBuilder.from(graph).allowsSelfLoops(true).build();
/** * 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()); }
private static MutableGraph<Integer> createGraph(EdgeType edgeType) { switch (edgeType) { case UNDIRECTED: return GraphBuilder.undirected().allowsSelfLoops(true).build(); case DIRECTED: return GraphBuilder.directed().allowsSelfLoops(true).build(); default: throw new IllegalStateException("Unexpected edge type: " + edgeType); } }
@Test public void customComparable() { MutableGraph<ComparableSubClass> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.<ComparableSubClass>natural()).build(); ComparableSubClass node2 = new ComparableSubClass(2); ComparableSubClass node4 = new ComparableSubClass(4); ComparableSubClass node6 = new ComparableSubClass(6); ComparableSubClass node8 = new ComparableSubClass(8); graph.addNode(node4); graph.addNode(node2); graph.addNode(node6); graph.addNode(node8); assertThat(graph.nodeOrder().comparator()).isEqualTo(Ordering.natural()); assertThat(graph.nodes()).containsExactly(node2, node4, node6, node8).inOrder(); }
MutableGraph<Integer> graph = graphBuilder.allowsSelfLoops(true).build();
/** * Returns a graph in which each pair of nodes is connected by an undirected edge with the * probability specified by the constructor. */ public Graph<N> get() { MutableGraph<N> graph = GraphBuilder.undirected().expectedNodeCount(nodeCount).build(); for (int i = 0; i < nodeCount; i++) { graph.addNode(nodeSupplier.get()); } List<N> list = new ArrayList<N>(graph.nodes()); for (int i = 0; i < nodeCount - 1; i++) { N v_i = list.get(i); for (int j = i + 1; j < nodeCount; j++) { N v_j = list.get(j); if (random.nextDouble() < edgeConnectionProbability) { graph.putEdge(v_i, v_j); } } } return graph; }
@Test public void transpose_undirectedGraph() { MutableGraph<Integer> undirectedGraph = GraphBuilder.undirected().build(); undirectedGraph.putEdge(N1, N2); assertThat(transpose(undirectedGraph)).isSameAs(undirectedGraph); }
/** 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())); }
@Test public void directedGraph() { testGraphMutation(GraphBuilder.directed()); }
/** Returns a {@link GraphBuilder} for building undirected graphs. */ public static GraphBuilder<Object> undirected() { return new GraphBuilder<>(false); }
@Override public MutableGraph<Integer> createGraph() { return GraphBuilder.directed().allowsSelfLoops(false).build(); }
@Test public void nodeOrder_natural() { MutableGraph<Integer> graph = GraphBuilder.directed().nodeOrder(ElementOrder.<Integer>natural()).build(); addNodes(graph); assertThat(graph.nodeOrder()).isEqualTo(ElementOrder.sorted(Ordering.<Integer>natural())); assertThat(graph.nodes()).containsExactly(1, 3, 4).inOrder(); }
/** * Returns an immutable directed graph, containing only the specified node. * * @param node The single node in the returned graph * @param <N> The class of the nodes * @return an immutable directed graph with a single node */ public static <N> ImmutableGraph<N> singletonDirectedGraph(N node) { final MutableGraph<N> graph = GraphBuilder.directed().build(); graph.addNode(node); return ImmutableGraph.copyOf(graph); }