LaneGTUIdTransceiver.java

  1. package org.opentrafficsim.sim0mq.publisher;

  2. import org.djunits.Throw;
  3. import org.djutils.immutablecollections.ImmutableList;
  4. import org.djutils.metadata.MetaData;
  5. import org.djutils.metadata.ObjectDescriptor;
  6. import org.djutils.serialization.SerializationException;
  7. import org.opentrafficsim.core.gtu.GTU;
  8. import org.opentrafficsim.core.network.Link;
  9. import org.opentrafficsim.core.network.OTSNetwork;
  10. import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
  11. import org.opentrafficsim.road.network.lane.CrossSectionElement;
  12. import org.opentrafficsim.road.network.lane.CrossSectionLink;
  13. import org.opentrafficsim.road.network.lane.Lane;
  14. import org.sim0mq.Sim0MQException;

  15. /**
  16.  * Transceiver for the ids of the GTUs on a link.
  17.  * <p>
  18.  * Copyright (c) 2020-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  19.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  20.  * </p>
  21.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  22.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  23.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  24.  */
  25. public class LaneGTUIdTransceiver extends AbstractTransceiver
  26. {
  27.     /** The network. */
  28.     private final OTSNetwork network;

  29.     /**
  30.      * Construct a GTUIdTransceiver.
  31.      * @param network OTSNetwork; the OTS network
  32.      */
  33.     public LaneGTUIdTransceiver(final OTSNetwork network)
  34.     {
  35.         super("Lane GTU id transceiver",
  36.                 new MetaData("Link id, lane id", "Link id, lane id",
  37.                         new ObjectDescriptor[] { new ObjectDescriptor("Link id", "Link id", String.class),
  38.                                 new ObjectDescriptor("Lane id", "Lane id", String.class) }),
  39.                 new MetaData("String array", "String array filled with all currently valid GTU ids on the lane",
  40.                         new ObjectDescriptor[] { new ObjectDescriptor("String array",
  41.                                 "String array filled with all currently valid GTU ids on the lane", String[].class) }));
  42.         Throw.whenNull(network, "Network may not be null");
  43.         this.network = network;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public final Object[] get(final Object[] address, final ReturnWrapper returnWrapper)
  48.             throws Sim0MQException, SerializationException
  49.     {
  50.         String bad = verifyMetaData(getAddressFields(), address);
  51.         if (bad != null)
  52.         {
  53.             returnWrapper.nack("Bad address; need id of a link and id of a CrossSectionElement");
  54.             return null;
  55.         }
  56.         Link link = this.network.getLink((String) address[0]);
  57.         if (null == link || (!(link instanceof CrossSectionLink)))
  58.         {
  59.             returnWrapper.nack("Network does not contain a link with id " + address[0]);
  60.             return null;
  61.         }
  62.         CrossSectionLink csl = (CrossSectionLink) link;
  63.         CrossSectionElement cse = csl.getCrossSectionElement((String) address[1]);
  64.         if (null == cse)
  65.         {
  66.             returnWrapper.nack("Link " + address[0] + " does not contain a cross section element with id " + address[1]);
  67.             return null;
  68.         }
  69.         if (!(cse instanceof Lane))
  70.         {
  71.             returnWrapper.nack("CrossSectionElement " + address[1] + " of link with id " + address[0] + ", is not a lane");
  72.             return null;
  73.         }
  74.         Lane lane = (Lane) cse;
  75.         ImmutableList<LaneBasedGTU> gtus = lane.getGtuList();
  76.         Object[] result = new Object[gtus.size()];
  77.         int nextIndex = 0;
  78.         for (GTU gtu : gtus)
  79.         {
  80.             result[nextIndex++] = gtu.getId();
  81.         }
  82.         return result;
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public String toString()
  87.     {
  88.         return "LaneGTUIdTransceiver [network=" + this.network + ", super=" + super.toString() + "]";
  89.     }

  90. }