Source code: alice/tucson/Automaton.java
1 /*
2 * TuCSoN coordination infrastructure - Copyright (C) 2001 deis.unibo.it
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package alice.tucson;
19 import alice.logictuple.*;
20
21 /**
22 * Defines a more involved type of TuCSoN agent (compared to Agent one).
23 * <p>
24 * The agent is structured as a finite state automaton.
25 * The behaviour of its states is defined writing public methods;
26 * the <code> become </code> method is used to go make a transition
27 * from state to state, which in this case means from method to method.
28 * It's based on continuation-passing style of arranging computation.
29 * <p>
30 * TuCSoN automata are spawned using Threads:
31 * <ul>
32 * <li><code> new Thread(new MyAutomaton()).start</code>
33 * </ul>
34 * <p>
35 * the first state (method) executed is always "boot" state (method).
36 * <p>
37 * to stop shutdown the automaton the state "end" is provided:
38 * <ul>
39 * <li> <code> become("end") </code>
40 * <ul>
41 *
42 * @see alice.tucson.Agent
43 *
44 * @author <a href="mailto:aricci@deis.unibo.it">Alessandro Ricci</a>
45 * @version 1.0
46 */
47 public abstract class Automaton extends Agent implements java.io.Serializable {
48
49 /** method name representing state behaviour */
50 protected String state="boot";
51
52 /** arguments value eventually associated to state transition */
53 protected Object[] arguments=null;
54
55 /** arguments class eventually associated to state transition */
56 static protected Class[] argType;
57
58 /**
59 * Constructs the automaton specifying its identifier
60 *
61 * @param id the automaton identifier
62 */
63 public Automaton(AgentId id){
64 super(id);
65 try {
66 argType=new Class[]{ Class.forName("[Ljava.lang.Object;") };
67 } catch (Exception ex){
68 }
69 }
70
71 /**
72 * Constructs the automaton specifying its name
73 *
74 * @param name the automaton name
75 */
76 public Automaton(String name){
77 super(name);
78 }
79
80 /**
81 * Changes the state of the automaton
82 *
83 * @param s the state name, which must coincide with the name
84 * of a public method of the object
85 */
86 protected void become(String s){
87 if (!state.equals("end")){
88 state=s;
89 arguments=null;
90 }
91 }
92
93 /**
94 * Changes the state of the automaton, providing auciliary information
95 *
96 * @param s the state name, which must coincide with the name
97 * of a public method of the object
98 * @param args auxiliary information needed in the next state
99 */
100 protected void become(String s, Object[] args){
101 if (!state.equals("end")){
102 state=s;
103 arguments=args;
104 }
105 }
106
107 /**
108 * Specifies the behaviour of the automaton in its boot state,
109 * which is reached when the automaton start its thread of control
110 */
111 public abstract void boot();
112
113 /**
114 * Defines the automaton behaviour
115 */
116 public void run(){
117 while (true){
118 try {
119 if (!state.equals("end")){
120 if (arguments==null){
121 this.getClass().getDeclaredMethod(state,null).invoke(this,null);
122 } else {
123 this.getClass().getDeclaredMethod(state,argType).invoke(this,arguments);
124 }
125 } else {
126 end();
127 break;
128 }
129 } catch (Exception ex){
130 ex.printStackTrace();
131 error();
132 }
133 }
134 }
135
136
137 /**
138 * Specifies the behaviour of the automaton in its end state
139 */
140 public void end(){
141 }
142
143 /**
144 * Specifies the behaviour of the automaton in its error state
145 */
146 public void error(){
147 become("end");
148 }
149 }