/*
 * Copyright 2013 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.benchmark.impl.statistic.bestsolutionmutation;

import java.awt.BasicStroke;
import java.io.File;
import java.text.NumberFormat;
import java.util.Locale;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.optaplanner.benchmark.impl.result.ProblemBenchmarkResult;
import org.optaplanner.benchmark.impl.result.SingleBenchmarkResult;
import org.optaplanner.benchmark.impl.report.BenchmarkReport;
import org.optaplanner.benchmark.impl.statistic.ProblemStatistic;
import org.optaplanner.benchmark.impl.statistic.common.MillisecondsSpentNumberFormat;
import org.optaplanner.benchmark.config.statistic.ProblemStatisticType;
import org.optaplanner.benchmark.impl.statistic.SingleStatistic;

@XStreamAlias("bestSolutionMutationProblemStatistic")
public class BestSolutionMutationProblemStatistic extends ProblemStatistic {

    protected File graphFile = null;

    public BestSolutionMutationProblemStatistic(ProblemBenchmarkResult problemBenchmarkResult) {
        super(problemBenchmarkResult, ProblemStatisticType.BEST_SOLUTION_MUTATION);
    }

    @Override
    public SingleStatistic createSingleStatistic(SingleBenchmarkResult singleBenchmarkResult) {
        return new BestSolutionMutationSingleStatistic(singleBenchmarkResult);
    }

    /**
     * @return never null
     */
    public File getGraphFile() {
        return graphFile;
    }

    // ************************************************************************
    // Write methods
    // ************************************************************************

    @Override
    public void writeGraphFiles(BenchmarkReport benchmarkReport) {
        Locale locale = benchmarkReport.getLocale();
        NumberAxis xAxis = new NumberAxis("Time spent");
        xAxis.setNumberFormatOverride(new MillisecondsSpentNumberFormat(locale));
        NumberAxis yAxis = new NumberAxis("Best solution mutation count");
        yAxis.setNumberFormatOverride(NumberFormat.getInstance(locale));
        yAxis.setAutoRangeIncludesZero(true);
        XYPlot plot = new XYPlot(null, xAxis, yAxis, null);
        plot.setOrientation(PlotOrientation.VERTICAL);
        int seriesIndex = 0;
        for (SingleBenchmarkResult singleBenchmarkResult : problemBenchmarkResult.getSingleBenchmarkResultList()) {
            XYSeries series = new XYSeries(singleBenchmarkResult.getSolverBenchmarkResult().getNameWithFavoriteSuffix());
            XYItemRenderer renderer = new XYLineAndShapeRenderer();
            if (singleBenchmarkResult.isSuccess()) {
                BestSolutionMutationSingleStatistic singleStatistic = (BestSolutionMutationSingleStatistic)
                        singleBenchmarkResult.getSingleStatistic(problemStatisticType);
                for (BestSolutionMutationStatisticPoint point : singleStatistic.getPointList()) {
                    long timeMillisSpent = point.getTimeMillisSpent();
                    long mutationCount = point.getMutationCount();
                    series.add(timeMillisSpent, mutationCount);
                }
            }
            plot.setDataset(seriesIndex, new XYSeriesCollection(series));

            if (singleBenchmarkResult.getSolverBenchmarkResult().isFavorite()) {
                // Make the favorite more obvious
                renderer.setSeriesStroke(0, new BasicStroke(2.0f));
            }
            plot.setRenderer(seriesIndex, renderer);
            seriesIndex++;
        }
        JFreeChart chart = new JFreeChart(problemBenchmarkResult.getName() + " best solution mutation statistic",
                JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        graphFile = writeChartToImageFile(chart, problemBenchmarkResult.getName() + "BestSolutionMutationStatistic");
    }

}

@@ -1083,32 +1083,182 @@
 dShapeRenderer;%0A
+import org.jfree.chart.renderer.xy.YIntervalRenderer;%0Aimport org.jfree.data.xy.XYIntervalSeries;%0Aimport org.jfree.data.xy.XYIntervalSeriesCollection;%0A
 import org.jfree
@@ -2747,498 +2747,48 @@
     
-Locale locale = benchmarkReport.getLocale();%0A        NumberAxis xAxis = new NumberAxis(%22Time spent%22);%0A        xAxis.setNumberFormatOverride(new MillisecondsSpentNumberFormat(locale));%0A        NumberAxis yAxis = new NumberAxis(%22Best solution mutation count%22);%0A        yAxis.setNumberFormatOverride(NumberFormat.getInstance(locale));%0A        yAxis.setAutoRangeIncludesZero(true);%0A        XYPlot plot = new XYPlot(null, xAxis, yAxis, null);%0A        plot.setOrientation(PlotOrientation.VERTICAL
+XYPlot plot = createPlot(benchmarkReport
 );%0A 
@@ -2945,16 +2945,24 @@
       XY
+Interval
 Series s
@@ -2971,24 +2971,32 @@
 ies = new XY
+Interval
 Series(singl
@@ -3111,30 +3111,25 @@
 r = new 
-XYLineAndShape
+YInterval
 Renderer
@@ -3641,155 +3641,496 @@
     
-series.add(timeMillisSpent, mutationCount);%0A                %7D%0A            %7D%0A            plot.setDataset(seriesIndex, new XYSeriesCollection(series)
+double yValue = mutationCount;%0A                    // In an XYInterval the yLow must be lower than yHigh%0A                    series.add(timeMillisSpent, timeMillisSpent, timeMillisSpent,%0A                            yValue, (yValue %3E 0.0) ? 0.0 : yValue, (yValue %3E 0.0) ? yValue : 0.0);%0A                %7D%0A            %7D%0A            XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();%0A            dataset.addSeries(series);%0A            plot.setDataset(seriesIndex, dataset
 );%0A%0A
@@ -4731,10 +4731,604 @@
 %0A    %7D%0A%0A
+    private XYPlot createPlot(BenchmarkReport benchmarkReport) %7B%0A        Locale locale = benchmarkReport.getLocale();%0A        NumberAxis xAxis = new NumberAxis(%22Time spent%22);%0A        xAxis.setNumberFormatOverride(new MillisecondsSpentNumberFormat(locale));%0A        NumberAxis yAxis = new NumberAxis(%22Best solution mutation count%22);%0A        yAxis.setNumberFormatOverride(NumberFormat.getInstance(locale));%0A        yAxis.setAutoRangeIncludesZero(true);%0A        XYPlot plot = new XYPlot(null, xAxis, yAxis, null);%0A        plot.setOrientation(PlotOrientation.VERTICAL);%0A        return plot;%0A    %7D%0A%0A
 %7D%0A

Change best solution mutation statistic graph type