Code Examples
A repository of 155 code examples for BeepBeep
ForkPush.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 basic;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.Pushable;
22 import static ca.uqac.lif.cep.Connector.INPUT;
23 import ca.uqac.lif.cep.io.Print;
24 import ca.uqac.lif.cep.tmf.Fork;
25 
26 /**
27  * Use the {@link ca.uqac.lif.cep.tmf.Fork Fork} processor to replicate
28  * input events in multiple output streams. Graphically, the processors
29  * of this example can be drawn as follows:
30  * <p>
31  * <img
32  * src="./doc-files/basic/ForkPush.png"
33  * alt="Processor graph">
34  * <p>
35  * The expected output of this program is:
36  * <pre>
37  * P0 foo
38  * P1 foo
39  * P2 foo
40  * P0 bar
41  * P1 bar
42  * P2 bar
43  * </pre>
44  * @author Sylvain HallĂ©
45  * @difficulty Easy
46  */
47 public class ForkPush
48 {
49  public static void main(String[] args) throws InterruptedException
50  {
51  /* We create a fork processor that will replicate each input event
52  * in three output streams. The "3" passed as an argument to the fork's
53  * constructor signifies this. */
54  ///
55  Fork fork = new Fork(3);
56 
57  /* We now create three "print" processors. Each simply prints to the
58  * standard output whatever event they receive. We ask each of them to
59  * append their printed line with a different prefix ("Px") so we can
60  * know who is printing what. */
61  Print p0 = new Print().setSeparator("\n").setPrefix("P0 ");
62  Print p1 = new Print().setSeparator("\n").setPrefix("P1 ");
63  Print p2 = new Print().setSeparator("\n").setPrefix("P2 ");
64 
65  /* We finally connect each of the three outputs streams of the fork
66  * (numbered 0, 1 and 2) to the input of each print processor. */
67  Connector.connect(fork, 0, p0, INPUT);
68  Connector.connect(fork, 1, p1, INPUT);
69  Connector.connect(fork, 2, p2, INPUT);
70 
71  /* Let's now push an event to the input of the fork and see what
72  * happens. */
73  Pushable p = fork.getPushableInput();
74  p.push("foo");
75 
76  /* You should normally see
77  * P0 foo
78  * P1 foo
79  * P2 foo
80  * be printed almost instantaneously. This shows that all three print
81  * processors received their input event at the "same" time. This is
82  * not exactly true: the fork processor pushes the event to each of its
83  * outputs in sequence; however, since the time it takes to do so is so
84  * short, we can consider this to be instantaneous. */
85  ///
86 
87  /* Wait a little and do it again */
88  Thread.sleep(2000);
89  p.push("bar");
90  }
91 
92 }
Use the Fork processor to replicate input events in multiple output streams.
Definition: ForkPush.java:47