Package com.hexaly.modeler
Class HexalyModeler
java.lang.Object
com.hexaly.modeler.HexalyModeler
- All Implemented Interfaces:
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
Hexaly modeling language.
- Since:
- 10.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddModuleLookupPath(String path) Adds a new lookup path for modules.voidDeletes all paths used to search for modules.voidclose()createBool(boolean value) Creates a boolean value.createDouble(double value) Creates a double value.createFunction(HxmFunctor functor) Creates an external HxmFunction.createFunction(String name, HxmFunctor functor) Creates an external HxmFunction.createInt(long value) Creates an integer value.Creates anHxmMap.<E> HxmMapCreates aHxmMapand fills it with the contents of the provided list.createModule(String moduleName) Creates an empty module with the given name.Creates a nil value.Returns a new Hexaly Optimizer instance that can be used to launch a module with the methodHxmModule.run(com.hexaly.optimizer.HexalyOptimizer, java.lang.Iterable<java.lang.String>).createString(String value) Creates a string value.voiddelete()Deletes this modeler environment.booleanReturns the module with the given name.Returns the stream used by the modeler for its standard error output.Returns the stream used by the modeler for its standard output methods likeprintorprintln.inthashCode()loadModule(String moduleName, String filePath) Loads the module written in the Hexaly modeling language at the indicated location.voidsetStdErr(PrintStream stream) Sets the stream used by the modeler for its standard error output.voidsetStdOut(PrintStream stream) Sets the stream used by the modeler for its standard output methods likeprintorprintln.
-
Constructor Details
-
HexalyModeler
public HexalyModeler()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 modeling language.- See Also:
-
-
Method Details
-
delete
public void delete()Deletes this modeler environment. All references bound to this modeler will be automatically freed. This also includes optimizers created using thecreateOptimizer(). -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
loadModule
Loads the module written in the Hexaly modeling language at the indicated location. The loaded module will take the name specified in parameter. UnlikegetModule(java.lang.String), this method ignores all paths indicated byaddModuleLookupPath(java.lang.String), 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 associatedHxmModuleobject.- Parameters:
moduleName- Name taken by the module once loaded.filePath- Path to the module file.- Returns:
- Module created.
- See Also:
-
createModule
Creates an empty module with the given name. The variables of the module can then be manipulated through the associatedHxmModuleobject.- Parameters:
moduleName- Module name.- Returns:
- Module created.
- See Also:
-
getModule
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 byaddModuleLookupPath(java.lang.String). The variables of the module can then be manipulated through the associatedHxmModuleobject.- Parameters:
moduleName- Module name.- Returns:
- Module loaded.
- See Also:
-
addModuleLookupPath
Adds a new lookup path for modules. The paths specified when calling this method are taken into account when the modeling virtual machine loads a module, either by a direct call to thegetModule(java.lang.String)method, or indirectly when loading a dependency to another module. By default, the lookup path contains at least the current runtime folder.- Parameters:
path- New path to consider for modules.
-
clearModuleLookupPaths
public void clearModuleLookupPaths()Deletes all paths used to search for modules. This method deletes all the paths previously added by
addModuleLookupPath(java.lang.String)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(java.lang.String). Without a path, you won't be able to load any modules other than the native ones supplied by the virtual machine. -
createOptimizer
Returns a new Hexaly Optimizer instance that can be used to launch a module with the methodHxmModule.run(com.hexaly.optimizer.HexalyOptimizer, java.lang.Iterable<java.lang.String>). The returned optimizer will be automatically disposed when the modeler is destroyed or when the current reference scope is released.- Returns:
- Optimizer.
-
createFunction
Creates an external HxmFunction. The argument must be derived fromHxmFunctor. 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 a module under the name "myCustomFunction".public class MyCustomFunction extends HxmFunctor { @Override HxmValue call(HexalyModeler modeler, List<HxmValue> 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 optimizer API directly (seeHxIntExternalFunctionorHxDoubleExternalFunction)- 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:
-
createFunction
Creates an external HxmFunction. The argument must be derived fromHxmFunctor. 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 a module under the name "myCustomFunction".public class MyCustomFunction extends HxmFunctor { @Override HxmValue call(HexalyModeler modeler, List<HxmValue> 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 optimizer API directly (seeHxIntExternalFunctionorHxDoubleExternalFunction)- Parameters:
functor- Implementation of the external function.- Returns:
- Function created.
- See Also:
-
createMap
Creates anHxmMap. 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 withHxmModule.setMap(java.lang.String, com.hexaly.modeler.HxmMap)or can be part of another map withHxmMap.setMap(java.lang.String, com.hexaly.modeler.HxmMap).- Returns:
- Map created.
- See Also:
-
createMap
Creates aHxmMapand 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 HxmMap (such as integer, double, strings, HxExpression, HxmMap, HxmModule or HxmFunction) 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 withHxmModule.setMap(java.lang.String, com.hexaly.modeler.HxmMap)or can be part of another map withHxmMap.setMap(java.lang.String, com.hexaly.modeler.HxmMap).- Parameters:
values- List of values to place in the map.- Returns:
- Map created containing all the values in the list.
-
createNil
Creates a nil value.- Returns:
- Created nil value.
- See Also:
-
createInt
Creates an integer value. The value can be assigned to any variable in a module withHxmModule.setValue(java.lang.String, com.hexaly.modeler.HxmValue)or can be part of a map as key or value.- Returns:
- Created integer value.
- See Also:
-
createDouble
Creates a double value. The value can be assigned to any variable in a module withHxmModule.setValue(java.lang.String, com.hexaly.modeler.HxmValue)or can be part of a map as key or value.- Returns:
- Created double value.
- See Also:
-
createBool
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 withHxmModule.setValue(java.lang.String, com.hexaly.modeler.HxmValue)or can be part of a map as key or value.- Returns:
- Created boolean value.
- See Also:
-
createString
Creates a string value. The value can be assigned to any variable in a module withHxmModule.setValue(java.lang.String, com.hexaly.modeler.HxmValue)or can be part of a map as key or value.- Returns:
- Created string value.
- See Also:
-
getStdOut
Returns the stream used by the modeler for its standard output methods likeprintorprintln. 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
Sets the stream used by the modeler for its standard output methods likeprintorprintln. 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/printlnrelated functions will do nothing.- Parameters:
stream- Stream to use for the standard output or null to disable standard output.
-
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
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() -
equals
-