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:
- See:
- Since:
10.0
Summary¶
Deletes this modeler environment. |
|
Returns a new LocalSolver instance that can be used to launch a module with the method LSPModule.Run. |
|
Returns the module with the given name. |
|
Adds a new lookup path for modules. |
|
Deletes all paths used to search for modules. |
|
Loads the module written in LSP at the indicated location. |
|
Creates an empty module with the given name. |
|
Returns the LocalSolver environment associated with this modeler instance. |
|
Creates an external LSPFunction. |
|
Creates a LSPMap. |
|
Creates a nil value. |
|
Creates an integer value. |
|
Creates a double value. |
|
Creates a boolean value. |
|
Creates a string value. |
|
Returns the writer used by the modeler for its standard output methods like Write or WriteLine. |
|
Sets the writer used by the modeler for its standard output methods like Write or WriteLine. |
|
Returns the writer used by the modeler for its standard error output. |
|
Sets the writer used by the modeler for its standard error output. |
Instance methods¶
- LSPModeler()¶
Constructs a complete modeler environment.
- See:
- 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:
- LSPModule GetModule(string moduleName)¶
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
AddModuleLookupPath
.The variables of the module can then be manipulated through the associated
LSPModule
object.- Arguments:
moduleName (string) – Module name.
- Returns:
Module created.
- Return type:
- void AddModuleLookupPath(string path)¶
Adds a new lookup path for modules. The paths specified when calling this method are taken into account when the LSP virtual machine loads a module, either by a direct call to the
GetModule
method, or indirectly when loading a dependency to another module. By default, the lookup path contains at least the current runtime folder.- Arguments:
path (string) – New path to consider for modules.
- void ClearModuleLookupPaths()¶
Deletes all paths used to search for modules. This method deletes all the paths previously added by
AddModuleLookupPath
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
AddModuleLookupPath
. Without a path, you won’t be able to load any LSP modules other than the native ones supplied by the virtual machine.
- LSPModule LoadModule(string moduleName, string filePath)¶
Loads the module written in LSP at the indicated location. The loaded module will take the name specified in parameter. Unlike
GetModule
, this method ignores all paths indicated byAddModuleLookupPath
, 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
LSPModule`
object.- Arguments:
moduleName (string) – Name taken by the module once loaded.
filePath (string) – Path to the module file.
- Returns:
Module created.
- Return type:
- 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:
- LocalSolver GetSolver()¶
Returns the
LocalSolver
environment associated with this modeler instance.- Returns:
Solver associated with this modeler.
- Return type:
- LSPFunction CreateFunction(LSPFunctor functor)¶
- LSPFunction CreateFunction(string name, LSPFunctor functor)
Creates an external
LSPFunction
. The argument must implementLSPFunctor
. 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:
- 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 toLSPModule.SetMap()
or can be part of another map withLSPMap.SetMap()
.- Returns:
Map created.
- See:
- 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 withLSPModule.SetMap()
or can be part of another map withLSPMap.SetMap()
.- Returns:
Map created.
- See:
- 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 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 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 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:
- 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.