Page 1 of 1
Java - Vensim Integration Problem
Posted: Fri Jan 26, 2007 4:21 pm
by gyucel
Hi,
I am working on a small demo model in order to see how I can connect Agent-based model with a SD model on Vensim.
I have a strange problem. At a certain point in flow sequence in ABM, I am calling some methods like Vensim.command("GAME>GAMEINTERVAL|3); or Vensim.command( "SIMULATE>SETVAL|xxxx"). They are working quite well.
When I call vensim.command("GAME>GAMEON") at the same point, program is jammed.
Just before calling GAMEON, I call Vensim.check_status(); and get the result 5 (which indicates that Vensim is in game mode). So Vensim is in game mode, and I am passing the command for stepping forward, but get no response, error message, etc. It gets jammed.
Anybody out there who can provide a clue about the solution? Maybe Bob, as always?
Thanks in advance
Gonenc
Posted: Sat Jan 27, 2007 1:24 pm
by bob@vensim.com
Hi Gonenc,
The first thing to do is to figure out if it something about the model or something about the command sequence. Write a contained java program that loads the model, execute MENU>GAME then GAME>GAMEON a few time then GAME>ENDGAME and see if that works. If not it is the model (so see if you can game the model in Vensim proper). IF so, then something about the sequence is at fault. -
Posted: Wed Jan 31, 2007 4:28 pm
by gyucel
Thanks for the reply Bob,
I have tried with a very simple java program. The sequence I follow is;
* Load the model
* Setup it in the game mode
* Change some auxiliary values
* Proceed game several tımes wıth GAMEON command
* End game wıth ENDGAME
Works perfectly fıne.
It ıs a bıt hard to explain when problem happens precisely, but i will try.
I have another java code, which contains a method called execute. This execute is the method called at every tick of the agent based model. RePast controls the schedule of actions in agent based model, so it controls the calls made to this execute method. In simple words of someone who knows little about java like me, from some point on in that java code RePast takes over and starts calling execute repeatedly method according to a schedule.
If I call GAMEON before RePast takes over and starts agent based action, no problem again.
When I put it into execute method, it fails.
How it fails is also interesting. We tried to debug it. vensim.command("GAME>GAMEON") is called. But we get nothing back from this call. Java code expects a return from the call, but we cannot get anything (success, failure or any other vensim error message)
I have talked with some other java people around. They could not figure out the reason. They are a bit suspicious about the possibility that multi-thread may be causing a problem with Vensim. I am not sure what exactly this means, but maybe it gives a clue .
I hope I could give an idea about the problem, and hope someone has a guess about the source of the problem.
thanks in advance for any response,
gonenc
[Edited on 1-31-2007 by gyucel]
Posted: Wed Jan 31, 2007 4:42 pm
by Administrator
>> I have talked with some other java people around. They
>> could not figure out the reason. They are a bit suspicious
>> about the possibility that multi-thread may be causing a
>> problem with Vensim. I am not sure what exactly this
>> means, but maybe it gives a clue .
Is the java code running on multiple threads? If yes, there is a possibility that you are calling the same function within the Vensim DLL while it is already running. That would obviously cause a problem.
If it is multi threaded, you will need to implement some sort of control over when the Vensim functions are called.
Tony.
Posted: Thu Feb 01, 2007 2:22 pm
by gyucel
Thanks Tony,
After your comment I did some more debugging and experiment. Here is what happens.
In the main thread, model is loaded and initialized in the game mode.
When RePast starts, another thread starts and GAMEON command is passed in that thread. This is when code pauses.
First experiment:
Instead of GAMEON command, I tried to call SIMULATE>SETVAL instead of GAMEON in this second thread. It works.
Second experiment:
I carried all model loading and initializing commands into the RePast related section, so all of them are called from a single thread (the second one I mentioned above). Then GAMEON works.
This second experiment seems to be a solution, but it is not that nice. Now in every tick of the agent based model, I have to load model, initialize it, run it, end game. In the next tick, all over again.
Now, I will try to find a better way of doing that.
Meanwhile, any idea about why GAMEON causes problem, but not SETVAL?
And any suggestions for doing the second experiment in a better and more clever way?
Regards
Gonenc
Posted: Thu Feb 01, 2007 3:00 pm
by Administrator
No idea why it would work with SETVAL and not GAMEON, maybe something to do with file IO.
The problem is not really to do with the Vensim DLL at all.
In C++ you can create a variable that allows a thread access to a class
http://msdn2.microsoft.com/en-us/librar ... S.80).aspx
There must be something similar in Java to allow this.
Tony.
[Edited on 1-2-2007 by Administrator]
[Edited on 1-2-2007 by Administrator]
Posted: Sat Feb 03, 2007 12:18 am
by bob@vensim.com
Hi Gonenc,
At a guess the SETVAL command is doing little enought that it does not cause problems. Most likely GAMEON is getting executed too early relative to loading etc. You need to use a mutex to manage interthread sequencing - I am not sure how these are implemented in Java. One cheap solution that should work would be to just force a delay on the first execution of the GAME>GAMEON command using some sort of Sleep command.
Posted: Fri Feb 09, 2007 4:38 pm
by gyucel
Thnaks a lot Bob and Tom,
Now I am trying to figure out how to implement mutex in java.
Another observation that may ring some bells;
Problem seems to be setting the vensim to game mode (MENU>GAME) from one thread and later calling MENU>GAMEON from another thread. (Interesting thing is although application is multi-threaded, they are not active simultaneously as far as I can see).
If I put them one following the other in the same method, no problem.
I will let you know if it works with the mutex, if I can manage to do it properly.
gonenc