View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.old;
2   
3   import org.djunits.unit.AccelerationUnit;
4   import org.djunits.unit.SpeedUnit;
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.opentrafficsim.core.animation.gtu.colorer.AccelerationGTUColorer;
8   import org.opentrafficsim.core.animation.gtu.colorer.GTUColorer;
9   import org.opentrafficsim.core.animation.gtu.colorer.IDGTUColorer;
10  import org.opentrafficsim.core.animation.gtu.colorer.SpeedGTUColorer;
11  import org.opentrafficsim.core.animation.gtu.colorer.SwitchableGTUColorer;
12  import org.xml.sax.SAXException;
13  
14  /**
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
20   * initial version Jul 28, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  final class GTUColorerTag
24  {
25      /** Utility class. */
26      private GTUColorerTag()
27      {
28          // do not instantiate
29      }
30  
31      /** default colorer, optionally set by XMLNetworkLaneParser */
32      // TODO solve hack in better xml spec
33      static GTUColorer defaultColorer = null;
34  
35      /**
36       * Parses the right GTUColorer from ID|SPEED|ACCELERATION|LANECHANGEURGE|SWITCHABLE.
37       * @param name String; name of the GTUColorer
38       * @param globalTag GlobalTag; to define the default parameters of the colorers
39       * @return the corresponding GTUColorer
40       * @throws SAXException in case the name does not correspond to a known GTUColorer
41       */
42      static GTUColorer parseGTUColorer(final String name, final GlobalTag globalTag) throws SAXException
43      {
44          switch (name)
45          {
46              case "ID":
47                  return new IDGTUColorer();
48  
49              case "SPEED":
50                  return makeSpeedGTUColorer(globalTag);
51  
52              case "ACCELERATION":
53                  return makeAccelerationGTUColorer(globalTag);
54  
55              case "SWITCHABLE":
56                  return makeSwitchableGTUColorer(globalTag);
57  
58              default:
59                  throw new SAXException("GTUCOLORER: unknown name " + name + " not one of ID|SPEED|ACCELERATION|SWITCHABLE");
60          }
61      }
62  
63      /**
64       * @param globalTag GlobalTag; to define the default parameters of the colorers
65       * @return the corresponding GTUColorer
66       */
67      static GTUColorer makeSpeedGTUColorer(final GlobalTag globalTag)
68      {
69          if (defaultColorer != null)
70              return defaultColorer;
71          if (globalTag.speedGTUColorerMaxSpeed != null)
72          {
73              return new SpeedGTUColorer(globalTag.speedGTUColorerMaxSpeed);
74          }
75          return new SpeedGTUColorer(new Speed(100.0, SpeedUnit.KM_PER_HOUR));
76      }
77  
78      /**
79       * @param globalTag GlobalTag; to define the default parameters of the colorers
80       * @return the corresponding GTUColorer
81       */
82      static GTUColorer makeAccelerationGTUColorer(final GlobalTag globalTag)
83      {
84          if (defaultColorer != null)
85              return defaultColorer;
86          // TODO use parameters for AccelerationGTUColorer
87          return new AccelerationGTUColorer(new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2),
88                  new Acceleration(1.0, AccelerationUnit.METER_PER_SECOND_2));
89      }
90  
91      /**
92       * @param globalTag GlobalTag; to define the default parameters of the colorers
93       * @return the corresponding GTUColorer
94       */
95      static GTUColorer makeSwitchableGTUColorer(final GlobalTag globalTag)
96      {
97          if (defaultColorer != null)
98              return defaultColorer;
99          GTUColorer[] gtuColorers =
100                 new GTUColorer[] {new IDGTUColorer(), makeSpeedGTUColorer(globalTag), makeAccelerationGTUColorer(globalTag)};
101         // TODO default colorer
102         return new SwitchableGTUColorer(0, gtuColorers);
103     }
104 }