implement dfa
This commit is contained in:
49
src/main/java/util/dfa/DFA.java
Normal file
49
src/main/java/util/dfa/DFA.java
Normal 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));
|
||||
}
|
||||
}
|
8
src/main/java/util/dfa/DFANoSuchEdgeException.java
Normal file
8
src/main/java/util/dfa/DFANoSuchEdgeException.java
Normal file
@ -0,0 +1,8 @@
|
||||
package util.dfa;
|
||||
|
||||
public class DFANoSuchEdgeException extends RuntimeException {
|
||||
|
||||
public DFANoSuchEdgeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
29
src/main/java/util/dfa/Edge.java
Normal file
29
src/main/java/util/dfa/Edge.java
Normal 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;
|
||||
}
|
||||
}
|
10
src/main/java/util/dfa/IEdge.java
Normal file
10
src/main/java/util/dfa/IEdge.java
Normal file
@ -0,0 +1,10 @@
|
||||
package util.dfa;
|
||||
|
||||
public interface IEdge {
|
||||
|
||||
INode getStart();
|
||||
|
||||
INode getEnd();
|
||||
|
||||
char getChar();
|
||||
}
|
18
src/main/java/util/dfa/INode.java
Normal file
18
src/main/java/util/dfa/INode.java
Normal 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);
|
||||
}
|
51
src/main/java/util/dfa/Node.java
Normal file
51
src/main/java/util/dfa/Node.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user