Traversing and Iterating Schema Definition Objects


This document explains how to get information about a schema definition either by traversing the schema definition object or through the use of iterators.

Traversing Schema Definition Objects

To traverse a schema definition object, you must first obtain a reference to an sdSession instance.

sdSession *mySession = sdSession::get();

Next, access the namespace for the schema definitions:

sdNameSpace *ns = mySession -> getNameSpace("appSchema");

The getNameSpace function initializes the schema definition for the appSchema namespace if it is not already initialized.

Once an sdNameSpace object is acquired, an sdObject can be found by enum name, which is more efficient, or by string value.

sdObject *mySegment = ns -> findObject(0);
sdObject *mySegment = ns -> findObject("mySegment");

An sdAttribute can be found by enum name or string value:

sdNamespace::findAttribute(0);
sdNamespace::findAttribute("segmentBeginPoint");

Given an sdAttribute, you can get the object on which it resides, the attribute's value, and whether or not the attribute has a single value, an array of values, or a collection of values. Refer to the sdAttribute class documentation for details.

An sdRelationshipBase can also be found by enum name or string value:

sdNamespace::findRelationship(0);
sdNamespace::findRelationship("style");

Using Iterators

You can use iterators to find sdObjects and sdAttributes for which the name or enum values are not known. These iterators return const objects.

The following example iterates within the ns namespace for sdObjects. In the example, objIter is a variable indicating a mapped pair. The first part is the enum value, the second part is the sdObject.

sdNameSpace::ObjectConstIter objIter(ns -> beginObject());

for (objIter; objIter != ns->endObject(); ++objIter) {
    const sdObject *object = objIter -> second;
     ...
}

You can iterate over the attributes or relationships of an sdObject. The next example iterates over the attributes of a schema definition named mySegment. The true argument to the beginAttribute function specifies that only local attributes should be returned. So, attributes such as segmentBeginPoint and segmentEndPoint are returned, but attributes belonging to oaShape (from which mySegment inherits) are not returned.

sdObjectAttributeIter attrPosition(mySegment -> beginAttribute(true));

for (attrPosition; attrPosition != mySegment -> endAttribute(true); ++attrPosition) {
    const sdAttribute  *attr = *attrPosition;    
    ...
}

Relationships can also be iterated.

sdObjectRelationshipIter relPosition(mySegment -> beginRelationship(true));


for (relPosition; relPosition != mySegment -> endRelationship(true); ++relPosition) {
    const sdRelationship  *rel = *relPosition;    
    ...
}

 



Copyright © 2001-2010 Cadence Design Systems, Inc.
All rights reserved.