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 Less+More Like combines both MoreLikeThis and LessLikeThis. It copies 023 * the values of the selected case into the query and returns a FilterConfig 024 * object with the negative conditions. 025 * That way, this method should be used together with FilteredKNNRetrieval. 026 * <p>See: 027 * <p>L. McGinty and B. Smyth. Comparison-based recommendation. In ECCBR'02: 028 * Proceedings of the 6th European Conference on Advances in Case-Based 029 * Reasoning, pages 575-589, London, UK, 2002. Springer-Verlag. 030 * 031 * @see jcolibri.extensions.recommendation.navigationByProposing.queryElicitation.MoreLikeThis 032 * @see jcolibri.extensions.recommendation.navigationByProposing.queryElicitation.LessLikeThis 033 * @author Juan A. Recio-Garcia 034 * @author Developed at University College Cork (Ireland) in collaboration with Derek Bridge. 035 * @version 1.0 036 * 037 */ 038 public class MoreAndLessLikeThis implements ComparisonQueryElicitation 039 { 040 /******************************************************************************/ 041 /** STATIC METHODS **/ 042 /******************************************************************************/ 043 044 /** 045 * The Less+More Like combines both MoreLikeThis and LessLikeThis. It copies 046 * the values of the selected case into the query and returns a FilterConfig 047 * object with the negative conditions. 048 */ 049 public static void moreAndLessLikeThis(CBRQuery query, CBRCase selectedCase,Collection<CBRCase> proposedCases, FilterConfig filterConfig) 050 { 051 for(Attribute at: AttributeUtils.getAttributes(selectedCase.getDescription())) 052 { 053 Object selectedValue = AttributeUtils.findValue(at, selectedCase); 054 AttributeUtils.setValue(at, query, selectedValue); 055 HashSet<Object> alternatives = new HashSet<Object>(); 056 for(CBRCase c: proposedCases) 057 { 058 Object value = AttributeUtils.findValue(at, c); 059 alternatives.add(value); 060 } 061 if(alternatives.size()!=1) 062 return; 063 Object value = alternatives.iterator().next(); 064 if(selectedValue==null) 065 { 066 if(value == null) 067 return; 068 else 069 filterConfig.addPredicate(at, new NotEqualTo(value)); 070 }else if(!selectedValue.equals(value)) 071 filterConfig.addPredicate(at, new NotEqualTo(value)); 072 } 073 074 075 } 076 077 078 /******************************************************************************/ 079 /** OBJECT METHODS **/ 080 /******************************************************************************/ 081 082 private FilterConfig _filterConfig; 083 084 public MoreAndLessLikeThis(FilterConfig filterConfig) 085 { 086 _filterConfig = filterConfig; 087 } 088 089 /** 090 * The Less+More Like combines both MoreLikeThis and LessLikeThis. It copies 091 * the values of the selected case into the query and returns a FilterConfig 092 * object with the negative conditions. 093 */ 094 public void reviseQuery(CBRQuery query, CBRCase selectedCase, Collection<CBRCase> proposedCases) 095 { 096 moreAndLessLikeThis(query, selectedCase, proposedCases,_filterConfig); 097 } 098 }