From 51dd52aa8683ec35cdb0662205dcd9f09d4f4fc5 Mon Sep 17 00:00:00 2001 From: Gabriel Tofvesson Date: Wed, 27 Mar 2019 21:53:36 +0100 Subject: [PATCH] Prettified debug output (sorted initial state and accept-states) Properly tested epsilon transitions --- .idea/vcs.xml | 6 +++++ README.md | 0 src/FiniteAutomata.kt | 39 ++++++++++++++++++---------- src/dev/w1zzrd/automata/Automaton.kt | 14 +++++++--- src/dev/w1zzrd/automata/State.kt | 4 +++ 5 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 README.md diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/src/FiniteAutomata.kt b/src/FiniteAutomata.kt index 88ac673..8382e59 100644 --- a/src/FiniteAutomata.kt +++ b/src/FiniteAutomata.kt @@ -1,24 +1,37 @@ import dev.w1zzrd.automata.* fun main(args: Array){ - val language = Language.makeLanguage(0, 1, 2) - val automaton = Automaton(language, false) + val language = Language.makeLanguage(0, 1) + val nfa = Automaton(language, false) - val stateA = automaton.makeState("a") - val stateB = automaton.makeState("b") - val stateC = automaton.makeState("c") - val stateD = automaton.makeState("d", true) + val stateS = nfa.makeState("s") + val stateQ1 = nfa.makeState("q1") + val stateQ2 = nfa.makeState("q2", true) + val stateP = nfa.makeState("p") + val stateQ = nfa.makeState("q") + val stateR = nfa.makeState("r", true) - stateA.addConnective(arrayOf(0, 1), stateB) - stateA.addConnective(arrayOf(1, 2), stateC) + stateS.addEpsilon(stateQ1, stateP) - stateB.addConnective(arrayOf(0, 2), stateD) + stateQ1.addConnective(0, stateQ1) + stateQ1.addConnective(1, stateQ2) - stateC.addConnective(arrayOf(0, 1), stateD) + stateQ2.addConnective(0, stateQ1) - stateD.addConnective(0, stateA) + stateP.addConnective(arrayOf(0, 1), stateP) + stateP.addConnective(1, stateQ) - automaton.entryPoint = stateA + stateQ.addConnective(arrayOf(0, 1), stateR) - val dfa = automaton.toDeterministicAutomaton(true) + nfa.entryPoint = stateS + + val dfa = nfa.toDeterministicAutomaton(true) + + val dtraverser = dfa.makeTraverser() + dtraverser.traverse(1, 1, 0, 0) + println(dtraverser.currentState.toString()) + + val ntraverser = nfa.makeTraverser() + ntraverser.traverse(1, 1, 0, 0) + println(ntraverser.currentState.toString()) } \ No newline at end of file diff --git a/src/dev/w1zzrd/automata/Automaton.kt b/src/dev/w1zzrd/automata/Automaton.kt index 9e9696d..7bc7b6a 100644 --- a/src/dev/w1zzrd/automata/Automaton.kt +++ b/src/dev/w1zzrd/automata/Automaton.kt @@ -97,7 +97,7 @@ class Automaton(val language: Language, val deterministic: Boolean){ } if(printTable) - for(key in tableEntries.keys){ + for(key in tableEntries.keys.sortedBy { if(it.contentsEquals(startingState)) 0 else if (it.isAcceptState()) 2 else 1 }){ print( (if(key.contentsEquals(startingState)) "→ " else " ") + (if(key.isAcceptState()) "F" else " ") + @@ -131,7 +131,13 @@ class Automaton(val language: Language, val deterministic: Boolean){ private fun MutableList>.isAcceptState() = firstOrNull { it.acceptState } != null - private inner class StateTraverser(entryPoint: State) { + fun makeTraverser(): StateTraverser{ + if(entryPoint == null) + throw IllegalStateException("Entry point state must be defined!") + return StateTraverser(entryPoint!!) + } + + inner class StateTraverser(entryPoint: State) { var currentState: MutableList> = ArrayList() val accepted: Boolean @@ -150,8 +156,9 @@ class Automaton(val language: Language, val deterministic: Boolean){ val nextState = ArrayList>() for(state in currentState) - for(traverseState in state.getConnective(verb)) + for(traverseState in state.getConnective(verb)) { nextState.traverseEpsilon(traverseState) + } currentState = nextState } @@ -161,6 +168,7 @@ class Automaton(val language: Language, val deterministic: Boolean){ for(epsilonState in state.getEpsilon()) if(!contains(epsilonState)) traverseEpsilon(epsilonState) + } } } \ No newline at end of file diff --git a/src/dev/w1zzrd/automata/State.kt b/src/dev/w1zzrd/automata/State.kt index 1db85fe..55c0187 100644 --- a/src/dev/w1zzrd/automata/State.kt +++ b/src/dev/w1zzrd/automata/State.kt @@ -44,6 +44,10 @@ class State( override fun hashCode(): Int { return name.hashCode() } + + override fun toString(): String { + return name + } } class StateFactory(val language: Language, val deterministic: Boolean){