ThingWorx Edge Java SDK Edge Extensions > Dynamically Creating Edge Thing Shapes
Dynamically Creating Edge Thing Shapes
Annotations are not appropriate for all situations. An SDK user may wish to add shapes at runtime instead of define them at compile time. To permit this, the functions used by theThingworxEdgeThingShapeDefinition are exposed on the ETT. The snippet below demonstrates how to create an Edge Thing Shape dynamically at runtime during construction of the ETT.
public PropertyTestShape(EdgeThingTemplate thingInstance, String identifier) {
super(thingInstance, identifier);
String NO_NAMESPACE = null;

defineProperty("a","a Number", BaseTypes.NUMBER,NO_NAMESPACE);
defineProperty("b","a String", BaseTypes.STRING,NO_NAMESPACE);
defineProperty("c","a Boolean", BaseTypes.BOOLEAN,NO_NAMESPACE);
}
The concept of namespaces is explained in the next section, Applying Namespaces to ETS Properties, Services, and Data Shapes.
In the example above, a generic EdgeThingTemplate is constructed, but you could also use a class that extends EdgeThingShape. Composition of ETT at runtime is useful in testing scenarios, allowing tests cases to construct Edge Thing Shapes from multiple shape building blocks in different combinations, without requiring a concrete EdgeThingTemplate class for each test. Note that an array of EdgeShapeDefinitions can be passed to an ETT at runtime. There is also no reason why you could not use shape compile-time and runtime shape definitions in conjunction with each other.
Another runtime creation scenario is the dynamic declaration of shape properties at runtime. This scenario would allow an Edge Thing Shape to actually change shape in response to a function, all at runtime. The shape could also expose properties that it discovers during its own configuration. The following example shows how you could add properties at runtime without the use of annotations:
public PropertyTestShape(EdgeThingTemplate thingInstance, String identifier) {
super(thingInstance, identifier);
String NO_NAMESPACE = null;

defineProperty("a","a Number", BaseTypes.NUMBER,NO_NAMESPACE);
defineProperty("b","a String", BaseTypes.STRING,NO_NAMESPACE);
defineProperty("c","a Boolean", BaseTypes.BOOLEAN,NO_NAMESPACE);

}
Note that in this example, the properties are declared during construction, which is identical to how properties declared when annotations are used. Dynamic property declaration can be used in conjunction with annotation-based property declaration. Property declaration can also be done at any point in the life cycle of a shape.
Was this helpful?