embpy04.gms : Test continuation of embedded code blocks

Description

By default, embedded Python code can be paused and continued in another
block, which allows to access previously defined symbols and saves multiple
initializations. With the command line paramter "freeEmbeddedPython", this
behavior can be changed.

Contributor: Lutz Westermann, February 2019


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embpy04.gms

$title 'Test continuation of embedded code blocks' (EMBPY04,SEQ=797)

$onText
By default, embedded Python code can be paused and continued in another
block, which allows to access previously defined symbols and saves multiple
initializations. With the command line paramter "freeEmbeddedPython", this
behavior can be changed.

Contributor: Lutz Westermann, February 2019
$offText


* On the major platforms (Windows, Linux, Mac), GMSPYTHONHOME gets set automatically 
* if pySetup=1 (default). If the %gams.sysdir%GMSPython directory does not exist on 
* these platforms pySetup will be reset to 0 and GAMS relies on an external Python 
* setup.
$if not setEnv GMSPYTHONHOME
$if %gams.pySetup%==1 $abort.noError Embedded code Python not ready to be used
$if not setEnv GMSPYTHONHOME $log --- Using external Python

Scalar x /0/;

$onmultiR
$onEmbeddedCode Python:
x = 2
gams.set("x",[x])
$offEmbeddedCode x
$ife x<>2 $abort Unexpected value in line %system.line%


* Symbol x set above should still be available
*   Moving from one compile time block to another
$onEmbeddedCode Python:
x = x*2
gams.set("x",[x])
$offEmbeddedCode x
$ife x<>4 $abort Unexpected value in line %system.line%


* Symbol x set above should still be available
*   Moving from a compile time block to a execution time block
EmbeddedCode Python:
x = x*2
gams.set("x",[x])
pauseEmbeddedCode x
abort$(x<>8) 'Unexpected value in line %system.line%';


* Symbol x set above should still be available
*   Pausing an execution time block and continuing a execution time block
continueEmbeddedCode:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>16) 'Unexpected value in line %system.line%';


* Symbol x set above should still be available
*   Ending an execution time block and starting a new execution time block
EmbeddedCode Python:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>32) 'Unexpected value in line %system.line%';

* Next there are examples with "freeEmbeddedPython" set to 1 to change the
* default beahvior

* Setting freeEmbeddedPython=1 should not make a difference for pause/continue
$onEcho > test.gms
Scalar x /0/;

EmbeddedCode Python:
x = 2
gams.set("x",[x])
pauseEmbeddedCode x
abort$(x<>2) 'Unexpected value in line %system.line%';

continueEmbeddedCode:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>4) 'Unexpected value in line %system.line%';
$offEcho

$call gams test.gms freeEmbeddedPython=1 lo=%GAMS.lo%
$ifE errorLevel<>0 $abort Unexpected problem in line %system.line%


* The following examples are all expected to fail, because a symbol
*   defined in the first block should be know in the secone one anymore

*   Moving from one compile time block to another
$onEcho > test.gms
Scalar x /0/;

$onEmbeddedCode Python:
x = 2
gams.set("x",[x])
$offEmbeddedCode x
abort$(x<>2) 'Unexpected value in line %system.line%';

$onEmbeddedCode Python:
x = x*2
gams.set("x",[x])
$offEmbeddedCode x
abort$(x<>4) 'Unexpected value in line %system.line%';
$offEcho

$call gams test.gms freeEmbeddedPython=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort Unexpected problem in line %system.line%


*   Moving from a compile time block to a execution time block
$onEcho > test.gms
Scalar x /0/;

$onEmbeddedCode Python:
x = 2
gams.set("x",[x])
$offEmbeddedCode x
abort$(x<>2) 'Unexpected value in line %system.line%';

embeddedCode Python:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>4) 'Unexpected value in line %system.line%';
$offEcho

$call gams test.gms freeEmbeddedPython=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort Unexpected problem in line %system.line%


*   Ending an execution time block and starting a new execution time block
$onEcho > test.gms
Scalar x /0/;

embeddedCode Python:
x = 2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>2) 'Unexpected value in line %system.line%';

embeddedCode Python:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>4) 'Unexpected value in line %system.line%';
$offEcho

$call gams test.gms freeEmbeddedPython=1 lo=%GAMS.lo%
$ifE errorLevel=0 $abort Unexpected problem in line %system.line%