SableScript problem using If statement

Use this forum to post Sable related questions.

Moderator: Administrator

Post Reply
rleclaire
Junior Member
Posts: 6
Joined: Thu Feb 01, 2007 5:27 pm

SableScript problem using If statement

Post 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.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post 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.
rleclaire
Junior Member
Posts: 6
Joined: Thu Feb 01, 2007 5:27 pm

Post 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.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post 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
rleclaire
Junior Member
Posts: 6
Joined: Thu Feb 01, 2007 5:27 pm

Post 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.
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

Can you email me the offending screen, script and model?

Tony.
tony@ventanasystems.co.uk
Administrator
Super Administrator
Posts: 4590
Joined: Wed Mar 05, 2003 3:10 am

Post 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.
Post Reply