185 lines
4.7 KiB
Java
185 lines
4.7 KiB
Java
package ab1.impl.GRUPPE;
|
|
|
|
import ab1.FinalizedStateException;
|
|
import ab1.NFA;
|
|
import ab1.Transition;
|
|
|
|
import java.util.Collection;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
public class NFAImpl implements NFA {
|
|
private Set<String> states;
|
|
private Set<Transition> transitions;
|
|
private String initialState;
|
|
private Set<String> acceptingStates;
|
|
private boolean isFinalized;
|
|
|
|
public NFAImpl(String startState) {
|
|
this.initialState = startState;
|
|
this.states = new HashSet<>();
|
|
this.transitions = new HashSet<>();
|
|
this.acceptingStates = new HashSet<>();
|
|
this.isFinalized = false;
|
|
}
|
|
|
|
@Override
|
|
public Set<String> getStates() {
|
|
return this.states;
|
|
}
|
|
@Override
|
|
public Collection<Transition> getTransitions() {
|
|
return this.transitions;
|
|
}
|
|
|
|
@Override
|
|
public Set<String> getAcceptingStates() {
|
|
|
|
return this.acceptingStates;
|
|
}
|
|
|
|
@Override
|
|
public String getInitialState() {
|
|
return this.initialState;
|
|
}
|
|
|
|
@Override
|
|
public void addTransition(Transition transition) throws FinalizedStateException {
|
|
if (this.isFinalized) {
|
|
throw new FinalizedStateException();
|
|
} else {
|
|
this.transitions.add(transition);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void addAcceptingState(String state) throws FinalizedStateException {
|
|
if (this.isFinalized) {
|
|
throw new FinalizedStateException();
|
|
} else {
|
|
this.acceptingStates.add(state);
|
|
}
|
|
}
|
|
|
|
// 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
|
|
@Override
|
|
public NFA union(NFA other) throws FinalizedStateException {
|
|
return null;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public NFA intersection(NFA other) throws FinalizedStateException {
|
|
return null;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public NFA concatenation(NFA other) throws FinalizedStateException {
|
|
return null;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public NFA kleeneStar() throws FinalizedStateException {
|
|
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
|
|
@Override
|
|
public NFA plusOperator() throws FinalizedStateException {
|
|
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
|
|
@Override
|
|
public NFA complement() throws FinalizedStateException {
|
|
return null;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public boolean isFinalized() {
|
|
return isFinalized;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public void finalizeAutomaton() {
|
|
this.isFinalized = true;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public boolean isFinite() {
|
|
//check if finite
|
|
return false;
|
|
}
|
|
|
|
// #TODO
|
|
@Override
|
|
public boolean acceptsWord(String word) {
|
|
return false;
|
|
}
|
|
}
|