AbstractPerception.java

  1. package org.opentrafficsim.core.gtu.perception;

  2. import java.util.HashMap;
  3. import java.util.Map;

  4. import org.opentrafficsim.core.gtu.GTU;
  5. import org.opentrafficsim.core.gtu.GTUException;
  6. import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
  7. import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
  8. import org.opentrafficsim.core.network.NetworkException;

  9. import nl.tudelft.simulation.language.Throw;

  10. /**
  11.  * <p>
  12.  * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  13.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  14.  * <p>
  15.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jul 29, 2016 <br>
  16.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  17.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  18.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  19.  */

  20. public abstract class AbstractPerception implements Perception
  21. {

  22.     /** */
  23.     private static final long serialVersionUID = 20160729L;

  24.     /** Set of available perception categories. */
  25.     private final Map<Class<? extends AbstractPerceptionCategory>, AbstractPerceptionCategory> perceptionCategories =
  26.         new HashMap<>();
  27.    
  28.     /** GTU. */
  29.     private GTU gtu;
  30.    
  31.     /**
  32.      * Construct perception.
  33.      * @param gtu GTU
  34.      */
  35.     public AbstractPerception(final GTU gtu)
  36.     {
  37.         this.gtu = gtu;
  38.     }
  39.    
  40.     /** {@inheritDoc} */
  41.     @SuppressWarnings("checkstyle:designforextension")
  42.     public GTU getGtu()
  43.     {
  44.         return this.gtu;
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public final void addPerceptionCategory(final AbstractPerceptionCategory perceptionCategory)
  49.     {
  50.         // guarantees correct combination of class and perception category
  51.         this.perceptionCategories.put(perceptionCategory.getClass(), perceptionCategory);
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public final <T extends AbstractPerceptionCategory> boolean contains(final Class<T> clazz)
  56.     {
  57.         return this.perceptionCategories.containsKey(clazz);
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     @SuppressWarnings("unchecked")
  62.     public final <T extends AbstractPerceptionCategory> T getPerceptionCategory(final Class<T> clazz)
  63.         throws OperationalPlanException
  64.     {
  65.         Throw.when(!contains(clazz), OperationalPlanException.class, "Perception category" + clazz + " is not present.");
  66.         // addPerceptionCategory guarantees correct combination of class and perception category
  67.         return (T) this.perceptionCategories.get(clazz);
  68.     }
  69.    
  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public final void removePerceptionCategory(final AbstractPerceptionCategory perceptionCategory)
  73.     {
  74.         this.perceptionCategories.remove(perceptionCategory.getClass());
  75.     }

  76.     /** {@inheritDoc} */
  77.     @SuppressWarnings("checkstyle:designforextension")
  78.     public void perceive() throws GTUException, NetworkException, ParameterException
  79.     {
  80.         for (AbstractPerceptionCategory category : this.perceptionCategories.values())
  81.         {
  82.             category.updateAll();
  83.         }
  84.     }
  85.    
  86.     /** {@inheritDoc} */
  87.     @SuppressWarnings("checkstyle:designforextension")
  88.     public String toString()
  89.     {
  90.         StringBuilder s = new StringBuilder("Perception [");
  91.         String sep = "";
  92.         for (AbstractPerceptionCategory cat : this.perceptionCategories.values())
  93.         {
  94.             s.append(sep);
  95.             s.append(cat);
  96.             sep = ", ";
  97.         }
  98.         s.append("]");
  99.         return s.toString();
  100.     }
  101.    
  102. }