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

imit_model / AnyLogic / UsersManual(AnyLogic)

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

AnyLogic V User’s Manual

7.1.1.2 Port properties

A port has a set of customizable properties. To set the port properties, first select the port on the structure diagram by clicking, and next set the property values in the Properties window.

The following properties can be set on the General page of the Properties window (Figure 66):

Figure 66. General page of the port’s Properties window

General properties

Name – the name of the port.

Message type – [optional] every message received by the port is cast to this type. If the message cannot be cast, it is discarded. Using combo box you can choose from all known message classes of this project. Message class definition is described in section 7.2, “Messages”.

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

147

Chapter 7. Message passing

Port type – [optional] the type of the port. Port class definition is described in section 7.3, “Defining custom port classes”.

Has queue – if set, incoming messages will be placed in the port queue. See section 7.1.2, “Port queue” for details.

Queue size – [optional] the size of the queue. Leaving it blank makes the size of the queue “infinite” to store all incoming messages.

Exclude from build – if set, the port is excluded from the model.

Show name – if set, the name of the port is shown on the structure diagram.

The properties listed below can be set on the Code page of the Properties window (Figure 67):

Figure 67. Code page of the port’s Properties window

148

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

AnyLogic V User’s Manual

You can change the default port behavior by writing code on the Code page of the port’s Properties window. AnyLogic provides you an ability to specify port reactions to different occurrences by writing your own Java code.

Code properties

Constructor parameters – [optional] parameters to be passed to the port’s constructor, e.g.: /*count*/ 20, /*load*/ load.

On receive action – [optional] a sequence of Java statements to be executed on every message reception.

On send action – [optional] a sequence of Java statements to be executed on every message sending.

Additional class code – [optional] Java code to be inserted into the port class definition. Arbitrary constants, variables, and methods can be defined here.

An active object – owner of the port can be accessed by the getOwner() method of the port. The detailed description of AnyLogic port classes methods is given in section 7.3.1, “Predefined port classes”.

When writing your code for an active object – e.g., in its Additional class code – you can access a port myPort of an active object as its member variable myPort.

7.1.2Port queue

You can specify that a port has a queue to store incoming messages. Stored messages can be extracted afterwards. A port queue is commonly used to store port incoming messages while the active object is busy with processing another message – e.g., while a server processes client request in the client-server system. A port queue can also model a data storage.

The presence of a port queue and its capacity is specified on the General page of the Properties window.

You may have noticed that ports with queues and ports without them appear somewhat differently on the structure diagram. A port with a queue is displayed on the structure diagram with a dot inside it, see Figure 68.

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

149

Chapter 7. Message passing

Port

Port with

without

a queue

a queue

 

Figure 68.

Port queue is FIFO queue –i.e., the first placed in the queue message is extracted first. If the specified queue capacity is reached, an incoming message is lost. Queue can be made infinite to store all incoming messages.

You can access messages stored in the port queue using the methods get() and peek() automatically generated by AnyLogic for each port with a queue (the get() method extracts the message from the queue as well). These methods return the message of type specified in the Message type property of the port.

You can check the queue size using the size() method. You may check portA queue e.g. on timer expiration. Create timer and type the following code in its Expiry action property:

if ( portA.size() > 0 )

Object msg = portA.get();

First, the method size() checks if there are any messages stored in the queue. If any, the get() method extracts the first one.

7.1.3Connecting ports

To establish message passing you should connect the respective ports by connectors. Connectors are paths used by messages to flow through the model.

You can establish message passing between:

Ports of encapsulated objects

A public port and a private port

A public port and a port of an encapsulated object

A private port and a port of an encapsulated object.

150

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

AnyLogic V User’s Manual

You can also connect a port to a statechart, see section 7.1.4, “Connecting ports to statecharts”.

This section describes the technique of establishing connections between ports.

7.1.3.1 Connecting ports of encapsulated objects

To establish message passing between two encapsulated objects, you should connect ports of encapsulated objects.

To connect ports of encapsulated objects

1.Drag the port of one encapsulated object onto the port of another encapsulated object, or

Click the Connector toolbar button, click one port, and then click another port, or Choose Draw|Structure|Connector from the main menu, click one port, and then click another port.

The connector linking two ports appears.

Connector

Figure 69. Ports of encapsulated objects connected

7.1.3.2 Connecting a public port with a port of an encapsulated object

To establish communication between the encapsulated object and the container object neighborhood, you should connect a port of an encapsulated object with a public port of the container active object.

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

151

Chapter 7. Message passing

To connect a public port with a port of an encapsulated object

1.Drag the port of the encapsulated object onto the public port, or

Click the Connector toolbar button, click one port, and then click another port, or Choose Draw|Structure|Connector from the main menu, click one port, and then click another port.

The connector linking two ports appears.

Public

port

Figure 70. A public port connected to a port of an encapsulated object

7.1.3.3 Connecting a private port with a port of an encapsulated object

To establish communication between the encapsulated object and the container active object you should connect a port of an encapsulated object with a private port of the container object.

To connect a private port with a port of an encapsulated object

1.Drag the port of the encapsulated object onto the private port, or

Click the Connector toolbar button, click one port, and then click another port, or Choose Draw|Structure|Connector from the main menu, click one port, and then click another port.

The connector linking two ports appears.

Private port

Figure 71. A private port connected with a port of an encapsulated object

152

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

AnyLogic V User’s Manual

7.1.3.4 Connecting a public port with a private port

To establish a communication between a private port and other active objects you should connect the private port with a public port. However, if you need only to send messages via the private port to other active objects, it can be made public.

To connect a public port with a private port

1.Click the Connector toolbar button, click one port, and then click another port, or Choose Draw|Structure|Connector from the main menu, click one port, and then click another port.

The connector linking two ports appears.

Public

Private

port

port

 

Figure 72. Public and private ports connected

7.1.4Connecting ports to statecharts

Ports can also be connected to statecharts. In that case messages received in a port are forwarded to a connected statechart, where they are processed as signal events. Using such a connection, you can react to the message arrival in the statechart. For detailed information on statecharts see Chapter 9, “Statecharts”.

You can connect a statechart to:

A public port,

A private port.

You cannot connect active object’s statechart to a port of its encapsulated object.

This section describes the technique of establishing connections between ports and statecharts.

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

153

Chapter 7. Message passing

7.1.4.1 Connecting a public port to a statechart

You connect a public port to a statechart if you need to react in the statechart to messages received in the port from other active objects.

To connect a public port to a statechart

1.Click the Connector toolbar button, click the port, and then click the statechart, or Choose Draw|Structure|Connector from the main menu, click the port, and then click the statechart.

The connector linking the port and the statechart appears.

Public

Statechart

port

 

Figure 73. A public port connected to a statechart

7.1.4.2 Connecting a private port to a statechart

You can connect a private port to a statechart if you need to react in the statechart to messages received in the port. It is commonly used when the private port is connected to a port of an encapsulated object and you want to react in the statechart to messages received from the encapsulated object.

To connect a private port to a statechart

1.Click the Connector toolbar button, click the port, and then click the statechart, or Choose Draw|Structure|Connector from the main menu, click the port, and then click the statechart.

The connector linking the port and the statechart appears.

154

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

AnyLogic V User’s Manual

Private

Statechart

port

 

Figure 74. A private port connected to a statechart

7.1.5Message routing rules

Message processing at a port depends on the direction the message is going and on the type of a port.

Public ports act as relay ports, forwarding messages depending on the direction the message is going. If the message arrives at a public port from inside the active object, it is forwarded along all external connections of the port. Otherwise, if it is received from outside, it is forwarded along all port connections inside the active object.

A message received at a private port is forwarded only to connected statecharts.

Message routing rules are illustrated in Figure 75.

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

155

Chapter 7. Message passing

Legend: – message origin

– message arrival

– message is placed into a port queue

Figure 75. Messages routing rules

Note that in case a port has a queue and is also connected to inner ports and statecharts, then an incoming message is both placed in the port queue and is sent to connected ports and statecharts. Therefore, you have to take care of processing all of them.

7.1.6Sending and processing messages

7.1.6.1 Sending messages

To send a message, you should simply call the method send() of a port, providing the instance of a message class as a parameter. Defining your own message classes is described in section 7.2, ”Messages”. If you need to just signal an object, you can send an instance of class Object that does not carry any data by calling the method send() with omitted parameter.

For example, you can type the following line of code in the Startup code code section of an active object to send a message of Message type via its portA port at the model startup.

156

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

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