1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2007 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
20 // Since: May 8, 2007
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/main/java/org/xerial/util/graph/DFA.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.util.graph;
\r
27 import java.util.HashMap;
\r
28 import java.util.HashSet;
\r
30 import org.xerial.util.CollectionUtil;
\r
35 * Deterministic finite automaton
\r
39 public class DFA<State, Alphabet>
\r
42 * if the current state is null, this DFS is in invalid sate (never reach any of terminal states)
\r
44 private State _currentState;
\r
45 private HashSet<State> _stateSet = new HashSet<State>();
\r
46 private HashSet<State> _terminalStateSet = new HashSet<State>();
\r
48 class TransitionTargetTable extends HashMap<Alphabet, State>
\r
50 private static final long serialVersionUID = 1L;
\r
52 public String toString()
\r
54 return CollectionUtil.displayMap(this, "->", ", ");
\r
58 class TransitionTable extends HashMap<State, TransitionTargetTable>
\r
60 private static final long serialVersionUID = 1L;
\r
62 public void addTransition(State from, Alphabet input, State to)
\r
64 TransitionTargetTable targetTable = this.get(from);
\r
65 if(targetTable == null)
\r
66 targetTable = this.put(from, new TransitionTargetTable());
\r
68 targetTable.put(input, to);
\r
71 public State nextState(State currentState, Alphabet input)
\r
73 TransitionTargetTable targetTable = this.get(currentState);
\r
74 if(targetTable == null)
\r
77 return targetTable.get(input);
\r
80 public String toString()
\r
82 return CollectionUtil.displayMap(this, ":", "\n");
\r
86 private TransitionTable _transitionTable = new TransitionTable();
\r
88 public DFA(State initialState)
\r
90 _currentState = initialState;
\r
93 public void addState(State s)
\r
97 public void addTerminalState(State s)
\r
100 _terminalStateSet.add(s);
\r
103 public void addTransition(State from, Alphabet input, State to)
\r
105 _stateSet.add(from);
\r
107 _transitionTable.addTransition(from, input, to);
\r
111 public boolean isTerminated()
\r
113 if(_currentState == null)
\r
115 return _terminalStateSet.contains(_currentState);
\r
118 public State transit(Alphabet input)
\r
120 if(_currentState == null)
\r
121 return null; // null state cannot go anywhere
\r
123 State nextState = _transitionTable.nextState(_currentState, input);
\r
124 _currentState = nextState;
\r
125 return _currentState;
\r
129 public String toString()
\r
131 return _transitionTable.toString();
\r