es.ucm.fdi.gaia.jcolibri.casebase.CachedLinealCaseBase Maven / Gradle / Ivy
Show all versions of jCOLIBRI Show documentation
package es.ucm.fdi.gaia.jcolibri.casebase;
import java.util.ArrayList;
import java.util.Collection;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase;
import es.ucm.fdi.gaia.jcolibri.cbrcore.CaseBaseFilter;
import es.ucm.fdi.gaia.jcolibri.cbrcore.Connector;
import es.ucm.fdi.gaia.jcolibri.exception.InitializingException;
/**
* Cached case base that only persists cases when closing.
* learn() and forget() are not synchronized with the persistence until close() is invoked.
*
* This class presents better performance that LinelCaseBase as only access to the persistence once.
* This case base is used for evaluation.
*
* @author Juan A. Recio-García
* @see LinealCaseBase
*/
public class CachedLinealCaseBase implements CBRCaseBase {
private Connector connector;
private Collection originalCases;
private Collection workingCases;
/**
* Closes the case base saving or deleting the cases of the persistence media
*/
public void close() {
Collection casesToRemove = new ArrayList<>(originalCases);
casesToRemove.removeAll(workingCases);
connector.deleteCases(casesToRemove);
Collection casesToStore = new ArrayList<>(workingCases);
casesToStore.removeAll(originalCases);
connector.storeCases(casesToStore);
connector.close();
}
/**
* Forgets cases. It only removes the cases from the storage media when closing.
*/
public void forgetCases(Collection cases) {
workingCases.removeAll(cases);
}
/**
* Returns working cases.
*/
public Collection getCases() {
return workingCases;
}
/**
* TODO.
*/
public Collection getCases(CaseBaseFilter filter) {
// TODO
return null;
}
/**
* Initializes the Case Base with the cases read from the given connector.
*/
public void init(Connector connector) throws InitializingException {
this.connector = connector;
originalCases = this.connector.retrieveAllCases();
workingCases = new java.util.ArrayList(originalCases);
}
/**
* Learns cases that are only saved when closing the Case Base.
*/
public void learnCases(Collection cases) {
workingCases.addAll(cases);
}
}