Code Examples
A repository of 155 code examples for BeepBeep
SlicerSimple.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 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 static ca.uqac.lif.cep.Connector.INPUT;
22 import static ca.uqac.lif.cep.Connector.OUTPUT;
23 import ca.uqac.lif.cep.GroupProcessor;
24 import ca.uqac.lif.cep.Pullable;
25 import ca.uqac.lif.cep.functions.Constant;
26 import ca.uqac.lif.cep.functions.Cumulate;
27 import ca.uqac.lif.cep.functions.CumulativeFunction;
28 import ca.uqac.lif.cep.functions.Function;
29 import ca.uqac.lif.cep.functions.IdentityFunction;
30 import ca.uqac.lif.cep.functions.TurnInto;
31 import ca.uqac.lif.cep.tmf.QueueSource;
32 import ca.uqac.lif.cep.tmf.Slice;
33 import ca.uqac.lif.cep.util.Numbers;
34 
35 /**
36  * Use the {@link ca.uqac.lif.cep.tmf.Slice SlicerMap} to perform a
37  * computation on multiple subsets of an input stream.
38  * <p>
39  * The principle of the <tt>SlicerMap</tt> processor is to "slice" an
40  * input stream into multiple sub-streams. For this, a function, called
41  * the <em>slicing function</em>, is evaluated on each input event. The
42  * value of that function decides what sub-stream that event
43  * belongs to. A different instance of a processor is created for each
44  * possible value of the slicing function, and the input event is pushed
45  * to the corresponding processor.
46  * <p>
47  * The slicer keeps an associative map; keys correspond to values of the
48  * slicing function, and values are the last event produced by the
49  * corresponding processor. Every time an event is received, it returns as
50  * its output the updated map.
51  * <p>
52  * In this program, the slicing function is the identity, and the processor
53  * given to the slicer is a simple counter that increments every time an event
54  * is received. Since there is one such counter for each different input
55  * event, the slicer effectively maintains the count of how many times each
56  * value has been seen in its input stream. Graphically, this can be
57  * represented as:
58  * <p>
59  * <img src="./doc-files/basic/SlicerSimple.png" alt="Processor chain">
60  * <p>
61  * The expected output of this program is:
62  * <pre>
63  * {1=1.0}
64  * {1=1.0, 6=1.0}
65  * {1=1.0, 4=1.0, 6=1.0}
66  * {1=1.0, 3=1.0, 4=1.0, 6=1.0}
67  * {1=1.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
68  * {1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
69  * {1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0, 9=1.0}
70  * &hellip;
71  * </pre>
72  * @author Sylvain HallĂ©
73  * @difficulty Easy
74  */
75 public class SlicerSimple
76 {
77  public static void main(String[] args)
78  {
79  /// We first setup a stream of numbers to be used as a source
80  QueueSource source = new QueueSource();
81  source.setEvents(1, 6, 4, 3, 2, 1, 9);
82 
83  /* The function we'll use to create slices is the identity.
84  * This will create one distinct subtrace per number .*/
85  Function slicing_fct = new IdentityFunction(1);
86 
87  /* The processor chain to apply to each subtrace is a simple
88  * counter of events. */
89  GroupProcessor counter = new GroupProcessor(1, 1);
90  {
91  TurnInto to_one = new TurnInto(new Constant(1));
92  Cumulate sum = new Cumulate(
93  new CumulativeFunction<Number>(Numbers.addition));
94  Connector.connect(to_one, sum);
95  counter.addProcessors(to_one, sum);
96  counter.associateInput(INPUT, to_one, INPUT);
97  counter.associateOutput(OUTPUT, sum, OUTPUT);
98  }
99 
100  /* Create the slicer processor, by giving it the slicing function and
101  * the processor to apply on each slide. */
102  Slice slicer = new Slice(slicing_fct, counter);
103  Connector.connect(source, slicer);
104 
105  /* Let us now pull and print 10 events from the slicer. */
106  Pullable p = slicer.getPullableOutput();
107  for (int i = 0; i < 10; i++)
108  {
109  Object o = p.pull();
110  System.out.println(o);
111  }
112  ///
113  }
114 }
Use the SlicerMap to perform a computation on multiple subsets of an input stream.
static void main(String[] args)