Code Examples
A repository of 155 code examples for BeepBeep
ExtendedMooreMachine.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.Context;
22 import ca.uqac.lif.cep.Pullable;
23 import ca.uqac.lif.cep.functions.Constant;
24 import ca.uqac.lif.cep.functions.ContextAssignment;
25 import ca.uqac.lif.cep.functions.ContextVariable;
26 import ca.uqac.lif.cep.functions.FunctionTree;
27 import ca.uqac.lif.cep.functions.StreamVariable;
28 import ca.uqac.lif.cep.tmf.QueueSource;
29 import ca.uqac.lif.cep.fsm.FunctionTransition;
30 import ca.uqac.lif.cep.fsm.MooreMachine;
31 import ca.uqac.lif.cep.fsm.TransitionOtherwise;
32 import ca.uqac.lif.cep.util.Equals;
33 import ca.uqac.lif.cep.util.Numbers;
34 import ca.uqac.lif.cep.util.Booleans.And;
35 
36 /**
37  * Create an extended state machine with state variables. This is done by
38  * having the state machine manipulate its {@link Context} object.
39  * <p>
40  * You should first have a look at {@link SimpleMooreMachine} before reading
41  * this example.
42  *
43  * @author Sylvain HallĂ©
44  *
45  */
46 public class ExtendedMooreMachine
47 {
48  public static void main(String[] args)
49  {
50  ///
51  MooreMachine machine = new MooreMachine(1, 1);
52  machine.setContext("c", 0);
53  machine.setContext("n", 1);
54  ///
55  {
56  //!
57  FunctionTree guard = new FunctionTree(And.instance,
58  new FunctionTree(Equals.instance,
59  StreamVariable.X, new Constant("hasNext")),
60  new FunctionTree(Numbers.isLessThan,
61  new ContextVariable("c"), new ContextVariable("n")));
62  //!
63  //*
64  ContextAssignment asg = new ContextAssignment("c",
65  new FunctionTree(Numbers.addition,
66  new ContextVariable("c"), new Constant(1))
67  );
68  //*
69  //%
70  machine.addTransition(0, new FunctionTransition(
71  guard, 0, asg));
72  //%
73  //#
74  machine.addTransition(0, new FunctionTransition(
75  new FunctionTree(And.instance,
76  new FunctionTree(Equals.instance,
77  StreamVariable.X, new Constant("next")),
78  new FunctionTree(Equals.instance,
79  new ContextVariable("c"), new ContextVariable("n"))),
80  0,
81  new ContextAssignment("c", new Constant(0)),
82  new ContextAssignment("n",
83  new FunctionTree(Numbers.addition,
84  new ContextVariable("n"), new Constant(1))
85  )
86  ));
87  //#
88  //@
89  machine.addTransition(0, new TransitionOtherwise(1));
90  machine.addTransition(1, new TransitionOtherwise(1));
91  //@
92  //(
93  machine.addSymbol(0, new ContextVariable("c"));
94  //(
95  QueueSource source = new QueueSource();
96  source.setEvents("hasNext", "next", "hasNext", "hasNext", "next", "next", "hasNext");
97  source.loop(false);
98  Connector.connect(source, machine);
99  Pullable p = machine.getPullableOutput();
100  while (p.hasNext())
101  {
102  Object o = p.pull();
103  System.out.println(o);
104  }
105  }
106  }
107 }
Create an extended state machine with state variables.