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 localsolver.modeler.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).

See:LocalSolver
See:LSPModule
Since:10.0

Summary

Methods
Dispose Deletes this modeler environment.
CreateSolver Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.Run.
LoadModule Loads a program written in LSP format into a LSPModule whose name corresponds to the provided filename.
CreateModule Creates an empty module with the given name.
GetModule Returns the loaded module with the given name.
GetSolver Returns the LocalSolver environment associated with this modeler instance.
CreateFunction Creates an external LSPFunction.
CreateMap Creates a LSPMap.
CreateNil Creates a nil value.
CreateInt Creates an integer value.
CreateDouble Creates a double value.
CreateBool Creates a boolean value.
CreateString Creates a string value.
GetStdOut Returns the writer used by the modeler for its standard output methods like Write or WriteLine.
SetStdOut Sets the writer used by the modeler for its standard output methods like Write or WriteLine.
GetStdErr Returns the writer used by the modeler for its standard error output.
SetStdErr Sets the writer used by the modeler for its standard error output.

Instance methods

LSPModeler()

Constructs a complete modeler environment.

See:LocalSolver
void Dispose()

Deletes this modeler environment. All references bound to this modeler will be automatically freed. This also includes solvers created using the LSPModeler.CreateSolver method.

LocalSolver CreateSolver()

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.
See:localsolver.LocalSolver
LSPModule LoadModule(string filepath)

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

Arguments:filepath (string) – Path to the file.
Returns:Module loaded.
Return type:LSPModule
LSPModule CreateModule(string moduleName)

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

Arguments:moduleName (string) – Name of the module.
Returns:Module created.
Return type:LSPModule
LSPModule GetModule(string moduleName)

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:

LSPModule jsonModule = modeler.GetModule("json");

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

Arguments:moduleName (string) – Module name.
Returns:Module created.
Return type:LSPModule
LocalSolver GetSolver()

Returns the LocalSolver environment associated with this modeler instance.

Returns:Solver associated with this modeler.
Return type:LocalSolver
LSPFunction CreateFunction(LSPFunctor functor)
LSPFunction CreateFunction(string name, LSPFunctor functor)

Creates an external LSPFunction. The argument must implement LSPFunctor. When the function is called, the modeler instance will be made accessible to the function, as well as the arguments in a list.

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”:

module.SetFunction("myCustomFunction", (modeler, arguments) => {
    return modeler.CreateDouble(arguments[0].AsDouble() + arguments[1].AsDouble());
});

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 LSExternalFunction Delegate).

Arguments:
  • functor – External function to call, passed as a delegate.
  • name (string) – 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.

See:

LSPFunctor

LSPMap CreateMap()

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 thanks to LSPModule.SetMap() or can be part of another map with LSPMap.SetMap().

Returns:Map created.
See:LSPMap
LSPMap CreateMap(IList<long> values)
LSPMap CreateMap(IList<double> values)
LSPMap CreateMap(IList<bool> values)
LSPMap CreateMap(IList<string> values)
LSPMap CreateMap(IList<LSPFunction> values)
LPSMap CreateMap(IList<LSPMap> values)
LSPMap CreateMap(IList<LSPModule> values)
LSPMap CreateMap(IList<LSPValue> values)

Creates a LSPMap and fills it with the provided values. The map can be assigned to any variable in a module with LSPModule.SetMap() or can be part of another map with LSPMap.SetMap().

Returns:Map created.
See:LSPMap
LSPValue CreateNil()

Creates a nil value.

Returns:Nil value created.
See:LSPValue
LSPValue CreateInt(long value)

Creates an integer value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:Integer value created.
See:LSPValue
LSPValue CreateDouble(double value)

Creates a double value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:Double value created.
See:LSPValue
LSPValue CreateBool(bool value)

Creates a boolean value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:Boolean value created.
See:LSPValue
LSPValue CreateString(string value)

Creates a string value. The value can be assigned to any variable in a module with LSPModule.SetValue() or can be part of a map as key or value.

Returns:String value created.
See:LSPValue
System.IO.TextWriter GetStdOut()

Returns the writer used by the modeler for its standard output methods like Write or WriteLine. The default writer used by the modeler corresponds to the C# standard output, retrieved with Console.Out.

Returns:The writer used by the modeler for its standard output or null if the standard output is disabled.
void SetStdOut(System.IO.TextWriter writer)

Sets the writer used by the modeler for its standard output methods like Write or WriteLine. The default is to redirect all the modeler’s outputs to the C# standard output. If the given writer is null, the standard output of the modeler will be disabled and all calls to the Write/WriteLine related functions will do nothing.

Arguments:writer – writer to use for the standard output or null to disable standard output.
System.IO.TextWriter GetStdErr()

Returns the writer used by the modeler for its standard error output. The default writer used by the modeler corresponds to the C# standard error output, retrieved with Console.Error.

Returns:The writer used by the modeler for its standard error output or null if the standard error output is disabled.
void SetStdErr(System.IO.TextWriter writer)

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

Arguments:writer – writer to use for the standard error output or null to disable standard error output.