Create network by code
If needed, you can create a network programmatically. Please note that you can do it only on model startup, and the network cannot be modified later.
In this example we create a network that consists of a point node
and a rectangular node, which are connected by a path. Agents enter the
network in the point node. They then move along the path to the to the rectangular node. Inside the rectangular node, agents move to the attractor.
To create a network programmatically, provide Java code in the On
startup action of the Main agent (Agent actions properties section). In our demo model the code is the following:
// Create an attractor
Attractor a = new
Attractor( 25.0, 25.0,
// Create a rectangular node
rn = new
RectangularNode( this, SHAPE_DRAW_2D3D, true,
300.0, 250.0, 0.0, 100.0, 90.0, 0.0,
null, dodgerBlue, 2.0, LINE_STYLE_DASHED,
// Create point nodes
pn = new
SHAPE_DRAW_2D3D, true, 50.0, 200.0, 0.0, 1.0, dodgerBlue );
// Create segments
MarkupSegmentLine ms1 = new
50.0, 200.0, 0.0, 350.0, 200.0, 0.0 );
MarkupSegmentLine ms2 = new
350.0, 200.0, 0.0, 350.0, 250.0, 0.0 );
// Create paths
Path path = new
SHAPE_DRAW_2D3D, true, true,
PATH_DASHEDLINE, dodgerBlue, 2.0, pn, rn, ms1, ms2 );
// Create a network
Network n = new
"myNetwork", SHAPE_DRAW_2D3D, 0, true, true, pn, rn, path );
// Add point
nodes/paths to presentation
Creating an attractor
First, we create an attractor
with the help of the the Attractor
constructor and provide the attractor's X-Y coordinates and
orientation angle (in radians) as arguments.
Creating a rectangular node
Next, we create a rectangular
node by providing the following arguments to the RectangularNode constructor:
- Agent owner
- the reference to the agent where the point node will be added. We
keyword to refer to the current agent, Main.
- ShapeDrawMode drawMode
- the constant that defines whether the point node will be
2D (SHAPE_DRAW_2D), in
or both in 2D and 3D animation (SHAPE_DRAW_2D3D).
isPublic - sets the Public
property of the element.
- double x, double y, double z - the X, Y and Z
coordinates of the node's top-right corner.
- double width,
double height, double rotation - the node
width, height and rotation angle.
- Paint fillColor,
Paint lineColor - the
node's fill and outline colors. You can use one of the color constants.
We provide null as
the fill color.
- double lineWidth
- the width of the node outline. We set it to 2.0
- LineStyle lineStyle
- the constant that defines the style of the node outline. The outline
can be dashed (LINE_STYLE_DASHED),
or solid (LINE_STYLE_SOLID).
positionChoiceMode - the constant that defines the layout of agents inside the node. You can choose the layout to
be arranged (POSITION_CHOICE_ARRANGED),
or regulated by attractors (POSITION_CHOICE_BY_ATTRACTORS). We want to use an attractor in our model, so we provide the third
- Attractor... attractors
- attractor (or several attractors) to be put inside the node. Here we provide the
link to the attractor that we created earlier. This argument is used only when the previous argument is set to POSITION_CHOICE_BY_ATTRACTORS.
Creating point nodes
Now we create two point
nodes. We use the PointNode
constructor and provide the coordinates of the point node, the point's radius and the point color as its arguments.
Creating markup segments
Next, we create two linear markup segments, which we will later combine into a single two-line path. The MarkupSegmentLine constructor
accepts six arguments: X, Y, Z coordinates of the line's start
point followed by the end point's coordinates.
To create a path,
we use the Path constructor. For each
path we do the following:
- Set the common appearance properties (2D or 3D shape drawing mode,
color, line width and path drawing type). We want to draw a
path as a simple dashed line, so we pass PATH_DASHEDLINE as the drawingType argument.
- Pass true
as the bidirectional
argument so that the path is bidirectional (the movement in both directions is allowed).
- Provide the links to the source and target nodes (pn, rn).
- Provide the links to the linear markup segments that form the
path line (ms1 and
We now have all the elements that are needed to create
a network. In the Network
constructor, we provide the reference to the agent the network will be
added to (this), the
name of the network ("myNetwork"),
specify drawing mode (2D, 3D or both) and the network's Z-coordinate (which is
zero in our
case). We pass true value for isPublic
and visible arguments and
provide the references to nodes and paths that we created earlier (pn, pn1, rn, path1, path2):
Network n = new
"myNetwork", SHAPE_DRAW_2D3D, 0, true, true, pn, pn1,
rn, path1, path2 );
Adding elements into presentation
Finally, we add the created nodes and paths into the presentation
group to make them visible at runtime. To do this, we call the add()
function of the presentation group for each network element.