first commit

This commit is contained in:
Walcher 2024-01-02 15:25:05 +01:00
parent 5d48bb53e7
commit b418c37417
36 changed files with 5575 additions and 0 deletions

View file

@ -0,0 +1,4 @@
package ab1;
public class FinalizedStateException extends RuntimeException {
}

117
src/main/java/ab1/NFA.java Normal file
View file

@ -0,0 +1,117 @@
package ab1;
import java.util.Collection;
/**
* The {@code NFA} interface represents a Non-deterministic Finite Automaton (NFA) and provides methods to manipulate
* NFAs as well as run words against the NFA to check if they are in the language or not. The NFA has two states:
* edit and accept. While in edit-state, new transitions and accepting states can be added until
* {@link #finalizeAutomaton()} is called. If a method is called in the wrong state, a {@link FinalizedStateException}
* should be thrown.
*/
public interface NFA {
/**
* @return all states present in the automaton
*/
Collection<String> getStates();
/**
* @return all transitions currently configured in the automaton
*/
Collection<Transition> getTransitions();
/**
* @return all states present in the automata, marked as accepting state
*/
Collection<String> getAcceptingStates();
/**
* @return initial state of the automata. This cannot change over the lifetime of the object
*/
String getInitialState();
/**
* Add a new transition to the automata.
*
* @param transition information about the new transition. If the automaton already has a transition for the
* given fromState and readSymbol, it should be added nonetheless.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was already called
*/
void addTransition(Transition transition) throws FinalizedStateException;
/**
* Flag a state to be an accepting state. If the state was accepting before, nothing changes.
*
* @param state - label of the new accepting state
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was already called
*/
void addAcceptingState(String state) throws FinalizedStateException;
/**
* @param other any NFA
* @return a new NFA instance that accepts the language L(return) = L(this) L(other).
* other. Both, other and this NFA must not change during this operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA union(NFA other) throws FinalizedStateException;
/**
* @param other - any NFA
* @return a new NFA instance that accepts the language L(return) = L(this) L(other).
* Both, other and this NFA must not change during this operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA intersection(NFA other) throws FinalizedStateException;
/**
* @param other - any NFA
* @return a new NFA instance that accepts the language L(return) = L(this) + L(other).
* Both, other and this NFA must not change during this operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA concatenation(NFA other) throws FinalizedStateException;
/**
* @return a new NFA instance that accepts the language L(this)*.
* This NFA must not change during the operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA kleeneStar() throws FinalizedStateException;
/**
* @return a new NFA instance that accepts the language L(this).
* This NFA must not change during the operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA plusOperator() throws FinalizedStateException;
/**
* @return a new NFA instance that accepts the complement of L(this).
* This NFA must not change during the operation.
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
NFA complement() throws FinalizedStateException;
/**
* @return if the automaton is editable or not
*/
boolean isFinalized();
/**
* Marks the automaton as finalized. Only now words can be tested.
*/
void finalizeAutomaton();
/**
* @return whether the language of this automaton is finite or infinite.
*/
boolean isFinite();
/**
* @param word - word to check, can be empty
* @return true, iff the word lies in the language of the automaton
* @throws FinalizedStateException if {@link #finalizeAutomaton()} was not already called
*/
boolean acceptsWord(String word) throws FinalizedStateException;
}

View file

@ -0,0 +1,5 @@
package ab1;
public interface NFAFactory {
NFA buildNFA(String startState);
}

View file

@ -0,0 +1,14 @@
package ab1;
import lombok.Builder;
/**
* Describes a transition of one character
*
* @param fromState - state, the automata must have for the transition to take effect
* @param readSymbol - next symbol in the word. Can be null for ε.
* @param toState - state, the automata has after the transition
*/
@Builder
public record Transition(String fromState, Character readSymbol, String toState) {
}

View file

@ -0,0 +1,11 @@
package ab1.impl.GRUPPE;
import ab1.NFA;
import ab1.NFAFactory;
public class NFAFactoryImpl implements NFAFactory {
@Override
public NFA buildNFA(String startState) {
return new NFAImpl();
}
}

View file

@ -0,0 +1,90 @@
package ab1.impl.GRUPPE;
import ab1.FinalizedStateException;
import ab1.NFA;
import ab1.Transition;
import java.util.Collection;
import java.util.Set;
public class NFAImpl implements NFA {
@Override
public Set<String> getStates() {
return null;
}
@Override
public Collection<Transition> getTransitions() {
return null;
}
@Override
public Set<String> getAcceptingStates() {
return null;
}
@Override
public String getInitialState() {
return null;
}
@Override
public void addTransition(Transition transition) throws FinalizedStateException {
}
@Override
public void addAcceptingState(String state) throws FinalizedStateException {
}
@Override
public NFA union(NFA other) throws FinalizedStateException {
return null;
}
@Override
public NFA intersection(NFA other) throws FinalizedStateException {
return null;
}
@Override
public NFA concatenation(NFA other) throws FinalizedStateException {
return null;
}
@Override
public NFA kleeneStar() throws FinalizedStateException {
return null;
}
@Override
public NFA plusOperator() throws FinalizedStateException {
return null;
}
@Override
public NFA complement() throws FinalizedStateException {
return null;
}
@Override
public boolean isFinalized() {
return false;
}
@Override
public void finalizeAutomaton() {
}
@Override
public boolean isFinite() {
return false;
}
@Override
public boolean acceptsWord(String word) {
return false;
}
}