001package es.ucm.fdi.gaia.jcolibri.method.reuse.classification;
002
003
004import java.util.Collection;
005
006import org.apache.logging.log4j.LogManager;
007
008import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCase;
009import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRQuery;
010import es.ucm.fdi.gaia.jcolibri.extensions.classification.ClassificationSolution;
011import es.ucm.fdi.gaia.jcolibri.method.retrieve.RetrievalResult;
012import es.ucm.fdi.gaia.jcolibri.util.CopyUtils;
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 */
022public 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                                LogManager.getLogger().error(e);
055                        }
056                
057                queryWithPredSoln.setDescription(CopyUtils.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}