Skip to content

Commit 34ec51a

Browse files
committed
add NFA Automata class and factory class.
1 parent f85bd8a commit 34ec51a

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

Automata.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Automata {
2+
3+
private State inputState;
4+
private State outputState;
5+
6+
public Automata(State input, State output) {
7+
this.inputState = input;
8+
this.outputState = output;
9+
}
10+
11+
public State getInputState() {
12+
return inputState;
13+
}
14+
15+
public State getOutputState() {
16+
return outputState;
17+
}
18+
19+
public void setInputState(State newInputState) {
20+
inputState = newInputState;
21+
}
22+
23+
public void setOutputState(State newOutputState) {
24+
outputState = newOutputState;
25+
}
26+
27+
/**
28+
* Tests whether this NFA matches the input string.
29+
* Delegates to the input state.
30+
*/
31+
// public String test(String str) {
32+
// return inputState.test(str);
33+
// }
34+
}

AutomataFactory.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class AutomataFactory {
2+
public Automata createNFA(State input, State output) {
3+
return new Automata(input, output);
4+
}
5+
6+
public Automata concatNFA(Automata first, Automata second) {
7+
State firstOutputState = first.getOutputState();
8+
firstOutputState.setAccepting(false);
9+
return new Automata(first.getInputState(), second.getOutputState());
10+
}
11+
12+
// public NFA BuildNFA(String symbol) {
13+
//
14+
// State input = new State("q1", false);
15+
// State output = new State("q2", true);
16+
//
17+
// input.addTransitionForSymbol(symbol, output);
18+
//
19+
// return new NFA(input, output);
20+
// }
21+
//
22+
// public NFA concatPair(NFA first, NFA second) {
23+
// return new NFA(first.initialState, second.finalState);
24+
// }
25+
26+
}

Main.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import java.util.List;
2-
import java.io.IOException;
3-
41
public class Main {
5-
6-
public static void main(String[] args) throws IOException {
7-
2+
public static void main(String[] args) {
83
State s1 = new State("A", false);
94
State s2 = new State("B", false);
10-
State s3 = new State("C", true);
115

12-
s1.addTransitionForSymbol("a", s2);
13-
s2.addTransitionForSymbol("b", s3);
14-
List<State> states = s2.getTransitionsForSymbol("b");
6+
AutomataFactory factory = new AutomataFactory();
7+
Automata m1 = factory.createNFA(s1, s2);
8+
9+
System.out.println(m1.getInputState().getName());
1510

16-
for (State state : states) {
17-
System.out.println(state.getName());
18-
}
11+
// s1.addTransitionForSymbol("a", s2);
12+
// s2.addTransitionForSymbol("b", s3);
13+
// List<State> states = s2.getTransitionsForSymbol("b");
14+
//
15+
// for (State state : states) {
16+
// System.out.println(state.getName());
17+
// }
1918
}
2019
}

State.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public boolean getIsAccepting() {
2222
return accepting;
2323
}
2424

25+
public void setAccepting(boolean newValue) {
26+
accepting = newValue;
27+
}
28+
2529
public void addTransitionForSymbol(String symbol, State state) {
2630

2731
List<State> stateArray = states.get(symbol);

0 commit comments

Comments
 (0)