001package es.ucm.fdi.gaia.jcolibri.extensions.maintenance_evaluation;
002
003
004import org.apache.logging.log4j.LogManager;
005
006import es.ucm.fdi.gaia.jcolibri.cbraplications.StandardCBRApplication;
007import es.ucm.fdi.gaia.jcolibri.cbrcore.CBRCaseBase;
008import es.ucm.fdi.gaia.jcolibri.evaluation.Evaluator;
009import es.ucm.fdi.gaia.jcolibri.exception.ExecutionException;
010import es.ucm.fdi.gaia.jcolibri.method.maintenance.AbstractCaseBaseEditMethod;
011import es.ucm.fdi.gaia.jcolibri.method.reuse.classification.KNNClassificationConfig;
012
013/**
014 * This abstract class defines the common behaviour of a maintenance evaluator.
015 * 
016 * @author Lisa Cummins.
017 */
018public abstract class MaintenanceEvaluator extends Evaluator {
019
020    protected StandardCBRApplication app;
021    protected AbstractCaseBaseEditMethod editMethod = null;
022    protected KNNClassificationConfig simConfig = null;
023
024    /**
025     * The label for the percentage reduction in the case-base
026     * after maintenance is performed.
027     */
028    public static final String PERCENT_REDUCED = "Percent reduced";
029        
030    /**
031     * Initialise this evaluator with the CBR application to evaluate.
032     * @param cbrApp the CBR application that this evaluator will use. 
033     */
034    public void init(StandardCBRApplication cbrApp) 
035    {   if(report == null || !(report instanceof DetailedEvaluationReport))
036        {       report = new DetailedEvaluationReport();
037        }
038                
039        app = cbrApp;
040                try 
041                {       app.configure();
042                } catch (ExecutionException e)
043                {       LogManager.getLogger().error(e);
044                }
045    }
046    
047    /**
048     * Initialise this evaluator with the CBR application to evaluate and the 
049     * edit method and similarity configuration to perform maintenance.
050     * @param cbrApp the CBR application that this evaluator will use. 
051     * @param editMethod the maintenance algorithm to use.
052     * @param simConfig the similarity configuration to use.
053     */
054    public void init(StandardCBRApplication cbrApp, AbstractCaseBaseEditMethod editMethod, KNNClassificationConfig simConfig) {
055        this.init(cbrApp);
056            setEditMethod(editMethod);
057            setSimConfig(simConfig);
058    }
059
060    /**
061     * Sets the edit method to be the given edit method.
062     * @param editMethod the edit method to set.
063     */
064    public void setEditMethod(AbstractCaseBaseEditMethod editMethod)
065    {   this.editMethod = editMethod;
066    }
067        
068    /**
069     * Sets the similarity configuration to be the given similarity configuration.
070     * @param simConfig the similarity configuration to set.
071     */
072    public void setSimConfig(KNNClassificationConfig simConfig)
073    {   this.simConfig = simConfig;
074    }
075    
076    /**
077     * Edit the case base and store the percentage reduction in the report.
078     * @param caseBase the case base to be edited.
079     */
080    protected void editCaseBase(CBRCaseBase caseBase)
081    {   //Perform maintenance on this case base
082        int sizeBefore = caseBase.getCases().size();
083        LogManager.getLogger().info("Editing Case-Base");
084        editMethod.edit(caseBase, simConfig);
085        LogManager.getLogger().info("Finished Editing Case-Base");
086        int sizeAfter = caseBase.getCases().size();
087        double percentReduced = ((sizeBefore - sizeAfter)/(double)sizeBefore) * 100.0;
088        report.addDataToSeries(PERCENT_REDUCED, percentReduced);
089    }
090}