Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 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.drools.planner.benchmark.statistic.bestscore;
 
 import java.io.File;
 import java.io.Writer;
 import java.util.List;
 import java.util.Map;
 
 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.StandardXYItemRenderer;
 import  org.jfree.chart.renderer.xy.XYItemRenderer;
 import  org.jfree.chart.renderer.xy.XYStepRenderer;
 import  org.jfree.data.xy.XYSeries;
 import  org.jfree.data.xy.XYSeriesCollection;
 
 public class BestScoreStatistic extends AbstractSolverStatistic {
 
     private List<StringconfigNameList = new ArrayList<String>();
     // key is the configName
             = new LinkedHashMap<StringBestScoreStatisticListener>();
     private ScoreDefinition scoreDefinition = null;
 
     public BestScoreStatistic() {
     }
 
     public void addListener(Solver solverString configName) {
         if (.contains(configName)) {
             throw new IllegalArgumentException("Cannot add a listener with the same configName (" + configName
                     + ") twice.");
         }
         .add(configName);
         BestScoreStatisticListener bestScoreStatisticListener = new BestScoreStatisticListener();
         solver.addEventListener(bestScoreStatisticListener);
         .put(configNamebestScoreStatisticListener);
         if ( == null) {
              = solver.getScoreDefinition();
         } else {
             if (!.getClass().equals(solver.getScoreDefinition().getClass())) {
                 throw new IllegalStateException("The scoreDefinition (" + solver.getScoreDefinition()
                         + ") should be of the same class as the other scoreDefinition (" +  + ")");
             }
         }
     }
 
     public void removeListener(Solver solverString configName) {
         BestScoreStatisticListener bestScoreStatisticListener = .get(configName);
         solver.removeEventListener(bestScoreStatisticListener);
     }
 
     // ************************************************************************
     // Write methods
     // ************************************************************************
 
     private List<BestScoreScvLineextractCsvLineList() {
         Map<LongBestScoreScvLinetimeToBestScoresLineMap = new HashMap<LongBestScoreScvLine>();
         for (Map.Entry<StringBestScoreStatisticListenerlistenerEntry : .entrySet()) {
             String configName = listenerEntry.getKey();
             List<BestScoreStatisticPointstatisticPointList = listenerEntry.getValue()
                     .getStatisticPointList();
             for (BestScoreStatisticPoint statisticPoint : statisticPointList) {
                 long timeMillisSpend = statisticPoint.getTimeMillisSpend();
                 BestScoreScvLine line = timeToBestScoresLineMap.get(timeMillisSpend);
                if (line == null) {
                    line = new BestScoreScvLine(timeMillisSpend);
                    timeToBestScoresLineMap.put(timeMillisSpendline);
                }
                line.getConfigNameToScoreMap().put(configNamestatisticPoint.getScore());
            }
        }
        List<BestScoreScvLinecsvLineList = new ArrayList<BestScoreScvLine>(timeToBestScoresLineMap.values());
        Collections.sort(csvLineList);
        return csvLineList;
    }
    protected static class BestScoreScvLine extends AbstractSolverStatisticScvLine {
        private Map<StringScoreconfigNameToScoreMap;
        public BestScoreScvLine(long timeMillisSpend) {
            super(timeMillisSpend);
             = new HashMap<StringScore>();
        }
        public Map<StringScoregetConfigNameToScoreMap() {
            return ;
        }
    }
    protected CharSequence writeCsvStatistic(File solverStatisticFilesDirectoryString baseName) {
        List<BestScoreScvLinescvLineList = extractCsvLineList();
        File csvStatisticFile = new File(solverStatisticFilesDirectorybaseName + "BestScoreStatistic.csv");
        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(csvStatisticFile), "utf-8");
            writer.append("\"TimeMillisSpend\"");
            for (String configName : ) {
                writer.append(",\"").append(configName.replaceAll("\\\"""\\\"")).append("\"");
            }
            writer.append("\n");
            for (BestScoreScvLine line : scvLineList) {
                writer.write(Long.toString(line.getTimeMillisSpend()));
                for (String configName : ) {
                    writer.append(",");
                    Score score = line.getConfigNameToScoreMap().get(configName);
                    if (score != null) {
                        Double scoreGraphValue = .translateScoreToGraphValue(score);
                        if (scoreGraphValue != null) {
                            writer.append(scoreGraphValue.toString());
                        }
                    }
                }
                writer.append("\n");
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Problem writing csvStatisticFile: " + csvStatisticFilee);
        } finally {
            IOUtils.closeQuietly(writer);
        }
        return "  <p><a href=\"" + csvStatisticFile.getName() + "\">CVS file</a></p>\n";
    }
    protected CharSequence writeGraphStatistic(File solverStatisticFilesDirectoryString baseName) {
        NumberAxis xAxis = new NumberAxis("Time millis spend");
        xAxis.setNumberFormatOverride(new MillisecondsSpendNumberFormat());
        NumberAxis yAxis = new NumberAxis("Score");
        yAxis.setAutoRangeIncludesZero(false);
        XYPlot plot = new XYPlot(nullxAxisyAxisnull);
        int seriesIndex = 0;
        for (Map.Entry<StringBestScoreStatisticListenerlistenerEntry : .entrySet()) {
            String configName = listenerEntry.getKey();
            XYSeries series = new XYSeries(configName);
            List<BestScoreStatisticPointstatisticPointList = listenerEntry.getValue().getStatisticPointList();
            for (BestScoreStatisticPoint statisticPoint : statisticPointList) {
                long timeMillisSpend = statisticPoint.getTimeMillisSpend();
                Score score = statisticPoint.getScore();
                Double scoreGraphValue = .translateScoreToGraphValue(score);
                if (scoreGraphValue != null) {
                    series.add(timeMillisSpendscoreGraphValue);
                }
            }
            XYSeriesCollection seriesCollection = new XYSeriesCollection();
            seriesCollection.addSeries(series);
            plot.setDataset(seriesIndexseriesCollection);
            XYItemRenderer renderer;
            // No direct lines between 2 points
            renderer = new XYStepRenderer();
            if (statisticPointList.size() <= 1) {
                // Workaround for https://sourceforge.net/tracker/?func=detail&aid=3387330&group_id=15494&atid=115494
                renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES);
            }
            plot.setRenderer(seriesIndexrenderer);
            seriesIndex++;
        }
        plot.setOrientation(PlotOrientation.VERTICAL);
        JFreeChart chart = new JFreeChart(baseName + " best score statistic",
                JFreeChart.DEFAULT_TITLE_FONT, plottrue);
        BufferedImage chartImage = chart.createBufferedImage(1024, 768);
        File graphStatisticFile = new File(solverStatisticFilesDirectorybaseName + "BestScoreStatistic.png");
        OutputStream out = null;
        try {
            out = new FileOutputStream(graphStatisticFile);
            ImageIO.write(chartImage"png"out);
        } catch (IOException e) {
            throw new IllegalArgumentException("Problem writing graphStatisticFile: " + graphStatisticFilee);
        } finally {
            IOUtils.closeQuietly(out);
        }
        return "  <img src=\"" + graphStatisticFile.getName() + "\"/>\n";
    }
New to GrepCode? Check out our FAQ X