2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util.automaton;
20 import java.io.PrintStream;
24 * generic class for modeling automatons
26 * Since this is used in so many extensions from both model and native code,
27 * it seems appropriate to add a basis implementation to util.
29 * To make it more amenable to modeling (e.g. for native peer implementation),
30 * we avoid using standard Java containers at the expense of efficiency for
31 * large numbers of states and transitions
33 public class Automaton <S extends State> {
35 static final int STATE_INC = 16;
36 static final int INPUT_INC = 16;
38 protected String label;
40 protected int nStates;
41 protected State[] states;
43 protected int nInputs;
44 protected String[] alphabet;
46 protected int current;
49 public Automaton (String label, int numberOfStates, int numberOfInputs){
51 states = new State[numberOfStates];
52 alphabet = new String[numberOfInputs];
55 public Automaton (String label, int numberOfStates){
56 this( label, numberOfStates, INPUT_INC);
59 public Automaton(String label){
60 this(label, STATE_INC, INPUT_INC);
63 public void addState (State newState){
64 if (nStates == states.length){
65 State[] a = new State[nStates + STATE_INC];
66 System.arraycopy(states, 0, a, 0, nStates);
70 states[nStates] = newState;
71 newState.setId(nStates);
75 public void addStates (State ... newStates){
76 int n = nStates + newStates.length;
77 if (n >= states.length){
78 State[] a = new State[n];
79 System.arraycopy(states, 0, a, 0, nStates);
83 for (int i=0; i<newStates.length; i++){
84 State s = newStates[i];
91 public String getLabel(){
95 public int getNumberOfStates(){
99 public S getCurrentState(){
100 return (S)states[current];
103 public String[] computeAlphabet(){
104 for (int i = 0; i < nStates; i++) {
106 int nTrans = s.getNumberOfTransitions();
109 for (int j = 0; j < nTrans; j++) {
110 Transition t = s.getTransition(j);
111 String label = t.getLabel();
113 for (int k = 0; k < nInputs; k++) {
114 if (alphabet[k].equals(label)) {
115 break nextTransition;
119 if (nInputs == alphabet.length) {
120 String[] a = new String[nInputs + INPUT_INC];
121 System.arraycopy(alphabet, 0, a, 0, nInputs);
125 alphabet[nInputs] = label;
133 public String[] getAlphabet(){
135 return computeAlphabet();
141 public void printOn (PrintStream ps){
142 ps.printf("Automaton '%s'\n", label);
144 for (int i=0; i<nStates; i++){
145 states[i].printOn(ps);