Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

imit_model / AnyLogic / UsersManual(AnyLogic)

.pdf
Скачиваний:
141
Добавлен:
06.06.2015
Размер:
4.28 Mб
Скачать

AnyLogic V User’s Manual

Time – relative or absolute occurrence time for scheduled events, no value for other events.

Info – inspect string for the event. E.g., if this is a message acting as a dynamic event, this field displays the string returned by the method toString() of the message.

At the event steps of the simulation, there exist current events – those that can be executed at the time now. Among the current events, there is one chosen to be executed next and others are considered as enabled (potentially could be executed next). You can control manually the choice among the current events:

To change the chosen event

1.Right-click the enabled event and choose Set chosen from the popup menu.

Among the non-current events (those that cannot be executed at the time now) there may be events scheduled at some particular time known in the future, and others that do not know their times (for example, a transition waiting for a change event or a port). The latter ones are called pending, and are not shown in the Events window by default, although you can view them if you wish:

To show/hide pending events

1.Right-click the events window and choose Hide Pending Events from the popup menu.

The occurrence times of the scheduled events may be displayed either as relative to “now” or as absolute values.

To display the occurrence times in relative/absolute values

1.Right-click the events window and choose Show Absolute Time from the popup menu. The occurrence time of a scheduled event may be modified.

To change the occurrence time of a scheduled event

1.Right-click the scheduled event and choose Modify from the popup menu. The Modify Event Scheduling dialog box opens.

© 1992-2004 XJ Technologies http://www.xjtek.com

347

Chapter 14. Debugging a model

2.Specify the new relative time of the event occurrence in the Time edit box.

3.Click OK.

14.3Breakpoints

You can debug your model by setting a breakpoint on almost any element of the model – on active object, statechart, state, transition, chart timer, dynamic timer. When an element with a breakpoint on it becomes active during the model execution, the model stops, and the icon is displayed in the status line. Thus you can trace model execution, detect some undesired activities or events in your model and perform some actions in response to the occurrences.

A breakpoint can be set from the Model Explorer or from an animated diagram. Breakpoints are displayed in animated diagrams dashed red. They are not displayed in the Model Explorer.

To set/clear breakpoint on a model element

1.Right-click the element and choose Breakpoint from the popup menu.

You can manage (remove, enable, and disable) breakpoints using the Edit Breakpoints dialog box, see Figure 159.

Figure 159. Edit Breakpoints dialog box

348

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

To open the Edit Breakpoints dialog box

1.Choose Model|Edit Breakpoints from the main menu, or Click Alt+F9.

The Edit Breakpoints dialog box is displayed.

To enable/disable a breakpoint

1.Select/clear the checkbox to the left of the breakpoint.

To remove a breakpoint

1.Select the breakpoint.

2.Click Remove button.

To remove all breakpoints

1.Click Remove All button.

To open the location of a breakpoint

1.Select the breakpoint.

2.Click Open button.

14.4 Logging a model

14.4.1Log window

You can output textual information for a model (global log) or for an individual active object during the model execution in AnyLogic log windows, see Figure 160. You can use it in debugging purposes to trace model execution by writing specific text to the log on different occurrences. The log is displayed as a read-only text, which can be copied onto the Clipboard. The log window of an active object appears in the Properties window when you

© 1992-2004 XJ Technologies http://www.xjtek.com

349

Chapter 14. Debugging a model

select an active object. Also a standalone log window can be opened from the popup menu of an active object.

Figure 160. Log window

To open the global log

1.Click the Global Log toolbar button, or Choose View|Global Log from the main menu.

To open the log window of an active object

1.Right-click the active object in the Model Explorer or on the animated structure diagram and choose Log from the popup menu.

350

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

14.4.2Writing to logs

Each active object has a log. This is the variable log of type PrintWriter defined in the class ActiveObject. You write to log in the same way as you write to, e.g., System.out. Usually, you use the methods print() and println().

In addition, a model has the so-called global log. This is the variable log of the same type PrintWriter defined as a static member variable of the class Engine. You can access the global log as Engine.log, or simply call the methods trace() and traceln() of the class

ActiveObject. The method trace() calls Engine.log.print(), and the method traceln() calls Engine.log.println().

The global log is convenient for output of information across several model replications, because it is not reset in between replications. It can also be used as a debugging tool, for example, to find out what is the order in which the model executes actions of different objects.

You work with logs using the following API (for more information, please consult AnyLogic Class Reference):

Related member variables and methods of ActiveObject java.io.PrintWriter log – log of the active object.

static void trace( java.lang.String value ) – prints a string to the global log.

static void traceln( java.lang.String value ) – prints a string with a line delimiter to the global log.

Related member variables of Engine

java.io.PrintWriter log – the global model log.

14.5 Runtime errors

Various errors may occur during the model execution. Runtime errors can be of two types:

Java exceptions

© 1992-2004 XJ Technologies http://www.xjtek.com

351

Chapter 14. Debugging a model

Simulation errors.

You can throw runtime error and terminate model execution as a reaction to different undesirable occurrences.

14.5.1Java exceptions

Java code written by the user may contain unintentional errors like division by zero, accessing null pointer and so on. Such errors are detected by Java runtime environment. If such an error occurs, Java throws an exception.

AnyLogic catches all exceptions. If an exception occurs, AnyLogic stops the model, notifies the user with a message box and dumps the exception to the global log. You can examine the log to find out where there is a bug.

14.5.2Throwing runtime errors

You can debug your model at runtime by throwing runtime errors as a reaction to undesirable occurrences using the static methods error() and assert() of the class Engine. The error() method throws runtime exception to the simulation engine. The assert() method checks if the model behaves normally by checking the specified Boolean condition to be true and throws a runtime exception if it is false. Consult AnyLogic Class Reference for more details.

Related methods of Engine

static void error(String message) – the method throws a runtime exception to the simulation engine. AnyLogic immediately stops model execution, shows the window, displaying the message and dumps the exception to the AnyLogic global log.

static void assert(boolean assertion, String message) – the method checks the assertion condition, and in case it's false, calls the error() method with specified message to raise a runtime error.

352

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

14.5.3Simulation errors

Simulation errors are logical errors of model execution. For example, if a statechart is unable to exit a branch because all exiting transitions are closed, it is a simulation error. Simulation errors are detected by AnyLogic rather than by Java runtime environment. If a simulation error occurs, AnyLogic stops the model and notifies the user with an error message.

14.6 Debugging Java code

AnyLogic does not have a built-in Java code debugger. However, you can debug Java code using a third-party debugger. The user is free to run a model within the debugger using command line execution feature (see section 18.1, “Running a model from the command line”) or to attach the debugger to the currently running model. Using the debugger control, you can trace the code, set breakpoints, output debug information, and so on.

The source code of AnyLogic Engine is not included in the distribution set. You are able to debug your own Java code, and not the engine code.

© 1992-2004 XJ Technologies http://www.xjtek.com

353

Chapter 15. Creating a model with dynamically changing structure

15. Creating a model with dynamically changing structure

AnyLogic is the only visual tool that supports creation of truly dynamic models – the ones with dynamically evolving structure and component interconnection.

AnyLogic supports:

Dynamic creation and removing of encapsulated objects. It is described in subsection 15.1, “Manual creation and destruction of encapsulated objects”. Moreover, dynamic creation and removing elements of a vector of replicated objects is enabled. See section 2.2, “Accessing and modifying a replicated object at runtime” for details.

Dynamic changing of interface elements connections. It is described in subsection 15.2, “Dynamically changing connections”.

15.1Manual creation and destruction of encapsulated objects

There are several situations when you may need to take care of the creation of encapsulated objects and tell AnyLogic not to do it automatically:

The object has a limited lifetime and should be created and destroyed dynamically as the model evolves.

The object has several constructors and it is not known in advance which one should be called.

The actual class of the object is not known in advance.

To create an encapsulated object manually, it is sufficient to write the code equivalent to the one generated by AnyLogic. The algorithm of writing the code is the following:

1.Prepare the place where you will store the reference to the encapsulated object. This can be a member variable of the corresponding type, an array, a vector, etc. In case

354

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

the encapsulated object is present on the structure diagram, but its property Auto create is not set, the member variable for the reference is generated by AnyLogic, but the encapsulated object is not created. Thus you can create the encapsulated object manually and store the reference in the member variable generated by AnyLogic.

2.When you wish to create an encapsulated object, construct an instance of the corresponding class using operator new and store the reference in the prepared place. Then call the method setup_myObject1() generated by AnyLogic or call the equivalent code. Do not forget to call register(). This method registers the object in AnyLogic simulation engine.

3.If you are creating an encapsulated object during the model initialization, pass false in the parameter autoRun of the method setup_myObject1(). If you are doing this during the model execution, pass true – this makes the encapsulated object automatically start all its activities. For your convenience AnyLogic generates the overloaded method setup_myObject1() that sets the parameter autoRun to true.

4.When you wish to destroy the encapsulated object, call the method dispose_myObject1() or call the equivalent code. Do not forget to call unregister() – this deletes the reference to the object from AnyLogic simulation

engine. Make sure nobody in the model refers to the object and the object itself does not refer to anybody. Note that the object will be deleted by the garbage collector with a certain delay.

In case an encapsulated object is created and destroyed dynamically, its animation appears and disappears synchronously with the object.

15.1.1Writing code executed on object creation and destruction

AnyLogic enables you to write code to be executed on various stages of active object creation and destruction. Thus, you can specify arbitrary actions to be performed on object creation and destruction.

15.1.1.1 Writing code executed on object creation

You can define code to be executed on various stages of active object creation. You can:

© 1992-2004 XJ Technologies http://www.xjtek.com

355

Chapter 15. Creating a model with dynamically changing structure

Define one or several constructors of active objects

Override the method onCreate()

Define the code section Startup code.

The order of execution of this code is the following:

1.First, the object constructor is called. It is fed with parameters specified in the Constructor parameters code section of the encapsulated object. You can define your own constructor and place the code there to be executed during the construction. Custom constructors can be defined in the Additional class code code section of the active object class.

2.The method create() of the active object is called. You cannot insert code into the method create(). AnyLogic places the code there that creates elements defined on the structure diagram of this active object class.

3.The method onCreate() of the active object is called. The user can override the method onCreate() to perform some actions, if necessary. Usually, this includes manual connection of ports and variables and manual creation of encapsulated objects. By the time onCreate() is called, all elements of the structure diagram are already created, except statecharts. The method onCreate() can be defined in the Additional class code code section of the active object class.

4.Finally, the method startup() is called. At the beginning of this method AnyLogic starts statecharts defined on the structure diagram. At the end of this method, AnyLogic inserts the code specified by the Startup code code section of the active object class. The execution of the startup code is the final stage of object creation.

15.1.1.2 Writing code executed on object destruction

You can define code to be executed on various stages of active object destruction. You can:

Override the method cleanup()

Override the method onDestroy()

The order of execution of this code is the following:

356

© 1992-2004 XJ Technologies http://www.xjtek.com

Соседние файлы в папке AnyLogic