public abstract class FSA extends Object implements Iterable<ByteBuffer>
Constructor and Description |
---|
FSA() |
Modifier and Type | Method and Description |
---|---|
abstract int |
getArc(int node,
byte label) |
int |
getArcCount(int node) |
abstract byte |
getArcLabel(int arc) |
abstract int |
getEndNode(int arc) |
abstract int |
getFirstArc(int node) |
abstract Set<FSAFlags> |
getFlags() |
abstract int |
getNextArc(int arc) |
int |
getRightLanguageCount(int node) |
abstract int |
getRootNode() |
Iterable<ByteBuffer> |
getSequences()
|
Iterable<ByteBuffer> |
getSequences(int node)
Returns an iterator over all binary sequences starting at the given FSA
state (node) and ending in final nodes.
|
abstract boolean |
isArcFinal(int arc) |
abstract boolean |
isArcTerminal(int arc) |
Iterator<ByteBuffer> |
iterator()
Returns an iterator over all binary sequences starting from the initial FSA
state (node) and ending in final nodes.
|
static FSA |
read(InputStream stream)
A factory for reading automata in any of the supported versions.
|
static <T extends FSA> |
read(InputStream stream,
Class<? extends T> clazz)
A factory for reading a specific FSA subclass, including proper casting.
|
protected static byte[] |
readRemaining(InputStream in) |
<T extends StateVisitor> |
visitAllStates(T v)
Visit all states.
|
<T extends StateVisitor> |
visitInPostOrder(T v)
Same as
visitInPostOrder(StateVisitor, int) , starting from root
automaton node. |
<T extends StateVisitor> |
visitInPostOrder(T v,
int node)
Visits all states reachable from
node in postorder. |
<T extends StateVisitor> |
visitInPreOrder(T v)
Same as
visitInPreOrder(StateVisitor, int) , starting from root
automaton node. |
<T extends StateVisitor> |
visitInPreOrder(T v,
int node)
Visits all states in preorder.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
forEach, spliterator
public abstract int getRootNode()
public abstract int getFirstArc(int node)
node
- Identifier of the node.node
or 0 if the node has no outgoing arcs.public abstract int getNextArc(int arc)
arc
- The arc's identifier.arc
and
leaving node
. Zero is returned if no more arcs are
available for the node.public abstract int getArc(int node, byte label)
node
- Identifier of the node.label
- The arc's label.node
and
labeled with label
. An identifier equal to 0 means the
node has no outgoing arc labeled label
.public abstract byte getArcLabel(int arc)
arc
- The arc's identifier.arc
.public abstract boolean isArcFinal(int arc)
arc
- The arc's identifier.true
if the destination node at the end of
this arc
corresponds to an input sequence created when
building this automaton.public abstract boolean isArcTerminal(int arc)
arc
- The arc's identifier.true
if this arc
does not have a
terminating node (@link getEndNode(int)
will throw an
exception). Implies isArcFinal(int)
.public abstract int getEndNode(int arc)
arc
- The arc's identifier.arc
.
Terminal arcs (those that point to a terminal state) have no end
node representation and throw a runtime exception.public abstract Set<FSAFlags> getFlags()
public int getArcCount(int node)
node
- Identifier of the node.public int getRightLanguageCount(int node)
node
- Identifier of the node.FSAFlags.NUMBERS
. The size
of the right language of the state, in other words.UnsupportedOperationException
- If the automaton was not compiled with FSAFlags.NUMBERS
.
The value can then be computed by manual count of
getSequences(int)
.public Iterable<ByteBuffer> getSequences(int node)
The returned iterator is a ByteBuffer
whose contents changes on
each call to Iterator.next()
. The keep the contents between calls
to Iterator.next()
, one must copy the buffer to some other
location.
Important. It is guaranteed that the returned byte buffer is backed by a byte array and that the content of the byte buffer starts at the array's index 0.
node
- Identifier of the starting node from which to return subsequences.public final Iterable<ByteBuffer> getSequences()
public final Iterator<ByteBuffer> iterator()
ByteBuffer
whose contents changes on each call to
Iterator.next()
. The keep the contents between calls to
Iterator.next()
, one must copy the buffer to some other location.
Important. It is guaranteed that the returned byte buffer is backed by a byte array and that the content of the byte buffer starts at the array's index 0.
iterator
in interface Iterable<ByteBuffer>
public <T extends StateVisitor> T visitAllStates(T v)
StateVisitor.accept(int)
immediately terminates the traversal.T
- A subclass of StateVisitor
.v
- Visitor to receive traversal calls.public <T extends StateVisitor> T visitInPostOrder(T v)
visitInPostOrder(StateVisitor, int)
, starting from root
automaton node.T
- A subclass of StateVisitor
.v
- Visitor to receive traversal calls.public <T extends StateVisitor> T visitInPostOrder(T v, int node)
node
in postorder. Returning
false from StateVisitor.accept(int)
immediately terminates the
traversal.T
- A subclass of StateVisitor
.v
- Visitor to receive traversal calls.node
- Identifier of the node.public <T extends StateVisitor> T visitInPreOrder(T v)
visitInPreOrder(StateVisitor, int)
, starting from root
automaton node.T
- A subclass of StateVisitor
.v
- Visitor to receive traversal calls.public <T extends StateVisitor> T visitInPreOrder(T v, int node)
StateVisitor.accept(int)
skips traversal of all sub-states of a
given state.T
- A subclass of StateVisitor
.v
- Visitor to receive traversal calls.node
- Identifier of the node.protected static final byte[] readRemaining(InputStream in) throws IOException
in
- The input stream.IOException
- Rethrown if an I/O exception occurs.public static FSA read(InputStream stream) throws IOException
stream
- The input stream to read automaton data from. The stream is not
closed.IOException
- If the input stream does not represent an automaton or is
otherwise invalid.public static <T extends FSA> T read(InputStream stream, Class<? extends T> clazz) throws IOException
T
- A subclass of FSA
to cast the read automaton to.stream
- The input stream to read automaton data from. The stream is not
closed.clazz
- A subclass of FSA
to cast the read automaton to.IOException
- If the input stream does not represent an automaton, is otherwise
invalid or the class of the automaton read from the input stream
is not assignable to clazz
.Copyright © 2016. All rights reserved.