001    /**
002     * FilterBasedRetrievalMethod.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/10/2007
008     */
009    package jcolibri.method.retrieve.FilterBasedRetrieval;
010    
011    import java.util.ArrayList;
012    import java.util.Collection;
013    
014    import jcolibri.cbrcore.Attribute;
015    import jcolibri.cbrcore.CBRCase;
016    import jcolibri.cbrcore.CBRQuery;
017    import jcolibri.cbrcore.CaseComponent;
018    import jcolibri.method.retrieve.FilterBasedRetrieval.predicates.FilterPredicate;
019    import jcolibri.util.AttributeUtils;
020    
021    /**
022     * Retrieves cases according boolean predicates (less, more, equal, ...) over the attributes.
023     * 
024     * @author Juan A. Recio-Garcia
025     * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
026     * @version 1.0
027     *
028     */
029    public class FilterBasedRetrievalMethod
030    {
031        /**
032         * Retrieves cases that match with the given predicates in filter config
033         * @param cases to retrieve from
034         * @param query to compare
035         * @param filterConfig contains the filter predicates
036         * @return a list of cases
037         */
038        public static Collection<CBRCase> filterCases(Collection<CBRCase> cases, CBRQuery query, FilterConfig filterConfig)
039        {
040            if(filterConfig == null)
041                return cases;
042            Collection<CBRCase> res = new ArrayList<CBRCase>();
043            for(CBRCase c:cases)
044                if(filter(c.getDescription(),query.getDescription(),filterConfig))
045                    res.add(c);
046            return res;
047        }
048        
049        /**
050         * Filters a case component
051         * @param cc
052         * @param qc
053         * @param config
054         * @return
055         */
056        private static boolean filter(CaseComponent cc, CaseComponent qc, FilterConfig config)
057        {
058            try
059            {
060                for(Attribute att : AttributeUtils.getAttributes(cc))
061                {
062                    if(CaseComponent.class.isAssignableFrom(att.getType()))
063                    {    
064                            if(!filter((CaseComponent)att.getValue(cc),(CaseComponent)att.getValue(qc),config))
065                                return false;
066                    }
067                    else if(att.equals(cc.getIdAttribute()))
068                            continue;
069                    else
070                    {
071                            FilterPredicate predicate = config.getPredicate(att);
072                            if(predicate == null)
073                                continue;
074                            if(!predicate.compute(att.getValue(cc), att.getValue(qc)))
075                                return false;
076                    }
077                }
078                return true;
079            } catch (Exception e)
080            {
081                org.apache.commons.logging.LogFactory.getLog(FilterBasedRetrievalMethod.class).error(e);
082                return false; 
083            }
084        }
085    }