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