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; } } }
Component label = node.getLabel(); if (label.getSatisfaction().hasInstance()) { logger.trace("node {} shareable because it has an instance", node); return true; if (label.getCachePolicy() == CachePolicy.NEW_INSTANCE) { logger.trace("node {} not shareable because it has a new-instance cache policy", node); return false; 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() {
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(); }
public Component makeSatisfaction() { return Component.create(satisfaction, policy); }
if (current.node.getLabel().equals(ROOT_SATISFACTION)) { Pair<DAGNode<Component, Dependency>, Dependency> rootNode = resolveFully(desire, current.context, deferralQueue); Satisfaction sat = parent.getLabel().getSatisfaction(); for (Desire d: sat.getDependencies()) { logger.debug("Attempting to resolve deferred dependency {} of {}", d, sat);
/** * Create a new Component wrapping the given satisfaction and cache policy. The injector is * responsible for using the satisfaction to implement this component consistent with its * cache policy. * * @param satisfaction The satisfaction to wrap * @param policy The policy used with this satisfaction * @throws NullPointerException the satisfaction or policy is null */ public static Component create(Satisfaction satisfaction, CachePolicy policy) { return new Component(satisfaction, policy); }
/** * 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); } }
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(); } }
@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; } }
Component label = node.getLabel(); if (label.getSatisfaction().hasInstance()) { logger.trace("node {} shareable because it has an instance", node); return true; if (label.getCachePolicy() == CachePolicy.NEW_INSTANCE) { logger.trace("node {} not shareable because it has a new-instance cache policy", node); return false; 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() {
Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node; instanceSat = Satisfactions.instance(obj); Component newLabel = Component.create(instanceSat, label.getCachePolicy());
/** * 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"); } }
@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); } }
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; } } }
Component csat = node.getLabel(); assert csat != null; Satisfaction sat = csat.getSatisfaction(); try { tgt = sat.visit(new Visitor(node, id));
Map<Desire, Instantiator> depMap = makeDependencyMap(node, backEdges); Instantiator raw = node.getLabel().getSatisfaction().makeInstantiator(depMap); CachePolicy policy = node.getLabel().getCachePolicy(); if (policy.equals(CachePolicy.NO_PREFERENCE)) { policy = defaultCachePolicy;
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(); } }
/** * 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; } }
Satisfaction satisfaction = label.getSatisfaction(); if (satisfaction.hasInstance()) { return node; instanceSat = Satisfactions.instance(obj); Component newLabel = Component.create(instanceSat, label.getCachePolicy());