Code Examples
A repository of 155 code examples for BeepBeep
BagsContains.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 util;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.Pullable;
22 import ca.uqac.lif.cep.UtilityMethods;
23 import ca.uqac.lif.cep.functions.ApplyFunction;
24 import ca.uqac.lif.cep.functions.Cumulate;
25 import ca.uqac.lif.cep.functions.CumulativeFunction;
26 import ca.uqac.lif.cep.tmf.QueueSource;
27 import ca.uqac.lif.cep.util.Bags;
28 import ca.uqac.lif.cep.util.Numbers;
29 
30 /**
31  * Usage of the {@link ca.uqac.lif.cep.util.Bags#contains Bags.contains}
32  * function. This chain of processors can be represented graphically as
33  * follows:
34  * <p>
35  * <img src="./doc-files/util/BagsContains.png" alt="Processor graph">
36  * @author Sylvain HallĂ©
37  * @difficulty Easy
38  */
39 public class BagsContains
40 {
41  public static void main(String[] args)
42  {
43  /// We create a QueueSource; note that this time, each event in
44  // the source is itself a *list*
45  QueueSource src1 = new QueueSource();
46  src1.addEvent(UtilityMethods.createList(1f, 3f, 5f));
47  src1.addEvent(UtilityMethods.createList(4f, 2f));
48  src1.addEvent(UtilityMethods.createList(4f, 4f, 8f));
49  src1.addEvent(UtilityMethods.createList(6f, 4f));
50  // We create a second queue source of 1s
51  QueueSource src2 = new QueueSource();
52  src2.setEvents(1);
53 
54  // We then create a processor chain that will check if
55  // the n-th list contains the number n
56  ApplyFunction contains = new ApplyFunction(Bags.contains);
57  Connector.connect(src1, 0, contains, 0);
58  Cumulate counter = new Cumulate(
59  new CumulativeFunction<Number>(Numbers.addition));
60  Connector.connect(src2, counter);
61  Connector.connect(counter, 0, contains, 1);
62  Pullable p = contains.getPullableOutput();
63  for (int i = 0; i < 4; i++)
64  {
65  System.out.println(p.pull());
66  }
67  ///
68  }
69 
70 
71 }
static void main(String[] args)
Usage of the Bags.contains function.