Define.pas
资源名称:Elavotor.rar [点击查看]
上传用户:sipnol
上传日期:2013-03-16
资源大小:318k
文件大小:28k
源码类别:
百货/超市行业
开发平台:
Visual C++
- unit Define;
- interface
- uses
- Classes,Forms,Controls,Windows,comctrls,stdctrls,Sysutils,grids,Dialogs;
- Const
- MaxFloor = 40; //最大楼梯层
- MaxPerson = 1000; //最大仿真人数
- MaxElavotor = 10; //最大电梯数
- ElavotorStatus_Idle = 0; //电梯空闲状态
- ElavotorStatus_UP = 1; //电梯上行状态
- ElavotorStatus_Down = 2; //电梯下行状态
- PersonStatus_idle = 0; //人员停留状态
- PersonStatus_Request = 1; //人员请求状态
- PersonStatus_Taking = 2; //人员乘梯状态
- type
- //定义线程对象
- TRunThread = class(TThread)
- private
- ElavotorID:integer;
- protected
- procedure RuningElavotor; //线程仿真电梯运行
- procedure Runing(ElavotorID:integer); //线程仿真电梯运行
- procedure Execute; override;
- end;
- TSystemMonitor = class(TThread)
- private
- protected
- procedure Monitor; //线程仿真电梯运行
- procedure Execute; override;
- end;
- //定义电梯对象
- TElavotor = class
- private
- protected
- public
- Speed:integer; //速度
- Direction:integer; //电梯运行方向 “0=idle" "1=up" "2=down"
- MaxLoadCapacity:integer; //电梯最大载客量
- CurrentLoadCapacity:integer; //电梯当前载客量
- DestFloorLayer:integer; //电梯目标楼层
- isFull:Boolean; //电梯满员
- nIdleTime:integer; //空闲时间计数器
- nBusyTime:integer; //工作时间计数器
- CurrentStatus:integer; //电梯当前状态 "idle" "up" "down"
- PersonTotal:integer; //总完成承载人数
- ID:integer; //电梯编号
- CurrentFloor:integer; //电梯当前所处楼层
- UpDownTime:integer; //每人上下电梯时间
- OSdelaytime:integer; //电梯开关门时间
- OuterRequestArray:TStringList; //等待队列 "xx-xx"
- InsideRequestArray:TStringList; //等待队列 "12-35"
- procedure OpenDoor(DelayTime:integer); //开门
- procedure CloseDoor(DelayTime:integer); //关门
- Procedure UPrun; //上行
- Procedure Downrun; //下行
- Procedure Continuerun; //继续运行
- Function CheckStop(CurrentFloor:integer):Boolean; //检查当前层是否有人上下
- Procedure TakeElavotor(CurrentFloor:integer); //乘梯
- end;
- //定义人员对象
- TPerson = Class
- Private
- Protected
- public
- Direction:integer; //请求的方向 "0=idle" "1=Up" "2=Down"
- FinishedCount:integer; //目前已乘坐次数
- DestinationFloor:integer; //请求目标楼层
- CurrentFloor:integer; //目前所处楼层
- PersonID:integer; //人员编号
- SelectedElavotorID:integer; //选择要乘的电梯编号
- PersonStatus:integer; //当前状态 "Request" "idle" "Taking"
- idle_Time:integer; //人员停留时间
- Request_Time:integer; //人员请求时间
- Taking_Time:integer; //人员乘梯时间
- MissionCompleted:Boolean; //是否结束仿真任务
- Function ChooseElavotor(CurrentFloor,DestinationFloor:integer):integer;//选择合适的电梯
- Procedure SendRequest(CurrentFloor,DestinationFloor:integer); //发送乘梯请求
- end;
- //定义楼层对象
- TFloor = class
- private
- protected
- public
- Requestlist:TStringList; //请求乘梯队列
- Idlelist:TStringList; //空闲队列 "UserID-CurTime-IdleTime"
- end;
- //定义系统控制对象
- TSystemControl = Class
- private
- protected
- public
- CurrentPersonTotal:integer; //当前已经到达大厦的人数
- FinishedPersonTotal:integer; //当前已经完成乘梯任务的人数
- SimTimeTotal:integer; //系统计数器
- NewPerson:integer; //新到人数
- Procedure Initsystem; //初始化系统
- Function InitAddedPerson:integer; //初始化新到来的人对象
- Procedure Statistic; //系统实时统计
- end;
- function Finduser(arraylist:TStringList;userId:integer):Boolean; //寻找队列中是否已经存在该客户
- function CanTake(ElavotorID,Destlayer:integer):Boolean; //目标楼层是否可以直接到达
- var
- Maxload :integer; //电梯最大乘客量
- PersonNum:integer; //参与仿真人数
- Ontime:integer; //人员到齐时间
- ElavotorSpeed:integer; //电梯运行速度
- UpDownTime:integer; //上下电梯时间
- TakeNum:integer; //乘做电梯次数
- Elavotor:array[0..9] of TElavotor; //电梯实例对象
- RunThread:array[0..9] of TRunThread; //运行电梯线程实例对象
- Floor:array[1..40] of TFloor; //定义楼梯实例对象
- Person:array of Tperson; //定义动态人员列表
- SystemControl:TSystemControl; //系统控制对象
- SystemMonitor:TsystemMonitor; //系统监控线程实例对象
- ElavotorTrack:array[0..9] of TTrackBar; //显示电梯进度
- Elavotor_idle_Label:array[0..9] of TStaticText; //显示空闲时间
- Elavotor_Busy_Label:array[0..9] of TStaticText; //显示运行时间
- Elavotor_Memo:array[0..9] of TMemo; //显示电梯内部运载情况
- ShowFloor:TstringGrid; //显示楼层人员停留
- DelIdle:Tstringlist; //临时删除队列
- implementation
- uses main;
- { TRunThread }
- function Finduser(arraylist:TStringList;userId:integer):Boolean;
- var i,j:integer;
- Tempstr,str,StrEID,strFloor:string;
- begin
- Result:=False;
- for i:=0 to arraylist.Count-1 Do
- begin
- Tempstr:=arraylist.Strings[i]; //等待信息
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1); //取得乘客编号
- StrEID:=Copy(Tempstr,pos(':',Tempstr)+1,
- length(tempstr)-pos(':',Tempstr)+1); //取得所等待的电梯编号
- if Strtoint(Str)=userId then //当前乘客是否存在
- begin
- Result:=True;
- if Person[userId].SelectedElavotorID<>Strtoint(StrEID) then //是否改变所乘电梯
- begin
- strFloor:=copy(Tempstr,0,pos(':',Tempstr)-1);
- Elavotor[Strtoint(StrEID)].OuterRequestArray.Delete( //删除不合适等待队列
- Elavotor[Strtoint(StrEID)].OuterRequestArray.indexof(strFloor));
- Floor[Person[userId].CurrentFloor].Requestlist.Delete(
- Floor[Person[userId].CurrentFloor].Requestlist.indexof(Tempstr));
- Person[userId].SendRequest(Person[userId].CurrentFloor, //选择另外一个电梯
- Person[userId].DestinationFloor);
- end;
- Exit;
- end;
- end;
- end;
- procedure TRunThread.RuningElavotor;
- begin
- self.Runing(self.ElavotorID);
- end;
- procedure TRunThread.Runing(ElavotorID: integer);
- begin
- Elavotor[ElavotorID].Continuerun;
- Application.ProcessMessages;
- end;
- procedure TRunThread.Execute;
- begin
- while not self.Terminated DO
- begin
- Synchronize(RuningElavotor);
- sleep(1000);
- end;
- end;
- function TSystemControl.InitAddedPerson: integer;
- var i,iTemp:integer;
- begin
- Result:=0;
- //人员随机到齐
- if self.CurrentPersonTotal>=PersonNum then
- begin
- Result:=1;
- exit;
- end else if (PersonNum-self.CurrentPersonTotal)<5 then
- begin
- Self.NewPerson:=PersonNum-self.CurrentPersonTotal;
- end else Self.NewPerson:=Random(5);
- if Self.NewPerson = 0 then exit;
- iTemp:=self.CurrentPersonTotal+Self.NewPerson;
- if iTemp>PersonNum then
- begin
- Self.NewPerson:=PersonNum + self.CurrentPersonTotal;
- iTemp:=PersonNum;
- exit;
- end;
- Setlength(Person,iTemp);
- for i:=0 to self.NewPerson-1 Do
- try
- person[self.CurrentPersonTotal+i]:=Tperson.Create;
- person[self.CurrentPersonTotal+i].FinishedCount:=0;
- person[self.CurrentPersonTotal+i].Direction:=1;
- person[self.CurrentPersonTotal+i].CurrentFloor:=1;
- Person[self.CurrentPersonTotal+i].PersonID:=self.CurrentPersonTotal+i;
- Person[self.CurrentPersonTotal+i].PersonStatus:=PersonStatus_Request;
- Person[self.CurrentPersonTotal+i].Taking_Time:=0;
- Person[self.CurrentPersonTotal+i].idle_Time:=0;
- Person[self.CurrentPersonTotal+i].Request_Time:=0;
- Person[self.CurrentPersonTotal+i].MissionCompleted:=false;
- person[self.CurrentPersonTotal+i].DestinationFloor:=Random(40)+1;
- while person[self.CurrentPersonTotal+i].DestinationFloor<=1 DO
- person[self.CurrentPersonTotal+i].DestinationFloor:=Random(40)+1;
- person[self.CurrentPersonTotal+i].SelectedElavotorID:=
- person[self.CurrentPersonTotal+i].ChooseElavotor(1,
- person[self.CurrentPersonTotal+i].DestinationFloor);
- person[self.CurrentPersonTotal+i].SendRequest(1,
- person[self.CurrentPersonTotal+i].DestinationFloor);
- finally
- end;
- self.CurrentPersonTotal:=iTemp;
- end;
- procedure TSystemControl.Initsystem;
- var i:integer;
- begin
- self.SimTimeTotal:=0;
- self.CurrentPersonTotal:=0;
- DelIdle:=Tstringlist.Create;
- systemMonitor:=TsystemMonitor.create(false); //创建系统监控线程
- systemMonitor.Suspend;
- Randomize;
- //初始化电梯对象参数
- try
- for i:=0 to 9 Do
- begin
- Elavotor[i]:=TElavotor.Create;
- Elavotor[i].ID:=i;
- Elavotor[i].Speed :=ElavotorSpeed;
- Elavotor[i].MaxLoadCapacity:=Maxload;
- Elavotor[i].UpDownTime:=UpDownTime;
- Elavotor[i].CurrentLoadCapacity :=0;
- Elavotor[i].Direction:=2;
- Elavotor[i].DestFloorlayer:=0;
- Elavotor[i].OSdelaytime:=1;
- Elavotor[i].CurrentStatus:=0;
- Elavotor[i].CurrentLoadCapacity :=0;
- Elavotor[i].nIdleTime:=0;
- Elavotor_idle_Label[i].Caption :=Inttostr(Elavotor[i].nidleTime);
- Elavotor[i].nBusyTime:=0;
- Elavotor_Busy_Label[i].Caption :=Inttostr(Elavotor[i].nBusyTime);
- Elavotor[i].CurrentFloor:=Random(40)+1;
- ElavotorTrack[i].Position:=1-Elavotor[i].CurrentFloor;
- Elavotor[i].PersonTotal :=0;
- Elavotor[i].OuterRequestArray :=TStringList.Create;
- Elavotor[i].InsideRequestArray :=TStringList.Create;
- RunThread[i]:=TRunThread.Create(false);
- RunThread[i].Suspend;
- RunThread[i].ElavotorID:= i;
- end;
- For i:=1 to 40 Do
- begin
- Floor[i]:=TFloor.Create;
- Floor[i].Requestlist:=TStringList.Create;
- Floor[i].Idlelist:=TStringList.Create;
- end;
- finally
- end;
- end;
- { TElavotor }
- function TElavotor.CheckStop(CurrentFloor:integer): Boolean;
- var i,j,iDown:integer;
- Tempstr,Str:string;
- begin
- Result:=false;
- //判断当前层是否有人下
- if (CurrentFloor>40) or (CurrentFloor<1) then exit;
- iDown:=self.InsideRequestArray.Count-1;
- for i:=0 to iDown Do
- begin
- Tempstr:=self.InsideRequestArray.Strings[i];
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1);
- j:=Strtoint(Str);
- if Person[j].MissionCompleted then Continue;
- if person[j].DestinationFloor=CurrentFloor then
- begin
- Result:=True;
- Exit;
- end;
- end;
- //判断是否满员
- if self.isFull then
- begin
- Result:=false;
- exit;
- end;
- //如果没有人下,判断是否有人上
- iDown:=Floor[CurrentFloor].Requestlist.Count-1;
- for i:=0 to iDown Do
- if CurrentFloor<>1 then
- begin
- Tempstr:=Floor[CurrentFloor].Requestlist.Strings[i];
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1);
- j:=Strtoint(Str);
- if not person[j].MissionCompleted then
- if person[j].CurrentFloor = CurrentFloor then
- begin
- Result:=True;
- Exit;
- end;
- end else if (CurrentFloor=1) or (CurrentFloor=40) then
- begin
- Result:=True;
- Exit;
- end;
- end;
- procedure TElavotor.CloseDoor(DelayTime: integer);
- begin
- self.nidleTime:=self.nidleTime+DelayTime;
- Elavotor_idle_Label[self.ID].Caption :=inttostr(self.nidleTime);
- end;
- procedure TElavotor.Continuerun;
- begin
- if Self.Direction=1 then self.UPrun
- else if Self.Direction=2 then self.Downrun
- else if Self.Direction=0 then
- begin
- self.nidleTime:=self.nidleTime+1;
- Elavotor_idle_Label[self.ID].Caption :=inttostr(self.nidleTime);
- exit;
- end;
- end;
- procedure TElavotor.Downrun;
- begin
- if (self.CurrentFloor=1) then
- begin
- self.Direction:=1;
- exit;
- end;
- Self.CurrentFloor:=self.CurrentFloor-ElavotorSpeed;
- if Self.CurrentFloor<=0 then Self.CurrentFloor:=1;
- ElavotorTrack[self.ID].Position:=1-self.CurrentFloor;
- self.nBusyTime:= self.nBusyTime+1;
- Elavotor_Busy_Label[self.ID].Caption :=inttostr(self.nBusyTime);
- if not CanTake(self.ID,self.CurrentFloor) then exit; //如果当前楼层不能停,就继续下
- self.TakeElavotor(self.CurrentFloor);
- end;
- procedure TElavotor.OpenDoor(DelayTime: integer);
- begin
- self.nidleTime:=self.nidleTime+DelayTime;
- Elavotor_idle_Label[self.ID].Caption :=inttostr(self.nidleTime);
- end;
- procedure TElavotor.TakeElavotor(CurrentFloor:integer);
- var i,j,k,iDown,MaxDest:integer;
- Tempstr,str,AddStr:string;
- Del:TstringList;
- LockThread:TThreadlist;
- begin
- LockThread:=TThreadlist.Create;
- LockThread.Add(Floor[CurrentFloor].Requestlist); //加入锁定共享数据列表
- LockThread.Add(Floor[CurrentFloor].Idlelist);
- LockThread.Add(self.OuterRequestArray);
- LockThread.LockList; //锁定共享数据对象
- self.Speed:=ElavotorSpeed;
- self.MaxLoadCapacity:=self.InsideRequestArray.Count;
- Del:=Tstringlist.Create;
- if (CurrentFloor>40) or (CurrentFloor<1) then exit;
- if self.CheckStop(CurrentFloor) then
- begin
- //当前层有人下
- iDown:=self.InsideRequestArray.Count-1;
- for i:=0 to iDown Do
- begin
- Tempstr:=self.InsideRequestArray.Strings[i];
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1);
- j:=Strtoint(Str);
- if person[j].DestinationFloor=CurrentFloor then
- begin
- if person[j].FinishedCount=TakeNum+1 then
- begin
- person[j].MissionCompleted:=true;
- Continue;
- end;
- person[j].CurrentFloor:=CurrentFloor;
- person[j].SelectedElavotorID:=0;
- person[j].FinishedCount:=person[j].FinishedCount+1;
- person[j].PersonStatus:=PersonStatus_idle;
- Del.Add(Tempstr);
- K:=Elavotor_Memo[self.ID].Lines.IndexOf(Tempstr);
- if k>=0 then
- Elavotor_Memo[self.ID].Lines.Delete(k);
- k:=Random(100)+20;
- Addstr:=inttostr(j)+'-'+inttostr(systemControl.SimTimeTotal)+':'+inttostr(k);
- Floor[CurrentFloor].Idlelist.Add(Addstr); //加入闲置队列UserID-basetime:idletime
- end;
- end;
- //删除当前楼层等待队列中以上电梯的人员
- for i:=0 to Del.Count-1 Do
- if Del.Strings[i]<>'' then
- begin
- k:=self.InsideRequestArray.IndexOf(Del.Strings[i]);
- if k>=0 then
- self.InsideRequestArray.Delete(k);
- end;
- //当前层有人上
- iDown:= Floor[CurrentFloor].Requestlist.Count-1; //计算当前楼层等待人数
- Del.Clear;
- for i:=0 to iDown Do //判断等待队列中每个人是否要上当前电梯
- begin
- Tempstr:=Floor[CurrentFloor].Requestlist.Strings[i];
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1);
- j:=Strtoint(Str);
- if person[j].CurrentFloor<>self.CurrentFloor then Continue else //是否电梯到达乘客当前楼层
- if person[j].SelectedElavotorID = Self.ID then
- begin
- if self.CurrentFloor=1 then self.Direction:=1;
- if self.CurrentFloor=40 then self.Direction:=2;
- if self.CurrentLoadCapacity=MaxLoad then //电梯是否已经满员
- begin
- self.isFull:=true;
- Break;
- end;
- self.CurrentLoadCapacity:=self.CurrentLoadCapacity+1; //增加当前乘客
- Addstr:=inttostr(j)+'-'+inttostr(Person[j].DestinationFloor);
- self.InsideRequestArray.Add(Addstr); //把当前乘客加入内部乘梯队列
- Elavotor_Memo[self.ID].Lines.Add(Addstr);
- k:=self.OuterRequestArray.IndexOf(Addstr);
- if k>=0 then
- Elavotor[self.ID].OuterRequestArray.Delete(k); //删除当前乘客在电梯外的等待队列信息
- person[j].PersonStatus:=PersonStatus_Taking; //置上当前电梯的人的状态为乘做
- Del.Add(Tempstr);
- end;
- end;
- //删除当前楼层等待队列中以上电梯的人员
- for i:=0 to Del.Count-1 Do
- if Del.Strings[i]<>'' then
- begin
- K:=Floor[CurrentFloor].Requestlist.IndexOf(Del.Strings[i]);
- if k>=0 then
- Floor[CurrentFloor].Requestlist.Delete(K);
- end;
- Del.Free;
- //改变电梯目标楼层
- MaxDest:=Self.DestFloorLayer;
- iDown:= self.InsideRequestArray.Count-1;
- if (self.CurrentFloor<>1) or (self.CurrentFloor<>40) then
- for i:=0 to iDown Do
- begin
- Tempstr:=self.InsideRequestArray.Strings[i];
- Str:=Copy(Tempstr,0,pos('-',Tempstr)-1); //UserID-DestFloor
- j:=Strtoint(Str); //取得乘客编号
- k:=strtoint(copy(Tempstr,pos('-',Tempstr)+1,length(Tempstr)-pos('-',Tempstr)+1));
- if self.Direction=Person[j].Direction then
- if MaxDest<k then MaxDest:=k; //电梯到达的最大楼层
- end;
- Self.DestFloorLayer:=MaxDest;
- end;
- LockThread.UnlockList;
- LockThread.Free;
- end;
- procedure TElavotor.UPrun;
- begin
- if (self.CurrentFloor=40) then
- begin
- self.Direction:=2;
- exit;
- end;
- self.CurrentFloor:=self.CurrentFloor+ElavotorSpeed;
- if Self.CurrentFloor>40 then Self.CurrentFloor:=40;
- self.nBusyTime:=self.nBusyTime+1;
- ElavotorTrack[self.ID].Position:=1-self.CurrentFloor;
- Elavotor_Busy_Label[self.ID].Caption :=inttostr(self.nBusyTime);
- if not CanTake(self.ID,self.CurrentFloor) then exit; //如果当前楼层不能停,就继续上
- self.TakeElavotor(Self.CurrentFloor);
- end;
- { TPerson }
- function CanTake(ElavotorID,Destlayer: integer):Boolean;
- begin
- Result:=false;
- case ElavotorID of
- 0,1:begin Result:=true; exit; end;
- 2,3:begin if (Destlayer=1) or (Destlayer>=25) and (Destlayer<=40) then Result:=true else Result:=false; exit;end;
- 4,5:begin if Destlayer<=25 then Result:=true; exit;end;
- 6,7:begin if (Destlayer=1) or (Destlayer>=2) and (Destlayer<=40) then
- if (Destlayer mod 2)=0 then Result:=true else Result:=false; exit;end;
- 8,9:begin if (Destlayer=1) or (Destlayer<=25) and (Destlayer<=39) then
- if not ((Destlayer mod 2)=0) then Result:=true else Result:=false; exit; end;
- end;
- end;
- function TPerson.ChooseElavotor(CurrentFloor,DestinationFloor: integer): integer;
- var i,j,iTemp:integer;
- CanTakeElavotor:array[0..9] of integer;
- begin
- Result:=0;
- iTemp:=41;
- if self.CurrentFloor=1 then
- begin
- for i:=0 to 9 DO
- if CanTake(i,self.DestinationFloor) then
- begin
- if Elavotor[i].Direction=2 then
- if Elavotor[i].OuterRequestArray.Count<Maxload then
- CanTakeElavotor[i]:=ABS(self.CurrentFloor-Elavotor[i].CurrentFloor)
- else CanTakeElavotor[i]:=41
- else CanTakeElavotor[i]:=41;
- end else CanTakeElavotor[i]:=41;
- end else if self.CurrentFloor=40 then
- begin
- for i:=0 to 9 DO
- if CanTake(i,self.DestinationFloor) then
- begin
- if Elavotor[i].Direction=1 then
- CanTakeElavotor[i]:=ABS(self.CurrentFloor-Elavotor[i].CurrentFloor)
- else CanTakeElavotor[i]:=41;
- end else CanTakeElavotor[i]:=41;
- end else
- for i:=0 to 9 DO
- begin
- if CanTake(i,self.DestinationFloor) then
- begin
- if (self.Direction=Elavotor[i].Direction) or (self.CurrentFloor=1)
- or (self.CurrentFloor=40) then
- begin
- if self.Direction=1 then
- if (self.CurrentFloor-Elavotor[i].CurrentFloor)>=0 then
- CanTakeElavotor[i]:=ABS(self.CurrentFloor-Elavotor[i].CurrentFloor);
- if self.Direction=2 then
- if (self.CurrentFloor-Elavotor[i].CurrentFloor)<=0 then
- CanTakeElavotor[i]:=ABS(self.CurrentFloor-Elavotor[i].CurrentFloor);
- end
- else CanTakeElavotor[i]:=41;
- end
- else
- CanTakeElavotor[i]:=41;
- end;
- for i:=0 to 9 DO
- begin
- if CanTakeElavotor[i]<=iTemp then
- begin
- iTemp:=CanTakeElavotor[i];
- j:=i;
- end;
- Result:=j;
- end;
- if Result>=41 then
- if Elavotor[0].OuterRequestArray.Count<MaxLoad then Result:=0
- else Result:=1;
- end;
- procedure TPerson.SendRequest(CurrentFloor,DestinationFloor:integer);
- var Temp:string;
- begin
- if self.MissionCompleted then exit;
- self.PersonStatus:=PersonStatus_Request;
- Temp:=inttostr(self.PersonID)+'-'+inttostr(self.DestinationFloor)+':'+Inttostr(self.SelectedElavotorID);
- if not Finduser(Floor[CurrentFloor].Requestlist,self.PersonID) then
- begin
- Elavotor[self.SelectedElavotorID].OuterRequestArray.Add(copy(Temp,0,pos(':',Temp)-1));
- Floor[CurrentFloor].Requestlist.Add(Temp);
- end;
- end;
- procedure TSystemControl.Statistic;
- var i,j,k,z,h,iDelIdle,DelStat:integer;
- Tempstr,str_idle,str_taking,str_Request,str:string;
- begin
- self.FinishedPersonTotal:=0;
- mainform.memo10.Clear; //当前请求乘梯人员
- mainform.memo11.Clear; //当前闲留人员
- mainform.memo12.Clear; //当前完成乘梯任务的人员
- mainform.memo13.Clear; //当前正在乘梯的人员
- for i:=0 to self.CurrentPersonTotal-1 DO
- if not person[i].MissionCompleted then
- case person[i].PersonStatus of
- PersonStatus_idle:
- begin
- person[i].idle_Time:=person[i].idle_Time+1;
- str_idle:='第'+Inttostr(person[i].PersonID)+'号乘客已闲留'+Inttostr(Person[i].idle_Time)+'秒';
- if mainform.memo13.lines.indexof(str_idle)<0 then
- mainform.memo11.lines.Add(str_idle);
- end;
- PersonStatus_Request:
- begin
- person[i].Request_Time:=person[i].Request_Time+1;
- str_Request:='第'+Inttostr(person[i].PersonID)+'号已等'+Inttostr(Person[i].Request_Time)+'秒'+Inttostr(Person[i].SelectedElavotorID)+'号梯'+Inttostr(Person[i].CurrentFloor)+'楼';
- if mainform.memo13.lines.indexof(str_Request)<0 then
- mainform.memo10.lines.Add(str_Request);
- //动态选择电梯
- Person[i].SelectedElavotorID:=Person[i].ChooseElavotor(
- Person[i].CurrentFloor,Person[i].DestinationFloor);
- finduser(floor[Person[i].CurrentFloor].Requestlist,Person[i].PersonID);
- end;
- PersonStatus_Taking:
- begin
- person[i].Taking_Time:=person[i].Taking_Time+1;
- str_taking:='第'+Inttostr(person[i].PersonID)+'号乘客已乘坐'+Inttostr(Person[i].Taking_Time)+'秒';
- if mainform.memo13.lines.indexof(str_taking)<0 then
- mainform.memo13.lines.Add(str_taking);
- end;
- end else begin
- self.FinishedPersonTotal:=self.FinishedPersonTotal+1;
- mainform.memo12.Lines.Add('第'+Inttostr(person[i].PersonID)+'号乘客');
- end;
- mainform.STB1.Panels[0].Text:='当前系统运行时间:'+Inttostr(self.SimTimeTotal)+'秒';
- mainform.STB1.Panels[1].Text:='当前系统仿真人数:'+Inttostr(self.CurrentPersonTotal)+'个';
- mainform.STB1.Panels[2].Text:='已经完成乘梯任务的人数: '+Inttostr(self.FinishedPersonTotal)+'个';
- j:=0;
- z:=0;
- K:=1;
- //检测是否有人完成闲留开始请求
- for i:=1 to 40 Do
- if Floor[i].Idlelist.Count>0 then
- begin
- for h:=0 to Floor[i].Idlelist.Count-1 Do
- begin
- Tempstr:=Floor[i].Idlelist.Strings[h];
- j:=Strtoint(copy(Tempstr,0,pos('-',Tempstr)-1));
- K:=Strtoint(copy(Tempstr,pos('-',Tempstr)+1,pos(':',Tempstr)-pos('-',Tempstr)-1));
- z:=Strtoint(copy(Tempstr,pos(':',Tempstr)+1,Length(Tempstr)-pos(':',Tempstr)+1));
- if (self.SimTimeTotal-z)>=k then
- begin
- person[j].DestinationFloor:=Random(40)+1;
- while person[j].DestinationFloor=person[j].CurrentFloor DO
- person[j].DestinationFloor:=Random(40)+1;
- if (Person[j].MissionCompleted=True) or (person[j].FinishedCount=TakeNum) then
- Person[j].DestinationFloor:=1;
- person[j].SelectedElavotorID:=person[j].ChooseElavotor(1,person[j].DestinationFloor);
- person[j].SendRequest(1,person[j].DestinationFloor);
- DelIdle.Add(Tempstr);
- end;
- end;
- //删除当前楼层闲留队列中闲留时间已到的人员
- for iDelIdle:=0 to DelIdle.Count-1 Do
- if DelIdle.Strings[iDelIdle]<>'' then
- begin
- K:=Floor[i].Idlelist.IndexOf(DelIdle.Strings[iDelIdle]);
- if K>=0 then
- Floor[i].Idlelist.Delete(k);
- end;
- DelIdle.Clear;//清空临时删除队列
- end;
- //监控电梯运行情况
- for i:=0 to 9 DO
- begin
- if Elavotor[i].Direction=0 then
- if Elavotor[i].OuterRequestArray.Count>0 then
- begin
- Tempstr:=Elavotor[i].OuterRequestArray.Strings[0];
- j:=Strtoint(copy(Tempstr,0,pos('-',Tempstr)-1));
- Elavotor[i].Direction:=person[j].Direction;
- end;
- end;
- //显示楼层人员停留情况
- ShowFloor.RowCount:=12;
- ShowFloor.ColCount:=11;
- ShowFloor.Cells[0,1]:='等待人数';
- ShowFloor.Cells[0,2]:='闲留人数';
- ShowFloor.Cells[0,4]:='等待人数';
- ShowFloor.Cells[0,5]:='闲留人数';
- ShowFloor.Cells[0,7]:='等待人数';
- ShowFloor.Cells[0,8]:='闲留人数';
- ShowFloor.Cells[0,10]:='等待人数';
- ShowFloor.Cells[0,11]:='闲留人数';
- z:=0;
- k:=1;
- for i:=1 to 4 Do
- begin
- for j:=1 to 10 DO
- begin
- z:=z+1;
- ShowFloor.Cells[j,K-1]:='第'+inttostr(z)+'层楼';
- Tempstr:=Inttostr(Floor[z].Requestlist.count)+'个';
- Str:=Inttostr(Floor[z].Idlelist.count)+'个';
- ShowFloor.Cells[j,k]:=Tempstr;
- ShowFloor.Cells[j,k+1]:=str;
- end;
- K:=K+3;
- end;
- if self.FinishedPersonTotal=PersonNum then
- begin
- Application.MessageBox('本次仿真结束!','电梯仿真',0);
- mainform.Close;
- end;
- end;
- { TSystemMonitor }
- procedure TSystemMonitor.Execute;
- begin
- While not self.Terminated Do
- begin
- Synchronize(self.Monitor);
- sleep(1000); //间隔一秒
- end;
- end;
- procedure TSystemMonitor.Monitor;
- begin
- self.Priority:=tpTimeCritical;
- systemcontrol.SimTimeTotal:=systemcontrol.SimTimeTotal+1;
- SystemControl.InitAddedPerson;
- SystemControl.Statistic;
- Application.ProcessMessages;
- end;
- end.