我如何将the回路线列表添加到汽车当前的路线列表之前?

问题描述 投票:0回答:1

[我想知道如何访问Car类中的方向状态,以便可以在Detour case类(List[Direction])中添加消息。所以我现在有一个Intersection类。在此类中,它具有汽车的接近方向(北和南)或(东和西),以及交叉路口的灯光变化。如何实现绕道功能以使汽车(演员)沿着绕道路线行驶?我当时在考虑添加前缀,但似乎无法在存储路线列表的Car类的构造函数中访问Car类的状态变量。

//Intersection Class
import akka.actor.{Actor, ActorRef}
import scala.concurrent.duration._

class Intersection(timeInterval: Int) extends Actor {

  var redEastWest: Boolean = true
  var waitingEastWest: List[ActorRef] = List()
  var waitingNorthSouth: List[ActorRef] = List()

  import context.dispatcher

  self ! ChangeLight

  override def receive: Receive = {
    case ApproachingEastWest =>
      if (redEastWest) {
        waitingEastWest ::= sender()
      } else {
        sender() ! GreenLight
      }
    case ApproachingNorthSouth =>
      if (redEastWest) {
        sender() ! GreenLight
      }else {
        waitingNorthSouth ::= sender()
      }
    case ChangeLight =>
      if (redEastWest) {
        println("Green East/West at intersection: " + self)
        waitingEastWest.foreach(_ ! GreenLight)
        waitingEastWest = List()
      } else {
        println("Green North/South at intersection: " + self)
        waitingNorthSouth.foreach(_ ! GreenLight)
        waitingNorthSouth = List()
      }
      redEastWest = !redEastWest
      context.system.scheduler.scheduleOnce(
      timeInterval.milliseconds,
      self,
      ChangeLight
    )
    case detour: Detour =>
      //code here
  }
}

//Class Containing My Messages
import akka.actor.ActorRef

case object ApproachingEastWest
case object ApproachingNorthSouth
case class Approaching(intersection: ActorRef, eastWest: Boolean)

case object CheckLight
case object ChangeLight
case object GreenLight

case class Direction(intersection: ActorRef, timeReachIntersection: Int, eastWest: Boolean)

case class Detour(detour: List[Direction])

case object GetNextDirection
case object ArrivedAtDestination

我想知道如何在Car类中访问方向状态,以便可以在Detour案例类(List [Direction])中添加消息。所以我目前所拥有的是一个交叉点...

scala akka actor event-based-programming
1个回答
0
投票

没有实施汽车演员,我只能根据假设给出答案。我假设汽车演员在交叉路口等待接收灯光变化的信号。如果是这样,我将在Car Actor代码中输入以下内容:

© www.soinside.com 2019 - 2024. All rights reserved.