implement dfa

This commit is contained in:
ChUrl
2020-12-01 15:02:29 +01:00
parent 43f9ca2b6b
commit 411e5257a1
6 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package util.dfa;
import java.util.Collection;
import java.util.Set;
public class DFA {
private final Set<INode> nodes;
private final INode start;
public DFA(Set<INode> nodes, INode start, Collection<IEdge> edges) {
this.nodes = nodes;
this.start = start;
for (INode node : nodes) {
edges.stream()
.filter(edge -> edge.getStart() == node)
.forEach(node::addEdge);
}
}
public INode getStart() {
return this.start;
}
public Set<INode> getNodes() {
return this.nodes;
}
public String accept(String word) {
return this.accept(word, this.start);
}
private String accept(String input, INode current) {
if (input.isEmpty()) {
return current.isFinal()
? current.getName() + " akzeptieren"
: current.getName() + " ablehnen";
}
char c = input.charAt(0);
if (!current.hasNext(c)) {
return current.getName() + " ablehnen";
}
return current.getName() + this.accept(input.substring(1), current.getNext(c));
}
}

View File

@ -0,0 +1,8 @@
package util.dfa;
public class DFANoSuchEdgeException extends RuntimeException {
public DFANoSuchEdgeException(String message) {
super(message);
}
}

View File

@ -0,0 +1,29 @@
package util.dfa;
public class Edge implements IEdge {
private final INode start;
private final INode end;
private final char read;
public Edge(INode start, char read, INode end) {
this.start = start;
this.read = read;
this.end = end;
}
@Override
public INode getStart() {
return this.start;
}
@Override
public INode getEnd() {
return this.end;
}
@Override
public char getChar() {
return this.read;
}
}

View File

@ -0,0 +1,10 @@
package util.dfa;
public interface IEdge {
INode getStart();
INode getEnd();
char getChar();
}

View File

@ -0,0 +1,18 @@
package util.dfa;
import java.util.Collection;
public interface INode {
String getName();
boolean isFinal();
void addEdge(IEdge edge);
Collection<IEdge> getEdges();
INode getNext(char c);
boolean hasNext(char c);
}

View File

@ -0,0 +1,51 @@
package util.dfa;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class Node implements INode {
private final String name;
private final boolean fin;
private final Map<Character, IEdge> edges = new HashMap<>();
public Node(String name, boolean fin) {
this.name = name;
this.fin = fin;
}
@Override
public String getName() {
return this.name;
}
@Override
public boolean isFinal() {
return this.fin;
}
@Override
public void addEdge(IEdge edge) {
this.edges.put(edge.getChar(), edge);
}
@Override
public Collection<IEdge> getEdges() {
return this.edges.values();
}
@Override
public INode getNext(char c) {
return Optional.ofNullable(this.edges.get(c))
.map(IEdge::getEnd)
.orElseThrow(() -> new DFANoSuchEdgeException("Can't read " + c + " when in " + this.name));
}
@Override
public boolean hasNext(char c) {
return Optional.ofNullable(this.edges.get(c))
.isPresent();
}
}