com.karneim.util.collection.set

Class SAutomaton

public class SAutomaton extends Object

This class represents a (non-)deterministic final automaton (NFA/DFA). Use this class to create an automaton manually by adding states to the automaton and transitions to other states or to browse through the automaton's states and implement your own matching strategies.
to create an automaton manually try
import com.karneim.util.collection.set.*;
public class Test {
public static void main(String[] args) {
SAutomaton automaton = new SAutomaton();
{@link IStatePro} s1 = automaton.addState(false);
IStatePro s2 = automaton.addState(true);
s1.addTransition(new {@link CharSet}("0123456789"),s2);
s2.addTransition(new CharSet("0123456789"),s2);
automaton.setStartState(s1);
}
}

to browse through the automaton's states try
final {@link IStatePro} startState = automaton.getStartState();
final {@link StateProSet} states = new StateProSet(startState);
final {@link StateProSet.Iterator} it = states.iterator();
for (IStatePro state=it.next(); state!=null; state=it.next()) {
IStatePro.ITransition[] transitions = state.getTransitions();
for (int i=0; i transitions.length; ++i) {
states.add(transitions[i].getToState());
System.out.println(
"from " + transitions[i].getFromState()
+ " through " + transitions[i].getCharSet()
+ " to " + transitions[i].getToState()
);
}
}

to implement own matching strategies try
/**
* returns true if input is an existing path through automaton's states
* otherwise false.
*
public static boolean incompleteMatch(SAutomaton automaton,String input) {
{@link IState} current = automaton.getStartState().visit();
for (int i=0; i input.length(); ++i) {
current = current.next(input.charAt(i));
if (current == null) return false;
}
return true;
}

Version: 1.0

Author: Ralf Meyer

Nested Class Summary
interfaceSAutomaton.IChangeListener
The listener interface for receiving change events of a SAutomaton.
protected classSAutomaton.State
protected classSAutomaton.StatePro
protected classSAutomaton.Transition
Field Summary
protected AutomatonSet_Stringautomaton
protected Automaton.IChangedListenerautomatonChangedListener
protected LinkedListlisteners
protected HashMapstate2wrapper
protected HashMaptransition2wrapper
Constructor Summary
SAutomaton()
Creates a new empty automaton
SAutomaton(FSAData data)
SAutomaton(InputStream automatonDataStream)
protected SAutomaton(AutomatonSet_String automaton)
Method Summary
voidaddAll(SAutomaton automaton)
voidaddChangeListener(SAutomaton.IChangeListener listener)
Adds the specified listener to receive change events from this automaton.
IStateProaddState()
Adds a new non final state to this automaton.
IStateProaddState(boolean isFinal)
Adds a new final or non final state to this automaton.
voidclear()
Removes all states of this automaton.
voidcomplement()
protected Automaton.IChangedListenergetAutomatonChangedListener()
IStateProgetStartState()
Returns the current start state of the automaton. important: The result is null, if and only if the current start state is null
StateProSetgetStates()
Returns all states of this automaton whatever they are reachable through the current start state or not.
protected voidinit(FSAData a)
booleanisDeterministic()
voidminimize()
Minimizes this automaton as much as possible.
voidremoveAll(SAutomaton automaton)
booleanremoveChangeListener(SAutomaton.IChangeListener listener)
Removes the specified listener so that it no longer receives change events from this automaton.
booleanremoveState(IStatePro state)
Removes the specified state from this automaton.
voidretainAll(SAutomaton automaton)
voidsetStartState(IStatePro state)
Sets the automaton's start state to the specified state.
FSADatatoData()
voidtoData(OutputStream automatonDataStream)
protected static FSADatatoFSAData(Object obj)
StringtoString()

Field Detail

automaton

protected transient AutomatonSet_String automaton

automatonChangedListener

protected transient Automaton.IChangedListener automatonChangedListener

listeners

protected transient LinkedList listeners

state2wrapper

protected transient HashMap state2wrapper

transition2wrapper

protected transient HashMap transition2wrapper

Constructor Detail

SAutomaton

public SAutomaton()
Creates a new empty automaton

SAutomaton

public SAutomaton(FSAData data)

SAutomaton

public SAutomaton(InputStream automatonDataStream)

SAutomaton

protected SAutomaton(AutomatonSet_String automaton)

Method Detail

addAll

public void addAll(SAutomaton automaton)

addChangeListener

public void addChangeListener(SAutomaton.IChangeListener listener)
Adds the specified listener to receive change events from this automaton. The listener will be registered as listener in any case, even if it has been registered yet. If a listener instance is added two times, it will receive events twice. important: don't forget to remove the listener, if you don't need it any longer but still have the automaton in use. Otherwise your listener won't be carbage collected (because it is registered with this automaton) and still will receive events.

addState

public IStatePro addState()
Adds a new non final state to this automaton.

Returns: a new state

addState

public IStatePro addState(boolean isFinal)
Adds a new final or non final state to this automaton.

Returns: a new state

clear

public void clear()
Removes all states of this automaton.

complement

public void complement()

getAutomatonChangedListener

protected Automaton.IChangedListener getAutomatonChangedListener()

getStartState

public IStatePro getStartState()
Returns the current start state of the automaton. important: The result is null, if and only if the current start state is null

Returns: the current start state of the automaton

getStates

public StateProSet getStates()
Returns all states of this automaton whatever they are reachable through the current start state or not.

Returns:

init

protected void init(FSAData a)

isDeterministic

public boolean isDeterministic()

minimize

public void minimize()
Minimizes this automaton as much as possible. The resulting automaton has as less states as possible.
important: the current implementation removes all properties from all transitions

removeAll

public void removeAll(SAutomaton automaton)

removeChangeListener

public boolean removeChangeListener(SAutomaton.IChangeListener listener)
Removes the specified listener so that it no longer receives change events from this automaton. If the listener instance is registered more than ones, only one instance will be removed.

Parameters: listener

Returns: true if the listener was registered else false

UNKNOWN: IllegalArgumentException - if null is passed as listener

removeState

public boolean removeState(IStatePro state)
Removes the specified state from this automaton.
First all transitions pointing to state are removed and then the state itself.
If state is this automaton's start state then first of all this automaton's start state is set to null.
important: the specified state must have been created via the addState method of this automaton otherwise an IllegalArgumentException is thrown.

Parameters: state

Returns: true if this automaton owns the specified state else false

retainAll

public void retainAll(SAutomaton automaton)

setStartState

public void setStartState(IStatePro state)
Sets the automaton's start state to the specified state. If the automaton should have no start state pass null.
important: the specified state must be a state of this automaton which means
a) the state must have been created via the addState() method of this automaton
b) the state must not have been removed from this automaton via the removeState method

Parameters: state

toData

public FSAData toData()

toData

public void toData(OutputStream automatonDataStream)

toFSAData

protected static FSAData toFSAData(Object obj)

toString

public String toString()