View Javadoc

1   /*
2    * Created on Jun 25, 2003
3    * 
4    * Copyright (c) 2003, 2004 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.traffic.controlpoint.blocks;
12  
13  import java.util.ArrayList;
14  
15  /***
16   * This class implements a virtual block that will be guarded by n
17   * SingleTrackBlockTrafficLights. Only one vehicle is allowed in the Block.
18   * <p>
19   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
20   * University of Technology </a>, the Netherlands. <br>
21   * See for project information <a href="http://www.simulation.tudelft.nl">
22   * www.simulation.tudelft.nl </a> <br>
23   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
24   * Public License (GPL) </a>, no warranty <br>
25   * 
26   * @version Jun 20, 2004 <br>
27   * @author <a
28   * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
29   * Verbraeck </a>
30  */
31  public class VirtualBlock
32  {
33      /*** busy shows the number of vehicles in the guarded block */
34      protected int busy;
35  
36      /***
37       * requestList shows the list of SwicthBlockTrafficLights that are
38       * requesting access to the block
39       */
40      public ArrayList requestList;
41  
42      /*** name of the guarded block */
43      private String name;
44  
45      /***
46       * Constructs a VirtualBlock
47       * 
48       * @param name name of the VirtualBlock
49       */
50      public VirtualBlock(final String name)
51      {
52          this.name = name;
53          this.busy = 0;
54          this.requestList = new ArrayList();
55      }
56  
57      /***
58       * requestAccess process the request to gain access to the block
59       * 
60       * @param switchBlockTrafficLight the correpondent switchBlockTrafficLight
61       * that want to gain access to the block
62       */
63      public void requestAccess(
64              final SwitchBlockTrafficLight switchBlockTrafficLight)
65      {
66          System.out.println("Access to Virtual Block " + this.name
67                  + " has been requested");
68          if (this.busy == 0)
69          {
70              this.busy++;
71              switchBlockTrafficLight
72                      .changeState(SwitchBlockTrafficLight.STATE_GREEN);
73          } else
74          {
75              this.requestList.add(switchBlockTrafficLight);
76          }
77      }
78  
79      /***
80       * releaseBlock releases the block for other vehicles and check if there is
81       * any access request in the requesList
82       */
83      public void releaseBlock()
84      {
85          System.out.println("Virtual Block " + this.name + " has been released");
86          this.busy--;
87          if (!this.requestList.isEmpty())
88          {
89              SwitchBlockTrafficLight switchBlockTrafficLight = (SwitchBlockTrafficLight) (this.requestList
90                      .remove(0));
91              requestAccess(switchBlockTrafficLight);
92          }
93      }
94  
95      /***
96       * @return Returns the name.
97       */
98      public String getName()
99      {
100         return this.name;
101     }
102 }