-
Notifications
You must be signed in to change notification settings - Fork 2
/
Node.java
111 lines (99 loc) · 2.19 KB
/
Node.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.HashMap;
import java.util.Map;
/**
* An implementation of a state of a DFA.
* @author michaelroytman
*
*/
public class Node {
private String name;
private int stateNumber; //identification number
private Map<String, Node> transitions; //map of transition from state to another on a character
/**
* Constructor, given a name and an identification number.
* @param name
* @param stateNumber
*/
public Node(String name, int stateNumber) {
this.name = name + stateNumber;
this.stateNumber = stateNumber;
transitions = new HashMap<String, Node>();
}
/**
* Construction for start stat
* @param name
*/
public Node(String name) {
this.name = name;
this.stateNumber = 0;
transitions = new HashMap<String, Node>();
}
/**
* Getter method to return state name.
* @return
*/
public String getName() {
return name;
}
/**
* Getter method to return state identification number.
* @return
*/
public int getNumber() {
return stateNumber;
}
/**
* Getter method to return transition function map.
* @return
*/
public Map<String, Node> getTransitions() {
return transitions;
}
/**
* Method that adds a transition to state on symbol
* Precondition: symbol is in the alphabet, assumption
* @param symbol
* @param state
*/
public void addTransition(String symbol, Node state) {
transitions.put(symbol, state);
}
/**
* Makes the state an accept state by concatenating on a star.
*/
public void makeAcceptState() {
name = name + "*";
}
/**
* Returns whether or not state is an accept state.
* @return
*/
public boolean isAcceptState() {
return name.contains("*");
}
/**
* Returns whether or not state is not an accept state.
* @return
*/
public boolean isNonAcceptState() {
return !isAcceptState();
}
/**
* Method that checks for equality between states based on name and identification number.
*/
public boolean equals(Object o) {
if (o instanceof Node) {
Node node = (Node)o;
return (node.name.equals(name) && stateNumber == node.getNumber());
}
else {
return false;
}
}
/**
* toString method that returns the name of the state.
*/
public String toString() {
return name;
}
}