-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
184 lines (145 loc) · 6.24 KB
/
Copy pathState.java
File metadata and controls
184 lines (145 loc) · 6.24 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package FDA;
import Tools.Console;
import java.util.*;
import java.util.function.Function;
public class State<T> {
private ArrayList<Transition<T>> transitions = new ArrayList<>();
private Transition<T> other = null;
private String name = "Node";
private String noTransitionError = "NOT AVAILABLE TRANSITION FOR ELEMENT ";
public State(String name) {
this.name = name;
}
public State() {
}
public String getNoTransitionError() {
return noTransitionError;
}
public void setNoTransitionError(String noTransitionError) {
this.noTransitionError = noTransitionError;
}
public Transition<T> addTransition(T transitionValue, State<T> toTransit, boolean ignoreRead, boolean write) {
checkAddTransition(toTransit, ignoreRead);
Transition<T> transition = new Transition<T>(transitionValue, toTransit, ignoreRead, write);
transitions.add(transition);
return transition;
}
public Transition<T> addTransitionFunction(Function<T, Boolean> transitionFunction, State<T> toTransit, boolean ignoreRead, boolean write) {
checkAddTransition(toTransit, ignoreRead);
Transition<T> transition = new Transition<T>(transitionFunction, toTransit, ignoreRead, write);
transitions.add(transition);
return transition;
}
public Transition<T> addOtherElementTransitionFunction(State<T> toTransit, boolean ignoreRead, boolean write) {
checkAddTransition(toTransit, ignoreRead);
other = new Transition<T>(this::otherFunction, toTransit, ignoreRead, write);
return other;
}
private void checkAddTransition(State<T> toTransit, boolean ignoreRead) {
if (toTransit == null) {
throw new IllegalArgumentException("Transitioned node must be not null");
}
if (!(toTransit instanceof FinalState) && ignoreRead) {
throw new IllegalArgumentException("Ignore read can only be used in FinalStates");
}
}
private boolean otherFunction(T next) {
for (Transition<T> transition : transitions) {
if (transition.apply(next)) {
return false;
}
}
return true;
}
@Override
public String toString() {
return "State{" +
"transitions=" + transitions +
", name='" + name + '\'' +
'}';
}
protected FDAData<T> feedForward(FDA<T> fda, Transition<T> transition, List<T> sequence, T prev, boolean debug) {
//Check first if current node is final
if (this instanceof FinalState) {
if (prev != null) {
printPath(debug, prev.toString(), true, !transition.isReadNext(), transition.isWrite());
}
fda.printOnFinish(debug, true, prev.toString());
fda.onReadSequence((FinalState<T>) this, sequence);
//end read sequence
return new FDAData<T>(1, transition, this, prev);
}
if (prev != null) {
if (transition != null) {
printPath(debug, prev.toString(), false, false, transition.isWrite());
} else {
printPath(debug, prev.toString(), false, false, false);
}
}else{
printPath(debug, "", false, false, false);
}
//SELECTION OF NEXT CHARACTER
T character;
if (transition == null || transition.isReadNext()) {
if (fda.getIterator().hasNext()) {
character = (T) fda.getIterator().next();//read next character
} else {
FDAException exception = new FDAException(-1, "REACH END OF ITERATOR BUT NOT REACHED FINAL STATE");
boolean notfinish = fda.onError(exception);
int internalCode = notfinish?1:-1;
return new FDAData<T>(internalCode, transition, this, prev);
}
} else {
character = prev;
}
//Check available transition
State<T> nextState = null;
Transition<T> transitionUsed = null;
for (Transition<T> element : transitions) {
if (element.apply(character)) {
nextState = element.getToTransit();
transitionUsed = element;
}
}
if (nextState == null && other != null) {//Other element transition
nextState = other.getToTransit();
transitionUsed = other;
}
if (nextState == null) {//If there is no available transition
FDAException exception = new FDAException(-2, noTransitionError);
boolean notfinish = fda.onError(exception);
int internalCode = notfinish?1:-1;
return new FDAData<T>(internalCode, transition, this, fda.getIterator().getCurrentElement());
}
if (transitionUsed != null) {//If there is transition call semantic actions of transition
if (transitionUsed.isWrite()) {
sequence.add(character);
}
try {
transitionUsed.callActions(character, sequence, fda.getIterator());
} catch (FDAException e) {
boolean notfinish = fda.onError(e);
int internalCode = notfinish?1:-1;
return new FDAData<T>(internalCode, transition, this, fda.getIterator().getCurrentElement());
}
}
// go to next state of transition
return nextState.feedForward(fda, transitionUsed, sequence, character, debug);
}
private void printPath(boolean debug, String prev, boolean isFinal, boolean ignoreRead, boolean write) {
if (!debug) return;
String modifiers = "";
prev = prev.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t");
if (ignoreRead) {
modifiers += Console.ANSI_BLUE + "[IGNORE READ]";
}
if (write) {
modifiers += Console.ANSI_GREEN + "[W] ";
}
if (isFinal) {
Console.print(Console.ANSI_CYAN + "(" + prev + ") " + modifiers + Console.ANSI_YELLOW + "<<" + name + ">>" + Console.ANSI_PURPLE + " ==> ");
} else {
Console.print(Console.ANSI_CYAN + "(" + prev + ") " + modifiers + Console.ANSI_YELLOW + name + Console.ANSI_PURPLE + " ==> ");
}
}
}