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.
Add initial DSL and model support for state machines #
- Loading branch information
Showing
22 changed files
with
294 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
abstract class Action |
6 changes: 6 additions & 0 deletions
6
src/main/scala/de/retest/guistatemachine/dsl/FinalState.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,6 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
/** | ||
* There can be more than one end state. | ||
*/ | ||
abstract class FinalState extends State |
3 changes: 3 additions & 0 deletions
3
src/main/scala/de/retest/guistatemachine/dsl/InitialState.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,3 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
abstract class InitialState extends State |
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,21 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
import scala.collection.mutable.ListBuffer | ||
|
||
abstract class State { | ||
private[dsl] var previous: State = null | ||
// TODO Use a set? | ||
private[dsl] var transitions: ListBuffer[Transition] = ListBuffer.empty[Transition] | ||
|
||
def getInitial: InitialState = { | ||
def getFirst: State = if (previous eq null) this else previous.getInitial | ||
|
||
getFirst.asInstanceOf[InitialState] | ||
} | ||
|
||
def -(a: Action): Transition = { | ||
val t = Transition(this, a) | ||
transitions += t | ||
t | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/scala/de/retest/guistatemachine/dsl/StateMachine.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,47 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
import scala.collection.immutable.HashSet | ||
|
||
/** | ||
* NFA: | ||
* 5–Tupel (Z, Σ, δ, z0, E) | ||
* TODO NFAs can have multiple initial states. Do we really need this? | ||
* TODO Make the constructor private. | ||
*/ | ||
final case class StateMachine(initial: InitialState, var previous: StateMachine) { | ||
/** | ||
* Appends another state machine. | ||
*/ | ||
def ~(s: StateMachine): StateMachine = { | ||
s.previous = this | ||
s | ||
} | ||
|
||
/** | ||
* All states. | ||
*/ | ||
//def Z(): Set[State] | ||
/** | ||
* Input alphabet. | ||
*/ | ||
//def Σ(): Set[Action] | ||
/** | ||
* Partially defined function which returns the next state. | ||
*/ | ||
//def δ(s: State, a: Action): State | ||
/** | ||
* Initial state. | ||
* TODO NFAs can have multiple initial states. Do we really need this? | ||
*/ | ||
//def z0: InitialState = initial | ||
|
||
/** | ||
* All final states. | ||
*/ | ||
//def E: Set[FinalState] | ||
} | ||
|
||
object StateMachine { | ||
// TODO f should return a FinalState since all state machines have to end with one | ||
def apply(f: => State): StateMachine = StateMachine(f.getInitial, null) | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/scala/de/retest/guistatemachine/dsl/StateMachines.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,25 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
import scala.collection.immutable.HashMap | ||
|
||
object StateMachines { | ||
def apply(f: => StateMachine): Seq[StateMachine] = { | ||
def constructSeq(s: StateMachine, seq: Seq[StateMachine]): Seq[StateMachine] = | ||
if (s.previous ne null) constructSeq(s.previous, seq ++ Seq(s)) else seq | ||
|
||
constructSeq(f, Seq()) | ||
} | ||
|
||
def toModel(s: Seq[StateMachine]): de.retest.guistatemachine.model.StateMachines = { | ||
val hashmap = new HashMap[de.retest.guistatemachine.model.Id, de.retest.guistatemachine.model.StateMachine]() | ||
|
||
// TODO Implement conversion from the DSL to the model. | ||
/* | ||
s.foreach(x => { | ||
de.retest.guistatemachine.model.StateMachine | ||
}) | ||
*/ | ||
|
||
de.retest.guistatemachine.model.StateMachines(de.retest.guistatemachine.model.Map[de.retest.guistatemachine.model.StateMachine](hashmap)) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/scala/de/retest/guistatemachine/dsl/Transition.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,11 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
// TODO Make constructor private pls | ||
case class Transition(from: State, a: Action, var to: State = null) { | ||
|
||
def -(to: State): State = { | ||
to.previous = from | ||
this.to = to | ||
to | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/scala/de/retest/guistatemachine/dsl/UnknownState.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,6 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
/** | ||
* All actions can be executed for an unknown state. | ||
*/ | ||
case object UnknownState extends State |
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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
case class Action() |
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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
case class Actions(actions: Map[Action]) |
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,5 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
import scala.collection.immutable.HashMap | ||
|
||
final case class State(transitions: Transitions) |
6 changes: 6 additions & 0 deletions
6
src/main/scala/de/retest/guistatemachine/model/StateMachine.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,6 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
/** | ||
* State machine which represents a GUI test. | ||
*/ | ||
final case class StateMachine(states: States, actions: Actions) |
3 changes: 3 additions & 0 deletions
3
src/main/scala/de/retest/guistatemachine/model/StateMachines.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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
final case class StateMachines(stateMachines: Map[StateMachine]) |
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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
case class States(states: Map[State]) |
3 changes: 3 additions & 0 deletions
3
src/main/scala/de/retest/guistatemachine/model/Transition.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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
final case class Transition(to : Id, action : Id) |
3 changes: 3 additions & 0 deletions
3
src/main/scala/de/retest/guistatemachine/model/Transitions.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,3 @@ | ||
package de.retest.guistatemachine.model | ||
|
||
final case class Transitions(transitions: Map[Transition]) |
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
36 changes: 36 additions & 0 deletions
36
src/test/scala/de/retest/guistatemachine/dsl/StateMachinesSpec.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,36 @@ | ||
package de.retest.guistatemachine.dsl | ||
|
||
import org.scalatest.WordSpec | ||
import org.scalatest.Matchers | ||
|
||
/** | ||
* Tests the construction of state machines with the DSL. | ||
*/ | ||
class StateMachinesSpec extends WordSpec with Matchers { | ||
|
||
"StateMachines" should { | ||
"be constructed as NFAs from objects" in { | ||
case object Start extends InitialState | ||
case object S0 extends State | ||
case object S1 extends State | ||
case object End extends FinalState | ||
case object EnterText extends Action | ||
case object PressExitButton extends Action | ||
|
||
StateMachines { | ||
StateMachine { | ||
Start - EnterText - S0 | ||
Start - EnterText - S1 | ||
S0 - PressExitButton - End | ||
S1 - PressExitButton - End | ||
} ~ | ||
StateMachine { | ||
Start - EnterText - S0 | ||
Start - EnterText - S1 | ||
S0 - PressExitButton - End | ||
S1 - PressExitButton - End | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.