org.lenskit.inject.GraphtUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lenskit-core Show documentation
Show all versions of lenskit-core Show documentation
The core of LensKit, providing basic implementations and algorithm support.
/*
* LensKit, an open source recommender systems toolkit.
* Copyright 2010-2014 LensKit Contributors. See CONTRIBUTORS.md.
* Work on LensKit has been funded by the National Science Foundation under
* grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.lenskit.inject;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import org.grouplens.grapht.CachePolicy;
import org.grouplens.grapht.Component;
import org.grouplens.grapht.Dependency;
import org.grouplens.grapht.graph.DAGEdge;
import org.grouplens.grapht.graph.DAGNode;
import org.grouplens.grapht.reflect.*;
import org.grouplens.grapht.reflect.internal.*;
import org.lenskit.RecommenderConfigurationException;
import org.lenskit.data.dao.DataAccessObject;
import org.slf4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Provider;
import javax.inject.Singleton;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* Helper utilities for Grapht integration.
*
* @author GroupLens Research
* @since 0.11
*/
public final class GraphtUtils {
private GraphtUtils() {
}
/**
* 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 graph, Logger logger) throws RecommenderConfigurationException {
Set> placeholders = getPlaceholderNodes(graph);
Satisfaction sat = null;
for (DAGNode node: placeholders) {
Component csat = node.getLabel();
// special-case DAOs for non-checking
if (DataAccessObject.class.isAssignableFrom(csat.getSatisfaction().getErasedType())) {
logger.debug("found DAO placeholder {}");
} 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");
}
}
/**
* Get the placeholder nodes from a graph.
*
* @param graph The graph.
* @return The set of nodes that have placeholder satisfactions.
*/
public static Set> getPlaceholderNodes(DAGNode graph) {
Predicate isPlaceholder = new Predicate() {
@Override
public boolean apply(@Nullable Component input) {
return input != null && input.getSatisfaction() instanceof PlaceholderSatisfaction;
}
};
Predicate> nodePredicate = DAGNode.labelMatches(isPlaceholder);
return FluentIterable.from(graph.getReachableNodes())
.filter(nodePredicate)
.toSet();
}
/**
* Determine if a node is a shareable component.
*
*
* @param node The node.
* @return {@code true} if the component is shareable.
*/
public static boolean isShareable(DAGNode node) {
Component label = node.getLabel();
if (label.getSatisfaction().hasInstance()) {
return true;
}
if (label.getCachePolicy() == CachePolicy.NEW_INSTANCE) {
return false;
}
Class> type = label.getSatisfaction().getErasedType();
if (type.getAnnotation(Shareable.class) != null) {
return true;
}
if (type.getAnnotation(Singleton.class) != null) {
return true;
}
// finally examine the satisfaction in more detail
return label.getSatisfaction().visit(new AbstractSatisfactionVisitor() {
@Override
public Boolean visitDefault() {
return false;
}
@Override
public Boolean visitProviderClass(Class extends Provider>> pclass) {
Method m = null;
try {
m = pclass.getMethod("get");
} catch (NoSuchMethodException e) {
/* fine, leave it null */
}
if (m != null && m.getAnnotation(Shareable.class) != null) {
return true;
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public Boolean visitProviderInstance(Provider> provider) {
// cast to raw type to work around inference issue
return visitProviderClass((Class) provider.getClass());
}
});
}
/**
* Determine whether a desire is transient.
*
* @param d The desire to test.
* @return {@code true} if the desire is transient.
*/
public static boolean desireIsTransient(@Nonnull Desire d) {
InjectionPoint ip = d.getInjectionPoint();
return ip.getAttribute(Transient.class) != null;
}
public static boolean edgeIsTransient(DAGEdge, Dependency> input) {
Desire desire = input.getLabel().getInitialDesire();
return desireIsTransient(desire);
}
public static Predicate> edgeIsTransient() {
return new Predicate>() {
@Override
public boolean apply(@Nullable DAGEdge, Dependency> input) {
Desire desire = input == null ? null : input.getLabel().getInitialDesire();
return desire != null && !desireIsTransient(desire);
}
};
}
private static Function,List> ORDER_KEY = new Function, List>() {
@Nullable
@Override
public List apply(@Nullable DAGEdge input) {
if (input == null) {
throw new NullPointerException("cannot order null edge");
}
Desire desire = input.getLabel().getInitialDesire();
InjectionPoint ip = desire.getInjectionPoint();
List key = new ArrayList<>(4);
if (ip instanceof ConstructorParameterInjectionPoint) {
ConstructorParameterInjectionPoint cpi = (ConstructorParameterInjectionPoint) ip;
key.add("0: constructor");
key.add(Integer.toString(cpi.getParameterIndex()));
} else if (ip instanceof SetterInjectionPoint) {
SetterInjectionPoint spi = (SetterInjectionPoint) ip;
key.add("1: setter");
key.add(spi.getMember().getName());
key.add(Integer.toString(spi.getParameterIndex()));
} else if (ip instanceof FieldInjectionPoint) {
FieldInjectionPoint fpi = (FieldInjectionPoint) ip;
key.add("2: field");
key.add(fpi.getMember().getName());
} else if (ip instanceof NoArgumentInjectionPoint) {
/* this shouldn't really happen */
NoArgumentInjectionPoint fpi = (NoArgumentInjectionPoint) ip;
key.add("8: no-arg");
key.add(fpi.getMember().getName());
} else if (ip instanceof SimpleInjectionPoint) {
key.add("5: simple");
} else {
key.add("9: unknown");
key.add(ip.getClass().getName());
}
return key;
}
};
/**
* An ordering over dependency edges.
*/
public static final Ordering> DEP_EDGE_ORDER =
Ordering.natural()
.lexicographical()
.onResultOf(ORDER_KEY);
/**
* Find the set of shareable nodes (objects that will be replaced with instance satisfactions in
* the final graph).
*
* @param graph The graph to analyze.
* @return The set of root nodes - nodes that need to be instantiated and removed. These nodes
* are in topologically sorted order.
*/
public static LinkedHashSet> getShareableNodes(DAGNode graph) {
LinkedHashSet> shared = Sets.newLinkedHashSet();
List> nodes = graph.getSortedNodes();
for (DAGNode node : nodes) {
if (!isShareable(node)) {
continue;
}
// see if we depend on any non-shared nodes
// since nodes are sorted, all shared nodes will have been seen
boolean isShared = true;
for (DAGEdge edge: node.getOutgoingEdges()) {
if (!edgeIsTransient(edge)) {
isShared &= shared.contains(edge.getTail());
}
}
if (isShared) {
shared.add(node);
}
}
return shared;
}
/**
* 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 findSatisfyingNode(DAGNode graph,
final QualifierMatcher qmatch,
final Class> type) {
Predicate> pred = new Predicate>() {
@Override
public boolean apply(@Nullable DAGEdge input) {
return input != null
&& type.isAssignableFrom(input.getTail()
.getLabel()
.getSatisfaction()
.getErasedType())
&& qmatch.apply(input.getLabel()
.getInitialDesire()
.getInjectionPoint()
.getQualifier());
}
};
DAGEdge edge = graph.findEdgeBFS(pred);
if (edge != null) {
return edge.getTail();
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy