[jboss-svn-commits] JBL Code SVN: r31852 - labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Feb 26 10:30:42 EST 2010


Author: ge0ffrey
Date: 2010-02-26 10:30:41 -0500 (Fri, 26 Feb 2010)
New Revision: 31852

Modified:
   labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/BestScoreStatistic.java
   labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/SolverStatistic.java
Log:
graph output prototype

Modified: labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/BestScoreStatistic.java
===================================================================
--- labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/BestScoreStatistic.java	2010-02-26 10:38:41 UTC (rev 31851)
+++ labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/BestScoreStatistic.java	2010-02-26 15:30:41 UTC (rev 31852)
@@ -1,9 +1,9 @@
 package org.drools.planner.benchmark.statistic;
 
+import java.awt.image.BufferedImage;
+import java.io.OutputStream;
 import java.util.List;
 import java.util.Map;
-import java.util.LinkedHashMap;
-import java.util.Set;
 import java.util.HashMap;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -13,89 +13,48 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 
+import javax.imageio.ImageIO;
+
 import org.drools.planner.core.Solver;
 import org.drools.planner.core.score.Score;
 import org.drools.planner.core.score.SimpleScore;
 import org.drools.planner.core.score.HardAndSoftScore;
 import org.apache.commons.io.IOUtils;
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.xy.XYSeries;
+import org.jfree.data.xy.XYSeriesCollection;
 
 /**
  * @author Geoffrey De Smet
  */
 public class BestScoreStatistic implements SolverStatistic {
 
-    // A LinkedHashMap because the order is important
+    private List<String> configNameList = new ArrayList<String>();
+    // key is the configName
     private Map<String, BestScoreStatisticListener> bestScoreStatisticListenerMap
-            = new LinkedHashMap<String, BestScoreStatisticListener>();
+            = new HashMap<String, BestScoreStatisticListener>();
 
-    public void addListener(Solver solver) {
-        addListener(solver, "solver");
-    }
-
     public void addListener(Solver solver, String configName) {
-        if (bestScoreStatisticListenerMap.containsKey(configName)) {
+        if (configNameList.contains(configName)) {
             throw new IllegalArgumentException("Cannot add a listener with the same configName (" + configName
                     + ") twice.");
         }
+        configNameList.add(configName);
         BestScoreStatisticListener bestScoreStatisticListener = new BestScoreStatisticListener();
         solver.addEventListener(bestScoreStatisticListener);
         bestScoreStatisticListenerMap.put(configName, bestScoreStatisticListener);
     }
 
-    public void removeListener(Solver solver) {
-        removeListener(solver, "solver");
-    }
-
     public void removeListener(Solver solver, String configName) {
         BestScoreStatisticListener bestScoreStatisticListener = bestScoreStatisticListenerMap.get(configName);
         solver.removeEventListener(bestScoreStatisticListener);
     }
 
     public void writeStatistic(File solverStatisticFilesDirectory, String baseName) {
-        // The configNameSet is ordered because it comes from a LinkedHashMap 
-        Set<String> configNameSet = bestScoreStatisticListenerMap.keySet();
-        List<TimeToBestScoresLine> timeToBestScoresLineList = extractTimeToBestScoresLineList();
-        File statisticFile = new File(solverStatisticFilesDirectory, baseName + "Statistic.csv");
-        Writer writer = null;
-        try {
-            writer = new OutputStreamWriter(new FileOutputStream(statisticFile), "utf-8");
-            writer.append("\"TimeMillisSpend\"");
-            for (String configName : configNameSet) {
-                writer.append(",\"").append(configName.replaceAll("\\\"","\\\"")).append("\"");
-            }
-            writer.append("\n");
-            for (TimeToBestScoresLine timeToBestScoresLine : timeToBestScoresLineList) {
-                writer.write(Long.toString(timeToBestScoresLine.getTimeMillisSpend()));
-                for (String configName : configNameSet) {
-                    writer.append(",");
-                    Score score = timeToBestScoresLine.getConfigNameToScoreMap().get(configName);
-                    if (score != null) {
-                        Integer scoreAlias;
-                        if (score instanceof SimpleScore) {
-                            SimpleScore simpleScore = (SimpleScore) score;
-                            scoreAlias = simpleScore.getScore();
-                        } else if (score instanceof HardAndSoftScore) {
-                            HardAndSoftScore hardAndSoftScore = (HardAndSoftScore) score;
-                            if (hardAndSoftScore.getHardScore() == 0) {
-                                scoreAlias = hardAndSoftScore.getSoftScore();
-                            } else {
-                                scoreAlias = null;
-                            }
-                        } else {
-                            throw new IllegalStateException("Score class (" + score.getClass() + ") not supported.");
-                        }
-                        if (scoreAlias != null) {
-                            writer.append(scoreAlias.toString());
-                        }
-                    }
-                }
-                writer.append("\n");
-            }
-        } catch (IOException e) {
-            throw new IllegalArgumentException("Problem writing statisticFile: " + statisticFile, e);
-        } finally {
-            IOUtils.closeQuietly(writer);
-        }
+        writeCsvStatistic(solverStatisticFilesDirectory, baseName);
+        writeGraphStatistic(solverStatisticFilesDirectory, baseName);
     }
 
     private List<TimeToBestScoresLine> extractTimeToBestScoresLineList() {
@@ -144,4 +103,87 @@
 
     }
 
+    private void writeCsvStatistic(File solverStatisticFilesDirectory, String baseName) {
+        List<TimeToBestScoresLine> timeToBestScoresLineList = extractTimeToBestScoresLineList();
+        File csvStatisticFile = new File(solverStatisticFilesDirectory, baseName + "Statistic.csv");
+        Writer writer = null;
+        try {
+            writer = new OutputStreamWriter(new FileOutputStream(csvStatisticFile), "utf-8");
+            writer.append("\"TimeMillisSpend\"");
+            for (String configName : configNameList) {
+                writer.append(",\"").append(configName.replaceAll("\\\"","\\\"")).append("\"");
+            }
+            writer.append("\n");
+            for (TimeToBestScoresLine timeToBestScoresLine : timeToBestScoresLineList) {
+                writer.write(Long.toString(timeToBestScoresLine.getTimeMillisSpend()));
+                for (String configName : configNameList) {
+                    writer.append(",");
+                    Score score = timeToBestScoresLine.getConfigNameToScoreMap().get(configName);
+                    if (score != null) {
+                        Number scoreAlias = extractScoreAlias(score);
+                        if (scoreAlias != null) {
+                            writer.append(scoreAlias.toString());
+                        }
+                    }
+                }
+                writer.append("\n");
+            }
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Problem writing csvStatisticFile: " + csvStatisticFile, e);
+        } finally {
+            IOUtils.closeQuietly(writer);
+        }
+    }
+
+    private void writeGraphStatistic(File solverStatisticFilesDirectory, String baseName) {
+        File graphStatisticFile = new File(solverStatisticFilesDirectory, baseName + "Statistic.png");
+
+        XYSeriesCollection seriesCollection = new XYSeriesCollection();
+        for (Map.Entry<String, BestScoreStatisticListener> listenerEntry : bestScoreStatisticListenerMap.entrySet()) {
+            String configName = listenerEntry.getKey();
+            XYSeries configSeries = new XYSeries(configName);
+            List<BestScoreStatisticPoint> statisticPointList = listenerEntry.getValue()
+                    .getBestScoreStatisticPointList();
+            for (BestScoreStatisticPoint statisticPoint : statisticPointList) {
+                long timeMillisSpend = statisticPoint.getTimeMillisSpend();
+                Score score = statisticPoint.getScore();
+                Number scoreAlias = extractScoreAlias(score);
+                configSeries.add(timeMillisSpend, scoreAlias);
+            }
+            seriesCollection.addSeries(configSeries);
+        }
+        JFreeChart chart = ChartFactory.createXYLineChart(
+                baseName + " best score statistic", "Time millis spend", "Score",
+                seriesCollection, PlotOrientation.VERTICAL,
+                true, true, false);
+        BufferedImage chartImage = chart.createBufferedImage(800, 600);
+        OutputStream out = null;
+        try {
+            out = new FileOutputStream(graphStatisticFile);
+            ImageIO.write(chartImage, "png", out);
+        } catch (IOException e) {
+            throw new IllegalArgumentException("Problem writing graphStatisticFile: " + graphStatisticFile, e);
+        } finally {
+            IOUtils.closeQuietly(out);
+        }
+    }
+
+    private Number extractScoreAlias(Score score) {
+        Number scoreAlias;
+        if (score instanceof SimpleScore) {
+            SimpleScore simpleScore = (SimpleScore) score;
+            scoreAlias = simpleScore.getScore();
+        } else if (score instanceof HardAndSoftScore) {
+            HardAndSoftScore hardAndSoftScore = (HardAndSoftScore) score;
+            if (hardAndSoftScore.getHardScore() == 0) {
+                scoreAlias = hardAndSoftScore.getSoftScore();
+            } else {
+                scoreAlias = null;
+            }
+        } else {
+            throw new IllegalStateException("Score class (" + score.getClass() + ") not supported.");
+        }
+        return scoreAlias;
+    }
+
 }

Modified: labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/SolverStatistic.java
===================================================================
--- labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/SolverStatistic.java	2010-02-26 10:38:41 UTC (rev 31851)
+++ labs/jbossrules/trunk/drools-planner/drools-planner-core/src/main/java/org/drools/planner/benchmark/statistic/SolverStatistic.java	2010-02-26 15:30:41 UTC (rev 31852)
@@ -10,12 +10,8 @@
  */
 public interface SolverStatistic {
 
-    void addListener(Solver solver);
-
     void addListener(Solver solver, String configName);
 
-    void removeListener(Solver solver);
-
     void removeListener(Solver solver, String configName);
 
     void writeStatistic(File solverStatisticFilesDirectory, String baseName);



More information about the jboss-svn-commits mailing list