Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Data manipulation by using a value to set a Data



The default way to manipulate complex data types is described in the Basic Data manipulation article.

However, there is a more direct way to manipulate complex data structures, by using a Data created in the module to set or get the value of the Service data.

Overview

The mechanism is to:
  • Create a Data object of the desired Data type, for example at the initialization of the module
  • During the usage of the module (for example, when subscribing to a service), set the value for this object, and use it to set the value of the Service data


It is possible to perform the same kind of affectation at any level of the Data structure. For example:
  • Setting the value of a Field from a Data
  • Setting the value of an Array element from a Data

Creating the Data

From a Data definition

If you want to perform the affectation at the Data level, you should create a Data directly from the Data definition. For example:
        public void init(Module module) {
          ServiceDefinition positionDef = module.getServiceDefinition("position");
          Data.Structure posStruct = (Data.Structure) positionDef.createData("thePosition");
        }
Note that this data is not linked in any way to the framework services. It may only be used to set and retrieve values.

From a Type

You can also create a Data directly from a Type, which can be useful if you have a complex Data structure with more than one level under the Data. For example:
        public void init(Module module) {
          Type.Structure positionType = module.getType("positionType");
          Data.Structure posStruct = (Data.Structure) positionType.createData();
        }

Getting the value to a Data

Getting the value from a Type

We could also create any data directly from a Type. For example:
      Data.Structure struct = structType.getValueAsData(<my object>);
      Data.Array array = arrayType.getValueAsData(<my object>);

Setting the value from a Data

From a Data definition

In the Creating the Data paragraph, suppose that we set the value of our "internal" Data. For example:
      posStruct.setFieldFloatValue("x", 0.2f);
      posStruct.setFieldFloatValue("y", 0.2f);
We could set the value of a Data for the Service using this "internal" Data. For example:
      position.setDataValue("thePosition", posStruct);
      position.invoke();

From an array Data definition

Suppose that we have an array of data, in this case the array definition could be:
      <types>
         <simpleType name="float" baseType="float" />
         <structType name="positionType">
            <field name="x" type="float" />
            <field name="y" type="float" />
         </structType>         
         <arrayType name="positionArrayType" type="positionType" />
      </types>
And the associated service:
      <publish name="position" >
         <data name="positionArray" type="positionArrayType" />
      </publish>
Our array would be an array of the positionType structure:
        public void init(Module module) {
          ArrayType positionArrayType = (ArrayType)module.getType("positionArrayType");
          Data.Array posArray = (Data.Array) positionArrayType.createData();
        }
Now we want to set the value for one of the array index. For example:
      StructType positionType = (StructType)module.getType("positionType");
      Data.Structure posStruct = (Data.Structure) positionType.createData();
      posStruct.setFieldFloatValue("x", 0.2f);
      posStruct.setFieldFloatValue("y", 0.2f);
       
      posArray.setValue(posStruct, 1); // we set the value of the array for the index 1
      positionService.setDataValue("positionArray", posArray);
      positionService.invoke();       

From a Type

We could set the value of a Data for the Service using this "internal" Data in exactly the same way as for a Data definition. For example:
      position.setDataValue("thePosition", posStruct);
      position.invoke();
However, this allows to perform this affectation at any level of a Data structure.

Examples

Example at the Data level

Suppose that we have the following types definition:
      <types>
         <simpleType name="float" baseType="float" />
         <structType name="position">
            <field name="x" type="float" />
            <field name="y" type="float" />
         </structType>
      </types>
And the following services definition:
      <services>
         <event name="position">
            <data name="thePosition" type="pos" />
         </event>
      </services>
We can for example use the following code to publish cyclically the position:
      public class MyModule {
        private ServiceInstance position = null;
        private ServiceDefinition positionDef = null;

        public void init(Module module) {
          positionDef = module.getServiceDefinition("position");
          position = module.getService("position");
        }

        public void published(ServiceInstance service) {
          Data.Structure posStruct = (Data.Structure) positionDef.createData("thePosition");
          posStruct.setFieldFloatValue("x", 0.2f);
          posStruct.setFieldFloatValue("y", 0.2f);
          position.setDataValue("thePosition", posStruct);
          position.invoke();
        }
      }

Example at the Type level

Suppose that we have the following types definition:
      <types>
         <simpleType name="float" baseType="float" />
         <simpleType name="string" baseType="string" />
         <structType name="waypoint">
            <field name="name" type="string" />
            <field name="position" type="position" />
         </structType>
         <structType name="position">
            <field name="latitude" type="float" />
            <field name="lontitude" type="float" />
         </structType>
      </types>
And the following services definition:
      <services>
         <event name="createWaypoint">
            <data name="waypoint" type="waypoint" />
         </event>
      </services>
We can for example use the following code to create one Waypoint:
      public class MyModule {
        private ServiceInstance createWaypoint = null;
        private StructType positionType = null;

        public void init(Module module) {
          createWaypoint = module.getService("createWaypoint");
          positionType = (StructType)module.getType("position");
        }

        public void subscribe(ServiceInstance service) {
          Data.Structure posStruct = (Data.Structure) positionType.createData();
          posStruct.setFieldFloatValue("latitude", 0.2f);
          posStruct.setFieldFloatValue("lontitude", 0.2f);
          Data.Structure waypoint = (Data.Structure)createWaypoint.getData("waypoint");
          waypoint.setFieldValue("position", posStruct);
          position.invoke();
        }
      }

See also


Categories: concepts

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence