Code Examples
A repository of 155 code examples for BeepBeep
SimpleMooreMachineCompact.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 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 finitestatemachines;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.Pullable;
22 import ca.uqac.lif.cep.fsm.FunctionTransition;
23 import ca.uqac.lif.cep.fsm.MooreMachine;
24 import ca.uqac.lif.cep.functions.StreamVariable;
25 import ca.uqac.lif.cep.functions.Constant;
26 import ca.uqac.lif.cep.functions.FunctionTree;
27 import ca.uqac.lif.cep.tmf.QueueSource;
28 import ca.uqac.lif.cep.util.Equals;
29 
30 /**
31  * Similar to {@link SimpleMooreMachine}, but with a bit of code written
32  * to avoid repetitions.
33  *
34  * @see SimpleMooreMachine#main(String[])
35  * @author Sylvain HallĂ©
36  *
37  */
39 {
40  public static void main(String[] args)
41  {
42  /* Define symbolic constants for the three states of the
43  * Moore machine. It is recommended that the actual numbers for
44  * each state form a contiguous interval of integers starting
45  * at 0. */
46  final int UNSAFE = 0, SAFE = 1, ERROR = 2;
47 
48  /* Create an empty Moore machine. */
49  MooreMachine machine = new MooreMachine(1, 1);
50 
51  /* Let us now define the transitions for this machine. This is just
52  * a tedious enumeration of all the arrows that are present in a
53  * graphical representation of the FSM. */
54  addTransition(machine, UNSAFE, "hasnext", SAFE);
55  addTransition(machine, UNSAFE, "next", ERROR);
56  addTransition(machine, SAFE, "hasnext", SAFE);
57  addTransition(machine, SAFE, "next", UNSAFE);
58  /* State 2 is a sink, you stay there forever. A possible way to say so
59  * is to define the condition on its only transition as the constant true;
60  * it will fire whatever the incoming event. */
61  machine.addTransition(ERROR, new FunctionTransition(
62  new Constant(true), ERROR));
63 
64  /* Since a Moore machine outputs a symbol when jumping into a
65  * new state, we must associate symbols to each of the three states.
66  * These symbols can be any object; here we use character strings. */
67  machine.addSymbol(UNSAFE, new Constant("safe")).addSymbol(SAFE, new Constant("unsafe")).addSymbol(ERROR, new Constant("error"));
68 
69  /* Done! We can now try our Moore machine on a sequence of events .*/
70  QueueSource source = new QueueSource();
71  source.setEvents("hasnext", "next", "hasnext", "hasnext", "next", "next");
72  Connector.connect(source, machine);
73 
74  /* Let's pull a few events to see what comes out... */
75  Pullable p = machine.getPullableOutput();
76  for (int i = 0; i < 7; i++)
77  {
78  String s = (String) p.pull();
79  System.out.println(s);
80  }
81  }
82 
83  /**
84  * Adds a new transition to a Moore machine. The condition on this
85  * transition is an equality check between a String event and a predefined
86  * label.
87  * @param m The machine to add the transition to
88  * @param source The source state
89  * @param label The label to compare the event to
90  * @param destination The destination state
91  */
92  protected static void addTransition(MooreMachine m, int source, String label, int destination)
93  {
94  m.addTransition(source, new FunctionTransition(new FunctionTree(Equals.instance, new StreamVariable(), new Constant(label)), destination));
95  }
96 
97 }
static void addTransition(MooreMachine m, int source, String label, int destination)
Adds a new transition to a Moore machine.
Similar to SimpleMooreMachine, but with a bit of code written to avoid repetitions.