View Javadoc
1   package org.opentrafficsim.road.gtu.lane;
2   
3   import java.rmi.RemoteException;
4   
5   import org.opentrafficsim.core.gtu.MoveCheckerException;
6   import org.opentrafficsim.core.network.Network;
7   import org.opentrafficsim.core.network.OTSNetwork;
8   
9   import nl.tudelft.simulation.event.EventInterface;
10  import nl.tudelft.simulation.event.EventListenerInterface;
11  
12  /**
13   * Abstract class that listens to move events of GTUs so checks can be performed.
14   * <p>
15   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Aug 6, 2019 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public abstract class AbstractLaneBasedMoveChecker implements EventListenerInterface
24  {
25  
26      /** Network. */
27      private final OTSNetwork network;
28  
29      /**
30       * Constructor.
31       * @param network OTSNetwork; network
32       */
33      public AbstractLaneBasedMoveChecker(final OTSNetwork network)
34      {
35          network.addListener(this, Network.GTU_ADD_EVENT);
36          network.addListener(this, Network.GTU_REMOVE_EVENT);
37          this.network = network;
38      }
39  
40      /** {@inheritDoc} */
41      @Override
42      public void notify(final EventInterface event) throws RemoteException
43      {
44          if (event.getType().equals(LaneBasedGTU.LANEBASED_MOVE_EVENT))
45          {
46              try
47              {
48                  checkMove(((LaneBasedGTU) event.getSource()));
49              }
50              catch (Exception ex)
51              {
52                  throw new MoveCheckerException(ex);
53              }
54          }
55          else if (event.getType().equals(Network.GTU_ADD_EVENT))
56          {
57              this.network.getGTU((String) event.getContent()).addListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
58          }
59          else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
60          {
61              this.network.getGTU((String) event.getContent()).removeListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
62          }
63          else
64          {
65              throw new RemoteException("AbstractMoveChecker is a listener to an unknown event type.");
66          }
67      }
68  
69      /**
70       * Check the move of the given GTU.
71       * @param gtu LaneBasedGTU; GTU.
72       * @throws Exception thrown when something is not all right
73       */
74      public abstract void checkMove(LaneBasedGTU gtu) throws Exception;
75  
76  }