Code Examples
A repository of 155 code examples for BeepBeep
SlicerOddEven.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 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 compute the
37  * sum of odd and even numbers separately.
38  * Graphically, this can be represented as:
39  * <p>
40  * <img src="./doc-files/basic/SlicerOddEven.png" alt="Processor chain">
41  * <p>
42  * The expected output of this program is:
43  * <pre>
44  * {1=1.0}
45  * {1=1.0, 6=1.0}
46  * {1=1.0, 4=1.0, 6=1.0}
47  * {1=1.0, 3=1.0, 4=1.0, 6=1.0}
48  * {1=1.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
49  * {1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0}
50  * {1=2.0, 2=1.0, 3=1.0, 4=1.0, 6=1.0, 9=1.0}
51  * &hellip;
52  * </pre>
53  * @author Sylvain HallĂ©
54  * @see SlicerSimple
55  * @difficulty Easy
56  */
57 public class SlicerOddEven
58 {
59  public static void main(String[] args)
60  {
61  /// We first setup a stream of numbers to be used as a source
62  QueueSource source = new QueueSource();
63  source.setEvents(1, 6, 4, 3, 2, 1, 9);
64 
65  /* The function we'll use to create slices is the identity.
66  * This will create one distinct subtrace per number .*/
67  Function slicing_fct = new IdentityFunction(1);
68 
69  /* The processor chain to apply to each subtrace is a simple
70  * counter of events. */
71  GroupProcessor counter = new GroupProcessor(1, 1);
72  {
73  TurnInto to_one = new TurnInto(new Constant(1));
74  Cumulate sum = new Cumulate(new CumulativeFunction<Number>(Numbers.addition));
75  Connector.connect(to_one, sum);
76  counter.addProcessors(to_one, sum);
77  counter.associateInput(INPUT, to_one, INPUT);
78  counter.associateOutput(OUTPUT, sum, OUTPUT);
79  }
80 
81  /* Create the slicer processor, by giving it the slicing function and
82  * the processor to apply on each slide. */
83  Slice slicer = new Slice(slicing_fct, counter);
84  Connector.connect(source, slicer);
85 
86  /* Let us now pull and print 10 events from the slicer. */
87  Pullable p = slicer.getPullableOutput();
88  for (int i = 0; i < 10; i++)
89  {
90  Object o = p.pull();
91  System.out.println(o);
92  }
93  ///
94  }
95 }
static void main(String[] args)
Use the SlicerMap to compute the sum of odd and even numbers separately.