View Javadoc
1   package org.sim0mq.message.modelcontroller;
2   
3   import org.djutils.exceptions.Throw;
4   import org.sim0mq.Sim0MQException;
5   import org.sim0mq.message.Sim0MQMessage;
6   
7   /**
8    * StatisticsMessage, MC.3. The Model sends this message as a response to RequestStatistics messages sent by the Federation
9    * Manager. It contains one value for a model output statistic.
10   * <p>
11   * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
13   * </p>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   */
16  public class MC3StatisticsMessage extends Sim0MQMessage
17  {
18      /** The name of the output variable whose value is requested. That should match with the name in the model. */
19      private final String variableName;
20  
21      /**
22       * If variableType is scalar, the data type is e.g., an integer, float etc. and the value generated in the model. If
23       * variableType is time series, the data type is an ‘array’ (type 11-16 or 27/28) or a time series (type 31/32).
24       */
25      private final Object variableValue;
26  
27      /** the unique message id. */
28      private static final String MESSAGETYPE = "MC.3";
29  
30      /** */
31      private static final long serialVersionUID = 20170422L;
32  
33      /**
34       * @param federationId the federation id can be coded using different types. Examples are two 64-bit longs indicating a
35       *            UUID, or a String with a UUID number, a String with meaningful identification, or a short or an int with a
36       *            simulation run number.
37       * @param senderId The sender id can be used to send back a message to the sender at some later time.
38       * @param receiverId The receiver id can be used to check whether the message is meant for us, or should be discarded (or an
39       *            error can be sent if we receive a message not meant for us).
40       * @param messageId The unique message number is meant to confirm with a callback that the message has been received
41       *            correctly. The number is unique for the sender, so not globally within the federation.
42       * @param variableName The name of the output variable whose value is requested. That should match with the name in the
43       *            model.
44       * @param variableValue If variableType is scalar, the data type is e.g., an integer, float etc. and the value generated in
45       *            the model. If variableType is timeseries, the data type is an ‘array’ (type 11-16 or 27/28) or a time series
46       *            (type 31/32).
47       * @throws Sim0MQException on unknown data type
48       * @throws NullPointerException when one of the parameters is null
49       */
50      public MC3StatisticsMessage(final Object federationId, final Object senderId, final Object receiverId,
51              final Object messageId, final String variableName, final Object variableValue)
52              throws Sim0MQException, NullPointerException
53      {
54          this(new Object[] {Sim0MQMessage.VERSION, true, federationId, senderId, receiverId, MESSAGETYPE, messageId, 2,
55                  variableName, variableValue});
56      }
57  
58      /**
59       * @param objectArray Object[]; Full message object array
60       * @throws Sim0MQException on unknown data type
61       * @throws NullPointerException when one of the parameters is null
62       */
63      public MC3StatisticsMessage(final Object[] objectArray) throws Sim0MQException, NullPointerException
64      {
65          super(objectArray, 2, MESSAGETYPE);
66          Throw.when(!(objectArray[8] instanceof String), Sim0MQException.class, "variableName (field 8) should be String");
67          this.variableName = objectArray[8].toString();
68          this.variableValue = objectArray[9];
69      }
70  
71      /**
72       * @return variableName
73       */
74      public String getVariableName()
75      {
76          return this.variableName;
77      }
78  
79      /**
80       * @return variableValue
81       */
82      public Object getVariableValue()
83      {
84          return this.variableValue;
85      }
86  
87      /**
88       * Builder for the StartFederate Message. Can string setters together, and call build() at the end to build the actual
89       * message.
90       * <p>
91       * Copyright (c) 2016-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
92       * <br>
93       * BSD-style license. See <a href="http://sim0mq.org/docs/current/license.html">Sim0MQ License</a>.
94       * </p>
95       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
96       */
97      public static class Builder extends Sim0MQMessage.Builder<MC3StatisticsMessage.Builder>
98      {
99          /** The name of the output variable whose value is requested. That should match with the name in the model. */
100         private String variableName;
101 
102         /**
103          * If variableType is scalar, the data type is e.g., an integer, float etc. and the value generated in the model. If
104          * variableType is time series, the data type is an ‘array’ (type 11-16 or 27/28) or a time series (type 31/32).
105          */
106         private Object variableValue;
107 
108         /**
109          * Empty constructor.
110          */
111         public Builder()
112         {
113             // nothing to do.
114         }
115 
116         /**
117          * @param newVariableName set variable name
118          * @return the original object for chaining
119          */
120         public final Builder setVariableName(final String newVariableName)
121         {
122             this.variableName = newVariableName;
123             return this;
124         }
125 
126         /**
127          * @param newVariableValue set variable value
128          * @return the original object for chaining
129          */
130         public final Builder setVariableValue(final Object newVariableValue)
131         {
132             this.variableValue = newVariableValue;
133             return this;
134         }
135 
136         @Override
137         public MC3StatisticsMessage build() throws Sim0MQException, NullPointerException
138         {
139             return new MC3StatisticsMessage(this.federationId, this.senderId, this.receiverId, this.messageId,
140                     this.variableName, this.variableValue);
141         }
142 
143     }
144 }