Code Examples
A repository of 155 code examples for BeepBeep
ForkPull.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.Pullable;
22 import ca.uqac.lif.cep.tmf.Fork;
23 import ca.uqac.lif.cep.tmf.QueueSource;
24 
25 /**
26  * Use the {@link ca.uqac.lif.cep.tmf.Fork Fork} processor to replicate
27  * input events in multiple output streams. Graphically, the processors
28  * of this example can be drawn as follows:
29  * <p>
30  * <img src="./doc-files/basic/ForkPull.png" alt="Processor graph">
31  * <p>
32  * The expected output of this program is:
33  * <pre>
34  * P0 foo
35  * P1 foo
36  * P2 foo
37  * P0 bar
38  * P1 bar
39  * P2 bar
40  * </pre>
41  * @author Sylvain HallĂ©
42  * @difficulty Easy
43  */
44 public class ForkPull
45 {
46  public static void main(String[] args) throws InterruptedException
47  {
48  /// Create a queue source with a few numbers
49  QueueSource source = new QueueSource().setEvents(1, 2, 3, 4, 5);
50 
51  /* We create a fork processor that will replicate each input event
52  * in two output streams. The "2" passed as an argument to the fork's
53  * constructor signifies this. Then we connect the source to the
54  * fork's input pipe. */
55  Fork fork = new Fork(2);
56  Connector.connect(source, fork);
57 
58  /* We get Pullables on both outputs of the fork. */
59  Pullable p0 = fork.getPullableOutput(0);
60  Pullable p1 = fork.getPullableOutput(1);
61 
62  /* Let's now pull an event from p0. The output is 1. */
63  System.out.println("Output from p0: " + p0.pull());
64 
65  /* Let's now pull an event from p1. Surprisingly, the output is 1.
66  * This can be explained by the fact that each input event in the
67  * fork is replicated to all its output pipes. The fact that we
68  * pulled an event from p0 has no effect on p1, and vice versa. */
69  System.out.println("Output from p1: " + p1.pull());
70  ///
71 
72  /* The independence between the fork's two outputs is further
73  * illustrated by this sequence of calls. Notice how each pullable
74  * moves through the input stream independently of calls to the
75  * other pullable. */
76  //*
77  System.out.println("Output from p0: " + p0.pull());
78  System.out.println("Output from p0: " + p0.pull());
79  System.out.println("Output from p1: " + p1.pull());
80  System.out.println("Output from p0: " + p0.pull());
81  System.out.println("Output from p1: " + p1.pull());
82  //*
83  }
84 
85 }
static void main(String[] args)
Definition: ForkPull.java:46
Use the Fork processor to replicate input events in multiple output streams.
Definition: ForkPull.java:44