Last active
April 3, 2016 23:13
-
-
Save carneeki/a14626daf861c32eb96ccfb4f59eb416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class FSM | |
{ | |
public class State | |
{ | |
final boolean acceptor; | |
private final int num; | |
final int r0; // what to return if given zero | |
final int r1; // what to return if given one | |
final int n0; // next state if given zero | |
final int n1; // next state if given one | |
/** | |
* Instantiate a state | |
* @param num the current state numer | |
* @param r0 what to return if given zero | |
* @param r1 what to return if given one | |
* @param n0 next state if given zero | |
* @param n1 next state if given one | |
* @param acceptor is this an accepting state? | |
*/ | |
State(int num, int r0, int r1, int n0, int n1, boolean acceptor) | |
{ | |
this.num = num; | |
this.r0 = r0; | |
this.r1 = r1; | |
this.n0 = n0; | |
this.n1 = n1; | |
this.acceptor = acceptor; | |
} | |
public int getOutput(int b) | |
{ | |
return (b == 1) ? r1 : r0; | |
} | |
public int getNext(int b) | |
{ | |
return (b == 1) ? n1 : n0; | |
} | |
} | |
public class StateResult | |
{ | |
final int input; | |
final int output; | |
final int next; | |
StateResult(int input, int output, int next) | |
{ | |
this.input = input; | |
this.output = output; | |
this.next = next; | |
} | |
} | |
private ArrayList<State> states; | |
private ArrayList<StateResult> results; | |
FSM() | |
{ | |
states = new ArrayList<State>(); | |
results = new ArrayList<StateResult>(); | |
} | |
/** | |
* clear and initialise the FSM so counting can start from 1 | |
*/ | |
public void clear() | |
{ | |
states.clear(); | |
states.add(new State(0, 0, 0, 0, 0, false)); // zero null state | |
results.clear(); | |
} | |
/** | |
* add a state to the FSM | |
* @param r0 what to return if given 0 | |
* @param r1 what to return if given 1 | |
* @param n0 next state if given 0 | |
* @param n1 next state if given 1 | |
* @param acceptor | |
*/ | |
public void addState(int r0, int r1, int n0, int n1, boolean acceptor) | |
{ | |
states.add(new State(states.size() + 1, r0, r1, n0, n1, acceptor)); | |
} | |
public static void main(String[] args) | |
{ | |
FSM f = new FSM(); | |
f.clear(); | |
f.addState(1,0,3,4,false); | |
f.addState(0,1,5,4,false); | |
f.addState(1,0,1,6,false); | |
f.addState(0,0,4,6,false); | |
f.addState(1,0,1,2,false); | |
f.addState(1,0,1,3,false); | |
f.run("0110111011001"); | |
System.out.printf(" IN|"); | |
for(StateResult r : f.results) | |
System.out.printf("& %d ",r.input); | |
System.out.printf("\\\\\n"); | |
System.out.printf(" OUT|"); | |
for(StateResult r : f.results) | |
System.out.printf("& %d ",r.output); | |
System.out.printf("\\\\\n"); | |
System.out.printf("NEXT|"); | |
for(StateResult r : f.results) | |
System.out.printf("& %d ",r.next); | |
System.out.printf("\\\\\n"); | |
} | |
public void run(String inputs) | |
{ | |
State active = states.get(1); // always start at state 1 | |
// (because MQ dept of mathematics aren't real | |
// computer scientists) | |
for (char c : inputs.toCharArray()) | |
{ | |
int i = (c == '1') ? 1 : 0; | |
results.add(new StateResult(i, active.getOutput(i), active.getNext(i))); | |
//System.out.printf("%d", active.getOutput(i)); | |
//active = states.get(active.getNext(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment