Infotables, ValueCollections, and Nested Infotable Configurations
This section presents information about infotables, ValueCollections, and nested infotable configurations. Click the title of a section to display its content:
InfoTables
Services have arguments and return values that are expressed using primitives such as String, Integer, Number, and Boolean. A set of service arguments are described by an infotable. It may help to think of an infotable as a spreadsheet, in which each column has a primitive type and there may be many rows. Service parameters and return values are passed and received as infotables with a single row. The Java SDK has tools for constructing infotables that are needed for invoking services.
In the ThingWorx environment, an INFOTABLE is an aggregate base type within the ThingWorx Platform and Edge products. An infotable has a DataShapeDefinition that describes the names, base types, and additional information about each field within the table. Data within an infotable is contained in rows. Each row can have one or more fields, described by the DataShapeDefinition of the infotable. Here is an example of an infotable definition, described using JavaScript, with its Data Shape and rows:
In addition to using infotables with services, you can use infotables to hold data for an event or the values that are returned by a read request for multiple properties. The infotable can contain one or more rows. When results are returned, you need to extract the values from each row, using the methods, getFirstRow(), getSecondRow(), and so on. You also use a helper function that matches the base type for the field, such as getStringValue().
Example: Infotable Used for Results of Property Read
Here is an example of using an infotable to return the results of a property read, extracted from the sample source file, SimpleClient.java:
// Define result as an InfoTable
InfoTable result
// Reading the "name" property of a Thing and
// extracting the results from an InfoTable
// Since "name" is a STRING, use getStringValue()
// to return the value of the "name" property.
result = client.readProperty(ThingworxEntityTypes.Things,
ThingName, "name", 10000);
String name = result.getFirstRow().getStringValue("name");
ValueCollections
A ValueCollection is a hash map that maps strings to primitives. You can use a ValueCollection within an infotable to represent rows of data. Essentially, each row contains a string/primitive pair, such as a property name and its value or a parameter for a service and its value. When creating a ValueCollection, use the put method to store the string/primitive pair in the collection.
The following example from the sample source file, SimpleClient.java, shows how you can use a ValueCollection to specify the parameters for a service:
//A ValueCollection is used to specify the parameters of a service
ValueCollection params = new ValueCollection();
params.put("path", new StringPrimitive("/simple.txt"));
params.put("data", new StringPrimitive("Here is the
contents of the file."));
params.put("overwrite", new BooleanPrimitive(true));
Persisting Nested Infotable Configurations
The ThingWorx Edge Java SDK supports configuration files that contain nested InfoTables. The SDK can also consume a configuration that contains nested InfoTables. Here is an excerpt from a test case:
public void testtest() throws Throwable {
ConfigurationTestTemplate instance =
new ConfigurationTestTemplate(CONFIG_THING_NAME, null);
ConfigurationTestShape shape =
(ConfigurationTestShape) instance.getShapeInstances().
get(ETS_NAME);
InfoTable nested = new InfoTable();
nested.addField(new FieldDefinition("ae", "", BaseTypes.STRING));
nested.addField(new FieldDefinition("bee", "", BaseTypes.NUMBER));
ValueCollection row = new ValueCollection();
row.put("ae", new StringPrimitive("1"));
row.put("bee", new NumberPrimitive(500.005));
nested.addRow(row);
InfoTable config = shape.getConfiguration();
row = config.getFirstRow();
row.put("c", new InfoTablePrimitive(nested));
shape.saveLocalConfiguration(config);
instance = new ConfigurationTestTemplate(ETS_NAME, null);
shape = (ConfigurationTestShape) instance.getShapeInstances().get(ETS_NAME);
config = shape.getConfiguration();
}
If you were to put a breakpoint on the saveLocalConfiguration() line and step through the code, you would find that the generated configuration file contains the nested InfoTable. In addition the configuration that the new instance loads contains the correct configuration with the nested InfoTable.
| The initial configuration does not specify a default for the InfoTable field c. If a default value had been specified, the value collection would not have contained the InfoTable primitive. |