Code Examples
A repository of 155 code examples for BeepBeep
SumAttributes.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 tuples;
19 
20 import java.io.InputStream;
21 
22 import ca.uqac.lif.cep.Connector;
23 import ca.uqac.lif.cep.Pullable;
24 import ca.uqac.lif.cep.functions.ApplyFunction;
25 import ca.uqac.lif.cep.io.ReadLines;
26 import ca.uqac.lif.cep.tmf.Fork;
27 import ca.uqac.lif.cep.tuples.FetchAttribute;
28 import ca.uqac.lif.cep.tuples.TupleFeeder;
29 import ca.uqac.lif.cep.util.Numbers;
30 
31 /**
32  * Compute the sum of two tuple attributes using the
33  * {@link ca.uqac.lif.cep.tuples.GetAttribute GetAttribute} function.
34  * This programs reads the file file1.csv, and computes the sum of
35  * columns A and B for each line.
36  * Graphically, this chain of processor can be described as follows:
37  * <p>
38  * <img src="./doc-files/tuples/SumAttributes.png" alt="Processor graph">
39  * <p>
40  * The output of this program is:
41  * <pre>
42  * 5.0
43  * 6.0
44  * 5.0
45  * 9.0
46  * 9.0
47  * </pre>
48  * A simpler version of this example can be found in
49  * {@link SumAttributesTree}.
50  * @author Sylvain HallĂ©
51  * @difficulty Easy
52  */
53 public class SumAttributes
54 {
55  public static void main(String[] args)
56  {
57  ///
58  InputStream is = SumAttributes.class.getResourceAsStream("file1.csv");
59  ReadLines reader = new ReadLines(is);
60  TupleFeeder tuples = new TupleFeeder();
61  Connector.connect(reader, tuples);
62  Fork fork = new Fork(2);
63  Connector.connect(tuples, fork);
64  ApplyFunction get_a = new ApplyFunction(new FetchAttribute("A"));
65  Connector.connect(fork, 0, get_a, 0);
66  ApplyFunction get_b = new ApplyFunction(new FetchAttribute("B"));
67  Connector.connect(fork, 1, get_b, 0);
68  ApplyFunction cast_a = new ApplyFunction(Numbers.numberCast);
69  Connector.connect(get_a, cast_a);
70  ApplyFunction cast_b = new ApplyFunction(Numbers.numberCast);
71  Connector.connect(get_b, cast_b);
72  ApplyFunction sum = new ApplyFunction(Numbers.addition);
73  Connector.connect(cast_a, 0, sum, 0);
74  Connector.connect(cast_b, 0, sum, 1);
75  Pullable p = sum.getPullableOutput();
76  while (p.hasNext())
77  {
78  System.out.println(p.next());
79  }
80  ///
81  }
82 }
Manipulate tuples.
Compute the sum of two tuple attributes using the GetAttribute function.