• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Scala-Robotics-Simulator / PPS-22-srs / #217

07 Aug 2025 08:50PM UTC coverage: 71.35% (-12.7%) from 84.0%
#217

push

github

srs-mate
refactor: fix suggestions

650 of 911 relevant lines covered (71.35%)

10.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/scala/io/github/srs/controller/ControllerModule.scala
1
package io.github.srs.controller
2

3
import scala.compiletime.deferred
4
import scala.concurrent.duration.FiniteDuration
5

6
import cats.syntax.foldable.toFoldableOps
7
import io.github.srs.model.*
8
import io.github.srs.model.SimulationConfig.SimulationStatus
9
import io.github.srs.model.UpdateLogic.*
10
import io.github.srs.model.logic.*
11
import io.github.srs.utils.SimulationDefaults.SimulationConfig.maxCount
12
import monix.catnap.ConcurrentQueue
13
import monix.eval.Task
14

15
/**
16
 * Module that defines the controller logic for the Scala Robotics Simulator.
17
 */
18
object ControllerModule:
19

×
20
  /**
21
   * Controller trait that defines the interface for the controller.
22
   *
23
   * @tparam S
24
   *   the type of the state, which must extend [[ModelModule.State]].
25
   */
26
  trait Controller[S <: ModelModule.State]:
27
    /**
×
28
     * Starts the controller with the initial state.
29
     *
30
     * @param initialState
31
     *   the initial state of the simulation.
32
     */
33
    def start(initialState: S): Task[Unit]
34

35
    /**
36
     * Runs the simulation loop, processing events from the queue and updating the state.
37
     *
38
     * @param s
39
     *   the current state of the simulation.
40
     * @param queue
41
     *   a concurrent queue that holds events to be processed.
42
     * @return
43
     *   a task that completes when the simulation loop ends.
44
     */
45
    def simulationLoop(s: S, queue: ConcurrentQueue[Task, Event]): Task[Unit]
46

47
  end Controller
48

49
  /**
50
   * Provider trait that defines the interface for providing a controller.
51
   * @tparam S
52
   *   the type of the state, which must extend [[ModelModule.State]].
53
   */
54
  trait Provider[S <: ModelModule.State]:
55
    val controller: Controller[S]
56

57
  /**
58
   * Defines the dependencies required by the controller module. In particular, it requires a
59
   * [[io.github.srs.view.ViewModule.Provider]] and a [[io.github.srs.model.ModelModule.Provider]].
60
   */
61
  type Requirements[S <: ModelModule.State] =
62
    io.github.srs.view.ViewModule.Provider[S] & io.github.srs.model.ModelModule.Provider[S]
63

64
  /**
65
   * Component trait that defines the interface for creating a controller.
66
   * @tparam S
67
   *   the type of the simulation state, which must extend [[ModelModule.State]].
68
   */
69
  trait Component[S <: ModelModule.State]:
70
    context: Requirements[S] =>
×
71
    given inc: IncrementLogic[S] = deferred
72
    given tick: TickLogic[S] = deferred
73
    given pause: PauseLogic[S] = deferred
74
    given resume: ResumeLogic[S] = deferred
75
    given stop: StopLogic[S] = deferred
76

77
    object Controller:
78
      /**
×
79
       * Creates a controller instance.
80
       *
81
       * @return
82
       *   a [[Controller]] instance.
83
       */
84
      def apply(): Controller[S] = new ControllerImpl
85

×
86
      /**
87
       * Private controller implementation that delegates the simulation loop to the provided model and view.
88
       */
89
      private class ControllerImpl extends Controller[S]:
90

×
91
        override def start(initialState: S): Task[Unit] =
92
          val randInt: Int = initialState.simulationRNG.nextIntBetween(0, maxCount)._1
×
93
          val list = List.fill(randInt)(Event.Increment)
×
94
          for
×
95
            queueSim <- ConcurrentQueue.unbounded[Task, Event]()
×
96
            _ <- context.view.init(queueSim)
×
97
            _ <- produceEvents(queueSim, list)
98
            _ <- simulationLoop(initialState, queueSim)
99
          yield ()
100

×
101
        override def simulationLoop(s: S, queue: ConcurrentQueue[Task, Event]): Task[Unit] =
102
          def loop(state: S): Task[Unit] =
×
103
            for
×
104
              events <- queue.drain(0, 50)
×
105
              newState <- handleEvents(events, state)
×
106
              _ <- context.view.render(newState)
107
              nextState <-
108
                if newState.simulationStatus == SimulationStatus.RUNNING then
109
                  tickEvents(newState.simulationSpeed.tickSpeed, newState)
110
                else Task.pure(newState)
111
              stop = newState.simulationStatus == SimulationStatus.STOPPED ||
112
                newState.simulationTime.exists(max => newState.elapsedTime >= max)
113
              _ <- if stop then Task.unit else loop(nextState)
114
            yield ()
115
          loop(s)
×
116

×
117
        private def produceEvents[A](queue: ConcurrentQueue[Task, A], events: List[A]): Task[Unit] =
118
          events.traverse_(queue.offer)
×
119

×
120
        private def tickEvents(tickSpeed: FiniteDuration, state: S): Task[S] =
121
          for
×
122
            _ <- Task.sleep(tickSpeed)
×
123
            tick <- handleEvent(Event.Tick(tickSpeed), state)
×
124
          yield tick
125

×
126
        private def handleEvents(events: Seq[Event], state: S): Task[S] =
127
          for finalState <- events.foldLeft(Task.pure(state)) { (taskState, event) =>
×
128
              for
×
129
                currentState <- taskState
130
                newState <- handleEvent(event, currentState)
131
              yield newState
132
            }
133
          yield finalState
×
134

×
135
        private def handleEvent(event: Event, state: S): Task[S] =
136
          event match
×
137
            case Event.Increment if state.simulationStatus == SimulationStatus.RUNNING =>
×
138
              context.model.increment(state)
×
139
            case Event.Tick(deltaTime) if state.simulationStatus == SimulationStatus.RUNNING =>
×
140
              context.model.tick(state, deltaTime)
×
141
            case Event.Pause => context.model.pause(state)
×
142
            case Event.Resume => context.model.resume(state)
×
143
            case Event.Stop => context.model.stop(state)
×
144
            case Event.TickSpeed(speed) => context.model.tickSpeed(state, speed)
×
145
            case _ => Task.pure(state)
×
146

×
147
      end ControllerImpl
148

149
    end Controller
150

151
  end Component
152

153
  /**
154
   * Interface trait that combines the provider and component traits for the controller module.
155
   *
156
   * @tparam S
157
   *   the type of the simulation state, which must extend [[ModelModule.State]].
158
   */
159
  trait Interface[S <: ModelModule.State] extends Provider[S] with Component[S]:
160
    self: Requirements[S] =>
161
end ControllerModule
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc