public final class LenskitRecommenderEngine extends java.lang.Object implements RecommenderEngine, java.io.Serializable
LensKit implementation of a recommender engine. It uses containers set up by the LenskitConfiguration
to set up actual recommenders, and can build multiple recommenders from the same model.
If you just want to quick create a recommender for evaluation or testing, consider using LenskitRecommender.build(LenskitConfiguration)
. For more control, use LenskitRecommenderEngineBuilder
; to load a pre-built recommender engine, use LenskitRecommenderEngineLoader
.
Constructor and Description |
---|
LenskitRecommenderEngine(DAGNode<Component,Dependency> graph,
boolean instantiable)
Build an engine encapsulating a dependency graph.
|
Modifier and Type | Method and Description |
---|---|
static LenskitRecommenderEngine |
build(LenskitConfiguration config)
Build a LensKit recommender engine from a configuration.
|
static LenskitRecommenderEngine |
build(LenskitConfiguration config,
DataAccessObject dao)
Build a LensKit recommender engine from a configuration.
|
LenskitRecommender |
createRecommender()
Deprecated.
|
LenskitRecommender |
createRecommender(DataAccessObject dao)
Create a LensKit recommender.
|
LenskitRecommender |
createRecommender(LenskitConfiguration config)
Construct a recommender with some additional configuration.
|
<T> T |
getComponent(java.lang.Class<T> type)
Get the component of a particular type, if one is already instantiated.
|
DAGNode<Component,Dependency> |
getGraph()
Get the dependency graph of the recommender engine.
|
boolean |
isInstantiable()
Query whether this engine is instantiable.
|
static LenskitRecommenderEngine |
load(java.io.File file)
Create a new recommender engine by reading a previously serialized engine from the given file.
|
static LenskitRecommenderEngine |
load(java.io.File file,
java.lang.ClassLoader loader)
Deprecated.
Use
LenskitRecommenderEngineLoader for sophisticated loading. |
static LenskitRecommenderEngine |
load(java.io.InputStream input)
Create a new recommender engine by reading a previously serialized engine from the given input stream.
|
static LenskitRecommenderEngineBuilder |
newBuilder()
Create a new recommender engine builder.
|
static LenskitRecommenderEngineLoader |
newLoader()
Create a new recommender engine loader.
|
void |
write(java.io.File file)
Write the state of this recommender engine to the given file so that it can be recreated later using another DAOFactory.
|
void |
write(java.io.File file,
CompressionMode compressed)
Write the state of this recommender engine to the given file so that it can be recreated later using another DAOFactory.
|
void |
write(java.io.OutputStream stream)
Write the state of this recommender engine to the given stream so that it can be recreated later using another DAOFactory.
|
public LenskitRecommenderEngine(@Nonnull DAGNode<Component,Dependency> graph, boolean instantiable)
Build an engine encapsulating a dependency graph. You generally do not want to use this - use LenskitRecommenderEngineBuilder
instead.
graph
- The graph.instantiable
- true
if the recommender can be instantiated as-is, false
otherwise.public static LenskitRecommenderEngine load(java.io.File file) throws java.io.IOException, RecommenderConfigurationException
Create a new recommender engine by reading a previously serialized engine from the given file. The new engine will be identical to the old except it will use the new DAOFactory. It is assumed that the file was created by using write(OutputStream)
. Classes will be loaded using a default class loader.
file
- The file from which to load the engine.java.io.IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.@Deprecated public static LenskitRecommenderEngine load(java.io.File file, java.lang.ClassLoader loader) throws java.io.IOException, RecommenderConfigurationException
LenskitRecommenderEngineLoader
for sophisticated loading.Create a new recommender engine by reading a previously serialized engine from the given file. The new engine will be identical to the old except it will use the new DAOFactory. It is assumed that the file was created by using write(OutputStream)
.
file
- The file from which to load the engine.loader
- The class loader to load from (null
to use a default class loader).java.io.IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.public static LenskitRecommenderEngine load(java.io.InputStream input) throws java.io.IOException, RecommenderConfigurationException
Create a new recommender engine by reading a previously serialized engine from the given input stream. The new engine will be identical to the old. It is assumed that the file was created by using write(OutputStream)
. Classes will be loaded using a default class loader.
input
- The stream from which to load the engine.java.io.IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.public void write(@Nonnull java.io.File file) throws java.io.IOException
Write the state of this recommender engine to the given file so that it can be recreated later using another DAOFactory. This uses default object serialization so if the factory has a PicoContainer or session bindings containing non-serializable types, this will fail.
file
- The file to write the rec engine to.java.io.IOException
- if there is an error serializing the engine.write(OutputStream)
public void write(@Nonnull java.io.File file, CompressionMode compressed) throws java.io.IOException
Write the state of this recommender engine to the given file so that it can be recreated later using another DAOFactory. This uses default object serialization so if the factory has a PicoContainer or session bindings containing non-serializable types, this will fail.
file
- The file to write the rec engine to.compressed
- Whether to compress the output file.java.io.IOException
- if there is an error serializing the engine.write(OutputStream)
public void write(@Nonnull @WillClose java.io.OutputStream stream) throws java.io.IOException
Write the state of this recommender engine to the given stream so that it can be recreated later using another DAOFactory. This uses default object serialization so if the factory has session bindings containing non-serializable types, this will fail.
stream
- The file to write the rec engine to.java.io.IOException
- if there is an error serializing the engine.load(InputStream)
@Deprecated public LenskitRecommender createRecommender()
createRecommender(DataAccessObject)
Create a recommender.
createRecommender
in interface RecommenderEngine
public LenskitRecommender createRecommender(LenskitConfiguration config) throws RecommenderConfigurationException
Construct a recommender with some additional configuration. This can be used to do things like add data source configuration on a per-recommender, rather than per-engine, basis.
config
- The configuration to adjust the recommender.RecommenderConfigurationException
- if there is an error configuring the recommender.public LenskitRecommender createRecommender(@WillNotClose DataAccessObject dao) throws RecommenderBuildException
Create a LensKit recommender.
dao
- The data access objectRecommenderBuildException
public boolean isInstantiable()
Query whether this engine is instantiable. Instantiable recommenders have all their placeholders removed and are ready to instantiate.
true
if the recommender is instantiable.@Nonnull public DAGNode<Component,Dependency> getGraph()
Get the dependency graph of the recommender engine.
@Nullable public <T> T getComponent(java.lang.Class<T> type)
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.
type
- The required component type.T
- The required component type.null
if no instance can be retreived (either because no such component is configured, or it is not yet instantiated).public static LenskitRecommenderEngine build(LenskitConfiguration config) throws RecommenderBuildException
Build a LensKit recommender engine from a configuration. The resulting recommender is independent of any subsequent modifications to the configuration.
config
- The configuration.RecommenderBuildException
public static LenskitRecommenderEngine build(LenskitConfiguration config, DataAccessObject dao) throws RecommenderBuildException
Build a LensKit recommender engine from a configuration. The resulting recommender is independent of any subsequent modifications to the configuration.
config
- The configuration.dao
- The data access objectRecommenderBuildException
public static LenskitRecommenderEngineBuilder newBuilder()
Create a new recommender engine builder.
public static LenskitRecommenderEngineLoader newLoader()
Create a new recommender engine loader.