Commit 6a37aa2b authored by mniehage's avatar mniehage
Browse files

plotting continuous places now also generates output dat file which can then...

plotting continuous places now also generates output dat file which can then be plotted by tool of choice
reorganised the plotting class, plotting with jfreechart can now be easier removed when generating jar
parent fa72ea01
......@@ -4,8 +4,8 @@
<discretePlace id="pd1" marking="10"/>
<discretePlace id="pd2" marking="0"/>
<discretePlace id="pd3" marking="0"/>
<continuousPlace capacity="0" infiniteCapacity="1" id="left" level="10"/>
<continuousPlace capacity="100" infiniteCapacity="0" id="right" level="0"/>
<continuousPlace capacity="0" infiniteCapacity="1" id="left" level="10" quantum="1"/>
<continuousPlace capacity="100" infiniteCapacity="0" id="right" level="0" quantum="1"/>
</places>
<transitions>
<continuousTransition id="tf1" rate="1.0"/>
......
set term pdfcairo enhanced crop font ',10'
set title "Fluid level of continuous places"
set xlabel "time"
set ylabel "fluid level"
set grid back
set key outside
FILES = system("ls -1 *.dat")
LABEL = system("ls -1 *.dat | sed -e 's/.dat//'")
do for [file in FILES] {
stats file u 1:2
}
set output "plot_fluid_levels.pdf"
plot [*:*][STATS_min_y*1.05:STATS_max_y*1.05] for [i=1:words(FILES)] word(FILES,i) u 1:2 title word(LABEL,i) noenhanced with lines
......@@ -15,7 +15,7 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.5</version>
<version>2.4.0-b180830.0359</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
......@@ -38,7 +38,7 @@
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.5</version>
<version>3.0.1</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
......@@ -62,7 +62,7 @@
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell</artifactId>
<version>1.1.0.RELEASE</version>
<version>1.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
......@@ -85,12 +85,12 @@
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreesvg</artifactId>
<version>3.4</version>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.19</version>
<version>1.5.3</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
......@@ -113,7 +113,7 @@
<dependency>
<groupId>ca.umontreal.iro.simul</groupId>
<artifactId>ssj</artifactId>
<version>3.1.1</version>
<version>3.3.1</version>
<exclusions>
<exclusion>
<groupId>org.bouncycastle</groupId>
......@@ -137,20 +137,20 @@
<dependency>
<groupId>org.mariuszgromada.math</groupId>
<artifactId>MathParser.org-mXparser</artifactId>
<version>4.1.1</version>
<version>4.4.2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>libs-milestone</id>
<url>http://repo.spring.io/libs-milestone/</url>
</repository>
<repository>
<id>libs-release</id>
<url>http://repo.spring.io/libs-release/</url>
</repository>
</repositories>
<!--<repositories>-->
<!-- <repository>-->
<!-- <id>libs-milestone</id>-->
<!-- <url>http://repo.spring.io/libs-milestone/</url>-->
<!-- </repository>-->
<!-- <repository>-->
<!-- <id>libs-release</id>-->
<!-- <url>http://repo.spring.io/libs-release/</url>-->
<!-- </repository>-->
<!-- </repositories>-->
<build>
......
package de.wwu.criticalsystems.libhpng.plotting;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Iterator;
public class ContinuousPlacesImage {
private XYLineGraph graph=new XYLineGraph("Fluid level of continuous places", "time", "fluid level");;
ContinuousPlacesImage() {
}
public void createImage() {
graph.pack();
//RefineryUtilities.centerFrameOnScreen(graph);
graph.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
graph.setVisible(true);
}
protected void addSeriesToGraph(String id, Integer series,ArrayList<ContinuousPlaceEntry> means, ArrayList<ContinuousPlaceEntry> ssquares) {
Color color = defineColor(series);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) graph.getPlot().getRenderer();
Shape bar = new Rectangle2D.Double(-0.5, -0.1, 1.0, 0.2);
graph.addSeries(id);
renderer.setSeriesPaint(series, color);
renderer.setSeriesShapesVisible((int) series, false);
Iterator<ContinuousPlaceEntry> iterator = means.iterator();
while(iterator.hasNext()){
ContinuousPlaceEntry entry = iterator.next();
graph.addSeriesEntry(id, entry.getTime(), entry.getFluidLevel());
}
graph.addSeries(id + "_up");
renderer.setSeriesPaint(series + 1, color);
renderer.setSeriesVisibleInLegend(series + 1, false);
renderer.setSeriesLinesVisible(series + 1, false);
renderer.setSeriesShapesVisible(series + 1, true);
renderer.setSeriesShape(series + 1, bar);
graph.addSeries(id + "_low");
renderer.setSeriesPaint(series + 2, color);
renderer.setSeriesVisibleInLegend(series + 2, false);
renderer.setSeriesLinesVisible(series + 2, false);
renderer.setSeriesShapesVisible(series + 2, true);
renderer.setSeriesShape(series + 2, bar);
}
private Color defineColor(Integer i) {
while (i > 5)
i -= 5;
int rgb = Color.HSBtoRGB(i.floatValue() / 5f, 0.5f, 0.9f);
return new Color(rgb);
}
}
......@@ -3,178 +3,162 @@ package de.wwu.criticalsystems.libhpng.plotting;
import de.wwu.criticalsystems.libhpng.model.ContinuousPlace;
import de.wwu.criticalsystems.libhpng.model.HPnGModel;
import de.wwu.criticalsystems.libhpng.model.Place;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.ui.RefineryUtilities;
import umontreal.ssj.probdist.StudentDist;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.util.ArrayList;
public class ContinuousPlacesPlotter {
public ContinuousPlacesPlotter() { }
public ContinuousPlacesPlotter() {
}
private ArrayList<ContinuousPlaceEntry> means = new ArrayList<ContinuousPlaceEntry>();
private ArrayList<ContinuousPlaceEntry> ssquares = new ArrayList<ContinuousPlaceEntry>();
public void plotContinuousPlaces(HPnGModel model, ArrayList<MarkingPlot> plots, Double maxTime, Double confidenceLevel, String imagePath){
int series=0;
public void createImage() {
}
public void plotContinuousPlaces(HPnGModel model, ArrayList<MarkingPlot> plots, Double maxTime, Double confidenceLevel, String imagePath) throws UnsupportedEncodingException {
// remove when generating jar
ContinuousPlacesImage image = new ContinuousPlacesImage();
int series = 0;
Double t;
System.out.println("Plotting graph...");
XYLineGraph graph = new XYLineGraph("Fluid level of continuous places", "time", "fluid level");
//find t distribution
if (plots.size() < 2)
t = 0.0;
else {
Double alphaHalf = (1.0 - confidenceLevel)/2.0;
else {
Double alphaHalf = (1.0 - confidenceLevel) / 2.0;
t = StudentDist.inverseF(plots.size() - 1, 1.0 - alphaHalf);
}
//plot means and confidence intervals for all continuous places
for (Place place : model.getPlaces()){
if (place.getClass().equals(ContinuousPlace.class)){
graph = addSeriesToGraph(graph, place.getId(), series);
plotMeansAndConfidenceIntervals((ContinuousPlace)place, plots, maxTime, graph, t);
series +=3;
for (Place place : model.getPlaces()) {
if (place.getClass().equals(ContinuousPlace.class)) {
plotMeansAndConfidenceIntervals((ContinuousPlace) place, plots, maxTime, t, imagePath);
image.addSeriesToGraph(place.getId(), series, means, ssquares);
series += 3;
}
}
graph.pack();
RefineryUtilities.centerFrameOnScreen(graph);
graph.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
graph.setVisible(true);
if (imagePath != null) {
graph.getSVG(imagePath);
}
image.createImage();
}
private void plotMeansAndConfidenceIntervals(ContinuousPlace place, ArrayList<MarkingPlot> plots, Double maxTime, XYLineGraph graph, Double t){
private void plotMeansAndConfidenceIntervals(ContinuousPlace place, ArrayList<MarkingPlot> plots, Double maxTime, Double t, String imagePath) {
Double meanFluid, interval, time, fluid, ssquareFluid;
PlotEntry currentEntry;
PlotEntry currentEntry = null;
means.clear();
ssquares.clear();
meanFluid = 0.0;
ssquareFluid = 0.0;
String path;
if (imagePath != null)
path = imagePath + place.getId()+ ".dat";
else
path = "plotData/" + place.getId() + ".dat";
BufferedWriter bufferedWriter = null;
try {
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("t " + place.getId() + System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
//for time=0.0, calculate mean and s² and save to graph
for (MarkingPlot plot : plots){
for (MarkingPlot plot : plots) {
currentEntry = plot.getPlacePlots().get(place.getId()).getNextEntryBeforeOrAtGivenTime(0.0);
fluid = ((ContinuousPlaceEntry)currentEntry).getFluidLevel();
fluid = ((ContinuousPlaceEntry) currentEntry).getFluidLevel();
meanFluid += fluid;
}
}
meanFluid = meanFluid / plots.size();
means.add(new ContinuousPlaceEntry(0.0, meanFluid, null));
graph.addSeriesEntry(place.getId(), 0.0, meanFluid);
for (MarkingPlot plot : plots){
//graph.addSeriesEntry(place.getId(), 0.0, meanFluid);
try {
bufferedWriter.append("0.0" + " " + meanFluid + System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
for (MarkingPlot plot : plots) {
currentEntry = plot.getPlacePlots().get(place.getId()).getNextEntryBeforeOrAtGivenTime(0.0);
fluid = ((ContinuousPlaceEntry)currentEntry).getFluidLevel();
ssquareFluid += Math.pow(fluid - meanFluid,2);
fluid = ((ContinuousPlaceEntry) currentEntry).getFluidLevel();
ssquareFluid += Math.pow(fluid - meanFluid, 2);
}
ssquareFluid = ssquareFluid / (plots.size()-1);
ssquareFluid = ssquareFluid / (plots.size() - 1);
ssquares.add(new ContinuousPlaceEntry(0.0, ssquareFluid, null));
graph.addSeriesEntry(place.getId() + "_up", 0.0, (meanFluid + t * Math.sqrt(ssquareFluid/plots.size())));
graph.addSeriesEntry(place.getId() + "_low", 0.0, (meanFluid - t * Math.sqrt(ssquareFluid/plots.size())));
//for all event timings up to maxTime, calculate mean and s² and save to graph
time=0.0;
interval = maxTime;
while (time <= maxTime && interval > 0.0){
time = 0.0;
interval = maxTime;
while (time <= maxTime && interval > 0.0) {
meanFluid = 0.0;
ssquareFluid = 0.0;
interval = maxTime-time;
for (MarkingPlot plot : plots){
interval = maxTime - time;
for (MarkingPlot plot : plots) {
currentEntry = plot.getPlacePlots().get(place.getId()).getNextEntryAfterGivenTime(time);
if (currentEntry.getTime() < (time + interval) && currentEntry.getTime() > time)
interval = currentEntry.getTime() - time;
interval = currentEntry.getTime() - time;
}
time+=interval;
for (MarkingPlot plot : plots){
time += interval;
for (MarkingPlot plot : plots) {
currentEntry = plot.getPlacePlots().get(place.getId()).getNextEntryBeforeOrAtGivenTime(time);
fluid = ((ContinuousPlaceEntry)currentEntry).getFluidLevel();
fluid = ((ContinuousPlaceEntry) currentEntry).getFluidLevel();
if (currentEntry.getTime() < time)
fluid = Math.max(0.0, fluid + ((ContinuousPlaceEntry)currentEntry).getDrift()*(time - currentEntry.getTime()));
fluid = Math.max(0.0, fluid + ((ContinuousPlaceEntry) currentEntry).getDrift() * (time - currentEntry.getTime()));
meanFluid += fluid;
}
meanFluid = meanFluid / plots.size();
}
meanFluid = meanFluid / plots.size();
means.add(new ContinuousPlaceEntry(time, meanFluid, 0.0));
graph.addSeriesEntry(place.getId(),time, meanFluid);
for (MarkingPlot plot : plots){
System.out.println("Time: " + time + " entry " + meanFluid);
try {
bufferedWriter.append(time + " " + meanFluid + System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
for (MarkingPlot plot : plots) {
currentEntry = plot.getPlacePlots().get(place.getId()).getNextEntryBeforeOrAtGivenTime(time);
fluid = ((ContinuousPlaceEntry)currentEntry).getFluidLevel();
fluid = ((ContinuousPlaceEntry) currentEntry).getFluidLevel();
if (currentEntry.getTime() < time)
fluid = Math.max(0.0, fluid + ((ContinuousPlaceEntry)currentEntry).getDrift()*(time - currentEntry.getTime()));
ssquareFluid += Math.pow(fluid - meanFluid,2);
}
ssquareFluid = ssquareFluid / (plots.size() - 1);
fluid = Math.max(0.0, fluid + ((ContinuousPlaceEntry) currentEntry).getDrift() * (time - currentEntry.getTime()));
ssquareFluid += Math.pow(fluid - meanFluid, 2);
}
ssquareFluid = ssquareFluid / (plots.size() - 1);
ssquares.add(new ContinuousPlaceEntry(time, ssquareFluid, null));
//graph.addSeriesEntry(place.getId() + "_up", time, (meanFluid + t * Math.sqrt(ssquareFluid/plots.size())));
//graph.addSeriesEntry(place.getId() + "_low", time, (meanFluid - t * Math.sqrt(ssquareFluid/plots.size())));
}
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private XYLineGraph addSeriesToGraph(XYLineGraph oldGraph, String id, Integer series) {
XYLineGraph graph = oldGraph;
Color color = defineColor(series);
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) graph.getPlot().getRenderer();
Shape bar = new Rectangle2D.Double(-0.5, -0.1, 1.0, 0.2);
graph.addSeries(id);
renderer.setSeriesPaint(series, color);
renderer.setSeriesShapesVisible((int)series, false);
graph.addSeries(id + "_up");
renderer.setSeriesPaint(series + 1, color);
renderer.setSeriesVisibleInLegend(series + 1, false);
renderer.setSeriesLinesVisible(series + 1, false);
renderer.setSeriesShapesVisible(series + 1, true);
renderer.setSeriesShape(series + 1, bar);
graph.addSeries(id+ "_low");
renderer.setSeriesPaint(series + 2, color);
renderer.setSeriesVisibleInLegend(series + 2, false);
renderer.setSeriesLinesVisible(series + 2, false);
renderer.setSeriesShapesVisible(series + 2, true);
renderer.setSeriesShape(series + 2, bar);
return graph;
}
private Color defineColor(Integer i){
while (i>5)
i-=5;
int rgb = Color.HSBtoRGB(i.floatValue()/5f,0.5f,0.9f);
return new Color(rgb);
}
}
......@@ -7,13 +7,8 @@ import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
public class XYLineGraph extends JFrame {
......@@ -48,14 +43,5 @@ public class XYLineGraph extends JFrame {
return (XYPlot)chart.getPlot();
}
public void getSVG(String imagePath){
SVGGraphics2D svg = new SVGGraphics2D(750,405);
chart.draw(svg, new Rectangle(750,405));
String svgElement = svg.getSVGElement();
try {
SVGUtils.writeToSVG(new File(imagePath),svgElement);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment