Class HxmModule

java.lang.Object
com.hexaly.modeler.HxmModule
All Implemented Interfaces:
AutoCloseable

public class HxmModule extends Object implements AutoCloseable
A module is a collection of global variables. As the Hexaly modeling language is a dynamically typed language, global variables can contain any value (including functions and other modules). Each HXM file is a module. Global variables can be modified and accessed by their name specified as a string. Most of the functions below exist in multiple versions: values of the global variables can be manipulated through their native type (like int, float, String, HxmMap, HxmFunction...) or manipulated with an HxmValue which is a container that can hold any type of value. Using the native type is more convenient and results in less overhead most of the time. HxmValue should only be used when you don't know the type of the manipulated value.
Since:
10.0
  • Method Details

    • close

      public void close()

      Releases the reference. If this module was already released, returns immediately and does nothing. Invoking any method on this object after this operation will throw an exception.

      Note: Releasing a reference does not necessarily imply that the underlying module object is destroyed. It is only destroyed if no more references point to it.

      Specified by:
      close in interface AutoCloseable
      Since:
      11.5
    • getDeclaredName

      public String getDeclaredName()
      Gets the full qualified name of the module.
    • run

      public void run(HexalyOptimizer optimizer, Iterable<String> arguments)

      Starts the execution of the module using the optimizer passed in parameter. Such execution is defined by the following steps:

      • The input function is executed if it exists in the module.
      • The model function is executed. It must be declared in the module.
      • The HxModel is then closed.
      • The param function is executed if it exists in the module.
      • The function HexalyOptimizer.solve() is called on the corresponding optimizer instance. If the display function is defined, it will be called during the resolution process.
      • The output function is executed if it exists in the module.

      Note that the optimizer used as a parameter must have been created by the HexalyModeler.createOptimizer() method.

      If arguments are given, each of them must be of the form argName=argValue. Each argument is then parsed and exposed as a new global variable in the module with argName as the name of the variable and argValue as its value. The format followed by the arguments is exactly the same as in the Hexaly Optimizer command line. For more information about the format of the Hexaly Optimizer command line you can read the command line section in the Hexaly Modeler reference guide.

      Parameters:
      optimizer - Instance on which modeling and optimization is performed.
      arguments - List of arguments to parse and expose as global variables.
    • run

      public void run(HexalyOptimizer optimizer, String... arguments)

      Starts the execution of the module using the optimizer passed in parameter. Such execution is defined by the following steps:

      • The input function is executed if it exists in the module.
      • The model function is executed. It must be declared in the module.
      • The HxModel is then closed.
      • The param function is executed if it exists in the module.
      • The function HexalyOptimizer.solve() is called on the corresponding optimizer instance. If the display function is defined, it will be called during the resolution process.
      • The output function is executed if it exists in the module.

      Note that the optimizer used as a parameter must have been created by the HexalyModeler.createOptimizer() method.

      If arguments are given, each of them must be of the form argName=argValue. Each argument is then parsed and exposed as a new global variable in the module with argName as the name of the variable and argValue as its value. The format followed by the arguments is exactly the same as in the Hexaly Optimizer command line. For more information about the format of the Hexaly Optimizer command line you can read the command line section in the Hexaly Modeler reference guide.

      Parameters:
      optimizer - Instance on which modeling and optimization is performed.
      arguments - List of arguments to parse and expose as global variables.
    • runMain

      public void runMain(Iterable<String> arguments)

      Runs the module in main mode. In this mode, the modeler behaves like a classical programming language. Therefore, the call sequence is free: you only need to implement a main method which will be the only one invoked by the modeler.

      Unlike the optimization mode, the main mode requires you to manually create the optimizer instances, close your models and launch the resolutions directly in your HXM files. For this you can rely on the builtin module hexaly. In return, you are free to run several successive resolutions or none at all if you just want to use Hexaly Modeler for its pure programming features.

      The arguments specified when calling this method are copied as is (as strings, without parsing) into a map that is passed as the first argument to the main function of your HXM file. If the main function of your file does not accept any arguments, the parameter arguments is simply ignored.

      For more details on the differences between the optimization and the main mode, read the main mode section of the Hexaly Modeler reference guide.

      Parameters:
      arguments - List of arguments to copy into a map and pass to the main function.
    • runMain

      public void runMain(String... arguments)

      Runs the module in main mode. In this mode, the modeler behaves like a classical programming language. Therefore, the call sequence is free: you only need to implement a main method which will be the only one invoked by the modeler.

      Unlike the optimization mode, the main mode requires you to manually create the optimizer instances, close your models and launch the resolutions directly in your HXM files. For this you can rely on the builtin module hexaly. In return, you are free to run several successive resolutions or none at all if you just want to use Hexaly Modeler for its pure programming features.

      The arguments specified when calling this method are copied as is (as strings, without parsing) into a map that is passed as the first argument to the main function of your HXM file. If the main function of your file does not accept any arguments, the parameter arguments is simply ignored.

      For more details on the differences between the optimization and the main mode, read the main mode section of the Hexaly Modeler reference guide.

      Parameters:
      arguments - List of arguments to copy into a map and pass to the main function.
    • getType

      public HxmType getType(String varName)
      Returns the type of the variable with the given name. If the variable does not exist in the module, HxmType.Nil is returned.
      Parameters:
      varName - Name of the variable.
      Returns:
      Type of the variable.
      See Also:
    • getValue

      public HxmValue getValue(String varName)
      Returns the HxmValue with the given name. If the variable does not exist, an HxmValue representing a nil value is returned.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxmValue associated with the given name.
      See Also:
    • getInt

      public long getInt(String varName)
      Returns the integer variable with the given name. The variable must exist and must hold an integer value.
      Parameters:
      varName - Name of the variable.
      Returns:
      Integer value associated with the given name.
    • getDouble

      public double getDouble(String varName)
      Returns the double variable with the given name. The variable must exist and must hold a double value.
      Parameters:
      varName - Name of the variable.
      Returns:
      Double value associated with the given name.
    • getBool

      public boolean getBool(String varName)
      Returns the boolean variable with the given name. The variable must exist and must hold a boolean value.
      Parameters:
      varName - Name of the variable.
      Returns:
      Boolean value associated with the given name.
    • getExpression

      public HxExpression getExpression(String varName)
      Returns the HxExpression with the given name. The variable must exist and must hold a value of type HxmType.Expression.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxExpression associated with the given name.
    • getString

      public String getString(String varName)
      Returns the string with the given name. The variable must exist and must hold a string value.
      Parameters:
      varName - Name of the variable.
      Returns:
      String associated with the given name.
    • getFunction

      public HxmFunction getFunction(String varName)
      Returns the HxmFunction with the given name. The variable must exist and must hold a function.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxmFunction associated with the given name.
    • getMap

      public HxmMap getMap(String varName)
      Returns the HxmMap with the given name. The variable must exist and must hold a map.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxmFunction associated with the given name.
    • getModule

      public HxmModule getModule(String varName)
      Returns the HxmModule with the given name. The variable must exist and must hold a module.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxmModule associated with the given name.
    • getClass

      public HxmClass getClass(String varName)
      Returns the HxmClass with the given name. The variable must exist and must hold a class.
      Parameters:
      varName - Name of the variable.
      Returns:
      HxmClass associated with the given name.
    • setValue

      public void setValue(String varName, HxmValue value)
      Sets the value associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      value - Value of the variable.
    • setInt

      public void setInt(String varName, long value)
      Sets the integer value associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      value - Value of the variable.
    • setDouble

      public void setDouble(String varName, double value)
      Sets the double value associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      value - Value of the variable.
    • setBool

      public void setBool(String varName, boolean value)
      Sets the boolean value associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      value - Value of the variable.
    • setExpression

      public void setExpression(String varName, HxExpression expr)
      Sets the HxExpression associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      expr - Value of the variable.
    • setString

      public void setString(String varName, String str)
      Sets the string associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      str - Value of the variable.
    • setFunction

      public void setFunction(String varName, HxmFunction function)
      Sets the HxmFunction associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      function - Value of the variable.
    • setMap

      public void setMap(String varName, HxmMap map)
      Sets the HxmMap associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      map - Value of the variable.
    • setModule

      public void setModule(String varName, HxmModule module)
      Sets the HxmModule associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      module - Value of the variable.
    • setClass

      public void setClass(String varName, HxmClass clazz)
      Sets the HxmClass associated with the variable with the given name. The variable is automatically created if it doesn't exist in the module.
      Parameters:
      varName - Name of the variable.
      clazz - Value of the variable.
    • setNil

      public void setNil(String varName)
      Unsets the variable with the given name. Do nothing it the variable does not exist.
      Parameters:
      varName - Name of the variable.
    • unset

      public void unset(String varName)
      Unsets the variable with the given name. Do nothing it the variable does not exist.
      Parameters:
      varName - Name of the variable.
    • isInt

      public boolean isInt(String varName)
      Returns true if the variable with the given name exists and holds an integer value.
      Parameters:
      varName - Name of the variable.
    • isDouble

      public boolean isDouble(String varName)
      Returns true if the variable with the given name exists and holds a double value.
      Parameters:
      varName - Name of the variable.
    • isBool

      public boolean isBool(String varName)
      Returns true if the variable with the given name exists and holds a boolean value.
      Parameters:
      varName - Name of the variable.
    • isExpression

      public boolean isExpression(String varName)
      Returns true if the variable with the given name exists and holds an HxExpression.
      Parameters:
      varName - Name of the variable.
    • isString

      public boolean isString(String varName)
      Returns true if the variable with the given name exists and holds a string value.
      Parameters:
      varName - Name of the variable.
    • isFunction

      public boolean isFunction(String varName)
      Returns true if the variable with the given name exists and holds an HxmFunction.
      Parameters:
      varName - Name of the variable.
    • isMap

      public boolean isMap(String varName)
      Returns true if the variable with the given name exists and holds an HxmMap.
      Parameters:
      varName - Name of the variable.
    • isModule

      public boolean isModule(String varName)
      Returns true if the variable with the given name exists and holds an HxmModule.
      Parameters:
      varName - Name of the variable.
    • isClass

      public boolean isClass(String varName)
      Returns true if the variable with the given name exists and holds an HxmClass.
      Parameters:
      varName - Name of the variable.
    • isNil

      public boolean isNil(String varName)
      Returns true if no variable with this name exists in the module or if the variable holds a nil value.
      Parameters:
      varName - Name of the variable.
    • asValue

      public HxmValue asValue()
      Returns the module as an HxmValue.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object