getValueAsxxx(String)
methods for each specific type.setValue
methods allow a wide extend of compatibility with types[1]
char
value will work with any number typeint
Datas. As such, as for int
values:int
int
value is the index of the state in the enumeration. For example:<types> <enumType name="intEnum" > <enumValue name="ONE" /> <enumValue name="TWO" /> </enumType> </types>In the following type definition, "ONE" will have the value 1, and "TWO" the value 2:
<types> <enumType name="intEnum" > <enumValue name="ONE" value="1" /> <enumValue name="TWO" value="2"/> </enumType> </types>However, several additional methods allow to get or set the value of an enumeration according to the name of the state.
data.setIntValue(2)
will set the state TWO
data.setStringValue("TWO")
will set the state TWO
<types> <enumType name="intEnum" > <enumValue name="ONE" /> <enumValue name="TWO" /> </enumType> </types>We can for example use the following methods to get its value:
public void subscribe(ServiceInstance service) { // return the index of the enumeration state int value = service.getValueAsInt("intEnum"); // return the name of the enumeration state String valueS = service.getValueAsString("intEnum"); }And to set the value:
public void push(ServiceInstance service) { // set the enumeration state by its index Data data = service.getData("intEnum"); // set the "TWO" state for the enumeration data.setValueAsInt(1); // set the "ONE" state for the enumeration data.setValueAsString("ONE"); }
String
Datas. They can be set or get as XML Nodes, or as Strings: The Data.Xml datas have two specific methods which allow to get or set the Data value as an XMLNode
:XMLNode
XMLNode
as the argumentXMLNode
represents an element in the XML structure, this element hold:XMLNode.getName()
XMLNode.getAttribute(String)
XMLNode.getChildren()
<types> <xmlType name="xml" /> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="xmlData" type="xml" /> </event> </services>If the publisher module sends the following
xmlData
content[4]
<root> <child name="theName" /> </root>Then we could write the following code on the receiver module:
public void subscribe(ServiceInstance service) { // return the XML root node XMLNode node = service.getValueAsXML("xmlData"); // will return the "child" child XMLNode theChild = node.getChildren().get(0); // will return the "theName" String String attrValue = theChild.getAttribute("name"); }
String
Datas. They can be set or get as JSONObject
, or as Strings: The Data.JSON datas have two specific methods which allow to get or set the Data value as a JSONObject
:JSONObject
JSONObject
as the argument<types> <jsonType name="json" /> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="jsonData" type="json" /> </event> </services>If the publisher module sends some
jsonData
content[5]
JSONObject
on the publisher side rather than as a raw Stringpublic void subscribe(ServiceInstance service) { // return the json object JSONObject jsonObj = service.getValueAsJSON("jsonData"); }
<types> <simpleType name="int" baseType="int" /> <arrayType name="arrayOfInt" type="int" /> </types>We can for example use the following method to get the "arrayOfInt" value:
public void subscribe(ServiceInstance service) { // return the array List<Object> array = service.getValueAsArray("arrayOfInt"); }To add new elements or update existing elements for this array, we just have to add new values to the list, for example:
// get the array List<Object> array = service.getValueAsArray("arrayOfInt"); // set the value 23 at index 0 array.set(0, 23); // add an element to the array array.add(24);We also can do the same thing using the
Data.Array
itself. For example:// get the data array Data.Array array = (Data.Array)service.getData("arrayOfInt"); // set the value 23 at index 0 array.setValue(23, 0); // add an element at the end of the array array.pushValue(24); // remove the last element of the array array.popValue();
Data
to a Data.Structure which has several methods allowing to get or set the value of a particular field:getFieldValueAsxxx(String)
methods return the value of a field with a specified namesetFieldxxxValue(String, Object)
methods set the value of a field with a specified name<types> <simpleType name="bool" baseType="boolean" /> <simpleType name="int" baseType="int" /> <structType name="struct"> <field name="field1" type="bool" /> <field name="field2" type="int" /> </structType> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="myStruct" type="struct" /> </event> </services>We can for example use the following methods to get the "myStruct" value for its associated fields:
public void subscribe(ServiceInstance service) { // get the structure Data.Structure struct = (Data.Structure)service.getData("myStruct"); boolean b = struct.getFieldValueAsBoolean("field1"); int i = struct.getFieldValueAsInt("field2"); }If we want to change the value for a field, we can perform:
// get the structure Data.Structure struct = (Data.Structure)service.getData("myStruct"); // set the value true for the "field1" field struct.setFieldBooleanValue("field1", true); // set the value 23 for the "field2" field struct.setFieldIntValue("field2", 23);
<types> <simpleType name="bool" baseType="boolean" /> <simpleType name="int" baseType="int" /> <unionType name="union" variant="int" > <member name="member1" type="bool" /> <member name="member2" type="int" /> </unionType> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="myUnion" type="union" /> </event> </services>We can for example use the following methods to get the "myUnion" value for its current variant:
public void subscribe(ServiceInstance service) { // get the union Data.Union union = (Data.Union)service.getData("myUnion"); Object value = union.getMemberValue(); int variant = union.getVariant(); }If we want to change the current value, we can perform:
// get the union Data.Union union = (Data.Union)service.getData("myUnion"); // set the value true for the "member1" variant union.setMemberValue("member1", true); }
<types> <simpleType name="bool" baseType="boolean" /> <simpleType name="int" baseType="int" /> <structType name="struct"> <field name="field1" type="bool" /> <field name="field2" type="int" /> </structType> <arrayType name="arrayOfStruct" type="struct" /> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="myStructArray" type="arrayOfStruct" /> </event> </services>To get the value of various fields in the array of structure, we can perform:
public void subscribe(ServiceInstance service) { // get the array Data.Array data = (Data.Array)service.getData("myStructArray"); List<Object> array = data.getValueAsArray(); // get the array type (the type of the elements of the array are structures of the "struct" type: StructType type = (StructType)data.getElementsType(); // get the first element of the array (note that this is a List) Object elt = array.get(0); // get the "field1" field boolean b = type.getFieldValueAsBoolean("field1", elt); // get the "field2" field int i = type.getFieldValueAsInt("field2", elt); }To set the value of the array for an index and a field, we can perform:
// get the array Data.Array data = (Data.Array)service.getData("myStructArray"); List<Object> array = data.getValueAsArray(); // get the array type (the type of the elements of the array are structures of the "struct" type: StructType type = (StructType)data.getElementsType(); // get the first element of the array (note that this is a List) Object elt = array.get(0); // set the "field1" field value type.setFieldValueAsBoolean("field1", elt, true); // set the "field2" field type.setFieldValueAsInt("field2", elt, 23);To add another element for the array, we must first create a default value for the structure:
// get the array Data.Array data = (Data.Array)service.getData("myStructArray"); List<Object> array = data.getValueAsArray(); // get the array type (the type of the elements of the array are structures of the "struct" type: StructType type = (StructType)data.getElementsType(); // create a new element with default values for the fields for the structure List<Object> elt = type.getDefaultValue(); // add the element to the array array.add(elt); // set the "field1" field value type.setFieldValueAsBoolean("field1", elt, true); // set the "field2" field type.setFieldValueAsInt("field2", elt, 23);
<types> <simpleType name="bool" baseType="boolean" /> <simpleType name="int" baseType="int" /> <structType name="internalStruct"> <field name="field1" type="bool" /> <field name="field2" type="int" /> </structType> <structType name="struct"> <field name="fieldBool" type="bool" /> <field name="structField" type="internalStruct" /> </structType> </types>And the associated service definition:
<services> <event name="myService" > <data name="myStructure" type="struct" /> </event> </services>To get the value of the
structField
field in the internalStruct
structure, we can perform:public void subscribe(ServiceInstance service) { // get the structure Data.Structure data = (Data.Structure)service.getData("myStructure"); List<Object> structValue = data.getFieldValueAsStructure("structField"); - // get the type of the internalStruct structure StructType structType = (StructType)module.getType("internalStruct"); // get the "field2" field of this structure int i = type.getFieldValueAsInt("field2", structValue); }To set the value of the structure for this field, we can perform:
// get the structure Data.Structure data = (Data.Structure)service.getData("myStructure"); List<Object> structValue = data.getFieldValueAsStructure("structField"); // get the type of the internalStruct structure StructType structType = (StructType)module.getType("internalStruct"); // set the value of the "field2" field of this structure structType.setFieldValueAsInt("field2", structValue, 10);
<types> <simpleType name="bool" baseType="boolean" /> <simpleType name="int" baseType="int" /> <structType name="struct"> <field name="field1" type="bool" /> <field name="field2" type="arrayOfInt" /> </structType> <arrayType name="arrayOfInt" type="int" /> </types>And the associated service definition:
<services> <event name="myService" id="1" > <data name="myStruct" type="struct" /> </event> </services>To get the value of the array field, we can perform:
public void subscribe(ServiceInstance service) { // get the structure Data.Structure struct = (Data.Structure)service.getData("myStruct"); // get the value for the "field2" field, this is an array of int List<Object> array = data.getFieldValueAsArray("field2"); // get the first element of the array int elt = (Integer)array.get(0); }
intType.isEqualsTo(5, 5); intType.isEqualsTo(5, 5f);
true
.
<enumType name="intEnum" > <enumValue name="ONE" /> <enumValue name="TWO" /> </enumType>The following expressions will both return true:
enumType.isEqualsTo(1, 1); intType.isEqualsTo(1, "TWO");
char
value will work with any number typeJSONObject
on the publisher side rather than as a raw StringCopyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence