Code Examples
A repository of 155 code examples for BeepBeep
LandmarkQuery.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 stockticker;
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.UtilityMethods;
24 import ca.uqac.lif.cep.functions.ApplyFunction;
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.FunctionTree;
29 import ca.uqac.lif.cep.functions.StreamVariable;
30 import ca.uqac.lif.cep.tmf.Filter;
31 import ca.uqac.lif.cep.tmf.Fork;
32 import ca.uqac.lif.cep.tmf.Insert;
33 import ca.uqac.lif.cep.tmf.Trim;
34 import ca.uqac.lif.cep.util.Bags;
35 import ca.uqac.lif.cep.util.Booleans;
36 import ca.uqac.lif.cep.util.Equals;
37 import ca.uqac.lif.cep.util.Lists;
38 import ca.uqac.lif.cep.util.NthElement;
39 import ca.uqac.lif.cep.util.Numbers;
40 
41 public class LandmarkQuery
42 {
43  public static void main(String[] args)
44  {
45  ///
46  TickerFeed feed = new TickerFeed(10, 20);
47  Fork fork1 = new Fork(2);
48  Connector.connect(feed, fork1);
49  Filter filter = new Filter();
50  Connector.connect(fork1, 0, filter, 0);
51  ApplyFunction gt_100 = new ApplyFunction(
52  new FunctionTree(Numbers.isGreaterThan,
53  new FunctionTree(
54  Numbers.numberCast, new NthElement(0)),
55  new Constant(10)));
56  Connector.connect(fork1, 1, gt_100, 0);
57  Connector.connect(gt_100, 0, filter, 1);
58  Fork fork2 = new Fork(3);
59  Connector.connect(filter, fork2);
60  Lists.Pack pack = new Lists.Pack();
61  Connector.connect(fork2, 0, pack, 0);
62  Trim trim = new Trim(1);
63  Connector.connect(fork2, 1, trim, 0);
64  ApplyFunction eq = new ApplyFunction(new FunctionTree(Booleans.not,
65  new FunctionTree(Equals.instance,
66  new FunctionTree(new NthElement(0), StreamVariable.X),
67  new FunctionTree(new NthElement(0), StreamVariable.Y))));
68  Connector.connect(trim, 0, eq, 0);
69  Connector.connect(fork2, 2, eq, 1);
70  Insert insert = new Insert(1, false);
71  Connector.connect(eq, insert);
72  Connector.connect(insert, 0, pack, 1);
73  ///
74 
75  //! On each list, check if MSFT is greater than 50
76  GroupProcessor gp = new GroupProcessor(1, 1);
77  ApplyFunction ms_50 = new ApplyFunction(
78  new FunctionTree(Booleans.implies,
79  new FunctionTree(
80  Equals.instance,
81  new Constant("MSFT"),
82  new FunctionTree(
83  new NthElement(1), StreamVariable.X)),
84  new FunctionTree(Numbers.isGreaterThan,
85  new FunctionTree(
86  new NthElement(2), StreamVariable.X),
87  new Constant(250))));
88  gp.addProcessor(ms_50);
89  gp.associateInput(0, ms_50, 0);
90  Cumulate c_50 = new Cumulate(
91  new CumulativeFunction<Boolean>(Booleans.and));
92  Connector.connect(ms_50, c_50);
93  gp.addProcessor(c_50);
94  gp.associateOutput(0, c_50, 0);
95  Bags.RunOn ro = new Bags.RunOn(gp);
96  Fork fork3 = new Fork(2);
97  Connector.connect(pack, fork3);
98  Filter f_ms_50 = new Filter();
99  Connector.connect(fork3, 0, f_ms_50, 0);
100  Connector.connect(fork3, 1, ro, 0);
101  Connector.connect(ro, 0, f_ms_50, 1);
102 
103  // Unpack each day into separate events
104  Lists.Unpack up = new Lists.Unpack();
105  Connector.connect(f_ms_50, up);
106  //!
107 
108  Pullable p = up.getPullableOutput();
109  while (p.hasNext())
110  {
111  System.out.println(UtilityMethods.print(p.pull()));
112  }
113  }
114 }
static void main(String[] args)