Added code

This commit is contained in:
Yorick Rommers
2017-05-01 13:25:43 +02:00
parent d246f18613
commit b8958f3988
6 changed files with 420 additions and 1 deletions
+70
View File
@@ -0,0 +1,70 @@
package com.imegumii;
import java.util.Arrays;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Created by imegumii on 18/04/2017.
*/
public class Automata <T extends Comparable> {
private Set<Transition<T>> transistions;
private SortedSet<T> states;
private SortedSet<T> beginStates;
private SortedSet<T> eindStates;
private SortedSet<Character> symbols;
public Automata(Character [] symbols) {
this(new TreeSet<Character>(Arrays.asList(symbols)));
}
public Automata(SortedSet<Character> symbols) {
states = new TreeSet<T>();
beginStates = new TreeSet<T>();
eindStates = new TreeSet<T>();
transistions = new TreeSet<>();
this.symbols = symbols;
}
public void addTransition (Transition<T> t) {
transistions.add(t);
states.add(t.vanState);
states.add(t.naarState);
}
public void defineAsStartState(T t) {
states.add(t);
beginStates.add(t);
}
public void defineAsEndState(T t) {
states.add(t);
eindStates.add(t);
}
public void printTransitions () {
transistions.forEach(System.out::println);
}
public boolean accept(String stringToSearch) {
T currentState;
states.forEach((s) -> {
System.out.println("State is " + s);
});
beginStates.forEach(System.out::println);
eindStates.forEach(System.out::println);
for (T beginState : beginStates) {
currentState = beginState;
// process currentState.
System.out.println(beginState);
}
return false;
}
}
+36 -1
View File
@@ -1,9 +1,44 @@
package com.imegumii; package com.imegumii;
import com.imegumii.codevandocent.*;
import com.sun.source.util.Trees;
import java.util.TreeSet;
public class Main { public class Main {
public static void Practicum1Opdr1() {
TreeSet<String> stringsToParse = new TreeSet<>();
stringsToParse.add("abbaa");
stringsToParse.add("babaa");
stringsToParse.add("abbaaabaab");
Character [] characters = {'a', 'b'};
Automata<String> myAutomata = new Automata<String>(characters);
myAutomata.addTransition(new Transition<String>("q0", 'a', "q1"));
myAutomata.addTransition(new Transition<String>("q1", 'b', "q2"));
myAutomata.addTransition(new Transition<String>("q2", 'b', "q0"));
myAutomata.defineAsStartState("q0");
myAutomata.defineAsEndState("q2");
myAutomata.printTransitions();
stringsToParse.forEach((s) -> {
boolean result = myAutomata.accept(s);
System.out.println("string " + s + " fully parsed, result: " + result);
});
}
public static void main(String[] args) { public static void main(String[] args) {
// write your code here
System.out.println("Here my code be"); System.out.println("Here my code be");
com.imegumii.codevandocent.Automata<String> t1 = TestAutomata.getExampleSlide8Lesson2();
t1.printTransitions();
System.out.println("Zo moet het dus, nu onze beurt.");
Practicum1Opdr1();
} }
} }
+47
View File
@@ -0,0 +1,47 @@
package com.imegumii;
/**
* Created by imegumii on 18/04/2017.
*/
public class Transition <T extends Comparable> implements Comparable<Transition<T>> {
public static final char ENDCHAR = '$';
public T vanState;
public char symbol;
public T naarState;
public Transition(T from, T to) {
this(from, ENDCHAR, to);
}
public Transition(T van, char s, T naar) {
this.vanState = van;
this.symbol = s;
this.naarState = naar;
}
public boolean equals (Object other) {
if (other == null) {
return false;
} else if (other instanceof Transition) {
Transition otherTransition = (Transition) other;
return this.vanState.equals(otherTransition.vanState) && this.naarState.equals(otherTransition.naarState) && this.symbol == otherTransition.symbol;
} else {
return false;
}
}
@Override
public int compareTo(Transition<T> t) {
int vanCompare = vanState.compareTo(t.vanState);
int symbolCompare = new Character (symbol).compareTo(t.symbol);
int naarCompare = naarState.compareTo(t.naarState);
return (vanCompare != 0 ? vanCompare : (symbolCompare != 0 ? symbolCompare : naarCompare));
}
@Override
public String toString() {
return "(" + this.vanState + ", " + this.symbol + ")-->" + this.naarState;
}
}
+105
View File
@@ -0,0 +1,105 @@
package com.imegumii.codevandocent;
/**
* The class Automata represents both DFA and Automata: some Automata's are also DFA
* Using the method isDFA we can check this
*
* We use '$' to denote the empty symbol epsilon
*
* @author Paul de Mast
* @version 1.0
*/
import java.util.*;
public class Automata<T extends Comparable>
{
// Or use a Map structure
private Set<Transition <T>> transitions;
private SortedSet<T> states;
private SortedSet<T> startStates;
private SortedSet<T> finalStates;
private SortedSet<Character> symbols;
public Automata()
{
this(new TreeSet<Character>());
}
public Automata(Character [] s)
{
this(new TreeSet<Character>(Arrays.asList(s)) );
}
public Automata(SortedSet<Character> symbols)
{
transitions = new TreeSet<Transition<T>>();
states = new TreeSet<T>();
startStates = new TreeSet<T>();
finalStates = new TreeSet<T>();
this.setAlphabet(symbols);
}
public void setAlphabet(Character [] s)
{
this.setAlphabet(new TreeSet<Character>(Arrays.asList(s)));
}
public void setAlphabet(SortedSet<Character> symbols)
{
this.symbols = symbols;
}
public SortedSet<Character> getAlphabet()
{
return symbols;
}
public void addTransition(Transition<T> t)
{
transitions.add(t);
states.add(t.getFromState());
states.add(t.getToState());
}
public void defineAsStartState(T t)
{
// if already in states no problem because a Set will remove duplicates.
states.add(t);
startStates.add(t);
}
public void defineAsFinalState(T t)
{
// if already in states no problem because a Set will remove duplicates.
states.add(t);
finalStates.add(t);
}
public void printTransitions()
{
for (Transition<T> t : transitions)
{
System.out.println (t);
}
}
public boolean isDFA()
{
boolean isDFA = true;
for (T from : states)
{
for (char symbol : symbols)
{
// isDFA = isDFA && getToStates(from, symbol).size() == 1;
}
}
return isDFA;
}
}
+76
View File
@@ -0,0 +1,76 @@
package com.imegumii.codevandocent;
/**
* This file shows how to build up some example automata
*
* @author (your name)
* @version (a version number or a date)
*/
public class TestAutomata
{
static public Automata<String> getExampleSlide8Lesson2()
{
Character [] alphabet = {'a', 'b'};
Automata<String> m = new Automata<String>(alphabet);
m.addTransition( new Transition<String> ("q0", 'a', "q1") );
m.addTransition( new Transition<String> ("q0", 'b', "q4") );
m.addTransition( new Transition<String> ("q1", 'a', "q4") );
m.addTransition( new Transition<String> ("q1", 'b', "q2") );
m.addTransition( new Transition<String> ("q2", 'a', "q3") );
m.addTransition( new Transition<String> ("q2", 'b', "q4") );
m.addTransition( new Transition<String> ("q3", 'a', "q1") );
m.addTransition( new Transition<String> ("q3", 'b', "q2") );
// the error state, loops for a and b:
m.addTransition( new Transition<String> ("q4", 'a') );
m.addTransition( new Transition<String> ("q4", 'b') );
// only on start state in a dfa:
m.defineAsStartState("q0");
// two final states:
m.defineAsFinalState("q2");
m.defineAsFinalState("q3");
return m;
}
static public Automata<String> getExampleSlide14Lesson2()
{
Character [] alphabet = {'a', 'b'};
Automata<String> m = new Automata<String>(alphabet);
m.addTransition( new Transition<String> ("A", 'a', "C") );
m.addTransition( new Transition<String> ("A", 'b', "B") );
m.addTransition( new Transition<String> ("A", 'b', "C") );
m.addTransition( new Transition<String> ("B", 'b', "C") );
m.addTransition( new Transition<String> ("B", "C") );
m.addTransition( new Transition<String> ("C", 'a', "D") );
m.addTransition( new Transition<String> ("C", 'a', "E") );
m.addTransition( new Transition<String> ("C", 'b', "D") );
m.addTransition( new Transition<String> ("D", 'a', "B") );
m.addTransition( new Transition<String> ("D", 'a', "C") );
m.addTransition( new Transition<String> ("E", 'a') );
m.addTransition( new Transition<String> ("E", "D") );
// only on start state in a dfa:
m.defineAsStartState("A");
// two final states:
m.defineAsFinalState("C");
m.defineAsFinalState("E");
return m;
}
}
+86
View File
@@ -0,0 +1,86 @@
package com.imegumii.codevandocent;
/**
* The class Automata represents both DFA and Automata: some Automata's are also DFA
* Using the method isDFA we can check this
*
* We use '$' to denote the empty symbol epsilon
*
* @author Paul de Mast
* @version 1.0
*/
public class Transition<T extends Comparable> implements Comparable<Transition<T>>
{
public static final char EPSILON = '$';
private T fromState;
private char symbol;
private T toState;
// this constructor can be used to define loops:
public Transition(T fromOrTo, char s)
{
this (fromOrTo, s, fromOrTo);
}
public Transition(T from, T to)
{
this(from, EPSILON, to);
}
public Transition(T from, char s, T to)
{
this.fromState = from;
this.symbol = s;
this.toState = to;
}
// overriding equals
public boolean equals ( Object other )
{
if ( other == null )
{
return false;
}
else if ( other instanceof Transition )
{
return this.fromState.equals (((Transition )other ).fromState) && this.toState.equals (((Transition )other ).toState) && this.symbol == (((Transition )other ).symbol);
}
else return false;
}
@SuppressWarnings("unchecked")
public int compareTo(Transition<T> t)
{
int fromCmp = fromState.compareTo(t.fromState);
int symbolCmp = new Character (symbol).compareTo(new Character (t.symbol));
int toCmp = toState.compareTo(t.toState);
return (fromCmp != 0 ? fromCmp : (symbolCmp != 0 ? symbolCmp : toCmp));
}
public T getFromState()
{
return fromState;
}
public T getToState()
{
return toState;
}
public char getSymbol()
{
return symbol;
}
public String toString()
{
return "(" + this.getFromState() + ", " + this.getSymbol() + ")" + "-->" + this.getToState();
}
}