Code Examples
A repository of 155 code examples for BeepBeep
FakeSignal.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2018 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 signal;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.functions.ApplyFunction;
22 import ca.uqac.lif.cep.functions.Cumulate;
23 import ca.uqac.lif.cep.functions.CumulativeFunction;
24 import ca.uqac.lif.cep.functions.TurnInto;
25 import ca.uqac.lif.cep.mtnp.DrawPlot;
26 import ca.uqac.lif.cep.mtnp.UpdateTableStream;
27 import ca.uqac.lif.cep.tmf.Fork;
28 import ca.uqac.lif.cep.tmf.Pump;
29 import ca.uqac.lif.cep.tmf.QueueSource;
30 import ca.uqac.lif.cep.tmf.VariableStutter;
31 import ca.uqac.lif.cep.widgets.ToImageIcon;
32 import ca.uqac.lif.cep.widgets.WidgetSink;
33 import ca.uqac.lif.cep.util.Numbers;
34 import ca.uqac.lif.mtnp.plot.gral.Scatterplot;
35 import java.awt.FlowLayout;
36 import javax.swing.JFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JPanel;
39 
40 public class FakeSignal
41 {
42 
43  public static void main(String[] args)
44  {
45  QueueSource values = new QueueSource();
46  values.setEvents(0, 10, -5, 0, -7, 0);
47  QueueSource numbers = new QueueSource();
48  numbers.setEvents(5, 5, 3, 5, 5, 5);
49  values.loop(false);
50  VariableStutter vs = new VariableStutter();
51  Connector.connect(values, 0, vs, 0);
52  Connector.connect(numbers, 0, vs, 1);
53  Fork fork = new Fork(2);
54  Connector.connect(vs, fork);
55  TurnInto one = new TurnInto(1);
56  Connector.connect(fork, 0, one, 0);
57  Cumulate sum_one = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
58  Connector.connect(one, sum_one);
59  Cumulate sum_values = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
60  Connector.connect(fork, 1, sum_values, 0);
61  UpdateTableStream uts = new UpdateTableStream("T", "V");
62  Connector.connect(sum_one, 0, uts, 0);
63  Connector.connect(sum_values, 0, uts, 1);
64  DrawPlot plot = new DrawPlot(new Scatterplot());
65  Connector.connect(uts, plot);
66  ApplyFunction to_icon = new ApplyFunction(ToImageIcon.instance);
67  Connector.connect(plot, to_icon);
68  Pump pump = new Pump();
69  Connector.connect(to_icon, pump);
70 
71  // Create a frame to display the plot
72  JFrame frame = new JFrame("My plot");
73  JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
74  frame.add(panel);
75  frame.setSize(800, 600);
76  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
77  JLabel label = new JLabel();
78  panel.add(label);
79  WidgetSink sink = new WidgetSink(label);
80  Connector.connect(pump, sink);
81  frame.setVisible(true);
82  pump.start();
83  }
84 }