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