001    /**
002     * WeightedMoreLikeThis.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     * 02/11/2007
008     */
009    package jcolibri.extensions.recommendation.navigationByProposing.queryElicitation;
010    
011    import java.util.Collection;
012    import java.util.HashSet;
013    
014    import jcolibri.cbrcore.Attribute;
015    import jcolibri.cbrcore.CBRCase;
016    import jcolibri.cbrcore.CBRQuery;
017    import jcolibri.method.retrieve.FilterBasedRetrieval.FilterConfig;
018    import jcolibri.method.retrieve.FilterBasedRetrieval.predicates.NotEqualTo;
019    import jcolibri.util.AttributeUtils;
020    
021    /**
022     * The LessLikeThis strategy is a simple one: if the rejected cases all have 
023     * the same feature-value combination, which is different from the preferred
024     * case then this combination can be added as a negative condition. This 
025     * negative condition is coded as a NotEqualTo(value) predicate in a 
026     * FilterConfig object. The query is not modified.
027     * That way, this method should be used together with FilterBasedRetrieval.
028     * <p>See:
029     * <p>L. McGinty and B. Smyth. Comparison-based recommendation. In ECCBR'02: 
030     * Proceedings of the 6th European Conference on Advances in Case-Based
031     * Reasoning, pages 575-589, London, UK, 2002. Springer-Verlag.
032     * 
033     * @see jcolibri.method.retrieve.FilterBasedRetrieval.FilterConfig
034     * @author Juan A. Recio-Garcia
035     * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge.
036     * @version 1.0
037     *
038     */
039    public class LessLikeThis implements ComparisonQueryElicitation
040    { 
041        /******************************************************************************/
042        /**                           STATIC METHODS                                 **/
043        /******************************************************************************/
044        
045        /**
046         * If the rejected cases all have 
047         * the same feature-value combination, which is different from the preferred
048         * case then this combination can be added as a negative condition. This 
049         * negative condition is coded as a NotEqualTo(value) predicate in a 
050         * FilterConfig object. The query is not modified.
051         */
052        public static void lessLikeThis(CBRQuery query, CBRCase selectedCase,Collection<CBRCase> proposedCases, FilterConfig filterConfig)
053        {
054            for(Attribute at: AttributeUtils.getAttributes(selectedCase.getDescription()))
055            {
056                Object selectedValue = AttributeUtils.findValue(at, selectedCase);
057                HashSet<Object> alternatives = new HashSet<Object>();
058                for(CBRCase c: proposedCases)
059                {
060                    Object value = AttributeUtils.findValue(at, c);
061                    alternatives.add(value);
062                }
063                if(alternatives.size()!=1)
064                    return;
065                Object value = alternatives.iterator().next();
066                if(selectedValue==null)
067                {
068                    if(value == null)
069                        return;
070                    else
071                        filterConfig.addPredicate(at, new NotEqualTo(value));
072                }else if(!selectedValue.equals(value))
073                    filterConfig.addPredicate(at, new NotEqualTo(value));    
074            }
075                
076                
077        }
078        
079        
080        /******************************************************************************/
081        /**                           OBJECT METHODS                                 **/
082        /******************************************************************************/
083    
084        private FilterConfig _filterConfig;
085        
086        public LessLikeThis(FilterConfig filterConfig)
087        {
088            _filterConfig = filterConfig;
089        }
090        
091        /**
092         * If the rejected cases all have 
093         * the same feature-value combination, which is different from the preferred
094         * case then this combination can be added as a negative condition. This 
095         * negative condition is coded as a NotEqualTo(value) predicate in a 
096         * FilterConfig object.
097         */
098        public void reviseQuery(CBRQuery query, CBRCase selectedCase, Collection<CBRCase> proposedCases)
099        {
100            lessLikeThis(query, selectedCase, proposedCases,_filterConfig);
101        }
102    }