Code Examples
A repository of 155 code examples for BeepBeep
MonotonicBid.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2016 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 auction;
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.functions.Constant;
25 import ca.uqac.lif.cep.functions.Cumulate;
26 import ca.uqac.lif.cep.functions.CumulativeFunction;
27 import ca.uqac.lif.cep.functions.FunctionTree;
28 import ca.uqac.lif.cep.io.ReadLines;
29 import ca.uqac.lif.cep.tmf.Filter;
30 import ca.uqac.lif.cep.tmf.Fork;
31 import ca.uqac.lif.cep.tmf.Slice;
32 import ca.uqac.lif.cep.tmf.Trim;
33 import ca.uqac.lif.cep.util.Bags;
34 import ca.uqac.lif.cep.util.Booleans;
35 import ca.uqac.lif.cep.util.Equals;
36 import ca.uqac.lif.cep.util.Maps;
37 import ca.uqac.lif.cep.util.NthElement;
38 import ca.uqac.lif.cep.util.Numbers;
39 import ca.uqac.lif.cep.util.Strings;
40 
41 /**
42  * Checks that all bids in an auction system are <em>monotonic</em>, i.e. each
43  * bid for an item is higher than the previous bid.
44  * @author Sylvain HallĂ©
45  */
46 public class MonotonicBid
47 {
48 
49  public static void main(String[] args)
50  {
51  ReadLines lines = new ReadLines(MonotonicBid.class.getResourceAsStream("auction1.csv"));
52  ApplyFunction split = new ApplyFunction(new Strings.SplitString(","));
53  Connector.connect(lines, split);
54  ///
55  Fork f = new Fork(2);
56  Connector.connect(split, f);
57  Filter filter = new Filter();
58  Connector.connect(f, 0, filter, 0);
59  ApplyFunction is_bid = new ApplyFunction(
60  new FunctionTree(Equals.instance,
61  new Constant("Bid"),
62  new NthElement(0)));
63  Connector.connect(f, 1, is_bid, 0);
64  Connector.connect(is_bid, 0, filter, 1);
65 
66  GroupProcessor bid_amount = new GroupProcessor(1, 1);
67  {
68  ApplyFunction get_amt = new ApplyFunction(new NthElement(2));
69  Fork b_f = new Fork(2);
70  Connector.connect(get_amt, b_f);
71  ApplyFunction gt = new ApplyFunction(Numbers.isLessThan);
72  Connector.connect(b_f, 0, gt, 0);
73  Trim trim = new Trim(1);
74  Connector.connect(b_f, 1, trim, 0);
75  Connector.connect(trim, 0, gt, 1);
76  bid_amount.associateInput(0, get_amt, 0);
77  bid_amount.associateOutput(0, gt, 0);
78  bid_amount.addProcessors(get_amt, b_f, gt, trim);
79  }
80  Slice slice = new Slice(new NthElement(1), bid_amount);
81  Connector.connect(filter, slice);
82  ApplyFunction values = new ApplyFunction(Maps.values);
83  Connector.connect(slice, values);
84  Bags.RunOn and = new Bags.RunOn(new Cumulate(
85  new CumulativeFunction<Boolean>(Booleans.and)));
86  Connector.connect(values, and);
87  ///
88  Pullable p = values.getPullableOutput();
89  while (p.hasNext())
90  {
91  System.out.println(p.next());
92  }
93 
94  }
95 }
Checks that all bids in an auction system are monotonic, i.e.