5.3 Traversing the Element Hierarchy

Each Operations Center element contains location information concerning its own position in the element hierarchy, as well as relationships with other elements in the hierarchy.

5.3.1 Children Property

The children property is an array of elements contained within the target element. If it is empty, its length is zero. For example:

// Print the name of each of the elements in the ‘Elements' root.
for( var i = 0 ; i < formula.Elements.children.length; ++i )
   writeln( formula.Elements.children[i].name )

5.3.2 Relationships Property

The relationships property is an array of elements of which the target element is a member, such as business views. If it is empty, its length is zero. For example:

// Lookup the relationships of this element (assuming it is in scope)
for( var i = 0 ; i < element.relationships.length; ++i )
   writeln(element.relationships[i].name )

5.3.3 Parent Property

The parent of the element exists within the naming relationship (NAM). The element has only one parent, and always has a parent, unless it is the topmost root element (Enterprise), or has been destroyed. For example:

writeln( ‘The parent of ‘ + element + ‘ is ‘ + element.parent )

5.3.4 Walk Function

The walk function can visit all the children within a given element, including the entire tree. To use this function, pass another function as an argument. This passed function is “visited” along the entire tree. For example:

function visitor( child )
{
   writeln( 'Visited element: ' + child )
}

formula.Root.walk( visitor )
An alternate way to visit each node is to pass a NOC Script object that contains a visit function.
Example:
var visitor = 
{
   count: 0,
   visit: function ( child )
   {
      visitor.count++
   }
}

formula.Root.walk ( visitor )
writeln( 'The visitor saw ' + visitor.count + ' elements' )

5.3.5 Hierarchy Utilities

Lookup by FindElement()

The findElement function can find a hierarchy-relative element within a given element. To find an element within the entire Operations Center element tree, use Root.findElement. For example:

var sessionsElement = formula.Root.findElement( 'sessions=Sessions/formulaServer=Server/root=Administration' )
writeln( sessionsElement )

Lookup by Relative Sub-Property

Access elements contained within the tree of another element as pseudo-properties of the element. For example:

var sessionsElement = formula.Administration.findElement( 'sessions=Sessions/formulaServer=Server' )
writeln( 'There are: ' + sessionsElement.children.length + ' sessions logged into Formula' )

This example finds the Sessions element, which is a child of the Operations Center Server element, and prints the count of the number of children in this element. Each session logged into Operations Center is represented by an element inside the Sessions element, so this accurately displays the number of sessions logged into Operations Center.