public class LSPModeler
extends java.lang.Object
implements java.lang.AutoCloseable
LocalSolver
class.LocalSolver
,
LSPModule
Constructor and Description |
---|
LSPModeler()
Constructs a complete modeler environment.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
LSPValue |
createBool(boolean value)
Creates a boolean value.
|
LSPValue |
createDouble(double value)
Creates a double value.
|
LSPFunction |
createFunction(LSPFunctor functor)
Creates an external LSPFunction.
|
LSPFunction |
createFunction(java.lang.String name,
LSPFunctor functor)
Creates an external LSPFunction.
|
LSPValue |
createInt(long value)
Creates an integer value.
|
LSPMap |
createMap()
Creates an
LSPMap . |
<E> LSPMap |
createMap(java.util.List<E> values)
Creates a
LSPMap 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 method
LSPModule.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) |
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
like
print or println . |
int |
hashCode() |
LSPModule |
loadModule(java.lang.String filePath)
Loads a program written in LSP format into a
LSPModule 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
like
print or println . |
public LSPModeler()
LocalSolver
public void delete()
createSolver()
.public void close()
close
in interface java.lang.AutoCloseable
public LSPModule loadModule(java.lang.String filePath)
LSPModule
whose name
corresponds to the provided filename (without path and .lsp extension).
The variables of the module can be manipulated through the associated
LSPModule
object.filePath
- Path to the file.LSPModule
public LSPModule createModule(java.lang.String moduleName)
LSPModule
object.moduleName
- Module name.LSPModule
public LocalSolver createSolver()
LSPModule.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.public LSPFunction createFunction(java.lang.String name, LSPFunctor functor)
LSPFunctor
. 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 (see
LSIntExternalFunction
or LSDoubleExternalFunction
)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.LSPFunctor
,
LSPFunction
public LSPFunction createFunction(LSPFunctor functor)
LSPFunctor
. 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 (see
LSIntExternalFunction
or LSDoubleExternalFunction
)functor
- Implementation of the external function.LSPFunctor
,
LSPFunction
public LSPMap createMap()
LSPMap
. 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 with LSPModule.setMap(java.lang.String, localsolver.modeler.LSPMap)
or
can be part of another map with LSPMap.setMap(java.lang.String, localsolver.modeler.LSPMap)
.LSPMap
public <E> LSPMap createMap(java.util.List<E> values)
LSPMap
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 with LSPModule.setMap(java.lang.String, localsolver.modeler.LSPMap)
or can be part
of another map with LSPMap.setMap(java.lang.String, localsolver.modeler.LSPMap)
.values
- List of values to place in the map.public LSPValue createNil()
LSPValue
public LSPValue createInt(long value)
LSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map
as key or value.LSPValue
public LSPValue createDouble(double value)
LSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map
as key or value.LSPValue
public LSPValue createBool(boolean value)
LSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a map as key or
value.LSPValue
public LSPValue createString(java.lang.String value)
LSPModule.setValue(java.lang.String, localsolver.modeler.LSPValue)
or can be part of a
map as key or value.LSPValue
public java.io.PrintStream getStdOut()
print
or println
. The default stream used
by the modeler corresponds to the Java standard output, retrieved with
System.out
.public void setStdOut(java.io.PrintStream stream)
print
or println
. 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 the print/println
related functions
will do nothing.stream
- Stream to use for the standard output or null to disable standard output.public java.io.PrintStream getStdErr()
System.err
.public void setStdErr(java.io.PrintStream stream)
stream
- Stream to use for the standard error output or null to disable standard error output.public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object