HexalyModeler Class

class hexaly.modeler.HexalyModeler

Modeler environment. Main class of the Modeler library which enables the creation and manipulation of a virtual machine that can load and execute programs written in the Hexaly modeling language (see the language reference for more information).

Since:

10.0

Summary

Attributes

stdout

Stream used by the modeler for its standard output methods.

stderr

Stream used by the modeler for its standard error output methods.

Methods

create_optimizer

Returns a new Hexaly Optimizer instance that can be used to launch a module with the method HxmModule.run().

get_module

Returns the module with the given name.

add_module_lookup_path

Adds a new lookup path for modules.

clear_module_lookup_paths

Deletes all paths used to search for modules.

load_module

Loads the module written in the Hexaly modeling language at the indicated location.

create_module

Creates an empty module with the given name.

create_map

Creates a HxmMap.

create_function

Creates an external HxmFunction.

delete

Deletes this modeler environment and all associated resources.

get_stdout

Returns the stream used by the modeler for its standard output methods like print.

set_stdout

Sets the stream used by the modeler for its standard output methods like print.

get_stderr

Returns the stream used by the modeler for its standard error output.

set_stderr

Sets the stream used by the modeler for its standard error output.

Instance attributes

HexalyModeler.stdout

Stream used by the modeler for its standard output methods. This is a shortcut for HexalyModeler.get_stdout().

HexalyModeler.stderr

Stream used by the modeler for its standard error output methods. This is a shortcut for HexalyModeler.get_stderr().

Instance methods

HexalyModeler.create_optimizer()

Returns a new Hexaly Optimizer instance that can be used to launch a module with the method HxmModule.run(). The returned optimizer will be automatically disposed when the modeler is destroyed or when the current reference scope is released.

Returns:

Optimizer.

Return type:

hexaly.optimizer.HexalyOptimizer

See:

HexalyOptimizer

HexalyModeler.get_module(module_name)

Returns the module with the given name. User modules can be accessed as well as builtin modules like the JSON module or the CSV module. If the module is not loaded, this method attempts to load it from the paths specified by add_module_lookup_path().

The variables of the module can then be manipulated through the associated HxmModule object.

Parameters:

module_name (str) – Module name.

Returns:

Module created.

Return type:

HxmModule

HexalyModeler.add_module_lookup_path(path)

Adds a new lookup path for modules. The paths specified when calling this method are taken into account when the modeling virtual machine loads a module, either by a direct call to the get_module() method, or indirectly when loading a dependency to another module. By default, the lookup path contains at least the current runtime folder.

Parameters:

path (str) – New path to consider for modules.

HexalyModeler.clear_module_lookup_paths()

Deletes all paths used to search for modules. This method deletes all the paths previously added by add_module_lookup_path() method, as well as the default location(s) (including the current execution folder).

__Important note:__ after calling this method, you must at least add a path with add_module_lookup_path(). Without a path, you won’t be able to load any modules other than the native ones supplied by the virtual machine.

HexalyModeler.load_module(module_name, filepath)

Loads the module written in the Hexaly modeling language at the indicated location. The loaded module will take the name specified in parameter. Unlike get_module(), this method ignores all paths indicated by add_module_lookup_path(), and if a module with a similar name is already loaded, an exception will be thrown.

Once loaded, the variables of the module can be manipulated through the associated HxmModule` object.

Parameters:
  • module_name (str) – Name taken by the module once loaded.

  • filepath (str) – Path to the module file.

Returns:

Module loaded.

Return type:

HxmModule

HexalyModeler.create_module(module_name)

Creates an empty module with the given name. The variables of the module can be manipulated through the returned module object.

Parameters:

module_name (str) – Name of the module.

Returns:

Module created.

Return type:

HxmModule

HexalyModeler.create_map()

Creates a HxmMap. A map is a data structure mapping keys to values that can also be used as an array-like structure. The map can be assigned to any variable in a module with HxmModule.__setitem__() or can be part of another map with HxmMap.__setitem__().

Returns:

Map created.

Return type:

HxmMap

HexalyModeler.create_function(name, function)
HexalyModeler.create_function(function)

Creates an external HxmFunction. The provided function must take a modeler instance as first argument and the modeling function arguments as subsequent arguments. For instance, the following example creates a simple function that accepts two arguments and returns the sum of both values. The generated function is then exposed in a module under the name “myCustomFunction”:

def my_custom_function(modeler, arg1, arg2):
    return arg1 + arg2

module["myCustomFunction"] = modeler.create_function(my_custom_function)

Note: This method should only be used to expose functions used during the modeling process. You should not use this method to create a function that will be used during the resolution as an external function. In this case, you should instead use the optimizer API directly (see create_int_external_function() or create_double_external_function() for example).

Parameters:
  • function – A python function that accepts a modeler instance as first argument and the modeling function arguments as subsequent arguments. The function must returns a value whose type is supported by the modeler (None, int, float, str, HxmMap, HxmFunction, HxmModule, HxExpression)

  • name (str) – Name of the function (optional). The name is only used to identify the function in the generated stack trace when an exception occurs. Once created, the function can be associated with any variable in any module, regardless of its name.

Returns:

Function created.

Return type:

HxmFunction

HexalyModeler.delete()

Deletes this modeler environment and all associated resources. This also deletes the optimizers created using HexalyModeler.create_optimizer(). This method is automatically called when you use the HexalyModeler in a with statement.

HexalyModeler.get_stdout()

Returns the stream used by the modeler for its standard output methods like print. The default stream used by the modeler corresponds to the Python standard output, retrieved with sys.stdout.

You can also use the shortcut member stdout

Returns:

The stream used by the modeler for its standard output or null if the standard output is disabled.

HexalyModeler.set_stdout(stream)

Sets the stream used by the modeler for its standard output methods like print. The default is to redirect all the modeler’s outputs to the Python standard output. If the given stream is null, the standard output of the modeler will be disabled and all calls to the print related functions will do nothing.

Parameters:

stream – stream to use for the standard output or null to disable standard output.

HexalyModeler.get_stderr()

Returns the stream used by the modeler for its standard error output. The default stream used by the modeler corresponds to the Python standard error output, retrieved with sys.stderr.

You can also use the shortcut member stderr

Returns:

The stream used by the modeler for its standard error output or null if the standard error output is disabled.

HexalyModeler.set_stderr(stream)

Sets the stream used by the modeler for its standard error output. The default is to redirect the standard error output stream to the Python standard error output stream. If the given stream is null, the standard error output of the modeler will be disabled.

Parameters:

stream – stream to use for the standard error output or null to disable standard error output.