LocalSolver logo
is now
Hexaly logo

We're excited to share that we are moving forward. We're leaving behind the LocalSolver brand and transitioning to our new identity: Hexaly. This represents a leap forward in our mission to enable every organization to make better decisions faster when faced with operational and strategic challenges.


LSPModeler Class

class LSPModeler

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 LSP 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_solver Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.run().
load_module Loads a program written in LSP format into a LSPModule whose name corresponds to the filename.
create_module Creates an empty module with the given name.
get_module Returns the loaded module with the given name.
create_map Creates a LSPMap.
create_function Creates an external LSPFunction.
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

LSPModeler.stdout

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

LSPModeler.stderr

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

Instance methods

LSPModeler.create_solver()

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

Returns:Solver.
Return type:LocalSolver
See:LocalSolver
LSPModeler.load_module(filepath)

Loads a program written in LSP format into a LSPModule whose name corresponds to the filename. The variables of the module can be manipulated through the returned module object.

Parameters:filepath (str) – Path to the file.
Returns:Module loaded.
Return type:LSPModule
LSPModeler.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:LSPModule
LSPModeler.get_module(module_name)

Returns the loaded module with the given name. User loaded modules can be accessed as well as builtin modules like the JSON module or the CSV module:

jsonModule = modeler.get_module("json")

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

Parameters:module_name (str) – Name of the module.
Returns:Module created.
Return type:LSPModule
LSPModeler.create_map()

Creates a LSPMap. 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 LSPModule.__setitem__() or can be part of another map with LSPMap.__setitem__().

Returns:Map created.
Return type:LSPMap
LSPModeler.create_function(name, function)
LSPModeler.create_function(function)

Creates an external LSPFunction. The provided function must take a modeler instance as first argument and the LSP 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 an LSP 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 solver API directly (see LSModel.create_int_external_function() or LSModel.create_double_external_function() for example).

Parameters:
  • function – A python function that accepts a modeler instance as first argument and the LSP function arguments as subsequent arguments. The function must returns a value whose type is supported by the modeler (None, int, float, str, LSPMap, LSPFunction, LSPModule, LSExpression)
  • 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:

LSPFunction

LSPModeler.delete()

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

LSPModeler.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.
LSPModeler.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.
LSPModeler.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.
LSPModeler.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.