001    /**
002     * IDIndexedCaseBase.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     * 28/11/2006
008     */
009    package jcolibri.casebase;
010    
011    import java.util.Collection;
012    
013    import jcolibri.cbrcore.CBRCase;
014    import jcolibri.cbrcore.CBRCaseBase;
015    import jcolibri.cbrcore.CaseBaseFilter;
016    import jcolibri.cbrcore.Connector;
017    import jcolibri.exception.AttributeAccessException;
018    
019    /**
020     * This is a modification of LinealCaseBase that also keeps an index of cases using their IDs. 
021     * Internally it uses a hash table that relates each ID with its corresponding case.
022     * It adds the method: getCase(Object ID)
023     * 
024     * @author Juan A. Recio-García
025     *
026     */
027    public class IDIndexedLinealCaseBase implements CBRCaseBase {
028    
029            private jcolibri.cbrcore.Connector connector;
030            private java.util.Collection<CBRCase> cases;
031            private java.util.HashMap<Object, CBRCase> index;
032    
033            /**
034             * Private method that executes the indexing of cases.
035             * @param cases
036             */
037            private void indexCases(Collection<CBRCase> cases)
038            {
039                    index = new java.util.HashMap<Object, CBRCase>();
040                    for(CBRCase c: cases)
041                    {
042                            try {
043                                    Object o = c.getDescription().getIdAttribute().getValue(c.getDescription());
044                                    index.put(o, c);
045                            } catch (AttributeAccessException e) { }
046                    }
047            }
048            
049            /* (non-Javadoc)
050             * @see jcolibri.cbrcore.CBRCaseBase#init()
051             */
052            public void init(Connector connector) {
053                    this.connector = connector;
054                    cases = this.connector.retrieveAllCases();      
055                    indexCases(cases);
056                            
057            }
058            
059            /* (non-Javadoc)
060             * @see jcolibri.cbrcore.CBRCaseBase#close()
061             */
062            public void close() {
063                    this.connector.close();
064    
065            }
066    
067            /* (non-Javadoc)
068             * @see jcolibri.cbrcore.CBRCaseBase#forgetCases(java.util.Collection)
069             */
070            public void forgetCases(Collection<CBRCase> cases) {
071                    // TODO Auto-generated method stub
072    
073            }
074    
075            /* (non-Javadoc)
076             * @see jcolibri.cbrcore.CBRCaseBase#getCases()
077             */
078            public Collection<CBRCase> getCases() {
079                    return cases;
080            }
081    
082            /* (non-Javadoc)
083             * @see jcolibri.cbrcore.CBRCaseBase#getCases(jcolibri.cbrcore.CaseBaseFilter)
084             */
085            public Collection<CBRCase> getCases(CaseBaseFilter filter) {
086                    // TODO Auto-generated method stub
087                    return null;
088            }
089    
090    
091            /* (non-Javadoc)
092             * @see jcolibri.cbrcore.CBRCaseBase#learnCases(java.util.Collection)
093             */
094            public void learnCases(Collection<CBRCase> cases) {
095                    connector.storeCases(cases);
096                    indexCases(cases);
097                    this.cases.addAll(cases);
098    
099            }
100    
101            /**
102             * Returns the case that corresponds with the id parameter.
103             */
104            public CBRCase getCase(Object id)
105            {
106                    return index.get(id);
107            }
108    
109    }