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