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