public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); if (!label.getSatisfaction().hasInstance()) { Satisfaction instanceSat = Satisfactions.nullOfType(label.getSatisfaction().getErasedType()); Component newLbl = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLbl); // retain all non-transient edges for (DAGEdge<Component,Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } DAGNode<Component,Dependency> repl = bld.build(); logger.debug("simulating instantiation of {}", node); return repl; } else { return node; } } }
logger.debug("graph has {} nodes", graph.getReachableNodes().size()); logger.debug("simulating instantiation"); RecommenderInstantiator instantiator = RecommenderInstantiator.create(graph); DAGNode<Component, Dependency> unshared = instantiator.simulate(); logger.debug("unshared graph has {} nodes", unshared.getReachableNodes().size()); try (BufferedWriter bw = new BufferedWriter(graphvizFile); GraphWriter gw = new GraphWriter(bw)) { GraphDumper dumper = new GraphDumper(graph, unshared.getReachableNodes(), gw); logger.debug("writing root node"); String rid = dumper.setRoot(graph); for (DAGEdge<Component, Dependency> e: graph.getOutgoingEdges()) { DAGNode<Component, Dependency> target = e.getTail(); Component csat = target.getLabel(); if (!satIsNull(csat.getSatisfaction())) { logger.debug("processing node {}", csat.getSatisfaction());
@Nullable public <T> T tryGetInstance(Class<T> type) throws InjectionException { Desire d = Desires.create(null, type, true); DAGEdge<Component, Dependency> e = graph.getOutgoingEdgeWithLabel(l -> l.hasInitialDesire(d)); if (e != null) { return type.cast(instantiator.instantiate(e.getTail())); } else { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node != null) { return type.cast(instantiator.instantiate(node)); } else { return null; } } }
/** * Get the placeholder nodes from a graph. * * @param graph The graph. * @return The set of nodes that have placeholder satisfactions. */ public static Set<DAGNode<Component, Dependency>> getPlaceholderNodes(DAGNode<Component,Dependency> graph) { return graph.getReachableNodes() .stream() .filter(n -> n.getLabel().getSatisfaction() instanceof PlaceholderSatisfaction) .collect(Collectors.toSet()); }
@Override public void describe(DAGNode<Component, Dependency> node, DescriptionWriter description) { node.getLabel().getSatisfaction().visit(new LabelDescriptionVisitor(description)); description.putField("cachePolicy", node.getLabel().getCachePolicy().name()); List<DAGNode<Component, Dependency>> edges = node.getOutgoingEdges() .stream() .sorted(GraphtUtils.DEP_EDGE_ORDER) .map(DAGEdge::getTail) .collect(Collectors.toList()); description.putList("dependencies", edges, INSTANCE); } }
LinkedHashSet<DAGNode<Component, Dependency>> shared = Sets.newLinkedHashSet(); graph.getSortedNodes() .stream() .filter(GraphtUtils::isShareable) for (DAGEdge<Component,Dependency> edge: node.getOutgoingEdges()) { if (!edgeIsTransient(edge)) { boolean es = shared.contains(edge.getTail());
List<DAGNode<V, E>> sorted = graph.getSortedNodes(); Pair.of(node.getLabel(), node.getAdjacentNodes()); assert !nodeTable.containsKey(key); nodeTable.put(key, node); V sat = toMerge.getLabel(); FluentIterable.from(toMerge.getOutgoingEdges()) .transform(DAGEdge.<V,E>extractTail()) .transform(Functions.forMap(mergedMap)) if (newNode == null) { DAGNodeBuilder<V,E> bld = DAGNode.newBuilder(); logger.debug("Adding new node to merged graph for satisfaction: {}", sat); for (DAGEdge<V, E> edge: toMerge.getOutgoingEdges()) { logger.debug("Node already in merged graph for satisfaction: {}", toMerge.getLabel()); pool.addAll(newRoot.getReachableNodes());
private Visitor(DAGNode<Component, Dependency> nd, String id) { currentNode = nd; nodeId = id; if (currentNode == null) { throw new IllegalStateException("dumper not running"); } Component csat = currentNode.getLabel(); assert csat != null; satisfaction = csat.getSatisfaction(); }
DAGNode<Component, Dependency> parent = current.node; assert parent.getOutgoingEdges().isEmpty(); if (current.node.getLabel().equals(ROOT_SATISFACTION)) { Pair<DAGNode<Component, Dependency>, Dependency> rootNode = resolveFully(desire, current.context, deferralQueue); graph = DAGNode.copyBuilder(graph) .addEdge(mergePool.merge(rootNode.getLeft()), rootNode.getRight()) .build(); } else if (graph.getReachableNodes().contains(parent)) { Satisfaction sat = parent.getLabel().getSatisfaction(); for (Desire d: sat.getDependencies()) { logger.debug("Attempting to resolve deferred dependency {} of {}", d, sat); if (merged.getReachableNodes().contains(parent)) { DAGNode.copyBuilder(parent) .addEdge(merged, result.getRight()) .build();
} else if (getReachableNodes().contains(node)) { DAGNodeBuilder<V,E> bld = newBuilder(label); for (DAGEdge<V,E> edge: outgoingEdges) { DAGNode<V,E> newTail = edge.getTail().replaceNode(node, replacement, memory); bld.addEdge(newTail, edge.getLabel());
if (!graph.getLabel().getSatisfaction().getErasedType().equals(Void.TYPE)) { throw new IllegalArgumentException("only full dependency graphs can be rewritten"); logger.debug("rewriting graph with {} nodes", graph.getReachableNodes().size()); InjectionContext.singleton(graph.getLabel().getSatisfaction()), replacementSubtrees); graph.transformEdges(Functions.forMap(replacementSubtrees, null));
GraphDumper(DAGNode<Component, Dependency> g, Set<DAGNode<Component, Dependency>> unshared, GraphWriter gw) { writer = gw; graph = g; unsharedNodes = Sets.newHashSet(unshared); unsharedNodes.retainAll(g.getReachableNodes()); logger.debug("{} shared nodes", unsharedNodes.size()); nodeIds = new HashMap<>(); nodeTargets = new HashMap<>(); edgeQueue = new LinkedList<>(); }
/** * Find a node with a satisfaction for a specified type. Does a breadth-first * search to find the closest matching one. * * @param type The type to look for. * @return A node whose satisfaction is compatible with {@code type}. */ @Nullable public static DAGNode<Component,Dependency> findSatisfyingNode(DAGNode<Component,Dependency> graph, final QualifierMatcher qmatch, final Class<?> type) { Optional<DAGEdge<Component, Dependency>> edge = graph.breadthFirstEdges() .filter(e -> type.isAssignableFrom(e.getTail() .getLabel() .getSatisfaction() .getErasedType())) .filter(e -> qmatch.apply(e.getLabel() .getInitialDesire() .getInjectionPoint() .getQualifier())) .findFirst(); return edge.map(DAGEdge::getTail) .orElse(null); } }
while (!work.isEmpty()) { DAGNode<V, E> node = work.remove(); for (DAGEdge<V, E> e : node.getOutgoingEdges()) {
node = DAGNode.singleton(result.makeSatisfaction()); deferQueue.add(new Deferral(node, newContext)); } else { DAGNodeBuilder<Component,Dependency> nodeBuilder = DAGNode.newBuilder(); nodeBuilder.setLabel(result.makeSatisfaction()); for (Desire d: result.satisfaction.getDependencies()) {
for (DAGEdge<V,E> edge: outgoingEdges) { DAGNode<V,E> tail = edge.getTail(); DAGNode<V,E> transformedTail = tail.transformEdges(function); DAGEdge<V,E> toQuery = edge; if (transformedTail != tail) { builder = newBuilder(label); for (DAGEdge<V,E> done: intact) { builder.addEdge(done.getTail(), done.getLabel());
newGraph = newGraph.replaceNode(node, repl, memory);
.setShared(!unsharedNodes.contains(currentNode)) .setIsProvider(pid != null); List<DAGEdge<Component, Dependency>> edges = Lists.newArrayList(currentNode.getOutgoingEdges()); Collections.sort(edges, GraphtUtils.DEP_EDGE_ORDER); for (DAGEdge<Component, Dependency> e: edges) { if (q != null && q.annotationType().getAnnotation(Parameter.class) != null) { logger.debug("dumping parameter {}", q); Component tcsat = targetNode.getLabel(); assert tcsat != null; Satisfaction tsat = tcsat.getSatisfaction();
@Nonnull @Override public Object apply(@Nullable DAGNode<Component, Dependency> input) { Preconditions.checkNotNull(input, "input node"); try { return instantiate(input); } catch (InjectionException e) { throw new RecommenderBuildException("cannot instantiate " + input.getLabel(), e); } }
/** * Get the placeholder nodes from a graph. * * @param graph The graph. * @return The set of nodes that have placeholder satisfactions. */ public static Set<DAGNode<Component, Dependency>> getPlaceholderNodes(DAGNode<Component,Dependency> graph) { return graph.getReachableNodes() .stream() .filter(n -> n.getLabel().getSatisfaction() instanceof PlaceholderSatisfaction) .collect(Collectors.toSet()); }