Code Examples
A repository of 155 code examples for BeepBeep
SimpleProcessorBuilder.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 dsl;
19 
20 import java.util.ArrayDeque;
21 
22 import ca.uqac.lif.bullwinkle.BnfParser.InvalidGrammarException;
23 import ca.uqac.lif.bullwinkle.Builds;
24 import ca.uqac.lif.cep.Connector;
25 import ca.uqac.lif.cep.Processor;
26 import ca.uqac.lif.cep.Pullable;
27 import ca.uqac.lif.cep.dsl.GroupProcessorBuilder;
28 import ca.uqac.lif.cep.tmf.CountDecimate;
29 import ca.uqac.lif.cep.tmf.Filter;
30 import ca.uqac.lif.cep.tmf.Passthrough;
31 import ca.uqac.lif.cep.tmf.QueueSource;
32 import ca.uqac.lif.cep.tmf.Trim;
33 
34 public class SimpleProcessorBuilder extends GroupProcessorBuilder
35 {
36  ///
37  public SimpleProcessorBuilder()
38  {
39  super();
40  try
41  {
42  setGrammar("<proc> := <trim> | <decim> | <filter> | <stream> ;\n"
43  + "<trim> := TRIM <num> FROM ( <proc> );\n"
44  + "<decim> := KEEP ONE EVERY <num> FROM ( <proc> );\n"
45  + "<filter> := FILTER ( <proc> ) WITH ( <proc> );\n"
46  + "<stream> := INPUT <num> ;\n"
47  + "<num> := ^[0-9]+;");
48  }
49  catch (InvalidGrammarException e)
50  {
51  // Do nothing
52  }
53  }
54  ///
55 
56  //!
57  @Builds(rule="<trim>", pop=true, clean=true)
58  public Trim handleTrim(Object ... parts)
59  {
60  Integer n = Integer.parseInt((String) parts[0]);
61  Processor p = (Processor) parts[1];
62  Trim trim = new Trim(n);
63  Connector.connect(p, trim);
64  add(trim);
65  return trim;
66  }
67  //!
68 
69  //*
70  @Builds(rule="<decim>", pop=true, clean=true)
71  public CountDecimate handleDecimate(Object ... parts)
72  {
73  Integer n = Integer.parseInt((String) parts[0]);
74  Processor p = (Processor) parts[1];
75  CountDecimate dec = new CountDecimate(n);
76  Connector.connect(p, dec);
77  add(dec);
78  return dec;
79  }
80  //*
81 
82  //@
83  @Builds(rule="<filter>", pop=true, clean=true)
84  public Filter handleFilter(Object ... parts)
85  {
86  Processor p1 = (Processor) parts[0];
87  Processor p2 = (Processor) parts[1];
88  Filter filter = new Filter();
89  Connector.connect(p1, 0, filter, 0);
90  Connector.connect(p2, 0, filter, 1);
91  add(filter);
92  return filter;
93  }
94  //@
95 
96  //.
97  @Builds(rule="<stream>")
98  public void handleStream(ArrayDeque<Object> stack)
99  {
100  Integer n = Integer.parseInt((String) stack.pop());
101  stack.pop(); // INPUT
102  Passthrough p = forkInput(n);
103  add(p);
104  stack.push(p);
105  }
106  //.
107 
108  public static void main(String[] args) throws ca.uqac.lif.bullwinkle.ParseTreeObjectBuilder.BuildException
109  {
111  {
112  System.out.println("First query");
113  Processor proc = builder.build(
114  "KEEP ONE EVERY 2 FROM (INPUT 0)");
115  QueueSource src = new QueueSource().setEvents(0, 1, 2, 3, 4, 5, 6, 8);
116  Connector.connect(src, proc);
117  Pullable pul1 = proc.getPullableOutput();
118  for (int i = 0; i < 5; i++)
119  System.out.println(pul1.pull());
120  }
121  {
122  System.out.println("Second query");
123  //m
124  Processor proc = builder.build(
125  "KEEP ONE EVERY 2 FROM (TRIM 3 FROM (INPUT 0))");
126  QueueSource src = new QueueSource().setEvents(0, 1, 2, 3, 4, 5, 6, 8);
127  Connector.connect(src, proc);
128  Pullable pul1 = proc.getPullableOutput();
129  for (int i = 0; i < 5; i++)
130  System.out.println(pul1.pull());
131  //m
132  }
133  {
134  System.out.println("Third query");
135  Processor proc = builder.build("FILTER (INPUT 0) WITH (INPUT 1)");
136  QueueSource src0 = new QueueSource().setEvents(0, 1, 2, 3, 4, 5, 6, 8);
137  QueueSource src1 = new QueueSource().setEvents(true, false, false, true, true, false);
138  Connector.connect(src0, 0, proc, 0);
139  Connector.connect(src1, 0, proc, 1);
140  Pullable pul1 = proc.getPullableOutput();
141  for (int i = 0; i < 5; i++)
142  System.out.println(pul1.pull());
143  }
144  }
145 
146 }