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(); }
/** * 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()); }
@Nullable private Optional<Object> getDiskCachedObject(Path file, DAGNode<Component,Dependency> node) { if (file != null && Files.exists(file)) { logger.debug("reading object for {} from cache (key {})", node.getLabel().getSatisfaction(), key); Object obj = readCompressedObject(file, node.getLabel().getSatisfaction().getErasedType()); logger.debug("read object {} from key {}", obj, key); return Optional.fromNullable(obj); } else { return null; } }
/** * Check a graph for placeholder satisfactions. * * @param graph The graph to check. * @throws RecommenderConfigurationException if the graph has a placeholder satisfaction. */ public static void checkForPlaceholders(DAGNode<Component, Dependency> graph, Logger logger) throws RecommenderConfigurationException { Set<DAGNode<Component, Dependency>> placeholders = getPlaceholderNodes(graph); Satisfaction sat = null; for (DAGNode<Component,Dependency> node: placeholders) { Component csat = node.getLabel(); // special-case DAOs for non-checking if (DataAccessObject.class.isAssignableFrom(csat.getSatisfaction().getErasedType())) { logger.debug("found DAO placeholder {}", csat.getSatisfaction()); } else { // all other placeholders are bad if (sat == null) { sat = csat.getSatisfaction(); } logger.error("placeholder {} not removed", csat.getSatisfaction()); } } if (sat != null) { throw new RecommenderConfigurationException("placeholder " + sat + " not removed"); } }
Component csat = node.getLabel(); assert csat != null; Satisfaction sat = csat.getSatisfaction(); try { tgt = sat.visit(new Visitor(node, id));
Component label = node.getLabel(); if (label.getSatisfaction().hasInstance()) { logger.trace("node {} shareable because it has an instance", node); return true; Class<?> type = label.getSatisfaction().getErasedType(); logger.trace("node {} has satisfaction type {}", node, type); if (type.getAnnotation(Shareable.class) != null) { return label.getSatisfaction().visit(new AbstractSatisfactionVisitor<Boolean>() { @Override public Boolean visitDefault() {
@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); } }
/** * Get the component of a particular type, if one is already instantiated. This is useful to extract pre-built * models from serialized recommender engines, for example. * @param type The required component type. * @param <T> The required component type. * @return The component instance, or {@code null} if no instance can be retreived (either because no such * component is configured, or it is not yet instantiated). */ @Nullable public <T> T getComponent(Class<T> type) { DAGNode<Component, Dependency> node = GraphtUtils.findSatisfyingNode(graph, Qualifiers.matchDefault(), type); if (node == null) { return null; } Satisfaction sat = node.getLabel().getSatisfaction(); if (sat instanceof InstanceSatisfaction) { return type.cast(((InstanceSatisfaction) sat).getInstance()); } else { return null; } }
/** * 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); } }
logger.debug("instantiating object for {}", node.getLabel().getSatisfaction()); Object result = instantiator.instantiate(node); if (result == null) {
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; } } }
DAGNode<Component, Dependency> target = e.getTail(); Component csat = target.getLabel(); if (!satIsNull(csat.getSatisfaction())) { logger.debug("processing node {}", csat.getSatisfaction()); String id = dumper.process(target); gw.putEdge(EdgeBuilder.create(rid, id)
Component tcsat = targetNode.getLabel(); assert tcsat != null; Satisfaction tsat = tcsat.getSatisfaction(); Object val = tsat.visit(new AbstractSatisfactionVisitor<Object>(null) { @Override
Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node;
public DAGNode<Component, Dependency> processNode(@Nonnull DAGNode<Component, Dependency> node, @Nonnull DAGNode<Component, Dependency> original) { Component label = node.getLabel(); Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node; } Object obj = instantiator.apply(node); Satisfaction instanceSat; if (obj == null) { instanceSat = Satisfactions.nullOfType(satisfaction.getErasedType()); } else { instanceSat = Satisfactions.instance(obj); } Component newLabel = Component.create(instanceSat, label.getCachePolicy()); // build new node with replacement label DAGNodeBuilder<Component,Dependency> bld = DAGNode.newBuilder(newLabel); // retain all non-transient edges for (DAGEdge<Component, Dependency> edge: node.getOutgoingEdges()) { if (!GraphtUtils.edgeIsTransient(edge)) { bld.addEdge(edge.getTail(), edge.getLabel()); } } return bld.build(); } }
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(); }
/** * Get an initial injection context. * @return The context from the initial injection. */ public static InjectionContext initialContext() { return InjectionContext.singleton(ROOT_SATISFACTION.getSatisfaction()); }
/** * 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()); }
@Nullable private Optional<Object> getDiskCachedObject(Path file, DAGNode<Component,Dependency> node) { if (file != null && Files.exists(file)) { logger.debug("reading object for {} from cache (key {})", node.getLabel().getSatisfaction(), key); Object obj = readCompressedObject(file, node.getLabel().getSatisfaction().getErasedType()); logger.debug("read object {} from key {}", obj, key); return Optional.fromNullable(obj); } else { return null; } }
@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 = Lists.transform(GraphtUtils.DEP_EDGE_ORDER.sortedCopy(node.getOutgoingEdges()), DAGEdge.<Component,Dependency>extractTail()); description.putList("dependencies", edges, INSTANCE); } }