BUAAOO P5-P7 Elevator Simulation

本文详细介绍了并发电梯模拟系统的项目设计,包括读取请求、协调调度、电梯模拟等核心功能模块,以及多线程安全、并发架构设计和复杂度评估等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Abstract

Introduction

Topic

The topic of project 5-7 is concurrent elevator simulation. The program read requests from stdin, simulate the elevator system, and print runtime infomation to stdout.

Request

<PersonID>-FROM-<FromLevel>-TO-<ToLevel>

  • On behalf of a pessenger
  • <PersonID> must be unique

Elevator

  • Function
    • Move: Move up or down to next level in <MoveTime>, and print ARRIVE-<Level>-<ElevatorName>
    • Open Door: Open Door in <OpenTime>, and print OPEN-<Level>-<ElevatorName> when begin opening
    • Close Door: Close Door in <CloseTime>, and print CLOSE-<Level>-<ElevatorName> when close finished
    • Pessenger in: Print IN-<PersonID>-<Level>-<ElevatorName>
    • Pessenger out: Print OUT-<PersonID>-<Level>-<ElevatorName>
  • Argument
    • Name
    • Time
      • OpenTime
      • CloseTime
      • MoveTime
    • Level
      • InitialLevel
      • SupLevel
      • InfLevel
      • ApprovedLevel
    • Capacity

Analysis

The major focus is concurrency: The program has to plan and simulate while reading requests, so it has to be concurrent. Thus we have to design a concurrent architechture and guarantee its thread safety.

Also, for multi-elevator scene, we need to design a coordinating thread to coordinate elevators.

The program's function can be classified as follows, and the threads can be designed correspondingly:

Reading Requests

Reading requests in real time, and pass them to coordinating thread.

Coordinating

Get requests from reading thread, set path for the person and pass it to scheduling thread.

Scheduling and controling

Get requests from reading thread, schedule and pass the action of elevator to elevator, and control the movement of passenger.

Elevator simulation

Get command from scheduling thread, open and close door, and go up or down stairs.

Course

This topic has three assignments.

P5-P6

Reading (and coordinating)

  • Read from given input interface
  • Analyse the request, put the person to corresponse location in concurrent two-dimensional map
    • Dimension 1: From level
    • Dimension 2: To level
  • Pass person to scheduling and controling thread through cuncurrent map: ConcurrentHashMap<MapKey, Set<Integer>>

Scheduling and controling

  • Move up and down again and again
    • Take in person whose destination direction is consistent with the elevator
    • Free person who arrived
    • U-turn if there's no person ahead
    • Stop if there's no person anywhere
  • Give command through TransferQueue<Command> to elevator

Elevator simulation

  • Execute command
    • Print information
    • Change level
    • Sleep for given time

P7

Reading and coordinating

  • Read from given input interface
  • Analyse and coordinate
    • If there's a single elevator approved to access both from and to floor
      • The person only have one request: from <FromLevel> to <ToLevel>
    • else
      • Because it takes at most two steps to go from <FromLevel> to <ToLevel>
      • Find the elevator who can access <FromLevel>
      • Find the elevator who can access <ToLevel>
      • Find the level which can be accessed by both elevator
      • The person has two request
        • From <FromLevel> to <TransferLevel>
        • From <TransgerLevel> to <ToLevel>
    • The coordinate algorithm can be refacted to recursive one to support any number of transfers
  • Pass
    • Pass person to scheduling and controling thread through cuncurrent map: ConcurrentHashMap<MapKey, Set<Integer>>
    • The mapkey presents the first request: new MapKey(<FromLevel>, <ToLevel>)
    • The rest requests stored in the person object

Scheduling and controling

  • Move up and down again and again
    • If current level is accessable
      • Take in person
        • Destination direction is consistent with the elevator
        • Destination level is accessable
      • Free person who arrived
        • If the person has rest requests, put him to correspensive location in reqMap, and delete its next request
    • U-turn if there's no person at accessable level ahead
    • Stop if there's no person anywhere
  • Give command through TransferQueue<Command> to elevator

Elevator simulation

  • Execute command
    • Print information
    • Change level
    • Sleep for given time

Class Design

Class Design

Measurement

Complexity Metric

Method Complexity

Methodev(G)iv(G)v(G)
com.nyan.EleSysTest.main(String[])111
com.nyan.elesys.Commander.Commander(int,String,Integer[],Integer,Integer,Integer,ConcurrentHashMap<MapKey, Set<Person>>,Long,Long,Long)111
com.nyan.elesys.Commander.closeDoor()122
com.nyan.elesys.Commander.downHave()323
com.nyan.elesys.Commander.inDownPsgers()133
com.nyan.elesys.Commander.inPsgers(Integer)445
com.nyan.elesys.Commander.inUpPsgers()133
com.nyan.elesys.Commander.levelHave(int)568
com.nyan.elesys.Commander.loop()759
com.nyan.elesys.Commander.openDoor()122
com.nyan.elesys.Commander.outPsgers()144
com.nyan.elesys.Commander.run()145
com.nyan.elesys.Commander.upHave()323
com.nyan.elesys.Coordinator.Coordinator(ConcurrentHashMap<MapKey, Set<Person>>,Integer[][])122
com.nyan.elesys.Coordinator.run()121114
com.nyan.elesys.Elevator.Elevator(Long,Long,Long,Integer,String,TransferQueue<Command>)111
com.nyan.elesys.Elevator.getLevelNum()111
com.nyan.elesys.Elevator.run()378
com.nyan.elesys.Level.Level(Integer)111
com.nyan.elesys.Level.add(Integer)122
com.nyan.elesys.Level.addOne()112
com.nyan.elesys.Level.equals(Object)222
com.nyan.elesys.Level.getLevelNum()111
com.nyan.elesys.Level.sub(Integer)122
com.nyan.elesys.Level.subOne()112
com.nyan.elesys.Level.toString()111
com.nyan.elesys.MapKey.MapKey(Integer,Integer)111
com.nyan.elesys.MapKey.equals(Object)233
com.nyan.elesys.MapKey.hashCode()111
com.nyan.elesys.MapKey.toString()111
com.nyan.elesys.Person.Person(Integer)111
com.nyan.elesys.Person.addReq(Request)111
com.nyan.elesys.Person.compareTo(Person)111
com.nyan.elesys.Person.nextReq()212
com.nyan.elesys.Person.toString()111
com.nyan.elesys.PsgsList.PsgsList(Integer,Integer)122
com.nyan.elesys.PsgsList.add(int,Set<Person>)111
com.nyan.elesys.PsgsList.get(int)111
com.nyan.elesys.PsgsList.getTotal()122
com.nyan.elesys.PsgsList.isEmpty()334
com.nyan.elesys.Request.Request(Integer,Integer)111
com.nyan.elesys.Request.getFromLevel()111
com.nyan.elesys.Request.getToLevel()111

Class Complexity

ClassOCavgWMC
com.nyan.EleSysTest11
com.nyan.elesys.Commandn/a0
com.nyan.elesys.Commander3.3340
com.nyan.elesys.Coordinator714
com.nyan.elesys.Elevator39
com.nyan.elesys.Level1.6213
com.nyan.elesys.MapKey1.255
com.nyan.elesys.Person1.26
com.nyan.elesys.PsgsList1.89
com.nyan.elesys.Request13

Package Complexity

Packagev(G)avgv(G)tot
com.nyan11
com.nyan.elesys2.67112

Module Complexity

Modulev(G)avgv(G)tot
P7Project2.63113

Project Complexity

Projectv(G)avgv(G)tot
project2.63113

Bug Review

Level movement step

Door movement time

Test Strategy

Concurrent Knowledge

Acknowledge

Reference

转载于:https://www.cnblogs.com/daniel10001/p/10758916.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值