embgms04.gms : Test continuation of embedded code blocks

Description

Embedded Python code can be paused and continued in another block, which
allows to access previously defined symbols and saves multiple initializations.

Contributor: Michael Bussieck, January 2023


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embgms04.gms

$title 'Test continuation of embedded code blocks' (EMBGMS04,SEQ=928)

$onText
Embedded Python code can be paused and continued in another block, which
allows to access previously defined symbols and saves multiple initializations.

Contributor: Michael Bussieck, January 2023
$offText


Scalar x /0/;

$onMultiR
$onEmbeddedCode GAMS: save=0
scalar x /2/;
$offEmbeddedCode x
$ifE x<>2 $abort Unexpected value in line %system.line%


* Symbol x set above is not available anymore
$onEmbeddedCode GAMS:
$if declared x $abort do not expect x to be visible
scalar x /4/;
$offEmbeddedCode x
$ifE x<>4 $abort Unexpected value in line %system.line%

* Symbol x set above is available via save file
$onEmbeddedCode GAMS: restart=0 save=1
$if not declared x $abort do expect x to be visible
x = 2*x;
$offEmbeddedCode x
$ifE x<>4 $abort Unexpected value in line %system.line%


* Symbol x set above should still be available via save file
*   Moving from a compile time block to a execution time block
EmbeddedCode GAMS: restart=1
x = x*2;
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;
endEmbeddedCode x
abort$(x<>16) 'Unexpected value in line %system.line%';

* Symbol x set above not available without explicit restart
*   Ending an execution time block and starting a new execution time block
EmbeddedCode GAMS:
$if declared x $abort do not expect x to be visible
scalar x /32/;
endEmbeddedCode x
abort$(x<>32) 'Unexpected value in line %system.line%';

EmbeddedCode GAMS:
scalar session /1/;
pauseEmbeddedCode
scalar session1; session1 = embeddedHandle;

EmbeddedCode GAMS:
scalar session /2/;
pauseEmbeddedCode
scalar session2; session2 = embeddedHandle;

continueEmbeddedCode:
abort$(session<>2) 'expect to be in session 2';
pauseEmbeddedCode

EmbeddedCode GAMS:
scalar session /3/;
endEmbeddedCode

continueEmbeddedCode session1:
abort$(session<>1) 'expect to be in session 1';
endEmbeddedCode

continueEmbeddedCode session2:
abort$(session<>2) 'expect to be in session 2';
endEmbeddedCode

*The following is expected to fail - we cannot continue, if there is no paused EC
$onEcho > test.gms
Scalar x;

put_utility 'save' / '0';
EmbeddedCode GAMS: restart=0
x = 4*2;
pauseEmbeddedCode x

scalar handle;
handle = embeddedHandle;

continueEmbeddedCode:
x = x*2;
endEmbeddedCode x

* This won't work because the session with handle is closed
continueEmbeddedCode handle:
x = x*2;
endEmbeddedCode x
$offEcho

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