Managing Gython objects

Gython scripts can fully control Metadata Objects containing information and settings needed for running data mining processes. These operations are:

Constructing and accessing objects

Every Data Mining Object can be constructed in the following way:

obj = ClassName(constructor_args)
            

Where ClassName is the object's class name and constructor_args is a comma-separated list of constructor arguments. For example, the ClassificationSettings object can be created in the following way:

fs = ClassificationFunctionSettings()
            

The standard way of accessing object properties is with the period, i.e.:

fs.algorithmSettings

The majority of MetaModel objects can be created by using 0-parameter constructors. However, some objects can have constructing parameters. The parameter list is one-to-one mapped to arguments from the Factory.create method for a given MetaModel object. For example, ClassificationTestTask can be created in two ways, because ClassificationTestTaskFactory have two create methods as specified in the javadoc part of the AdvancedMiner Documentation:

create()
create(testDataName, modelName, testResultName)
            

Thus, the object can be created in two ways:

Example 14.18. Constructing and accessing objects:

# the first method
task = ClassificationTestTask()
task.testDataName = 'test_data_pd'
task.modelName = 'my_model'
task.testResultName = 'results'

# the second method (the end result is the same):
task = ClassificationTestTask( 'test_data_pd', 'my_model', 'results' )

print "Task created"
    

Output:

Task created
    

Saving objects

The save command saves a local object to the Metadata Repository. The saved object must have a unique name (chosen by the user) and an identifier (chosen automatically). If the name is the same as the name of an object already stored in the repository, the old object may be overwritten or an exception will be signaled. The replace parameter controls this behavior. Note that only some objects can be saved to the repository. The saved object must be an instance of PersistentObject.

Syntax:

save(name, object[, replace])
  • name - the desired name of saved object. This string may be an object name or may have the form mralias.objectname

  • object - the object to be saved

  • replace - if true then any object with the name name already stored in the repository will be overwritten. The default value is true.

Example 14.19. Saving objects:

objectExists = 0
physicalData = PhysicalData('someTable')
try:
   save('physicalData', physicalData, 0)
except:
   objectExists = 1

save('physicalData', physicalData)
print 'object saved'
    

Output:

object saved
    

Loading objects

The load command loads an object from the Metadata Repository. All changes made to that object are visible only locally. The save function has to be used to store the changed object in the repository.

Syntax:

load(name)
  • name - the name of an object stored in the repository. This string may be an object name or may have the form mralias.objectname.

Example 14.20. Loading objects:

physicalData = load('physicalData')
print 'object loaded'
    

Output:

object loaded
    

Renaming objects

The rename command changes the name of an object stored in the Metadata Repository.

Syntax:

rename(old_name, new_name)
  • old_name - the current name of the object to be changed. This string may be an object name or may have the form mralias.objectname

  • new_name - the new name of the object. This string may be an object name or may have the form mralias.objectname

If the user specifies mralias, it should be the same for both object names. If an object with new_name already exists, an exception will be signalled.

Example 14.21. Renaming objects:

newName = 'model'

rename('modell', newName)
                

Executing tasks

The execute command executes tasks stored in the Metadata Repository. This command can work in the blocking (other script commands will wait until the task has been finished) or non-blocking mode. If more then one task is specified, they can be executed synchronously or asynchronously. If any exception occurs during task execution, it will always be displayed in the log window, and, in the case of blocking mode, it will also be visible in the output window. Moreover, if blocking mode is used, an exception will terminate the script.

Syntax:

execute(task, wait = 0 | 1)

where:

task =

name | { sync | async : [ task1, task2, ... ] }

  • name - the name of a task stored in the repository. That string may be an object name or may have the form mralias.objectname

  • wait - if 1, the command will not exit until the task is finished. If 0, the command only sends a request for task execution. The default is 1.

  • sync - the tasks will be executed synchronously

  • async - the tasks will be executed asynchronously

Example 14.22. Executing tasks:

execute('t1')
execute( { sync : ['t2', 't3', { async : [ 't4', 't5' ] } ] }, wait = 0)
                

Deleting objects

The delete command deletes an object from the Metadata Repository. All local references to this object remain valid.

Syntax:

delete(name)
  • name - the name of object to be deleted. This string may be an object name or may have the form mralias.objectname.

Example 14.23. Deleting objects

delete('model')

Checking object existence

The exists function checks if an object with the specified name is already available in the Metadata Repository. It returns a non zero value if it is. If such object does not exists, the value 0 is returned.

Syntax:

exists(name)
  • name - the name of qthe ueried object. This string may be an object name or may have the form mralias.objectname.

Example 14.24. Checking object existence:

if not exists('model'):
   execute('build_task')
                

Task termination

The terminate command terminates a task or transformation that is running on the server. This function does not wait for the task to terminate and does not guarantee immediate ending of the task. If a task is already terminated or was never run no exception will be thrown.

Syntax:

terminate(name)
  • name - the name of the task to terminate. This string may be an object name or may have the form mralias.objectname.

Example 14.25. Task termination:

terminate('task')
                

Saving script environment

The saveEnv function saves a part of the script environment to the Metadata Repository. The following types of object can be saved: float, int, complex, string, dict, list, tuple.

Syntax:

saveEnv(name[, [s1, s2, ...]], isGlobal, replace)
  • name - the name of the object that will store the saved environment

  • s1, s2, ... - the names of variables in the script to be saved. If the second parameter is ommited, all objects of valid type will be saved.

  • isGlobal - if true, global variables will be saved. Otherwise, only local variables will be saved.

  • replace - if true, any object with the name name already stored in the repository will be overwritten. The default value is true.

Example 14.26. Saving the script environment:

logicalData = 'ld'
physicalData = 'pd'
saveEnv('env', ['logicalData', 'physicalData'])
                

Loading script environment

The loadEnv command loads an object with the script variables from the Metadata Repository and sets the loaded variables variables in the current environment.

Syntax:

loadEnv(name)
  • name - the name of the object stored in the repository. This string may be an object name or may have the form mralias.objectname.

Example 14.27. Loading script environment

loadEnv('env')
print physicalData
                

Setting alias to the metadata repository

The mrAlias command sets a new Matadata Repository alias. The alias is a binding between a symbolic name and the Metadata Repository. The alias is used by all functions operating on MR objects. The default alias is arbitrally set by the platform. To explicitly set the default mrAlias, use the mrRegistry command.

Syntax:

mrAlias(mrAliasName, server, repository, user, password)
  • mrAliasName - the symbolic name of the metadata repository. Cannot be None.

  • server - the address and port of the main server

  • repository - the address and port of the Metadata Repository

  • user - user name

  • password - user password

Example 14.28. Setting an alias to the metadata repository

mrAlias('alias_name', '127.0.0.1:2531', '127.0.0.1:2531', 'user', 'password')
                

Sending messages to the log

The log command sends text with the level INFO to the log window.

Syntax:

log(text)
  • text - the text to be displayed

Example 14.29. Sending messages to the log:

log('Some information')
                

Registry Repository

There is an API for more advanced work with Metadata Repository connections. Please refer to RepoReg part of javadoc documentation for details. The function below provides access to a Registry instance.

Syntax:

mrRegistry()

Example 14.30. Setting the default registry repository:

mrRegistry().defaultRepository = 'repository_alias'
                

Project path

The projectPath function returns the absolute path to the current project in the repository.

Syntax:

projectPath()

Example 14.31. Getting the project path:

#print the path to the current project
print projectPath()