001 /** 002 * CachedLinealCaseBase.java 003 * jCOLIBRI2 framework. 004 * @author Juan A. Recio-García. 005 * GAIA - Group for Artificial Intelligence Applications 006 * http://gaia.fdi.ucm.es 007 * 03/05/2007 008 */ 009 010 package jcolibri.casebase; 011 012 import java.util.Collection; 013 014 import jcolibri.cbrcore.CBRCase; 015 import jcolibri.cbrcore.CBRCaseBase; 016 import jcolibri.cbrcore.CaseBaseFilter; 017 import jcolibri.cbrcore.Connector; 018 import jcolibri.exception.InitializingException; 019 020 /** 021 * Cached case base that only persists cases when closing. 022 * learn() and forget() are not synchronized with the persistence until close() is invoked. 023 * <p> 024 * This class presents better performance that LinelCaseBase as only access to the persistence once. This case base is used for evaluation. 025 * 026 * @author Juan A. Recio-García 027 * @see jcolibri.casebase.LinealCaseBase 028 */ 029 public class CachedLinealCaseBase implements CBRCaseBase { 030 031 private jcolibri.cbrcore.Connector connector; 032 private java.util.Collection<CBRCase> originalCases; 033 private java.util.Collection<CBRCase> workingCases; 034 035 /** 036 * Closes the case base saving or deleting the cases of the persistence media 037 */ 038 public void close() { 039 java.util.ArrayList<CBRCase> casestoRemove = new java.util.ArrayList<CBRCase>(originalCases); 040 casestoRemove.removeAll(workingCases); 041 org.apache.commons.logging.LogFactory.getLog(this.getClass()).info("Deleting "+casestoRemove.size()+" cases from storage media"); 042 connector.deleteCases(casestoRemove); 043 044 java.util.ArrayList<CBRCase> casestoStore = new java.util.ArrayList<CBRCase>(workingCases); 045 casestoStore.removeAll(originalCases); 046 org.apache.commons.logging.LogFactory.getLog(this.getClass()).info("Storing "+casestoStore.size()+" cases into storage media"); 047 048 connector.storeCases(casestoStore); 049 050 connector.close(); 051 052 } 053 054 /** 055 * Forgets cases. It only removes the cases from the storage media when closing. 056 */ 057 public void forgetCases(Collection<CBRCase> cases) { 058 workingCases.removeAll(cases); 059 060 } 061 062 /** 063 * Returns working cases. 064 */ 065 public Collection<CBRCase> getCases() { 066 return workingCases; 067 } 068 069 /** 070 * TODO. 071 */ 072 public Collection<CBRCase> getCases(CaseBaseFilter filter) { 073 // TODO 074 return null; 075 } 076 077 /** 078 * Initializes the Case Base with the cases read from the given connector. 079 */ 080 public void init(Connector connector) throws InitializingException { 081 this.connector = connector; 082 originalCases = this.connector.retrieveAllCases(); 083 workingCases = new java.util.ArrayList<CBRCase>(originalCases); 084 } 085 086 /** 087 * Learns cases that are only saved when closing the Case Base. 088 */ 089 public void learnCases(Collection<CBRCase> cases) { 090 workingCases.addAll(cases); 091 092 } 093 094 }