001    package jcolibri.method.reuse.classification;
002    
003    import static jcolibri.util.CopyUtils.copyCaseComponent;
004    
005    import java.util.Collection;
006    
007    import jcolibri.cbrcore.CBRCase;
008    import jcolibri.cbrcore.CBRQuery;
009    import jcolibri.extensions.classification.ClassificationSolution;
010    import jcolibri.method.retrieve.RetrievalResult;
011    
012    import org.apache.commons.logging.LogFactory;
013    
014    /**
015     * Provides the ability to classify a query by predicting its
016     * solution from supplied cases.
017     * 
018     * @author Derek Bridge
019     * @author Lisa Cummins
020     * 16/05/07
021     */
022    public abstract class AbstractKNNClassificationMethod implements KNNClassificationMethod
023    {
024    
025        /**
026         * Gets the predicted solution of the given cases according 
027         * to the classification type.
028         * @param cases a list of cases along with similarity scores.
029         * @return Returns the predicted solution.
030         */
031        public abstract ClassificationSolution getPredictedSolution(Collection<RetrievalResult> cases);
032        
033        /**
034         * Gets the predicted solution of the given cases according 
035         * to the classification type and returns a case that has the
036         * query description and the predicted solution.
037         * @param query the query.
038         * @param cases a list of cases along with similarity scores.
039         * @return Returns a case with the query description as its 
040         * description and the predicted solution as its solution. 
041         */
042        public CBRCase getPredictedCase(CBRQuery query, Collection<RetrievalResult> cases)
043        {
044            CBRCase queryWithPredSoln = null;
045            
046            if(cases.size() > 0)
047            {       //Make a copy of any case. This will be used as 
048                    //the query with its predicted solution.
049                    CBRCase c = cases.iterator().next().get_case();  
050    
051                    try {
052                                    queryWithPredSoln = c.getClass().newInstance();
053                            } catch (Exception e) {
054                                    LogFactory.getLog(this.getClass()).error(e);
055                            }
056                    
057                    queryWithPredSoln.setDescription(copyCaseComponent(query.getDescription()));
058                    
059                    ClassificationSolution predSolution = getPredictedSolution(cases);
060                    queryWithPredSoln.setSolution(predSolution);
061                    
062                    queryWithPredSoln.setJustificationOfSolution(null);
063                    
064                    queryWithPredSoln.setResult(null);
065            }
066                return queryWithPredSoln;
067        }
068    }