Code Examples
A repository of 155 code examples for BeepBeep
CounterSingle.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 customprocessors;
19 
20 import static ca.uqac.lif.cep.Connector.OUTPUT;
21 
22 import java.util.Queue;
23 
24 import ca.uqac.lif.cep.Pullable;
25 import ca.uqac.lif.cep.SynchronousProcessor;
26 import ca.uqac.lif.cep.UtilityMethods;
27 
28 /**
29  * This processor simply generates the trace of numbers 0, 1, 2, ...
30  * This is one of two possible ways of writing such a counter: by
31  * creating an instance of SynchronousProcessor with a purpose-built
32  * <code>compute()</code> method. Another way is illustrated by the
33  * {@link CounterGroup} class.
34  *
35  * @author Sylvain HallĂ©
36  * @difficulty Easy
37  */
38 public class CounterSingle extends SynchronousProcessor
39 {
40  /**
41  * The variable that will keep the current value of the counter
42  */
43  protected int m_counterValue;
44 
45  /*
46  * The constructor sets the input and output arity of this processor.
47  * The input arity is 0, as we do not need an input trace to
48  * generate something. The output arity is 1, as we output one trace
49  * of numbers.
50  *
51  * As for any other object, the constructor is also responsible for
52  * setting the initial state; in our case, this means setting our
53  * counter variable to 1.
54  */
55  public CounterSingle()
56  {
57  super(0, 1);
58  m_counterValue = 1;
59  }
60 
61  /*
62  * This is where the actual processing occurs. Method compute() is
63  * called every time a new output event must be generated.
64  */
65  @Override
66  protected boolean compute(Object[] inputs, Queue<Object[]> outputs)
67  {
68  // Create an array of objects of size 1
69  Object[] front = new Object[1];
70 
71  // Put into this array the current value of our counter,
72  // and then increment this counter
73  front[0] = m_counterValue;
74  m_counterValue++;
75 
76  // Put the array in the queue
77  outputs.add(front);
78 
79  // That's it
80  return true;
81  }
82 
83  /*
84  * We must implement method duplicate() so that BeepBeep
85  * can make copies of this processor if needed. This would occur,
86  * for example, if MyCounter were the argument of a
87  * WindowProcessor. In our case, cloning simply amounts to
88  * returning a new instance of MyCounter.
89  */
90  @Override
91  public CounterSingle duplicate(boolean with_state)
92  {
93  return new CounterSingle();
94  }
95 
96  /*
97  * Implementing this method is optional. Doing so allows the
98  * processor to be reset to its initial state. In the present
99  * case, resetting means setting the counter back to 0.
100  */
101  @Override
102  public void reset()
103  {
104  super.reset();
105  m_counterValue = 0;
106  }
107 
108  /*
109  * Simple main showing what the processor does. It should output the
110  * numbers 1 to 10.
111  */
112  public static void main(String[] args)
113  {
114  CounterSingle counter = new CounterSingle();
115  Pullable p = counter.getPullableOutput(OUTPUT);
116  for (int i = 0; i < 10; i++)
117  {
118  float n = (Float) p.pull();
119  System.out.println(n);
120  UtilityMethods.pause(1000);
121  }
122  }
123 }
This processor simply generates the trace of numbers 0, 1, 2, ...
int m_counterValue
The variable that will keep the current value of the counter.