001    package jcolibri.extensions.maintenance_evaluation;
002    
003    import jcolibri.cbraplications.StandardCBRApplication;
004    import jcolibri.cbrcore.CBRCaseBase;
005    import jcolibri.evaluation.Evaluator;
006    import jcolibri.exception.ExecutionException;
007    import jcolibri.method.maintenance.AbstractCaseBaseEditMethod;
008    import jcolibri.method.reuse.classification.KNNClassificationConfig;
009    
010    import org.apache.commons.logging.LogFactory;
011    
012    /**
013     * This abstract class defines the common behaviour of a maintenance evaluator.
014     * 
015     * @author Lisa Cummins.
016     */
017    public abstract class MaintenanceEvaluator extends Evaluator 
018    {
019        protected StandardCBRApplication app;
020        protected AbstractCaseBaseEditMethod editMethod = null;
021        protected KNNClassificationConfig simConfig = null;
022    
023        /**
024         * The label for the percentage reduction in the case-base
025         * after maintenance is performed.
026         */
027        public static final String PERCENT_REDUCED = "Percent reduced";
028            
029        /**
030         * Initialise this evaluator with the CBR application to evaluate.
031         * @param cbrApp the CBR application that this evaluator will use. 
032         */
033        public void init(StandardCBRApplication cbrApp) 
034        {   if(report == null || !(report instanceof DetailedEvaluationReport))
035            {       report = new DetailedEvaluationReport();
036            }
037                    
038            app = cbrApp;
039                    try 
040                    {       app.configure();
041                    } catch (ExecutionException e) 
042                    {       LogFactory.getLog(this.getClass()).error(e);
043                    }
044        }
045        
046        /**
047         * Initialise this evaluator with the CBR application to evaluate and the 
048         * edit method and similarity configuration to perform maintenance.
049         * @param cbrApp the CBR application that this evaluator will use. 
050         * @param editMethod the maintenance algorithm to use.
051         * @param simConfig the similarity configuration to use.
052         */
053        public void init(StandardCBRApplication cbrApp, AbstractCaseBaseEditMethod editMethod,
054                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            LogFactory.getLog(this.getClass()).info("Editing Case-Base");
084            editMethod.edit(caseBase, simConfig);
085            LogFactory.getLog(this.getClass()).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    }