diff --git a/README.md b/README.md index c37df0b..59810f9 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ There can be different backends which manage the state machine. This backend uses the GraphDB [Neo4J](https://neo4j.com/) (community edition) with an embedded database. It uses [Neo4J-OGM](https://neo4j.com/docs/ogm-manual/current/) to map our types to the graph database. +All state machines are stored in separate directories in `$HOME/.guistatemachines`. Each state machine is represented by a separate graph database stored in a separate directory. The nodes all have the property "sutState" which contains the corresponding SUT state serialized as XML. diff --git a/src/main/scala/de/retest/guistatemachine/api/GuiStateMachineApi.scala b/src/main/scala/de/retest/guistatemachine/api/GuiStateMachineApi.scala index b58dfb5..b067381 100644 --- a/src/main/scala/de/retest/guistatemachine/api/GuiStateMachineApi.scala +++ b/src/main/scala/de/retest/guistatemachine/api/GuiStateMachineApi.scala @@ -1,4 +1,6 @@ package de.retest.guistatemachine.api +import java.nio.file.Paths + import de.retest.guistatemachine.api.impl.GuiStateMachineApiImpl import de.retest.guistatemachine.api.neo4j.GuiStateMachineApiNeo4J @@ -38,12 +40,17 @@ trait GuiStateMachineApi { } object GuiStateMachineApi { - val default = new GuiStateMachineApiImpl /** * @return The standard implementaiton of the API. */ def apply(): GuiStateMachineApi = default + /** + * The directory in which all GUI state machines will be persisted. + */ + val StateMachinesDir = Paths.get(System.getProperty("user.home"), ".guistatemachines").toAbsolutePath.toString + + val default = new GuiStateMachineApiImpl val neo4j = new GuiStateMachineApiNeo4J } diff --git a/src/main/scala/de/retest/guistatemachine/api/neo4j/GuiStateMachineApiNeo4J.scala b/src/main/scala/de/retest/guistatemachine/api/neo4j/GuiStateMachineApiNeo4J.scala index 4afc579..1fd4654 100644 --- a/src/main/scala/de/retest/guistatemachine/api/neo4j/GuiStateMachineApiNeo4J.scala +++ b/src/main/scala/de/retest/guistatemachine/api/neo4j/GuiStateMachineApiNeo4J.scala @@ -1,17 +1,18 @@ package de.retest.guistatemachine.api.neo4j -import java.io.File +import java.io.{File, FilenameFilter} +import java.nio.file.Paths import com.typesafe.scalalogging.Logger -import de.retest.guistatemachine.api.impl.GuiStateMachineImpl import de.retest.guistatemachine.api.{GuiStateMachine, GuiStateMachineApi} import scala.collection.concurrent.TrieMap class GuiStateMachineApiNeo4J extends GuiStateMachineApi { - private val logger = Logger[GuiStateMachineImpl] + private val logger = Logger[GuiStateMachineApiNeo4J] private val stateMachines = TrieMap[String, GuiStateMachine]() - // TODO #19 Load existing state machines from the disk. + + loadExistingStateMachinesFromDisk() override def createStateMachine(name: String): GuiStateMachine = { val uri = getUri(name) @@ -41,5 +42,27 @@ class GuiStateMachineApiNeo4J extends GuiStateMachineApi { removeStateMachine(name) } // TODO #19 Removes from disk? - private def getUri(name: String): String = new File(name).toURI.toString + private def getUri(name: String): String = Paths.get(GuiStateMachineApi.StateMachinesDir, name).toUri.toString + + private def getExistingFolders(): Seq[String] = + new File(GuiStateMachineApi.StateMachinesDir).list(new FilenameFilter() { + def accept(dir: File, name: String): Boolean = new File(dir, name).isDirectory + }) + + private def loadExistingStateMachinesFromDisk(): Unit = { + val dir = new File(GuiStateMachineApi.StateMachinesDir) + + if (!dir.exists()) { + dir.mkdir() + + logger.info("No existing graphs in {}.", dir.toString) + } else { + logger.info("Loading existing graphs from {}.", dir.toString) + + getExistingFolders() foreach { f => + logger.info("Loading graph {}.", f) + createStateMachine(f) + } + } + } }