Page 1 of 1

SableScript problem using If statement

Posted: Sun Feb 11, 2007 9:57 pm
by rleclaire
I am having difficulty getting a simple SableScript to work. What I'm trying to accomplish is to use a combo box to allow the user to select a feature and then have a run button that reads in the appropriate .cin file for that feature and runs the simulation. The combo box is working fine because I can verify that the parameter (called 'ChooseCINFile') is changing its value as the user selects a feature. When I hit the run button I've defined it runs the following script:

sub GetFeature()

dim ChooseCINFile as integer

if (ChooseCINFile = 1)
call RunVenappCommand(SIMULATE>READCIN|POX-Sable.cin)
call SimulationRun()
end if

if (ChooseCINFile = 2)
call RunVenappCommand(SIMULATE>READCIN|POX-Sable.cin)
call SimulationRun()
end if

end sub

I know the script is getting executed because if I put a call SimulationRun() outside the If statements the simulation runs (there are no commands defined in the button properties to do this - I let the script do the run). I also know that the If statements are not executing because even if I make the condition If (1 = 1) it still won't execute what is inside the If statement. I've tried putting only the call to SimulationRun() inside the simplest If statement but it doesn't work - only if it is outside the If statement. Ideas?

I also have a separate question - when the user makes a selection in the aforementioned combo box and then does a run - at the end of the run the combo box parameter always reverts to its default value as defined in the model. How do I get it to stay with the value chosen by the user? I was able to do this by making the parameter a subscripted variable in the model but there must be a better way to do this?

Any help would be appreciated. Thanks.

Posted: Sun Feb 11, 2007 11:17 pm
by Administrator
Hi and welcome to the forum.

First, you need to make a small change to your script. Sablescript is completly separate from Vensim, so to populate your integer, you need to get the value from Vensim.

sub GetFeature()

dim ChooseCINFile as integer

ChooseCINFile = GETMODELVAR("ChooseCINFile")

if (ChooseCINFile = 1)
call RunVenappCommand(SIMULATE>READCIN|POX-Sable.cin)
call SimulationRun()
end if

if (ChooseCINFile = 2)
call RunVenappCommand(SIMULATE>READCIN|POX-Sable.cin)
call SimulationRun()
end if

end sub


Hope that makes sense.

Regarding your 2nd question, the only way to do this is to save a temporary CIN file before running the simulation, then reading it back in immediately afterwards.

Tony.

Posted: Mon Feb 12, 2007 12:46 am
by rleclaire
Tony - Thanks for the prompt reply. Unfortunately, no joy. It still behaves the same (it never gets to the SimulationRun inside the If statement). In case it matters I tried creating the .vpm file with and without adding the two .cin files to the project but it didn't matter. Any other thoughts would be appreciated.

Posted: Mon Feb 12, 2007 8:23 am
by Administrator
Can you try the following to see what the value of "ChooseCINFile" is?


sub GetFeature()

dim ChooseCINFile as integer

ChooseCINFile = GETMODELVAR("ChooseCINFile")

number = msgbox ( ChooseCINFile , "debug" , MB_OK )

end sub

Posted: Mon Feb 12, 2007 3:44 pm
by rleclaire
OK, thanks - that certainly illustrates a problem. Your line produces the following value in the debug window - 19586088963. However, I've also been displaying the value of ChooseCINFile in an edit box and I get the expected 1 or 2 value depending on the position of the combo box feature selector. It must be something about it getting passed into the script.

Posted: Mon Feb 12, 2007 8:44 pm
by Administrator
Can you email me the offending screen, script and model?

Tony.
tony@ventanasystems.co.uk

Posted: Tue Feb 13, 2007 10:21 am
by Administrator
In the original function, you had

dim ChooseCINFile as integer
ChooseCINFile = GETMODELVAR(ChooseCINFile)

ChooseCINFile in the call to GETMODELVAR was initialised as "0" as it is an integer. When the call to GETMODELVAR was made, ChooseCINFile was passed to it, so Sable actually asked Vensim the following.

ChooseCINFile = GETMODELVAR("0")

By changing to code to

dim nChooseCINFile as integer
nChooseCINFile = GETMODELVAR(ChooseCINFile)

Vensim now was asked the correct question. "ChooseCINFile" was not converted to a zero before passing the request to Vensim.

I would try and use VBScript or Jscript if possible. You can debug, and the engine behind it is much more robust than SableScript.

All the best,

Tony.