001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.algorithm.measure;
033
034import org.graphstream.algorithm.AlgorithmComputationTrigger;
035import org.graphstream.algorithm.DynamicAlgorithm;
036import org.graphstream.algorithm.AlgorithmComputationTrigger.Mode;
037import org.graphstream.graph.Graph;
038import org.graphstream.stream.Sink;
039import org.jfree.chart.ChartFactory;
040import org.jfree.chart.JFreeChart;
041import org.jfree.data.xy.XYSeriesCollection;
042
043public class ChartConnectivityMeasure extends ChartMeasure implements
044                DynamicAlgorithm {
045
046        public static class ChartVertexConnectivityMeasure extends
047                        ChartSeries2DMeasure implements DynamicAlgorithm {
048                Graph g;
049                Sink trigger;
050
051                public ChartVertexConnectivityMeasure() {
052                        super("vertex-connectivity");
053                        trigger = new AlgorithmComputationTrigger(Mode.BY_STEP, this);
054                }
055
056                /*
057                 * (non-Javadoc)
058                 * 
059                 * @see org.graphstream.algorithm.DynamicAlgorithm#terminate()
060                 */
061                public void terminate() {
062                        g.removeSink(trigger);
063                        g = null;
064                }
065
066                /*
067                 * (non-Javadoc)
068                 * 
069                 * @see org.graphstream.algorithm.Algorithm#compute()
070                 */
071                public void compute() {
072                        addValue(g.getStep(), ConnectivityMeasure.getVertexConnectivity(g));
073                }
074
075                /*
076                 * (non-Javadoc)
077                 * 
078                 * @see
079                 * org.graphstream.algorithm.Algorithm#init(org.graphstream.graph.Graph)
080                 */
081                public void init(Graph graph) {
082                        g = graph;
083                        g.addSink(trigger);
084                }
085        }
086
087        public static class ChartEdgeConnectivityMeasure extends
088                        ChartSeries2DMeasure implements DynamicAlgorithm {
089                Graph g;
090                Sink trigger;
091
092                public ChartEdgeConnectivityMeasure() {
093                        super("edge-connectivity");
094                        trigger = new AlgorithmComputationTrigger(Mode.BY_STEP, this);
095                }
096
097                /*
098                 * (non-Javadoc)
099                 * 
100                 * @see org.graphstream.algorithm.DynamicAlgorithm#terminate()
101                 */
102                public void terminate() {
103                        g.removeSink(trigger);
104                        g = null;
105                }
106
107                /*
108                 * (non-Javadoc)
109                 * 
110                 * @see org.graphstream.algorithm.Algorithm#compute()
111                 */
112                public void compute() {
113                        addValue(g.getStep(), ConnectivityMeasure.getEdgeConnectivity(g));
114                }
115
116                /*
117                 * (non-Javadoc)
118                 * 
119                 * @see
120                 * org.graphstream.algorithm.Algorithm#init(org.graphstream.graph.Graph)
121                 */
122                public void init(Graph graph) {
123                        g = graph;
124                        g.addSink(trigger);
125                }
126        }
127
128        protected ChartVertexConnectivityMeasure vertexConnectivity;
129        protected ChartEdgeConnectivityMeasure edgeConnectivity;
130
131        public ChartConnectivityMeasure() {
132                this(new ChartVertexConnectivityMeasure(),
133                                new ChartEdgeConnectivityMeasure());
134        }
135
136        public ChartConnectivityMeasure(
137                        ChartVertexConnectivityMeasure vertexConnectivity,
138                        ChartEdgeConnectivityMeasure edgeConnectivity) {
139                super("connectivity");
140
141                this.vertexConnectivity = vertexConnectivity;
142                this.edgeConnectivity = edgeConnectivity;
143        }
144
145        public ChartVertexConnectivityMeasure getVertexConnectivityMeasure() {
146                return vertexConnectivity;
147        }
148
149        public ChartEdgeConnectivityMeasure getEdgeConnectivityMeasure() {
150                return edgeConnectivity;
151        }
152
153        /*
154         * (non-Javadoc)
155         * 
156         * @see org.graphstream.algorithm.DynamicAlgorithm#terminate()
157         */
158        public void terminate() {
159                vertexConnectivity.terminate();
160                edgeConnectivity.terminate();
161        }
162
163        /*
164         * (non-Javadoc)
165         * 
166         * @see org.graphstream.algorithm.Algorithm#compute()
167         */
168        public void compute() {
169        }
170
171        /*
172         * (non-Javadoc)
173         * 
174         * @see
175         * org.graphstream.algorithm.Algorithm#init(org.graphstream.graph.Graph)
176         */
177        public void init(Graph graph) {
178                vertexConnectivity.init(graph);
179                edgeConnectivity.init(graph);
180        }
181
182        /*
183         * (non-Javadoc)
184         * 
185         * @see
186         * org.graphstream.algorithm.measure.ChartMeasure#createChart(org.graphstream
187         * .algorithm.measure.ChartMeasure.PlotParameters)
188         */
189        public JFreeChart createChart(PlotParameters params) throws PlotException {
190                JFreeChart chart;
191                XYSeriesCollection dataset = new XYSeriesCollection();
192
193                dataset.addSeries(vertexConnectivity.getXYSeries());
194                dataset.addSeries(edgeConnectivity.getXYSeries());
195
196                switch (params.type) {
197                case LINE:
198                        chart = ChartFactory.createXYLineChart(params.title,
199                                        params.xAxisLabel, params.yAxisLabel, dataset,
200                                        params.orientation, params.showLegend, false, false);
201                        break;
202                case BAR:
203                        chart = ChartFactory.createXYBarChart(params.title,
204                                        params.xAxisLabel, false, params.yAxisLabel, dataset,
205                                        params.orientation, params.showLegend, false, false);
206                        break;
207                case SCATTER:
208                        chart = ChartFactory.createScatterPlot(params.title,
209                                        params.xAxisLabel, params.yAxisLabel, dataset,
210                                        params.orientation, params.showLegend, false, false);
211                        break;
212                default:
213                        throw new PlotException("unsupported plot type");
214                }
215
216                return chart;
217        }
218
219        /*
220         * (non-Javadoc)
221         * 
222         * @see
223         * org.graphstream.algorithm.measure.ChartMeasure#plot(org.graphstream.algorithm
224         * .measure.ChartMeasure.PlotParameters)
225         */
226        public void plot(PlotParameters params) throws PlotException {
227                outputPlot(params, createChart(params));
228        }
229
230        /*
231         * (non-Javadoc)
232         * 
233         * @see
234         * org.graphstream.algorithm.measure.ChartMeasure#getDefaultPlotParameters()
235         */
236        public PlotParameters getDefaultPlotParameters() {
237                PlotParameters params = new PlotParameters();
238                params.title = name;
239                params.type = PlotType.LINE;
240                params.xAxisLabel = "steps";
241                params.yAxisLabel = "connectivity";
242
243                return params;
244        }
245}