PilotScript Hierarchical Functions

The following hierarchical functions are available for PilotScript components.

Many data records, such as property lists or molecules, are single objects that have no children and no hierarchy. However, some readers or operations create structures where the root object has additional children objects. For example, the Group Data using Tag component creates a group object and places the contents as children beneath it.

Note: To directly view the hierarchical tree, use a viewer such as the Data Record Tree Viewer.

Check if the compoment mentioned in the tip is available.

Most of the time, you do not need to be aware of the data structure flowing in a pipeline. You can use components to create and destroy these hierarchical objects, such as Group Data and Ungroup Data. However, it may be useful to query a data record to find out about its structure and hierarchical data, and even process it differently, depending on the results. Use the hierarchical functions in PilotScript for this purpose.

Hierarchical functions allow access to information about hierarchical data records. Properties in a hierarchical data record can be on any of the nodes that comprise the object. Normally, PilotScript functions work on the properties of the top-level parent, or root node. Properties on other nodes can be referred to using a special syntax which describes the location of the node in the hierarchy. In this syntax, any property name that begins with a forward slash is first examined to see if it can be interpreted as a hierarchical property name. Typically, the name can be considered a path used to traverse the tree; the names of the nodes in the path are separated by forward slashes, much like a file-system filename. The text after the final forward slash is the actual property name on the node specified by the previous path, which is called the location of the node.

Use hierarchical functions to operate on the node structure of hierarchical data records.

Hierarchical Function Name/Prototype Description Example

AddChild

String AddChild(String XPathLocation [, String nodeName, String nodeType])
Adds a child to the node at the given location and returns a string that describes the location of the new node. If the location is not valid, throws an error. nodeName can be any string and is used internally to recognize nodes that are specialized to particular subtypes (for example, SciTegic.FeatureGroup). The default is SciTegic.data.BaseNode, which is a type of basic node. Another useful node type is SciTegic.data.Molecule, which creates a molecule. Note: This function uses XPath syntax.
@Location := AddChild('/*/');

Creates a new generic node under the root node on a data record. The location of the new node is saved in the global Location.

@Location := AddChild('/generic/Molecule/', 'MyNodeType', 'SciTegic.data.BaseNode'); 

Creates a new node under the first molecule node under the root node. The location of the new node is saved in the global Location.

DataRoot

(NodeRef)
Returns a node reference (NodeRef) to the data record's root node.
#rootNode := DataRoot();

This gets the root node of the current data record. This can be used with the Node functions to navigate and manipulate the data record's hierarchy.

DataSetRoot

(NodeRef node)
Replaces the current data record root node with a new node. All child nodes of the current root node are also removed and replaced with the child nodes (if any) of the new node, which can be specified as a node reference, the textual type of a root node to create, or the serialization of a root node to create.
#childIndex = 2;

#targetNode := DataRoot();

/* Check index validity */

IF (NodeNumChildren(#targetNode) > #childIndex)
 THEN
	#targetNode := NodeIthChild(#targetNode, #childIndex);

END IF;

IF NOT NodesEqual(#targetNode, DataRoot())
 THEN
	DataSetRoot(#targetNode);

END IF;

Promote the second child node of the root node to be the new root node.

GlobalRoot

NodeRef GlobalRoot()
Returns a node reference to the global root node (a location for holding things that will persist for the life of your process and that are visible to all components in your process).
#SavedDataRoot := NodeAppendChild(GlobalRoot(), DataRoot());

Saves a copy of the current data root onto the global root. (You should clean up later using NodeDeleteChild.)

IsValidLocation

Boolean IsValidLocation(String XPathLocation)
Returns True, if the location is valid (when a node exists in a given data record); returns False otherwise.
IF (IsValidLocation('/SciTegic.data.GroupNode/'))
 THEN
   Status := 'Is Group';

END IF;  

Tests whether the top-level object is a group node.

IsValidLocation

Boolean IsValidLocation(String XPathLocation)
Returns True, if the location is valid (when a node exists in a given data record); returns False otherwise.
IF (IsValidLocation('/SciTegic.data.GroupNode/')) 
 THEN
   Status := 'Is Group';

END IF;  

Tests whether the top-level object is a group node.

NodeAppendChild

NodeRef NodeAppendChild(NodeRef parent [, NodeRef childToBe|String nodeType|Value serialization], Boolean clone)
Appends given node to end of child nodes list. If no second parameter is given, appends generic node. Otherwise, second parameter (node reference) is given type of node to create or node serialization to create. If node reference and third parameter are given and true, that node (and children) are cloned, and new cloned copy is appended. Node reference of new child node is returned.
#newnode := NodeAppendChild(#ParentNode, "generic");

Appends a generic node to the end of the list of child nodes on the parent.

NodeDeleteAllChildren

None NodeDeleteAllChildren(NodeRef node)
Deletes all children (and their children, and so on) of the given node.
NodeDeleteAllChildren(DataRoot());

Deletes all children on the data root node.

NodeDeleteChild

None NodeDeleteChild(NodeRef parent, NodeRef child)
Deletes a child (and all its descendents) from the parent.
NodeDeleteChild(#ParentNode, #ChildNode);

Deletes the given node from the list of children.

NodeGetFirstChild

(NodeRef parentNode[, String nodeType])
Returns a reference to the first child node of a specified type. If no node type is specified, the first child node is returned. Before calling this function, call NodeHasChild() to ensure that a suitable node actually exists.
IF (NodeHasChild(GlobalRoot(), "acme")) 
 THEN
    #acmeNode = NodeGetFirstChild(GlobalRoot(), "acme");

 ELSE
	#acmeNode := NodeAppendChild(GlobalRoot());
	
 NodeSetType(#acmeNode, "acme");

END IF;

Ensures that the global root node has a child node of type "acme" (creating it if necessary), and sets a variable to hold the child node reference.

NodeHasChild

(NodeRef parentNode[, String nodeType])
Determines if a node has a child node of a specified type. If the node type argument is not specified, then a child of any type is sought; then the function is equivalent to checking if NodeNumChildren() is non-zero.
IF (NodeHasChild(GlobalRoot(), "acme")) 
 THEN
    #acmeNode = NodeGetFirstChild(GlobalRoot(), "acme");

ELSE
	#acmeNode := NodeAppendChild(GlobalRoot());
	
 NodeSetType(#acmeNode, "acme");

END IF;

Ensures that the global root node has a child node of type "acme" (creating it if necessary), and sets a variable to hold the child node reference.

NodeIsNull

Boolean NodeIsNull(NodeRef node)
Returns True if the node is null, else False.
#NodeIsRoot := NodeIsNull(NodeParent(#node));

The root node has a null parent.

NodeIthChild

NodeRef NodeIthChild(NodeRef parent, Number index)
Returns a reference to the ith child node.
FOR #i in 1 .. NodeNumChildren(#Node) 
 LOOP

 #Child := NodeIthChild(#Node, #i);

   /* Do something with the child */

END IF;

Loops over every child.

NodeIthProperty

Property NodeIthProperty(NodeRef node, Number index)
Gets the ith property of the node.
#Root := DataRoot(); 

 #newNode := NodeAppendChild(#Root); 

 FOR #i in 1 .. NodeNumProperties(#root) 
 LOOP 
   
 NodeProperty(#newNode,
   
 NodeIthPropertyName(#root, #i)) := 
  
 NodeIthProperty(#root, #i); 

END LOOP;

NodeIthPropertyName

String NodeIthPropertyName(NodeRef node, Number index)
Returns the name of the ith property on a node.
#Root := DataRoot(); 

#newNode := NodeAppendChild(#Root); 

FOR #i in 1 .. NodeNumProperties(#root) 
 LOOP 
   
 NodeProperty(#newNode, 

   NodeIthPropertyName(#root, #i)) := 
   
 NodeIthProperty(#root, #i); 

END LOOP;

NodeMetaDataProperties

StringArrayValue NodeMetaDataProperties(NodeRef node)
Returns an array of all the metadata properties that exist on the given node. If none, returns a zero-length array.
NodeMetaNames := NodeMetaDataProperties(dataroot());

Put the array of metadata property names that exists on the data root into 'NodeMetaNames'.

NodeMetaDataProperty

Property NodeMetaDataProperty(NodeRef node, String metaPropName)
Accesses the metadata properties on a node.
IF (NodeMetaDataProperty(DataRoot(), 'InitFlag') IS NOT DEFINED) 
 THEN
 NodeMetaDataProperty(DataRoot(), 'InitFlag') := false;

END IF;

Tests whether a given metadata property exists on the root node. If not, initializes the property.

NodeNumChildren

Number NodeNumChildren(NodeRef node)
Returns the number of child nodes on the given node.
TotalProps := 0;

 FOR #c in 1 .. NodeNumChildren(DataRoot()) 
 LOOP

   #Child := NodeIthChild(DataRoot(), #c);
   
 TotalProps += NodeNumProperties(#Child);

END LOOP;

Calculates the total number of properties on all child nodes.

NodeNumProperties

Number NodeNumProperties(NodeRef node)
Returns the number of properties on the node.
#Root := DataRoot();
 
#newNode := NodeAppendChild(#Root);
 
FOR #i in 1 .. NodeNumProperties(#root)
 LOOP 

   NodeProperty(#newNode, 

   NodeIthPropertyName(#root, #i)) := 

   NodeIthProperty(#root, #i); 
END LOOP;

NodeParent

NodeRef NodeParent(NodeRef node)
Gets the parent of a particular node.
#parent := NodeParent(#node);

Gets the parent node of a node. The return value might be the null node if the node has no parent. You can test for this using NodeIsNull.

NodePathLocation

String NodePathLocation(NodeRef node)
Returns the path location to the given node relative to the data record root node.
#location := NodePathLocation(#node);

Gets the path to a NodeRef from the data record root node.

NodeProperty

Property NodeProperty(NodeRef node, String propname)
Returns a property on a node.
#Root := DataRoot();
 
#newNode := NodeAppendChild(#Root);
 
FOR #i in 1 .. NodeNumProperties(#root)
 LOOP 
   
 NodeProperty(#newNode, 

   NodeIthPropertyName(#root, #i)) := 

   NodeIthProperty(#root, #i); 

END LOOP;

NodeQuery

Array NodeQuery(NodeRef root, String xpath)
Queries a node for children and properties matching the given xpath expression. Matching nodes are returned in an Array containing NodeRefs.
#nodes := NodeQuery(DataRoot(), “//book”);

Query the root node for all descendents of type "book".

NodeRemoveMetaData

None NodeRemoveMetaData(Property or String propName, String metaPropName)
Removes the given metadata property if it exists.
NodeRemoveMetaData(dataroot(), 'Format');

Removes the metadata 'Format' from the data root.

NodesEqual

Boolean NodesEqual(NodeRef node1, NodeRef node2)
Returns True if the nodes are the same, else False.
#sameParent := NodesEqual(NodeParent(#node1), NodeParent(#node2));

Checks if the two nodes have the same parent.

NodeSerialize

Value NodeSerialize(NodeRef node)
Creates a value that contains a serialization of the node and its children. Use this serialization to recreate the node using NodeAppendChild or DataSetRoot.
#S := NodeSerialize(DataRoot());

Creates a serialization of the data root node.

NodeSetType

None NodeSetType(NodeRef node, String type)
Enables you to set your own type, (for example, to create your own data type in a reader).
NodeSetType(DataRoot(), 'MyNode');

Sets the type of the node so that I can identify my own data records later.

NodeType

String NodeType(NodeRef node)
Returns the node type, which identifies the type of the node.
IF (NodeType(DataRoot()) eq 'generic') 
 THEN
   /* Perform an action suitable for generic nodes */

END IF;

Tests the type of the data record's root node.

NumChildren

Number NumChildren(String XPathLocation)
Returns a number. It gets the number of children under the node at the given location in the hierarchy. If the location is not valid, throws an error.
IF (NumChildren('/*/') == 0) 
 THEN
%'/*/NoChildren' = (NumChildren('/*/') == 0);

END IF;

Sets a property on the top-level node if it has no child nodes.