Code Examples
A repository of 155 code examples for BeepBeep
WindowScatterplot.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 ca.uqac.lif.cep.functions.ApplyFunction;
22 import ca.uqac.lif.cep.mtnp.DrawPlot;
23 import ca.uqac.lif.cep.mtnp.UpdateTableArray;
24 import ca.uqac.lif.cep.tmf.CountDecimate;
25 import ca.uqac.lif.cep.tmf.QueueSource;
26 import ca.uqac.lif.cep.tmf.Window;
27 import ca.uqac.lif.cep.util.Bags;
28 import ca.uqac.lif.mtnp.plot.gral.Scatterplot;
29 
30 /**
31  * Update a windowed 2D scatterplot in realtime from two streams of numbers.
32  * This example is similar to {@link CumulativeScatterplot} (which you should
33  * study first), except for three things:
34  * <ul>
35  * <li>The two outputs of the {@link RandomTwoD} processor are merged into
36  * arrays of size 2 by the {@link ToArray} processor.</li>
37  * <li>The creation of a table out of input streams is done with an
38  * {@link UpdateTableArray} processor</li>
39  * <li>The creation of a table out of input streams is encased in a
40  * {@link Window} processor} of 100 events, and only one table every
41  * 20 is pushed downstream.</li>
42  * </ul>
43  * Graphically, this chain of processor can be described as follows:
44  * <p>
45  * <img src="./doc-files/plots/WindowScatterplot.png" alt="Processor graph">
46  * @author Sylvain HallĂ©
47  * @difficulty Medium
48  */
49 public class WindowScatterplot
50 {
51  public static void main(String[] args) throws InterruptedException
52  {
53  /* Create a stream of random x-y pairs, as in
54  * {@link CumulativeScatterplot} */
55  QueueSource one = new QueueSource();
56  one.addEvent(1);
57  RandomTwoD random = new RandomTwoD();
58  Connector.connect(one, random);
59 
60  /* Merge the two streams into arrays of size 2. Each output event
61  * is an array with the x and the y value. Since random has an
62  * output arity of 2, and array_convert has an input arity of 2,
63  * we don't need to explicitly connect both input/output pairs. */
64  ApplyFunction array_convert = new ApplyFunction(new Bags.ToArray(Number.class, Number.class));
65  Connector.connect(random, array_convert);
66 
67  /* Use the UpdateTable processor that takes as input a single
68  * stream of arrays. */
69  UpdateTableArray update_table = new UpdateTableArray("x", "y");
70 
71  /* We encase this processor in a window processor of width 100 */
72  Window window = new Window(update_table, 100);
73 
74  /* It is this processor that we connect to array_convert */
75  Connector.connect(array_convert, window);
76 
77  /* To slow down the refreshing of the plot, we keep only one
78  * table for every 20 output by the window processor. This is done
79  * with a CountDecimate processor. */
80  CountDecimate decimate = new CountDecimate(20);
81  Connector.connect(window, decimate);
82 
83  /* The plot, draw and display parts of this example are identical
84  * to {@link CumulativeScatterplot}. */
85  Scatterplot plot = new Scatterplot();
86  DrawPlot draw = new DrawPlot(plot);
87  Connector.connect(decimate, draw);
88  BitmapJFrame frame = new BitmapJFrame();
89  Connector.connect(draw, frame);
90  frame.start();
91  System.out.println("Displaying plot. Press Ctrl+C or close the window to end.");
92  while (true)
93  {
94  one.push();
95  Thread.sleep(10);
96  }
97  }
98 }
Update a windowed 2D scatterplot in realtime from two streams of numbers.
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.