Definition of constants
The following package myconstants defines one integer, one real, one string, and two boolean constants.
package myconstants public declarations MYCST_BIGM = 10000 ! A large integer value MYCST_TOL = 0.00001 ! A tolerance value MYCST_LINE = ! String constant "----------------------------------------------------------------" MYCST_FLAG = true ! Constant with value true MYCST_NOFLAG = false ! Constant with value false end-declarations end-package
The structure of a package is similar to the structure of Mosel models, with the difference that we use the keyword package instead of model to mark its beginning and end.
After compiling our package with the standard Mosel command (assuming the package is saved in file myconstants.mos)
mosel comp myconstants
it can be used in a Mosel model (file myconst_test.mos):
model "Test myconstants package" uses "myconstants" writeln(MYCST_LINE) writeln("BigM value: ", MYCST_BIGM, ", tolerance value: ", MYCST_TOL) writeln("Boolean flags: ", MYCST_FLAG, " ", MYCST_NOFLAG) writeln(MYCST_LINE) end-model
Please note the following:
- Package name: compiling a package will result in a file packagename.bim. This package is invoked in a Mosel model by the statement
uses "packagename"
The name of the Mosel package source file (.mos file) may be different from the name given to the BIM file. - Internal package name: the name given in the Mosel file after the keyword package is the internal name of the package. It must be a valid Mosel identifier (and not a string). This name may be different from the name given to the BIM file, but it seems convenient to use the same name for both.
- Package location: for locating packages Mosel applies the same rules as for locating modules; it first searches in the directory dso of the Xpress installation, that is, in XPRESSDIR/dso, and then in the directories pointed to by the environment variable MOSEL_DSO. The contents of the latter can be set freely by the user.
To try out the package examples in this chapter, you may simply include the current working directory ('.') in the locations pointed to by MOSEL_DSO, so that packages in the current working directory will be found, for example:
Windows: set MOSEL_DSO=.
Unix/Linux, C shell: setenv MOSEL_DSO .
Unix/Linux, Bourne shell: export MOSEL_DSO; MOSEL_DSO=.
Alternatively, you can use the compilation option -bx to indicate the location of package files (this option does not apply to DSOs):
mosel exe -bx ./ mymodel.mos
In general, and in particular for the deployment of an application, it is recommended to work with absolute paths in the definition of environment variables.
Having made sure that Mosel is able to find our package myconstants.bim, executing the test model above will produce the following output:
---------------------------------------------------------------- BigM value: 10000, tolerance value: 1e-05 Boolean flags: true false ----------------------------------------------------------------
When comparing with the C implementation of the module example myconstants in the Mosel Native Interface User Guide we can easily see that the package version is much shorter.