Implement new algorithm - UCM-GAIA/RecoLibry-Core GitHub Wiki
RecoLibry-Core allows us to implement new algorithms for recommendation systems. In this section, we explain the structure we must follow in the implementation of a new algorithm and also, if we need more components, how we define the dependency injections.
To make a new algorithm, we must create a class that implements the RecommenderAlgorithm
interface. This new class must complete the next three methods: init()
, recommend()
and close()
.
public class NewAlgorithm implements RecommenderAlgorithm {
public boolean init() {
}
public List<RecommenderResult> recommend(Query query) {
}
public boolean close() {
}
}
These methods have the following goals:
-
init()
: it executes the necessary steps to prepare the recommender system. For example, in a content-based system, this method loads the item information in the system. This step will be executed before the first recommendation. -
recommend()
: it executes the recommended algorithm to obtain a recommendation from a query. Each execution returns a list ofRecommendedResults
. -
close()
: it contains all necessary steps to close the recommender algorithm in a safe way. For example, if we need to save the algorithm state to execute later, it will be done in this method.
Another element needed in our recommender system will be a Query
object. It contains the information used to execute the recommender algorithm. To do that, we will create a new class that implements the Query
interface. It must implement the following methods: initialize()
, getAttributesNames()
, setAttributeValue()
and getAttributeValue()
.
public class NewQuery implements Query {
public void initialize() {
}
public List<String> getAttributesNames() {
}
public void setAttributeValue(String attributeName, Object value) {
}
public Object getAttributeValue(String attribute) {
}
}
These methods have the following goals:
-
initialize()
: it initializes the attributes of the query. For example, if the query contains an object, it should be created in this function. -
getAttributesName()
: it returns the name and data type of all attributes that can be modified in the query. -
setAttributeValue()
: it assigns the value to the attribute with the corresponding name. It has to be the unique method that modifies the query. -
getAttributeValue()
: it returns the value that has the attribute with the corresponding name.
In addition, you can create new mathods that your algorithm needs to use the query.
In addition, our classes must have a constructor method. This constructor may be the default builder. In this case, we only have to declare it at the beginning of our class:
public NewAlgorithm() {
}
However, if our algorithm or query needs other components or attributes, it is necessary to configure the constructor to define the dependency to inject. In this case, we'll add the @Inject
label on top of the constructor and add the necessary attributes:
@Inject
public NewAlgorithm(attr1, attr2, ...)
When these attributes are classes that we have created, it is not necessary to create a label to differentiate them. On the other hand, when these attributes are a class that is used in several components, such as Integer
, it is necessary to label them. To label an attribute we will use the @Named("name")
label with the name of the label in parentheses:
@Inject
public NewAlgorithm(@Named("attr1") Class attr1, ...)
You can visit the recommended system configuration section to learn how to configure the recommended system with dependency injection.