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

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

27 Jul 2025 10:48AM UTC coverage: 74.412% (-2.0%) from 76.435%
#109

push

github

srs-mate
refactor: use named parameter for state copy in Launcher

253 of 340 relevant lines covered (74.41%)

10.75 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 io.github.srs.model.ModelModule
4

5
/**
6
 * Module that defines the controller logic for the Scala Robotics Simulator.
7
 */
8
object ControllerModule:
9

×
10
  /**
11
   * Controller trait that defines the interface for the controller.
12
   *
13
   * @tparam S
14
   *   the type of the state, which must extend [[ModelModule.State]].
15
   */
16
  trait Controller[S <: ModelModule.State]:
17
    /**
×
18
     * Starts the controller with the initial state.
19
     *
20
     * @param initialState
21
     *   the initial state of the simulation.
22
     */
23
    def start(initialState: S): Unit
24

25
    /**
26
     * Runs the simulation loop, updating the state and rendering the view.
27
     * @param s
28
     *   the current state of the simulation.
29
     */
30
    def simulationLoop(s: S): Unit
31

32
  /**
33
   * Provider trait that defines the interface for providing a controller.
34
   * @tparam S
35
   *   the type of the state, which must extend [[ModelModule.State]].
36
   */
37
  trait Provider[S <: ModelModule.State]:
38
    val controller: Controller[S]
39

40
  /**
41
   * Defines the dependencies required by the controller module. In particular, it requires a
42
   * [[io.github.srs.view.ViewModule.Provider]] and a [[io.github.srs.model.ModelModule.Provider]].
43
   */
44
  type Requirements[S <: ModelModule.State] =
45
    io.github.srs.view.ViewModule.Provider[S] & io.github.srs.model.ModelModule.Provider[S]
46

47
  /**
48
   * Component trait that defines the interface for creating a controller.
49
   * @tparam S
50
   *   the type of the simulation state, which must extend [[ModelModule.State]].
51
   */
52
  trait Component[S <: ModelModule.State]:
53
    context: Requirements[S] =>
×
54

55
    object Controller:
56
      /**
×
57
       * Creates a controller instance.
58
       *
59
       * @return
60
       *   a [[Controller]] instance.
61
       */
62
      def apply(): Controller[S] = new ControllerImpl
63

×
64
      /**
65
       * Private controller implementation that delegates the simulation loop to the provided model and view.
66
       */
67
      private class ControllerImpl extends Controller[S]:
68

×
69
        /**
70
         * @inheritdoc
71
         */
72
        override def start(initialState: S): Unit =
73
          context.view.init()
×
74
          simulationLoop(initialState)
×
75

×
76
        /**
77
         * @inheritdoc
78
         */
79
        @annotation.tailrec
80
        override final def simulationLoop(s: S): Unit =
×
81
          val state = for
×
82
            newState <- context.model.update(s)
×
83
            _ <- Some(context.view.render(newState))
×
84
          yield newState
85

×
86
          state match
87
            case Some(ns) => simulationLoop(ns)
×
88
            case None => ()
×
89
      end ControllerImpl
×
90
    end Controller
91

92
  end Component
93

94
  /**
95
   * Interface trait that combines the provider and component traits for the controller module.
96
   *
97
   * @tparam S
98
   *   the type of the simulation state, which must extend [[ModelModule.State]].
99
   */
100
  trait Interface[S <: ModelModule.State] extends Provider[S] with Component[S]:
101
    self: Requirements[S] =>
102
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