Serializer and Builder

Within the Tensegrity API, object instances are made persistent by means of a Serializer. The Serializer interface is responsible for translating an object into a persisted AttributeList.

Figure 18.2. Serializer and Builder

Serializer and Builder

According to the figure above, a simple serialization to an XML file will look like this:

Example 18.10. Writing the XML file

// create a list
AttributeList attributeList = 
  attributeFactory.newAttributeList();
        
// iterate through a collection of objects to be written out
for (Iterator i = myObjectCollection.iterator(); i.hasNext();)
{
  SampleType object = (SampleType)i.next();
  // instatiate a serializer with that object 
  SampleSerializer serializer = new SampleSerializer(object);
  // trigger the serialization process of the serializer and
  // store the result in an Attribute
  Attribute att = 
      attributeFactory.newAttribute(
          "obj.name", serializer.serialize());
  attributeList.add(att);
}
        
XMLIOUtilities.writeXMLFile("test.xml", attributeList);

Serializing Composite Objects

In order to give you a better understanding of how a Tensegrity graph document is built and objects of the CompositeAPI are made persistent, the serialization process will be briefly outlined in this section. In short, objects get written out by a Serializer and read in by a Builder.

Composite objects are made persistent by their corresponding serializers. In other words, for every Composite type in the CompositeAPI, a corresponding Serializer can be found in the package com.tensegrity.composite.persistence. All classes implement the Serializer interface as it is defined in the GenericsAPI. At the time of writing, the following implementations are present:

  • CompositeSerializer

  • CompositeGroupSerializer

  • CompositeLabelSerializer

  • CompositeLineSerializer

  • CompositeSwimLaneSerializer

  • CompositeSwimLanePoolSerializer

  • CompositeTableSerializer

As you can see, there is a Serializer (and matching Builder) for every distinct Composite. Every one of these Composite classes requires its own Serializer and Builder because of the specific ways in which their attributes have to be handled.

Serializing a Composite takes place in the following way:

  1. All specific attributes of the composite are added to an AttributeList (as defined in the GenericsAPI).

  2. The AttributeList is written to an XML file.

Building Composites

As mentioned before, for every available Serializer, a matching Builder can be found in package com.tensegrity.composite.persistence of the Tensegrity API. The build method of the appropriate Builder will get called when a GraphDocument is being loaded. The build sequence is inverse to the serialize sequence described above.

The following snippet should give you an idea what the XML file for a serialized composite looks like. This one is a Composite containing a Rect2D.

Example 18.11. Tensegrity Graph Document XML Fragment

...
     
<list name="Nodes">
  <!-- Containing just one Node ... -->
  <list name="Node">
    <attribute name="Id" value="80-12966337"/>
    <attribute name="LayoutContext" value=""/>
    <attribute name="Rule" value="nrule1"/>
    <!-- .. that contains a Composite ... ->
    <list name="Composite">
      <attribute name="Resizable" type="Boolean" value="true"/>
      <attribute name="Moveable" type="Boolean" value="true"/>
      <attribute name="Selectable" type="Boolean" value="true"/>
      <attribute name="ShadowVisible" type="Boolean" value="false"/>
      <list name="CoordinateSystem">
        <attribute name="ScaleX" value="0.0, 400.0"/>
        <attribute name="ScaleY" value="0.0, 400.0"/>
      </list>
      <attribute name="Location" value="415,355"/>
      <attribute name="Size" value="400, 400"/>
      <!-- holding just one Primitive -->
      <list name="Primitives">
        <list name="Primitive">
          <attribute name="Type" value="Rect2D"/>
          <attribute name="Stroke" value="stroke1"/>
          <attribute name="Paint" value="paint2"/>
          <attribute name="BoundingBox" value="0,400,0,400"/>
          <attribute name="StyleItemName" value="LightTenseRectangle"/>
          <attribute name="GeometryItemName" value="DefaultRect"/>
          <set name="InfoValues">
            <attribute name="GeometryName" value="DefaultRect"/>
          </set>
        </list>
      </list>
    </list>
    <!-- Ports -->
    <list name="Ports">
        
        ...
        
    </list>
  </list>
</list>

...       
        

Hopefully you will agree that this fragment is easy to read and the structure is not at all difficult to understand! In “Listings” a complete Tensegrity graph document is listed.