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