Package localsolver.modeler
Class LSPModeler
- java.lang.Object
-
- localsolver.modeler.LSPModeler
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class LSPModeler extends java.lang.Object implements java.lang.AutoCloseable
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. The creation of an LSPModeler environment results in the creation of a dedicated LocalSolver environment as well. For more information on how to use the solver's API, see theLocalSolver
class.- Since:
- 10.0
- See Also:
LocalSolver
,LSPModule
-
-
Constructor Summary
Constructors Constructor Description LSPModeler()
Constructs a complete modeler environment.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
LSPValue
createBool(boolean value)
Creates a boolean value.LSPValue
createDouble(double value)
Creates a double value.LSPFunction
createFunction(java.lang.String name, LSPFunctor functor)
Creates an external LSPFunction.LSPFunction
createFunction(LSPFunctor functor)
Creates an external LSPFunction.LSPValue
createInt(long value)
Creates an integer value.LSPMap
createMap()
Creates anLSPMap
.<E> LSPMap
createMap(java.util.List<E> values)
Creates aLSPMap
and fills it with the contents of the provided list.LSPModule
createModule(java.lang.String moduleName)
Creates an empty module with the given name.LSPValue
createNil()
Creates a nil value.LocalSolver
createSolver()
Returns a new LocalSolver instance that can be used to launch a module with the methodLSPModule.run(localsolver.LocalSolver, java.lang.Iterable<java.lang.String>)
.LSPValue
createString(java.lang.String value)
Creates a string value.void
delete()
Deletes this modeler environment.boolean
equals(java.lang.Object obj)
LSPModule
getModule(java.lang.String moduleName)
Returns the loaded module with the given name.java.io.PrintStream
getStdErr()
Returns the stream used by the modeler for its standard error output.java.io.PrintStream
getStdOut()
Returns the stream used by the modeler for its standard output methods likeprint
orprintln
.int
hashCode()
LSPModule
loadModule(java.lang.String filePath)
Loads a program written in LSP format into aLSPModule
whose name corresponds to the provided filename (without path and .lsp extension).void
setStdErr(java.io.PrintStream stream)
Sets the stream used by the modeler for its standard error output.void
setStdOut(java.io.PrintStream stream)
Sets the stream used by the modeler for its standard output methods likeprint
orprintln
.
-
-
-
Constructor Detail
-
LSPModeler
public LSPModeler()
Constructs a complete 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 Also:
LocalSolver
-
-
Method Detail
-
delete
public void delete()
Deletes this modeler environment. All references bound to this modeler will be automatically freed. This also includes solvers created using thecreateSolver()
.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
loadModule
public LSPModule loadModule(java.lang.String filePath)
Loads a program written in LSP format into aLSPModule
whose name corresponds to the provided filename (without path and .lsp extension). The variables of the module can be manipulated through the associatedLSPModule
object.- Parameters:
filePath
- Path to the file.- Returns:
- Module created.
- See Also:
LSPModule
-
createModule
public LSPModule createModule(java.lang.String moduleName)
Creates an empty module with the given name. The variables of the module can then be manipulated through the associatedLSPModule
object.- Parameters:
moduleName
- Module name.- Returns:
- Module created.
- See Also:
LSPModule
-
getModule
public LSPModule getModule(java.lang.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 associatedLSPModule
object.- Parameters:
moduleName
- Module name.- Returns:
- Module loaded.
- See Also:
LSPModule
-
createSolver
public LocalSolver createSolver()
Returns a new LocalSolver instance that can be used to launch a module with the methodLSPModule.run(localsolver.LocalSolver, java.lang.Iterable<java.lang.String>)
. The returned solver will be automatically disposed when the modeler is destroyed or when the current reference scope is released.- Returns:
- Solver.
-
createFunction
public LSPFunction createFunction(java.lang.String name, LSPFunctor functor)
Creates an external LSPFunction. The argument must be derived fromLSPFunctor
. When the function is called, the modeler instance will be made accessible to the function, as well as the 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".public class MyCustomFunction extends LSPFunctor { @Override LSPValue call(LSPModeler modeler, List<LSPValue> arguments) override { return modeler.createDouble(arguments.get(0).asDouble() + arguments.get(1).asDouble()); } } MyCustomFunction customFunctor = new MyCustomFunction(); module.setFunction("myCustomFunction", modeler.createFunction(customFunctor));
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 (seeLSIntExternalFunction
orLSDoubleExternalFunction
)- Parameters:
name
- Name of the function. 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.functor
- Implementation of the external function.- Returns:
- Function created.
- See Also:
LSPFunctor
,LSPFunction
-
createFunction
public LSPFunction createFunction(LSPFunctor functor)
Creates an external LSPFunction. The argument must be derived fromLSPFunctor
. When the function is called, the modeler instance will be made accessible to the function, as well as the 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".public class MyCustomFunction extends LSPFunctor { @Override LSPValue call(LSPModeler modeler, List<LSPValue> arguments) override { return modeler.createDouble(arguments.get(0).asDouble() + arguments.get(1).asDouble()); } } MyCustomFunction customFunctor = new MyCustomFunction(); module.setFunction("myCustomFunction", modeler.createFunction(customFunctor));
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 (seeLSIntExternalFunction
orLSDoubleExternalFunction
)- Parameters:
functor
- Implementation of the external function.- Returns:
- Function created.
- See Also:
LSPFunctor
,LSPFunction
-
createMap
public LSPMap createMap()
Creates anLSPMap
. A map is a data structure mapping keys to values that can also be used as an array-like structure. Keys and values can be of any type except nil. The map can be assigned to any variable in a module withLSPModule.setMap(java.lang.String, localsolver.modeler.LSPMap)
or can be part of another map withLSPMap.setMap(java.lang.String, localsolver.modeler.LSPMap)
.- Returns:
- Map created.
- See Also:
LSPMap
-
createMap
public <E> LSPMap createMap(java.util.List<E> values)
Creates aLSPMap
and fills it with the contents of the provided list. The keys in the map will correspond to the indices of the values in the list. This method only supports types that can be added to a LSPMap (such as integer, double, strings, LSExpression, LSPMap, LSPModule or LSPFunction) 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 withLSPModule.setMap(java.lang.String, localsolver.modeler.LSPMap)
or can be part of another map withLSPMap.setMap(java.lang.String, localsolver.modeler.LSPMap)
.- Parameters:
values
- List of values to place in the map.- Returns:
- Map created containing all the values in the list.
-
createNil
public LSPValue createNil()
Creates a nil value.- Returns:
- Created nil value.
- See Also:
LSPValue
-
createInt
public LSPValue createInt(long value)
Creates an integer value. The value can be assigned to any variable in a module withLSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map as key or value.- Returns:
- Created integer value.
- See Also:
LSPValue
-
createDouble
public LSPValue createDouble(double value)
Creates a double value. The value can be assigned to any variable in a module withLSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map as key or value.- Returns:
- Created double value.
- See Also:
LSPValue
-
createBool
public LSPValue createBool(boolean value)
Creates a boolean value. Please note that there is no dedicated type for booleans in the modeler. They are simulated with integers: 1 denotes true and 0 denotes false. The created value can be assigned to any variable in a module withLSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map as key or value.- Returns:
- Created boolean value.
- See Also:
LSPValue
-
createString
public LSPValue createString(java.lang.String value)
Creates a string value. The value can be assigned to any variable in a module withLSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map as key or value.- Returns:
- Created string value.
- See Also:
LSPValue
-
getStdOut
public java.io.PrintStream getStdOut()
Returns the stream used by the modeler for its standard output methods likeprint
orprintln
. The default stream used by the modeler corresponds to the Java standard output, retrieved withSystem.out
.- Returns:
- The stream used by the modeler for its standard output or null if the standard output is disabled.
-
setStdOut
public void setStdOut(java.io.PrintStream stream)
Sets the stream used by the modeler for its standard output methods likeprint
orprintln
. The default is to redirect all the modeler's outputs to the Java standard output. If the given stream is null, the standard output of the modeler will be disabled and all calls to theprint/println
related functions will do nothing.- Parameters:
stream
- Stream to use for the standard output or null to disable standard output.
-
getStdErr
public java.io.PrintStream getStdErr()
Returns the stream used by the modeler for its standard error output. The default stream used by the modeler corresponds to the Java standard error output, retrieved withSystem.err
.- Returns:
- The stream used by the modeler for its standard error output or null if the standard error output is disabled.
-
setStdErr
public void setStdErr(java.io.PrintStream 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 Java 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.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
-