Code Examples
A repository of 155 code examples for BeepBeep
CumulativeScatterplot.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 Sylvain HallĂ©
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package plots;
19 
20 import ca.uqac.lif.cep.Connector;
21 import static ca.uqac.lif.cep.Connector.TOP;
22 import static ca.uqac.lif.cep.Connector.BOTTOM;
23 import ca.uqac.lif.cep.mtnp.DrawPlot;
24 import ca.uqac.lif.cep.mtnp.UpdateTable;
25 import ca.uqac.lif.cep.mtnp.UpdateTableStream;
26 import ca.uqac.lif.cep.tmf.Pump;
27 import ca.uqac.lif.cep.tmf.QueueSource;
28 import ca.uqac.lif.mtnp.plot.gral.Scatterplot;
29 
30 /**
31  * Update a 2D scatterplot in realtime from two streams of numbers.
32  * This example generates dummy (x,y) pairs of numbers, accumulates them
33  * into a table, and shows a scatterplot of those numbers in a window
34  * such as this one:
35  * <p>
36  * <img src="./doc-files/plots/window-plot.png" alt="Processor graph" width="400">
37  * <p>
38  * Graphically, this chain of processor can be described as follows:
39  * <p>
40  * <img src="./doc-files/plots/CumulativeScatterplot.png" alt="Processor graph">
41  * @author Sylvain HallĂ©
42  * @difficulty Easy
43  */
45 {
46 
47  public static void main(String[] args) throws InterruptedException
48  {
49  /* A stream of (x,y) pairs is first created,
50  * with x an incrementing integer, and y a randomly selected number
51  * (see {@link RandomTwoD}).
52  */
53  ///
54  QueueSource one = new QueueSource().setEvents(1);
55  Pump pump = new Pump(1000);
56  RandomTwoD random = new RandomTwoD();
57  Connector.connect(one, pump, random);
58 
59  /* The resulting x-stream and y-streams are then pushed into an
60  * {@link UpdateTableStream} processor. We instantiate this processor,
61  * telling it to create an empty {@link Table} object with two
62  * columns, called "x" and "y". */
63  UpdateTable table = new UpdateTableStream("x", "y");
64 
65  /* This creates a processor with two input streams, one for the "x"
66  * values, and the other for the "y" values. Each pair of values from
67  * the x and y streams is used to append a new line to the (initially
68  * empty) table. We connect the two
69  * outputs of the random processor to these two inputs. */
70  Connector.connect(random, TOP, table, TOP);
71  Connector.connect(random, BOTTOM, table, BOTTOM);
72 
73  /* The next step is to create a plot out of the table's content.
74  * The {@link DrawPlot} processor receives a Table and passes it to
75  * a {@link ca.uqac.lif.mtnp.Plot Plot} object from the MTNP
76  * library. In our case, we want to create a scatterplot from the
77  * table's contents. */
78  DrawPlot draw = new DrawPlot(new Scatterplot());
79  Connector.connect(table, draw);
80 
81  /* Each event that comes out of the DrawPlot processor is an array
82  * of bytes corresponding to a bitmap image. To display that image,
83  * we use the BitmapJFrame processor, which opens a window and
84  * displays the image inside. */
85  BitmapJFrame window = new BitmapJFrame();
86  Connector.connect(draw, window);
87 
88  /* We need to call the start() method so that the window becomes
89  * visible. */
90  window.start();
91 
92  /* All set! We'll now repeatedly tell the source to push events
93  * out. You should see the window being updated every second with
94  * more and more data points.
95  */
96  System.out.println("Displaying plot. Press Ctrl+C "
97  + "or close the window to end.");
98  Thread th = new Thread(pump);
99  th.start();
100  ///
101  }
102 }
Generates a random stream of x-y pairs.
Definition: RandomTwoD.java:49
Receives a byte array as an input, and shows it in a Swing window as a picture.
Update a 2D scatterplot in realtime from two streams of numbers.