This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from retest/serialization
Refactor to separate serialization and add default GuiStateMachineApi #17
- Loading branch information
Showing
20 changed files
with
421 additions
and
299 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
maxColumn = 160 | ||
maxColumn = 160 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/scala/de/retest/guistatemachine/api/GuiStateMachineSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.retest.guistatemachine.api | ||
|
||
import de.retest.guistatemachine.api.impl.serialization.{GuiStateMachinGMLSerializer, GuiStateMachineJavaObjectStreamSerializer} | ||
|
||
trait GuiStateMachineSerializer { | ||
def save(filePath: String) | ||
def load(filePath: String) | ||
} | ||
|
||
object GuiStateMachineSerializer { | ||
def javaObjectStream(guiStateMachine: GuiStateMachine): GuiStateMachineSerializer = new GuiStateMachineJavaObjectStreamSerializer(guiStateMachine) | ||
def gml(guiStateMachine: GuiStateMachine): GuiStateMachineSerializer = new GuiStateMachinGMLSerializer(guiStateMachine) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...atemachine/api/impl/GraphActionEdge.scala → .../impl/serialization/GraphActionEdge.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...temachine/api/impl/GraphicsProvider.scala → ...impl/serialization/GraphicsProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../scala/de/retest/guistatemachine/api/impl/serialization/GuiStateMachinGMLSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package de.retest.guistatemachine.api.impl.serialization | ||
import java.io.{BufferedWriter, File, FileOutputStream, OutputStreamWriter} | ||
|
||
import com.github.systemdir.gml.YedGmlWriter | ||
import de.retest.guistatemachine.api.{GuiStateMachine, GuiStateMachineSerializer} | ||
import de.retest.recheck.ui.descriptors.SutState | ||
import org.jgrapht.graph.DirectedPseudograph | ||
|
||
class GuiStateMachinGMLSerializer(guiStateMachine: GuiStateMachine) extends GuiStateMachineSerializer { | ||
|
||
type GraphType = DirectedPseudograph[SutState, GraphActionEdge] | ||
|
||
/** | ||
* Converts the state machines into GML which can be read by editors like yED. | ||
* | ||
* @param filePath The file which the GML data is stored into. | ||
* @throws RuntimeException If a vertex or edge cannot be added, this exception is thrown. | ||
*/ | ||
override def save(filePath: String): Unit = { | ||
// get graph from user | ||
val toDraw = createGraph() | ||
|
||
// define the look and feel of the graph | ||
val graphicsProvider = new GraphicsProvider | ||
|
||
// get the gml writer | ||
val writer = | ||
new YedGmlWriter.Builder[SutState, GraphActionEdge, AnyRef](graphicsProvider, YedGmlWriter.PRINT_LABELS: _*) | ||
.setEdgeLabelProvider(_.toString) | ||
.setVertexLabelProvider(sutState => "%s - hash code: %d".format(sutState.toString, sutState.hashCode())) | ||
.build | ||
|
||
// write to file | ||
val outputFile = new File(filePath) | ||
val output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "utf-8")) | ||
try { writer.export(output, toDraw) } finally { output.close() } | ||
} | ||
|
||
override def load(filePath: String): Unit = throw new UnsupportedOperationException("Loading GML is not supported.") | ||
|
||
private def createGraph(): GraphType = { | ||
val graph = new GraphType(classOf[GraphActionEdge]) | ||
val allStatesSorted = guiStateMachine.getAllStates.toSeq.sortWith(hashCodeComparisonOfTuples) | ||
allStatesSorted.foreach { x => | ||
val vertex = x._1 | ||
if (!graph.addVertex(vertex)) { throw new RuntimeException(s"Failed to add vertex $vertex") } | ||
} | ||
|
||
allStatesSorted.foreach { x => | ||
val fromVertex = x._1 | ||
val allTransitionsSorted = x._2.getTransitions.toSeq.sortWith(hashCodeComparisonOfTuples) | ||
|
||
allTransitionsSorted foreach { transition => | ||
val actionTransitions = transition._2 | ||
val action = transition._1 | ||
actionTransitions.to.foreach { toState => | ||
val toVertex = toState.getSutState | ||
val edge = GraphActionEdge(fromVertex, toVertex, action) | ||
if (!graph.addEdge(fromVertex, toVertex, edge)) { throw new RuntimeException(s"Failed to add edge $edge") } | ||
} | ||
} | ||
} | ||
graph | ||
} | ||
|
||
private def hashCodeComparisonOfTuples[A, B](a: (A, B), b: (A, B)) = a._1.hashCode().compareTo(b._2.hashCode()) < 0 | ||
|
||
} |
Oops, something went wrong.