add plusOperator, kleeneStar
This commit is contained in:
parent
666280cdf6
commit
f8cfdd5c7e
3 changed files with 63 additions and 3 deletions
|
|
@ -32,7 +32,6 @@ public class NFAImpl implements NFA {
|
||||||
return this.transitions;
|
return this.transitions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getAcceptingStates() {
|
public Set<String> getAcceptingStates() {
|
||||||
|
|
||||||
|
|
@ -62,6 +61,26 @@ public class NFAImpl implements NFA {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// warum gabs die methode nicht schon vorher? weil i a idiot bin und man die direct adden kann.
|
||||||
|
/*
|
||||||
|
public void addAllStates(Set<String> states) throws FinalizedStateException {
|
||||||
|
if (this.isFinalized) {
|
||||||
|
throw new FinalizedStateException();
|
||||||
|
} else {
|
||||||
|
this.states.addAll(states);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAllTransitions(Set<Transition> transitions) throws FinalizedStateException {
|
||||||
|
if (this.isFinalized) {
|
||||||
|
throw new FinalizedStateException();
|
||||||
|
} else {
|
||||||
|
this.transitions.addAll(transitions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
// #TODO
|
// #TODO
|
||||||
@Override
|
@Override
|
||||||
public NFA union(NFA other) throws FinalizedStateException {
|
public NFA union(NFA other) throws FinalizedStateException {
|
||||||
|
|
@ -83,13 +102,54 @@ public class NFAImpl implements NFA {
|
||||||
// #TODO
|
// #TODO
|
||||||
@Override
|
@Override
|
||||||
public NFA kleeneStar() throws FinalizedStateException {
|
public NFA kleeneStar() throws FinalizedStateException {
|
||||||
return null;
|
if (this.isFinalized) {
|
||||||
|
throw new FinalizedStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
NFAImpl nfa = new NFAImpl(this.initialState);
|
||||||
|
|
||||||
|
// deep copy but without accepting states
|
||||||
|
nfa.states.addAll(this.states);
|
||||||
|
nfa.transitions.addAll(this.transitions);
|
||||||
|
|
||||||
|
for (String acceptingState : this.acceptingStates) {
|
||||||
|
Transition loopBackTransition =
|
||||||
|
// creating an epsilon transition (null) for each accepting state
|
||||||
|
new Transition(acceptingState, null, this.initialState);
|
||||||
|
nfa.transitions.add(loopBackTransition);
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding the initial state as accepting state
|
||||||
|
nfa.acceptingStates.add(this.initialState);
|
||||||
|
|
||||||
|
nfa.finalizeAutomaton();
|
||||||
|
|
||||||
|
return nfa;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #TODO
|
// #TODO
|
||||||
@Override
|
@Override
|
||||||
public NFA plusOperator() throws FinalizedStateException {
|
public NFA plusOperator() throws FinalizedStateException {
|
||||||
return null;
|
if (this.isFinalized) {
|
||||||
|
throw new FinalizedStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
NFAImpl nfa = new NFAImpl(this.initialState);
|
||||||
|
|
||||||
|
// simple deep copy
|
||||||
|
nfa.states.addAll(this.states);
|
||||||
|
nfa.transitions.addAll(this.transitions);
|
||||||
|
nfa.acceptingStates.addAll(this.acceptingStates);
|
||||||
|
|
||||||
|
// for each accepting state
|
||||||
|
for (String acceptingState : this.acceptingStates) {
|
||||||
|
Transition loopBackTransition =
|
||||||
|
// creating an epsilon transition (null) for each accepting state
|
||||||
|
new Transition(acceptingState, null, this.initialState);
|
||||||
|
nfa.transitions.add(loopBackTransition);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nfa;
|
||||||
}
|
}
|
||||||
|
|
||||||
// #TODO
|
// #TODO
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Reference in a new issue