public final class LenskitRecommenderEngine extends Object implements RecommenderEngine, 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
.
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.
|
DAGNode<Component,Dependency> |
createRecommenderGraph(LenskitConfiguration config) |
<T> T |
getComponent(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(File file)
Create a new recommender engine by reading a previously serialized engine from the given file.
|
static LenskitRecommenderEngine |
load(File file,
ClassLoader loader)
Deprecated.
Use
LenskitRecommenderEngineLoader for sophisticated loading. |
static LenskitRecommenderEngine |
load(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(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(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(OutputStream stream)
Write the state of this recommender engine to the given stream so that it can be recreated later using another DAOFactory.
|
public static LenskitRecommenderEngine load(File file) throws 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.IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.@Deprecated public static LenskitRecommenderEngine load(File file, ClassLoader loader) throws 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).IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.public static LenskitRecommenderEngine load(InputStream input) throws 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.IOException
- If there is an error reading from the file.RecommenderConfigurationException
- If the configuration cannot be used.public void write(@Nonnull File file) throws 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.IOException
- if there is an error serializing the engine.write(OutputStream)
public void write(@Nonnull File file, CompressionMode compressed) throws 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.IOException
- if there is an error serializing the engine.write(OutputStream)
public void write(@Nonnull @WillClose OutputStream stream) throws 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.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.
This method is only used for special cases needing detailed access to the recommender.
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 DAGNode<Component,Dependency> createRecommenderGraph(LenskitConfiguration config) throws RecommenderConfigurationException
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(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.