Code Examples
A repository of 155 code examples for BeepBeep
ForkMutable.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.Processor;
22 import ca.uqac.lif.cep.Pushable;
23 import ca.uqac.lif.cep.UniformProcessor;
24 
25 import static ca.uqac.lif.cep.Connector.INPUT;
26 
27 import ca.uqac.lif.cep.io.Print;
28 import ca.uqac.lif.cep.tmf.Fork;
29 import java.util.ArrayList;
30 import java.util.List;
31 
32 /**
33  * See the effect of sending a mutable object into a
34  * {@link ca.uqac.lif.cep.tmf.Fork Fork} processor. Graphically, the processors
35  * of this example can be drawn as follows:
36  * <p>
37  * <img
38  * src="./doc-files/basic/ForkMutable.png"
39  * alt="Processor graph">
40  * @author Sylvain HallĂ©
41  * @difficulty Easy
42  */
43 public class ForkMutable
44 {
45  public static void main(String[] args) throws InterruptedException
46  {
47  /* We create a fork processor that will replicate each input event
48  * in three output streams. The "3" passed as an argument to the fork's
49  * constructor signifies this. */
50  ///
51  Fork fork = new Fork(3);
52 
53  /* We now create three "print" processors. Each simply prints to the
54  * standard output whatever event they receive. We ask each of them to
55  * append their printed line with a different prefix ("Px") so we can
56  * know who is printing what. */
57  Print p0 = new Print().setSeparator("\n").setPrefix("P0 ");
58  Print p1 = new Print().setSeparator("\n").setPrefix("P1 ");
59  Print p2 = new Print().setSeparator("\n").setPrefix("P2 ");
60 
61  /* We finally connect each of the three outputs streams of the fork
62  * (numbered 0, 1 and 2) to the input of each print processor. */
63  Processor rf = new RemoveFirst();
64  Connector.connect(fork, 0, p0, INPUT);
65  Connector.connect(fork, 1, rf, INPUT);
66  Connector.connect(rf, p1);
67  Connector.connect(fork, 2, p2, INPUT);
68  ///
69 
70  /* Let's now push an event to the input of the fork and see what
71  * happens. */
72  //*
73  Pushable p = fork.getPushableInput();
74  List<Number> list = new ArrayList<Number>();
75  list.add(3); list.add(1); list.add(4);
76  p.push(list);
77  //*
78 
79  /* You should normally see
80  * P0 foo
81  * P1 foo
82  * P2 foo
83  * be printed almost instantaneously. This shows that all three print
84  * processors received their input event at the "same" time. This is
85  * not exactly true: the fork processor pushes the event to each of its
86  * outputs in sequence; however, since the time it takes to do so is so
87  * short, we can consider this to be instantaneous. */
88  }
89 
90  /**
91  * A processor that removes the first element of the list it receives
92  * and returns that list. It does not have a special purpose, except
93  * to illustrate the behaviour of Fork in some special cases.
94  */
95  protected static class RemoveFirst extends UniformProcessor
96  {
97  public RemoveFirst()
98  {
99  super(1, 1);
100 
101  }
102  @Override
103  protected boolean compute(Object[] inputs, Object[] outputs)
104  {
105  List<?> list = (List<?>) inputs[0];
106  list.remove(0);
107  outputs[0] = list;
108  return true;
109  }
110 
111  @Override
112  public Processor duplicate(boolean with_state)
113  {
114  // Not necessary to implement this for the example
115  return null;
116  }
117  }
118 }
See the effect of sending a mutable object into a Fork processor.