FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious example

Preprocessing Mosel code

Description
It is possible to use standard C preprocessors such as cpp or m4 with Mosel. Mosel treats the preprocessor symbol '#' at the beginning of lines like a comment sign, with the exception of line numbers/positioning markers that are handled to obtain the original line numbers (see the discussion in Section 2.5.3 Line control directives of the Mosel Language Reference Manual).
  • Using a C preprocessor as prefix in compilation:
    mosel comp "mmsystem.pipe:cpp preproc.mos -DA=1 -DDEBUG" -o preprocout.bim
    mosel run preprocout
  • Using a C preprocessor to output model source:
    cpp preproc.mos -DA=1 -DDEBUG -o preprocout.mos
    mosel preprocout.mos
Template substitution: The Mosel program 'template.mos' implements a substitution mechanism that automatically specializes Mosel code by duplicating code portions, such as generating multiple versions of subroutines for different types. The file 'testtemplate_templ.mos' contains some examples of its use (see instructions in the respective file headers).


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
preproc.mos[download]
template.mos[download]
testtemplate_templ.mos[download]





preproc.mos

(!****************************************************** 
   Mosel Example Problems
   ======================

   file preproc.mos
   ````````````````
   Example of using preprocessor definitions within a Mosel model

   !!! The C preprocessor 'cpp' must be available in order     
   !!! to process this file before running it with Mosel 

   Compile and run the resulting BIM file with:
mosel comp "mmsystem.pipe:cpp preproc.mos -DA=1 -DDEBUG" -o preproc.bim
mosel run preproc

   Produce the prepocessed source via cpp:
cpp preproc.mos -DA=1 -DDEBUG -o preprocA1.mos

   (c) 2020-2022 Fair Isaac Corporation
       author: S. Heipcke, Aug. 2020, rev. Oct. 2022
*******************************************************!)
model "preprocess via cpp"

#ifdef A
writeln("A defined")
#if A==1
writeln("A is 1")
#endif
#else
writeln("A not defined")
#endif

!writeln("Uncommenting this line produces a syntax error",,)

#ifdef DEBUG
writeln_("This is a debug message.")
#endif

writeln_("Terminating.")

end-model

Back to examples browserPrevious example