View Javadoc

1   /*
2    * @(#)DistributionParser.java Jun 25, 2004 Copyright (c) 2002-2005, 2004 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   
8   package nl.tudelft.simulation.xml.jstats;
9   
10  import java.io.IOException;
11  import java.net.URL;
12  
13  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
14  import nl.tudelft.simulation.jstats.distributions.Dist;
15  import nl.tudelft.simulation.jstats.distributions.DistBernoulli;
16  import nl.tudelft.simulation.jstats.distributions.DistBeta;
17  import nl.tudelft.simulation.jstats.distributions.DistBinomial;
18  import nl.tudelft.simulation.jstats.distributions.DistConstant;
19  import nl.tudelft.simulation.jstats.distributions.DistContinuous;
20  import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
21  import nl.tudelft.simulation.jstats.distributions.DistExponential;
22  import nl.tudelft.simulation.jstats.distributions.DistNormal;
23  import nl.tudelft.simulation.jstats.distributions.DistTriangular;
24  import nl.tudelft.simulation.jstats.streams.Java2Random;
25  import nl.tudelft.simulation.jstats.streams.StreamInterface;
26  import nl.tudelft.simulation.logger.Logger;
27  
28  import org.jdom.Element;
29  import org.jdom.Namespace;
30  
31  /***
32   * <br>
33   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
34   * University of Technology </a>, the Netherlands. <br>
35   * See for project information <a href="http://www.simulation.tudelft.nl">
36   * www.simulation.tudelft.nl </a> <br>
37   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
38   * General Public License (LGPL) </a>, no warranty.
39   * 
40   * @version Jun 25, 2004 <br>
41   * @author <a
42   *         href="mailto:a.verbraeck@tbm.tudelft.nl">Alexander
43   *         Verbraeck </a>
44   */
45  public final class DistributionParser
46  {
47      /*** the default schema file */
48      public static final URL DISTRIBUTIONFILE_SCHEMA = DistributionParser.class
49              .getResource("/xsd/distribution.xsd");
50  
51      /***
52       * constructs a new DistributionParser
53       */
54      private DistributionParser()
55      {
56          super();
57          // unreachable code
58      }
59  
60      /***
61       * parses a xml-element representing a distribution function
62       * 
63       * @param element The j-dom element
64       * @param simulator the simulator to use
65       * @return the distribution function
66       * @throws IOException on failure
67       */
68      public static Dist parseDistribution(final Element element,
69              final SimulatorInterface simulator) throws IOException
70      {
71          try
72          {
73              String streamId = element.getAttributeValue("stream");
74              StreamInterface stream = new Java2Random();
75              try
76              {
77                  stream = simulator.getReplication().getStream(streamId);
78              } catch (Exception e)
79              {
80                  Logger.warning(DistributionParser.class, "parseDistribution",
81                          "stream " + stream + " not found"
82                                  + "\nDefault stream used instead.");
83              }
84              Namespace xsi = Namespace
85                      .getNamespace("http://www.w3.org/2001/XMLSchema-instance");
86              String distributionType = element.getAttributeValue("type", xsi);
87              if (distributionType.startsWith("dsol:"))
88              {
89                  distributionType = distributionType.substring(5);
90              }
91              if ("bernouilli".equals(distributionType))
92              {
93                  double p = new Double(element.getAttributeValue("p"))
94                          .doubleValue();
95                  return new DistBernoulli(stream, p);
96              } else if ("beta".equals(distributionType))
97              {
98                  double alpha1 = new Double(element.getAttributeValue("alpha1"))
99                          .doubleValue();
100                 double alpha2 = new Double(element.getAttributeValue("alpha2"))
101                         .doubleValue();
102                 return new DistBeta(stream, alpha1, alpha2);
103             } else if ("binomial".equals(distributionType))
104             {
105                 long n = new Long(element.getAttributeValue("n")).longValue();
106                 double p = new Double(element.getAttributeValue("p"))
107                         .doubleValue();
108                 return new DistBinomial(stream, n, p);
109             } else if ("constant".equals(distributionType))
110             {
111                 double c = new Double(element.getAttributeValue("c"))
112                         .doubleValue();
113                 return new DistConstant(stream, c);
114             } else if ("exponential".equals(distributionType))
115             {
116                 double lambda = new Double(element.getAttributeValue("lambda"))
117                         .doubleValue();
118                 return new DistExponential(stream, lambda);
119             } else if ("normal".equals(distributionType))
120             {
121                 double mean = new Double(element.getAttributeValue("mean"))
122                         .doubleValue();
123                 double stdev = new Double(element.getAttributeValue("stdev"))
124                         .doubleValue();
125                 return new DistNormal(stream, mean, stdev);
126             } else if ("triangular".equals(distributionType))
127             {
128                 double a = new Double(element.getAttributeValue("a"))
129                         .doubleValue();
130                 double b = new Double(element.getAttributeValue("b"))
131                         .doubleValue();
132                 double c = new Double(element.getAttributeValue("c"))
133                         .doubleValue();
134                 return new DistTriangular(stream, a, b, c);
135             } else
136             {
137                 throw new Exception("Unknown distribution in "
138                         + element.getName());
139             }
140         } catch (Exception exception)
141         {
142             throw new IOException(exception.getMessage());
143         }
144     }
145 
146     /***
147      * parses a xml-element representing a distribution function
148      * 
149      * @param element The j-dom element
150      * @param simulator the simulator to use
151      * @return the distribution function
152      * @throws IOException on failure
153      */
154     public static DistDiscrete parseDiscreteDistribution(final Element element,
155             final SimulatorInterface simulator) throws IOException
156     {
157         return (DistDiscrete) DistributionParser.parseDistribution(element,
158                 simulator);
159     }
160 
161     /***
162      * parses a xml-element representing a distribution function
163      * 
164      * @param element The j-dom element
165      * @param simulator the simulator to use
166      * @return the distribution function
167      * @throws IOException on failure
168      */
169     public static DistContinuous parseContinuousDistribution(
170             final Element element, final SimulatorInterface simulator)
171             throws IOException
172     {
173         return (DistContinuous) DistributionParser.parseDistribution(element,
174                 simulator);
175     }
176 }