Navigating in Asset Hierarchies

In todays article I would like to explain how easy it is to navigate in Asset Hierarchies with method provided in the businessobject.jar file. Basically you can code all the shown stuff by using the basics you already learned about Mao’s, MboSet’s and Relationships, but why not take some easy predefined functions to do so.

For our example I will take a simple Asset Hierarchy which comes from the Maximo demo data set.

The following script will recursively output this hierarchy using the RMI technique:

import psdi.util.MXSession as MXSession
from psdi.util import MXException

    session = MXSession.getSession()

except MXException, conex:
    print 'conex.getErrorGroup()     :',conex.getErrorGroup()
    print 'conex.getErrorKey()       :',conex.getErrorKey()
    print 'conex.getDetail()         :',conex.getDetail()
    print 'conex.getDisplayMessage() :',conex.getDisplayMessage()

def getChildrenHierarchy(asset, level):
    if asset.hasChildren():
        tab = " " * (level * 3)
        print tab + "Asset " + asset.getString("ASSETNUM") + " has childrends:"

        # GetChildren uses Relationship "ASSETCHILDREN"
        childrenSet = asset.getChildren()
        childrenMbo = childrenSet.moveFirst()
        while(childrenMbo != None):
            print tab + "--------------------"
            print tab + "Children Asset: " + childrenMbo.getString("ASSETNUM")
            if childrenMbo.hasParents():

                ownerMbo = childrenMbo.getMyParent()
                print tab + "Owner Object (Parent) for Children is " + ownerMbo.getString("ASSETNUM")

                parentMboSet = childrenMbo.getParents()
                parentMbo = parentMboSet.moveFirst()
                while (parentMbo != None):
                    print tab + "Parent MBO is: " + parentMbo.getString("ASSETNUM")
                    parentMbo = parentMboSet.moveNext()

                rootParentMbo = childrenMbo.getRootParent()
                print tab + "Root-Parent for Children is " + rootParentMbo.getString("ASSETNUM")
            # now recursivly look for more childrens...
            if childrenMbo.hasChildren():
                getChildrenHierarchy(childrenMbo, level + 1)

            childrenMbo = childrenSet.moveNext() 

# Main Program
if session.isConnected():
    assetSet = session.getMboSet('ASSET')
    assetSet.setWhere("ASSETNUM = '11400'")
    asset = assetSet.moveFirst()

    assetNum = asset.getString("ASSETNUM")
    print "Asset ", assetNum, " is the top asset in the hierarchy."

    getChildrenHierarchy(asset, 1)

The following table is a reference of all methods we have used in the script:

assetSet = asset.getChildren()Returns a Mbo Set with all children of an asset based on the "ASSETCHILDREN" relationship
asset.hasChildren()Checks if the asset has children asset objects (returns boolean)
asset.hasParents()Checks if the asset has parent asset objects (returns boolean)
assetSet = asset.getParents()Gets a Mbo Set of all Parent asset objects based on the "PARENT" relationship.
myMbo = asset.getMyParent()Returns the Mbo of the owner Object. In our sample, a simple hierarchy, it will provide the same Mbo like the getParents() method. But the owner Mbo is more generic and could be for example a Workorder, if you navigate from a Workorder Object to an asset related to a Workorder.
topAsset = asset.getRootParent()Gets the root Mbo of the asset hierarchy.