Code Examples
A repository of 155 code examples for BeepBeep
ApplianceMooreMachine.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 nialm;
19 
20 import ca.uqac.lif.cep.fsm.FunctionTransition;
21 import ca.uqac.lif.cep.fsm.MooreMachine;
22 import ca.uqac.lif.cep.fsm.TransitionOtherwise;
23 import ca.uqac.lif.cep.functions.Constant;
24 import ca.uqac.lif.cep.functions.Function;
25 import ca.uqac.lif.cep.functions.FunctionTree;
26 import ca.uqac.lif.cep.functions.StreamVariable;
27 import ca.uqac.lif.cep.util.Numbers;
28 
29 public class ApplianceMooreMachine extends MooreMachine
30 {
31  //State names; this is just to improve readability
32  private static final int ST_0 = 0;
33  private static final int ST_1 = 1;
34  private static final int ST_2 = 2;
35  private static final int ST_3 = 3;
36  private static final int ST_4 = 4;
37 
38  public ApplianceMooreMachine(float peak, float plateau, float drop, float interval)
39  {
40  super(2, 1);
41  // Create transition relation
42  addTransition(ST_0, new FunctionTransition(// in state 0, event = peak, go to state 1
43  withinRange(0, peak, interval),
44  ST_1));
45  addTransition(ST_0,
46  // in state 0, event = otherwise, go to state 0
47  new TransitionOtherwise(ST_0));
48  addTransition(ST_1, new FunctionTransition(// in state 1, event = plateau, go to state 2
49  withinRange(1, plateau, interval),
50  ST_2));
51  addTransition(ST_1,
52  // in state 1, event = otherwise, go to state 1
53  new TransitionOtherwise(ST_1));
54  addTransition(ST_2, new FunctionTransition(// in state 2, event = drop, go to state 3
55  withinRange(1, drop, interval),
56  ST_3));
57  addTransition(ST_2,
58  // in state 2, event = otherwise, go to state 4
59  new TransitionOtherwise(ST_4));
60  addTransition(ST_3, new FunctionTransition(// in state 3, event = peak, go to state 1
61  withinRange(0, peak, interval),
62  ST_1));
63  addTransition(ST_3,
64  // in state 3, event = otherwise, go to state 0
65  new TransitionOtherwise(ST_0));
66  addTransition(ST_4, new FunctionTransition(// in state 4, event = drop, go to state 3
67  withinRange(1, drop, interval),
68  ST_3));
69  addTransition(ST_4,
70  // in state 4, event = otherwise, go to state 4
71  new TransitionOtherwise(ST_4));
72  // Add symbols to some states
73  addSymbol(ST_0, new Constant("No change"));
74  addSymbol(ST_1, new Constant("No change"));
75  addSymbol(ST_2, new Constant("Appliance ON"));
76  addSymbol(ST_3, new Constant ("Appliance OFF"));
77  addSymbol(ST_4, new Constant("No change"));
78  }
79 
80  /**
81  * Generate a transition expressing the fact that a value is within a range
82  * @param c The component to look at (0=peak, 1=plateau)
83  * @param value The value
84  * @param interval The half-width of the range
85  * @return The condition
86  */
87  private static Function withinRange(int component, float value, float interval)
88  {
89  StreamVariable var = StreamVariable.X;
90  if (component == 1)
91  {
92  var = StreamVariable.Y;
93  }
94  FunctionTree f = new FunctionTree(Numbers.isLessThan,
95  new FunctionTree(Numbers.absoluteValue,
96  new FunctionTree(Numbers.subtraction,
97  var,
98  new Constant(value))),
99  new Constant(interval));
100  return f;
101  }
102 }