How to find an item by name in EA model through code without iteration?

Post Reply
mdpremkumar
Posts: 26
Joined: 17 Apr 2017, 09:06

Hi,

The standardFunctions.py script library provides a method namely FindEssentialInstanceByName, that could be used to find specific EA instance of a particular class. But the logic is based on a for loop by getting all direct instances of provided class. If the total number of instances are less, this method will work fine. Otherwise this method may be slow if we repeatedly call for checking existing instances before creating new instance.

Is there an out of the box, better performing mechanism to find items by name, inside a python script without using an iteration? Request to share some thoughts on this.

Thanks.
Prem
User avatar
jonathan.carter
Posts: 1087
Joined: 04 Feb 2009, 15:44

Hi,

Thanks very much for your suggestion.

There are a couple of things that I would like to suggest on this.
The first is that there is an outer-level search that you could use - EssentialGetInstance() - that uses a cascade of different approaches for finding the correct instance.

Code: Select all

def EssentialGetInstance(theClassName, theInstanceID, theInstanceName, theExternalID, theExternalRepositoryName):
One of the functions that this uses, is the FindEssentialInstanceByName(), which is used to find the correct instance by looking at the value of the slot called "name". More about what we might be able to do here in a moment.

If you know things like the external reference for the instance that you are importing, you can use EssentialGetInstance() to search for (or create if it's not found) the instance in the target repository using in order:
  • The instance ID (in the target repository)
  • The external reference
  • The value of the name slot - FindEssentialInstanceByName()
  • Create a new instance
Given the scale of instances that you are trying to import, we might be able to speed things up if we use a Python map() function to find a more efficient way of finding the instances. I have had very good experience of using the map() function to get good speed improvements for large volumes.

As an example, here is a snippet of some Python that I put together to remove old External Repository Instance References from a repository (very large number of instances). A simple iteration calling delete was performing very poorly. However, the following worked very well.

Code: Select all

#
# Delete all instances of the specified class.
def deleteAllInstances(theClassName):
    print "\nDeleting all instances of " + theClassName
    
    anInstList = kb.getCls(theClassName).getDirectInstances()
    aListSize = anInstList.size()

    # Temporarily halt generating events until the update has completed.
    kb.setGenerateEventsEnabled(0)
    map(deleteInst, anInstList)
    
    # Re-enable events
    kb.setGenerateEventsEnabled(1)
    anInstList = kb.getCls(theClassName).getDirectInstances()
    
    aDeletedCount = aListSize - anInstList.size()
    print "Successfully deleted %d instances of " % (aDeletedCount) + theClassName

# Delete all instances of Classes specified in list
def deleteAllInstancesOfClass(theClassNameList):
    """ Remove all instances of classes in the list
    
        theClassList - the list of class names of which to delete instancess
    """
    map(deleteAllInstances, theClassNameList)
 
The deleteAllInstancesOfClass() function takes a list of Class names.

Hope this helps - but do let us know if you need more!

Jonathan
Essential Project Team
Post Reply