private ImmutableGraph<Entity> buildEntityGraph(Entity rootEntity, Set<Entity> entities, Map<String, ValueReference> parameters) { final Map<EntityDescriptor, Entity> entityDescriptorMap = entities.stream() .collect(Collectors.toMap(Entity::toEntityDescriptor, Function.identity())); final MutableGraph<Entity> dependencyGraph = GraphBuilder.directed() .allowsSelfLoops(false) .expectedNodeCount(entities.size()) .build(); for (Map.Entry<EntityDescriptor, Entity> entry : entityDescriptorMap.entrySet()) { final EntityDescriptor entityDescriptor = entry.getKey(); final Entity entity = entry.getValue(); final EntityFacade<?> facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE); final Graph<Entity> entityGraph = facade.resolveForInstallation(entity, parameters, entityDescriptorMap); LOG.trace("Dependencies of entity {}: {}", entityDescriptor, entityGraph); dependencyGraph.putEdge(rootEntity, entity); Graphs.merge(dependencyGraph, entityGraph); LOG.trace("New dependency graph: {}", dependencyGraph); } final Set<Entity> unexpectedEntities = dependencyGraph.nodes().stream() .filter(entity -> !rootEntity.equals(entity)) .filter(entity -> !entities.contains(entity)) .collect(Collectors.toSet()); if (!unexpectedEntities.isEmpty()) { throw new UnexpectedEntitiesException(unexpectedEntities); } return ImmutableGraph.copyOf(dependencyGraph); }
/** 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 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 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; }
/** 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; }
/** 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; }
/** * 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; }
public GraphTypeMap(final int expectedSize) { typeGraph = GraphBuilder.directed().allowsSelfLoops(false) .expectedNodeCount(expectedSize).build(); handlers = new HashMap<>(expectedSize); }
public GraphTypeMap(final int expectedSize) { typeGraph = GraphBuilder.directed().allowsSelfLoops(false) .expectedNodeCount(expectedSize).build(); handlers = new HashMap<>(expectedSize); }
/** * 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; }
/** Returns an empty {@link MutableCTree} with the properties of this {@link TreeBuilder}. */ // TODO(jrtom): decide how we're going to handle different implementations. // For the graph stuff, we don't really need different implementations, but // for trees, maybe we do; at least for binary trees vs. trees with no restrictions on outgoing edges... public <N1 extends N> MutableCTree<N1> build() { GraphBuilder<Object> graphBuilder = GraphBuilder.directed().allowsSelfLoops(false); if (expectedNodeCount.isPresent()) { graphBuilder = graphBuilder.expectedNodeCount(expectedNodeCount.get()); } MutableGraph<N1> delegate = graphBuilder.nodeOrder(nodeOrder).build(); @SuppressWarnings("unchecked") Optional<N1> rootCast = (Optional<N1>) root; return new DelegateCTree<N1>(delegate, rootCast); }
throws IOException { MutableGraph<Schema> schemaDeps = GraphBuilder.directed().allowsSelfLoops(false) .expectedNodeCount(4).build(); Map<Schema, String> idlRepresentation = new HashMap<>(4); final Set<Schema> toDeclare = new HashSet<>(4);
Preconditions.checkArgument( g.nodes().containsAll(nodes), "Input graph must contain all specified nodes"); MutableGraph<N> newGraph = GraphBuilder.from(g).expectedNodeCount(nodes.size()).build();
.expectedNodeCount(candidates.size()) .build(); Map<String, PluginDescription> candidateMap = Maps.uniqueIndex(candidates,
/** 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; }
/** 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 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 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; }
public SampleGraph(final Method m, final SampleNode node) { int nrNodes = node.getNrNodes(); vertexMap = MultimapBuilder.hashKeys(nrNodes).hashSetValues(1).build(); aggregates = new THashMap<>(nrNodes); sampleTree = GraphBuilder.directed() .nodeOrder(ElementOrder.unordered()) .allowsSelfLoops(false) .expectedNodeCount(nrNodes) .build(); aggGraph = GraphBuilder.directed() .nodeOrder(ElementOrder.unordered()) .allowsSelfLoops(false) .expectedNodeCount(nrNodes) .build(); rootVertex = tree2Graph(m, node); }