View Javadoc
1   package org.opentrafficsim.core.gtu.perception;
2   
3   import java.util.LinkedHashMap;
4   import java.util.Map;
5   
6   import org.opentrafficsim.core.gtu.GTU;
7   import org.opentrafficsim.core.gtu.GTUException;
8   import org.opentrafficsim.core.gtu.behavioralcharacteristics.ParameterException;
9   import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
10  import org.opentrafficsim.core.network.NetworkException;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2017 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/docs/current/license.html">OpenTrafficSim License</a>.
16   * <p>
17   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jul 29, 2016 <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  
23  public abstract class AbstractPerception implements Perception
24  {
25  
26      /** */
27      private static final long serialVersionUID = 20160729L;
28  
29      /** Set of available perception categories. */
30      private final Map<Class<? extends PerceptionCategory>, PerceptionCategory> perceptionCategories = new LinkedHashMap<>();
31  
32      /** GTU. */
33      private GTU gtu;
34  
35      /**
36       * Construct perception.
37       * @param gtu GTU
38       */
39      public AbstractPerception(final GTU gtu)
40      {
41          this.gtu = gtu;
42      }
43  
44      /** {@inheritDoc} */
45      @SuppressWarnings("checkstyle:designforextension")
46      public GTU getGtu()
47      {
48          return this.gtu;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public final void addPerceptionCategory(final PerceptionCategory perceptionCategory)
54      {
55          // guarantees correct combination of class and perception category
56          this.perceptionCategories.put(perceptionCategory.getClass(), perceptionCategory);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final <T extends PerceptionCategory> boolean contains(final Class<T> clazz)
62      {
63          for (Class<?> category : this.perceptionCategories.keySet())
64          {
65              if (clazz.isAssignableFrom(category))
66              {
67                  // isAssignableFrom takes care of implementation of the category
68                  return true;
69              }
70          }
71          return false;
72      }
73  
74      /** {@inheritDoc} */
75      @Override
76      @SuppressWarnings("unchecked")
77      public final <T extends PerceptionCategory> T getPerceptionCategory(final Class<T> category) throws OperationalPlanException
78      {
79          for (Class<?> clazz : this.perceptionCategories.keySet())
80          {
81              if (category.isAssignableFrom(clazz))
82              {
83                  // addPerceptionCategory guarantees correct combination of class and perception category
84                  // isAssignableFrom takes care of implementation of the category
85                  return (T) this.perceptionCategories.get(clazz);
86              }
87          }
88          throw new OperationalPlanException("Perception category" + category + " is not present.");
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public final void removePerceptionCategory(final PerceptionCategory perceptionCategory)
94      {
95          for (Class<?> category : this.perceptionCategories.keySet())
96          {
97              if (perceptionCategory.getClass().isAssignableFrom(category))
98              {
99                  // addPerceptionCategory guarantees correct combination of class and perception category
100                 // isAssignableFrom takes care of implementation of the category
101                 this.perceptionCategories.remove(perceptionCategory.getClass());
102                 return;
103             }
104         }
105     }
106 
107     /** {@inheritDoc} */
108     @SuppressWarnings("checkstyle:designforextension")
109     public void perceive() throws GTUException, NetworkException, ParameterException
110     {
111         for (PerceptionCategory category : this.perceptionCategories.values())
112         {
113             category.updateAll();
114         }
115     }
116 
117     /** {@inheritDoc} */
118     @SuppressWarnings("checkstyle:designforextension")
119     public String toString()
120     {
121         StringBuilder s = new StringBuilder("Perception [");
122         String sep = "";
123         for (PerceptionCategory cat : this.perceptionCategories.values())
124         {
125             s.append(sep);
126             s.append(cat);
127             sep = ", ";
128         }
129         s.append("]");
130         return s.toString();
131     }
132 
133 }