Code Examples
A repository of 155 code examples for BeepBeep
GroupBinary.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 ca.uqac.lif.cep.GroupProcessor;
22 import ca.uqac.lif.cep.Pullable;
23 import ca.uqac.lif.cep.functions.ApplyFunction;
24 import ca.uqac.lif.cep.tmf.Fork;
25 import ca.uqac.lif.cep.tmf.QueueSource;
26 import ca.uqac.lif.cep.tmf.Trim;
28 
29 /**
30  * Encapsulate a chain of processors into a
31  * {@link ca.uqac.lif.cep.Group Group}, with an output arity of 2.
32  * The chain of processors in this example can be
33  * represented graphically as:
34  * <p>
35  * <img src="./doc-files/basic/GroupBinary.png" alt="Processor graph">
36  * @see SumTwo
37  * @author Sylvain HallĂ©
38  * @difficulty Easy
39  */
40 public class GroupBinary
41 {
42  public static void main(String[] args)
43  {
44  // Create a source of arbitrary numbers
45  QueueSource source = new QueueSource().setEvents(1, 2, 3, 4, 5, 6);
46 
47  // Fork the stream in two and connect it to the source
48  GroupProcessor group = new GroupProcessor(1, 2);
49  {
50  ///
51  Fork fork = new Fork(2);
52  ApplyFunction div = new ApplyFunction(IntegerDivision.instance);
53  Connector.connect(fork, 0, div, 0);
54  Trim trim = new Trim(1);
55  Connector.connect(fork, 1, trim, 0);
56  Connector.connect(trim, 0, div, 1);
57  group.addProcessors(fork, trim, div);
58  group.associateInput(0, fork, 0);
59  group.associateOutput(0, div, 0);
60  group.associateOutput(1, div, 1);
61  ///
62  }
63 
64  // Connect the source to the group
65  Connector.connect(source, group);
66 
67  /* Let us now print what we receive by pulling on the output of
68  * group. */
69  Pullable p0 = group.getPullableOutput(1);
70  Pullable p1 = group.getPullableOutput(1);
71  for (int i = 0; i < 6; i++)
72  {
73  int q = (Integer) p0.pull();
74  int r = (Integer) p1.pull();
75  System.out.println(q + " remainder " + r);
76  }
77  ///
78  }
79 }
Create custom Function objects, directly or by combining existing functions.
Definition: AddNumbers.java:18
A function that computes integer division.
Show the basic usage of Function objects.
Encapsulate a chain of processors into a Group, with an output arity of 2.