Events Step by StepModel Implementation (1)
The main() Method |
||
We won't use any of DESMO-J's GUI features for this simple model. Therefore, we have to implement a main() method to run the model. The main() method of a DESMO-J model has a very typical structure. The main tasks of this method are:
We will now take a step by step look at the main() method of our van carrier model. /** * Runs the model. * * @param args is an array of command-line arguments (will be ignored here) */ public static void main(java.lang.String[] args) { // create model and experiment EventsExample model = new EventsExample(null, "Simple Event-Oriented Van Carrier Model", true, true); // null as first parameter because it is the main model and has no mastermodel Experiment exp = new Experiment("EventExampleExperiment"); // ATTENTION, since the name of the experiment is used in the names of the // output files, you have to specify a string that's compatible with the // filename constraints of your computer's operating system.
The first statement instantiates a model object. Please note that the first parameter of the model constructor is passed the value null. This is to express that the model just instantiated is not part of another model (no The second statement creates a new experiment. As you can see in the comment, it is necessary to choose a name that is a valid filename for your OS. DESMO-J does not check the given string, it will only add the four suffixes:
and then write all model output to these files. Note that the experiment has no model parameter in its constructor call. It is not a part of the model, it only gets connected to it. // ... main() continued // connect both model.connectToExperiment(exp); This method is used to create a connection between the model and its associated experiment. From now on, all components of the model have access to the simulation infrastructure (e.g. event list, simulation clock) hidden inside the experiment. The framework will call the model's init() method within this statement. // ... main() continued // set experiment parameters exp.setShowProgressBar(true); // display a progress bar (or not) exp.stop(new TimeInstant(1500, TimeUnit.MINUTES)); // set end of simulation at 1500 minutes exp.tracePeriod(new TimeInstant(0), new TimeInstant(100, TimeUnit.MINUTES)); // set the period of the trace exp.debugPeriod(new TimeInstant(0), new TimeInstant(50, TimeUnit.MINUTES)); // and debug output // ATTENTION! // Don't use too long periods. Otherwise a huge HTML page will // be created which crashes Netscape :-) The first statement specifies if the progress of the simulation experiment is to be visualised via a progress bar. Pass the value true to the setShowProgressBar() method if you want a progress bar to be displayed. The second statement specifies the commonly used end criterion: a point in simulation time at which the simulation shall stop. It is also possible to set a more complex ending criterion (API) with a predefined condition. The third and fourth statement specify the length of the trace and debug record. DESMO-J will stop recording these two report files for the simulation run after the given period of simulation time. Don't use too long periods, otherwise the size of the written HTML files will become quite huge. Your browser might encounter severe problems with displaying the files and crash. // ... main() continued // start the experiment at simulation time 0.0 exp.start(); // --> now the simulation is running until it reaches its end criterion // ... // ... // <-- afterwards, the main thread returns here // generate the report (and other output files) exp.report(); // stop all threads still alive and close all output files exp.finish(); } What's left is to start the experiment... and then collect its results and shut everything off nicely. That's all. |
||
http://desmoj.sourceforge.net/tutorial/events/impl4.html |