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

imit_model / AnyLogic / UsersManual(AnyLogic)

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

AnyLogic V User’s Manual

ActiveObject

 

 

 

 

 

ClassA

 

ClassB

 

 

 

 

 

 

 

 

ActiveObject

 

 

 

 

ActiveObject

 

 

 

ClassB

 

 

 

 

 

 

 

 

 

 

 

 

ClassA

 

 

 

ClassA

 

 

 

ClassB

 

 

 

 

 

 

 

 

 

 

 

 

ClassC

 

ClassD

 

 

ClassC

 

ClassD

 

 

 

 

 

 

 

 

 

 

ActiveObject

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ClassA

 

ClassB

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A class generated

 

 

 

 

 

 

 

 

 

 

 

 

 

by AnyLogic (has

 

 

 

 

 

 

 

 

 

 

 

 

 

structure diagram)

 

 

 

ClassC

 

 

ClassD

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 17. Inheritance among active object classes

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

47

Chapter 2. Replicated objects

2. Replicated objects

AnyLogic supports object replication - very easy and convenient way of modeling regularly organized structures of objects of arbitrary size and topology, such as vector, mesh, torus, hypercube, chain, ring, etc. AnyLogic saves you from the boring process of manual creation of the specified number of objects and establishing connections between them as this approach is limited, tedious, and forms the system with a predefined and constant number of objects.

In AnyLogic you simply declare a replicated object and specify the number of instances as a parameter. A replicated object represents a collection of active objects of the same class. Object replication provides very economical representation of complex structures of objects with arbitrary interconnections.

Object replication enables you to:

Build scalable systems by creating replicated objects with the number of elements defined by a parameter of the model.

Create complex structures of objects with sophisticated topologies by establishing arbitrary interconnections between replicated objects either from AnyLogic UI or programmatically.

Model systems with dynamically changing structures by adding and removing objects to the group of replicated objects at runtime.

Do not confuse object replication with model replications. Model replication is a single model run and has nothing to do with object replication.

2.1 Creating a replicated object

In AnyLogic you can create a collection of objects of the same type simply by creating a replicated object. First, encapsulate an object you want to create a structure from, and then declare it replicated.

48

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

AnyLogic V User’s Manual

To declare a replicated object

1.On the structure diagram, click the encapsulated object you want to make replicated.

2.On the Replication page of the Properties window, specify the number of individual elements of the replicated object (the replication factor) in the Number of objects edit box.

The name of the replicated object is displayed on the structure diagram with the trailing asterisk.

2.1.1Replication factor parameterization

You can specify any expression evaluating to integer or double type as a number of elements of the encapsulated object. Thus, you can parameterize the number of object instances in a replicated object. You may need this to facilitate modifying the number of objects of several replicated objects. See Chapter 3, “Parameters” for more information on parameters.

If you use a parameter as a replication factor of an encapsulated object, AnyLogic creates a number of instances equal to the initial value of the parameter. However, AnyLogic does not automatically adjust the number of instances in case you change the parameter at runtime. If such behavior is needed, you should use the handler method onChange_myParam() to take care of creation and destruction of instances (see Chapter 15, “Creating a model with dynamically changing structure”).

2.2Accessing and modifying a replicated object at runtime

This section describes accessing individual elements of a replicated object from code and creation/removing objects to/from a group of replicated objects at runtime. See section 15.1, “Manual creation and destruction of encapsulated objects” for general information about creating and destroying encapsulated objects at runtime.

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

49

Chapter 2. Replicated objects

2.2.1Accessing replicated objects from code

The number of active objects in a replicated object anObject can be retrieved by calling anObject.size().

In order to get any individual element from anObject vector of replicated objects, you can use anObject.item(index) method, which takes an index of the object in the vector, from 0 to anObject.size() – 1.

Individual elements of a replicated object can find out their indexes by calling getIndex().

2.2.2Adding/removing objects to/from a replicated object

To add a new object to an anObject replicated object, use the setup_anObject() method generated by AnyLogic. The method adds an object to the array of replicated objects and registers the object within the simulation framework. If there are any connectors to other objects designed in the structure diagram, those connections are established for the new object. The method takes two arguments: an object and an object identifier. The object identifier must be unique within the replicated object vector to distinguish objects in the Model Explorer at runtime. For the reasons of simplicity, you can use the hashCode() method, returning the unique integer value.

For example, write the following code to add c object to clients replicated object.

setup_clients(c, c.hashCode( ));

Use generated by AnyLogic method dispose_anObject() to remove the object from the anObject vector of objects.

Related method of ActiveObject

final void setReplication(int index) – the framework method. You can call it to place the element of a replicated object in the place specified by index parameter.

50

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

AnyLogic V User’s Manual

2.3 Connecting replicated objects

AnyLogic provides a convenient mechanism of creating structures of objects with sophisticated topologies. It supports arbitrary types of interconnections between individual elements of replicated objects and between a replicated object and other objects (probably, also replicated).

First, this section describes how to connect ports and variables of replicated objects graphically. Then the information on establishing arbitrary types of interconnections between replicated objects is given. As connection rules are the same for variables and ports, they are described generally for interface elements of replicated objects.

2.3.1 Connecting interface elements of replicated objects graphically

To connect interface elements of replicated objects

1.Drag the interface element of one object onto the interface element of another object, or

Click the Connector toolbar button, click the first interface element and then click the second interface element, or

Choose Draw|Structure|Connector from the main menu, click the first interface element and then click the second interface element.

The connector linking two interface elements appears (see Figure 18).

objectA*

objectB*

Figure 18. Interface elements of replicated objects connected

When you connect two replicated objects using a connector, you get configurations shown in Figure 19, i.e. each element in the client vector is connected with the server object.

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

51

Chapter 2. Replicated objects

System

client*

=

server

System

client

client

client

 

server

System

System

client*

client

=

server*

server

client

server

client

server

Figure 19. Connection of replicated objects

2.3.2Connecting replicated objects with other objects

A replicated object represents a collection of object copies. When you graphically connect an interface element of some encapsulated object to an interface element of a replicated object, it is connected to interface elements of all object copies of the replicated object (see Figure 19). In some cases you may want to connect an interface element of one object to the interface elements of the certain individual elements of the replicated object only. In AnyLogic you can establish arbitrary types of connections by adjusting connector properties.

To set a connection type between interface elements of a replicated object and another object

1.In the structure diagram, select the connector, linking the interface element of the replicated object with the interface element of the other object.

52

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

AnyLogic V User’s Manual

2.In the Properties window, specify individual elements of the replicated object to be connected to the interface element of the other object from the Connect from ‘interface element’ of drop-down list:

All objects – the default value. Interface elements of all individual elements of the objects replicated object are connected to the interface element of the other object.

First of objects – the interface element of the first individual element only is connected. Last of objects – the interface element of the last individual element only is connected.

Figure 20 illustrates all these connection types.

machines*

network

...

 

a)

 

...

 

b)

 

...

 

c)

 

Figure 20.

You may connect interface elements of two replicated objects as well. By default, interface elements of all object copies of one replicated object are connected to interface elements of all object copies of another replicated object (see Figure 19).

However, you can connect interface elements of certain object copies only. Therefore you need to specify the object copies of replicated objects you want to connect. This is done in

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

53

Chapter 2. Replicated objects

Connect from ‘interface element’ of and To ‘interface element’ of connector’s properties in the same way as described above for the connection of a replicated object with an encapsulated object.

Figure 21 illustrates some possible cases of connecting interface elements of two replicated objects.

Figure 21. Replicated objects connection cases

In the case two replicated objects have the same number of individual elements, you can connect the i-th element of one replicated object with the i-th element of another replicated object, choosing objects(i) from both drop-down lists – see c case in Figure 21.

54

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

AnyLogic V User’s Manual

2.3.3Connecting individual elements of a replicated object

You may specify arbitrary interconnections between individual object copies of a replicated object. You can connect the same or different interface elements of some individual elements of a replicated object. Connect them in the same manner as you connect individual elements of a replicated object with another object.

To connect individual elements of a replicated object

1.In the structure diagram, connect the required interface elements of the replicated object. You can connect an interface element to itself.

2.Select the created connector in the structure diagram.

3.In the Properties window, specify individual elements of the replicated object you want to connect for each connected interface element in Connect from ‘interface element’ of and To ‘interface element’ of drop-down lists.

Choose All objects, First of objects, or Last of objects to connect all individual elements, the first individual element, or the last individual element of the objects replicated object only.

You can connect interface elements of the same object copies in a vector of replicated objects by choosing objects(i) from both drop-down lists.

Also, you can connect interface elements of the adjacent object copies by choosing objects(i) and objects(i+1).

Some connection cases are shown in Figure 22.

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

55

Chapter 2. Replicated objects

Figure 22. Regular interconnections in a vector of replicated objects

Thus, in AnyLogic you can easily create a regularly structured system.

2.3.4Connecting replicated objects programmatically

Sometimes you may need to create a complex structure of objects with sophisticated topologies that cannot be established graphically. Or you may need to model a system with dynamically changing connections. Use the methods connect() and disconnect() to establish connections between replicated objects and their individual elements at runtime.

Use the connect()/disconnect() methods of the Port or VariableRef classes to connect/disconnect ports or variables of two objects. The methods take two arguments: two port objects, or two variable objects. The name of a port object is the name of the port in

56

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

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