From b8958f39889b4697bfafc71b3ad10d498cf52fb2 Mon Sep 17 00:00:00 2001 From: Yorick Rommers Date: Mon, 1 May 2017 13:25:43 +0200 Subject: [PATCH] Added code --- src/com/imegumii/Automata.java | 70 ++++++++++++ src/com/imegumii/Main.java | 37 +++++- src/com/imegumii/Transition.java | 47 ++++++++ src/com/imegumii/codevandocent/Automata.java | 105 ++++++++++++++++++ .../imegumii/codevandocent/TestAutomata.java | 76 +++++++++++++ .../imegumii/codevandocent/Transition.java | 86 ++++++++++++++ 6 files changed, 420 insertions(+), 1 deletion(-) create mode 100644 src/com/imegumii/Automata.java create mode 100644 src/com/imegumii/Transition.java create mode 100755 src/com/imegumii/codevandocent/Automata.java create mode 100755 src/com/imegumii/codevandocent/TestAutomata.java create mode 100755 src/com/imegumii/codevandocent/Transition.java diff --git a/src/com/imegumii/Automata.java b/src/com/imegumii/Automata.java new file mode 100644 index 0000000..d389fa7 --- /dev/null +++ b/src/com/imegumii/Automata.java @@ -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 { + + private Set> transistions; + + private SortedSet states; + private SortedSet beginStates; + private SortedSet eindStates; + + private SortedSet symbols; + + public Automata(Character [] symbols) { + this(new TreeSet(Arrays.asList(symbols))); + } + + public Automata(SortedSet symbols) { + states = new TreeSet(); + beginStates = new TreeSet(); + eindStates = new TreeSet(); + + transistions = new TreeSet<>(); + + this.symbols = symbols; + } + + public void addTransition (Transition 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; + } +} diff --git a/src/com/imegumii/Main.java b/src/com/imegumii/Main.java index e6339f3..dd0d800 100644 --- a/src/com/imegumii/Main.java +++ b/src/com/imegumii/Main.java @@ -1,9 +1,44 @@ package com.imegumii; +import com.imegumii.codevandocent.*; +import com.sun.source.util.Trees; + +import java.util.TreeSet; + public class Main { + public static void Practicum1Opdr1() { + TreeSet stringsToParse = new TreeSet<>(); + stringsToParse.add("abbaa"); + stringsToParse.add("babaa"); + stringsToParse.add("abbaaabaab"); + + Character [] characters = {'a', 'b'}; + Automata myAutomata = new Automata(characters); + myAutomata.addTransition(new Transition("q0", 'a', "q1")); + + myAutomata.addTransition(new Transition("q1", 'b', "q2")); + + myAutomata.addTransition(new Transition("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) { - // write your code here System.out.println("Here my code be"); + com.imegumii.codevandocent.Automata t1 = TestAutomata.getExampleSlide8Lesson2(); + t1.printTransitions(); + System.out.println("Zo moet het dus, nu onze beurt."); + + Practicum1Opdr1(); + } } diff --git a/src/com/imegumii/Transition.java b/src/com/imegumii/Transition.java new file mode 100644 index 0000000..956c3d2 --- /dev/null +++ b/src/com/imegumii/Transition.java @@ -0,0 +1,47 @@ +package com.imegumii; + +/** + * Created by imegumii on 18/04/2017. + */ +public class Transition implements Comparable> { + 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) { + 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; + } +} diff --git a/src/com/imegumii/codevandocent/Automata.java b/src/com/imegumii/codevandocent/Automata.java new file mode 100755 index 0000000..62d0bb1 --- /dev/null +++ b/src/com/imegumii/codevandocent/Automata.java @@ -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 +{ + + // Or use a Map structure + private Set> transitions; + + private SortedSet states; + private SortedSet startStates; + private SortedSet finalStates; + private SortedSet symbols; + + public Automata() + { + this(new TreeSet()); + } + + public Automata(Character [] s) + { + this(new TreeSet(Arrays.asList(s)) ); + } + + public Automata(SortedSet symbols) + { + transitions = new TreeSet>(); + states = new TreeSet(); + startStates = new TreeSet(); + finalStates = new TreeSet(); + this.setAlphabet(symbols); + } + + public void setAlphabet(Character [] s) + { + this.setAlphabet(new TreeSet(Arrays.asList(s))); + } + + public void setAlphabet(SortedSet symbols) + { + this.symbols = symbols; + } + + public SortedSet getAlphabet() + { + return symbols; + } + + public void addTransition(Transition 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 : 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; + } + +} diff --git a/src/com/imegumii/codevandocent/TestAutomata.java b/src/com/imegumii/codevandocent/TestAutomata.java new file mode 100755 index 0000000..8362582 --- /dev/null +++ b/src/com/imegumii/codevandocent/TestAutomata.java @@ -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 getExampleSlide8Lesson2() + { + Character [] alphabet = {'a', 'b'}; + Automata m = new Automata(alphabet); + + m.addTransition( new Transition ("q0", 'a', "q1") ); + m.addTransition( new Transition ("q0", 'b', "q4") ); + + m.addTransition( new Transition ("q1", 'a', "q4") ); + m.addTransition( new Transition ("q1", 'b', "q2") ); + + m.addTransition( new Transition ("q2", 'a', "q3") ); + m.addTransition( new Transition ("q2", 'b', "q4") ); + + m.addTransition( new Transition ("q3", 'a', "q1") ); + m.addTransition( new Transition ("q3", 'b', "q2") ); + + // the error state, loops for a and b: + m.addTransition( new Transition ("q4", 'a') ); + m.addTransition( new Transition ("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 getExampleSlide14Lesson2() + { + Character [] alphabet = {'a', 'b'}; + Automata m = new Automata(alphabet); + + m.addTransition( new Transition ("A", 'a', "C") ); + m.addTransition( new Transition ("A", 'b', "B") ); + m.addTransition( new Transition ("A", 'b', "C") ); + + m.addTransition( new Transition ("B", 'b', "C") ); + m.addTransition( new Transition ("B", "C") ); + + m.addTransition( new Transition ("C", 'a', "D") ); + m.addTransition( new Transition ("C", 'a', "E") ); + m.addTransition( new Transition ("C", 'b', "D") ); + + m.addTransition( new Transition ("D", 'a', "B") ); + m.addTransition( new Transition ("D", 'a', "C") ); + + m.addTransition( new Transition ("E", 'a') ); + m.addTransition( new Transition ("E", "D") ); + + // only on start state in a dfa: + m.defineAsStartState("A"); + + // two final states: + m.defineAsFinalState("C"); + m.defineAsFinalState("E"); + + return m; + } + +} diff --git a/src/com/imegumii/codevandocent/Transition.java b/src/com/imegumii/codevandocent/Transition.java new file mode 100755 index 0000000..96cccd9 --- /dev/null +++ b/src/com/imegumii/codevandocent/Transition.java @@ -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 implements Comparable> +{ + + 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) + { + 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(); + } + +}