nw:get-context
nw:get-context
Reports the content of the current graph context as a list containing two agentsets: the agentset of turtles that are part of the context and the agentset of links that are part of the context.
Let’s say we start with a blank slate and the default context consisting of turtles
and links
, nw:get-context
will report a list the special turtles
and links
breed agentsets:
observer> clear-all
observer> show nw:get-context
observer: [turtles links]
If we add some turtles and links to our context, we’ll still see the same thing, even though turtles
and links
have internally grown:
observer> crt 2 [ create-links-with other turtles ]
observer> show nw:get-context
observer: [turtles links]
If you had set your context to normal agentsets instead (built with turtle-set
, link-set
or with
) here is what you would see:
observer> clear-all
observer> nw:set-context turtle-set turtles link-set links
observer> show nw:get-context
observer: [(agentset, 0 turtles) (agentset, 0 links)]
If you then create new turtles and links, they are not added to the context because normal agentsets don’t grow (see Special agentsets vs normal agentsets):
observer> crt 2 [ create-links-with other turtles ]
observer> show nw:get-context
observer: [(agentset, 0 turtles) (agentset, 0 links)]
But if you construct new agentsets and set the context to them, your new agents will be there:
observer> nw:set-context turtle-set turtles link-set links
observer> show nw:get-context
observer: [(agentset, 2 turtles) (agentset, 1 link)]
If you want to see the actual content of your context, it is easy to turn your agentsets into lists that can be nicely displayed. Just use a combination of map
and sort
:
observer> show map sort nw:get-context
observer: [[(turtle 0) (turtle 1)] [(link 0 1)]]
Finally, you can use nw:get-context
to store a context that you eventually want to restore:
extensions [ nw ]
to store-and-restore-context
clear-all
crt 2 [
set color red
create-links-with other turtles with [ color = red ] [
set color yellow
]
]
crt 2 [
set color blue
create-links-with other turtles with [ color = blue ] [
set color green
]
]
nw:set-context turtles with [ color = red ] links with [ color = yellow ]
show map sort nw:get-context
let old-turtles item 0 nw:get-context
let old-links item 1 nw:get-context
nw:set-context turtles with [ color = blue ] links with [ color = green ]
show map sort nw:get-context
nw:set-context old-turtles old-links
show map sort nw:get-context
end
Here is the result:
observer> store-and-restore-context
observer: [[(turtle 0) (turtle 1)] [(link 0 1)]]
observer: [[(turtle 2) (turtle 3)] [(link 2 3)]]
observer: [[(turtle 0) (turtle 1)] [(link 0 1)]]
Take me to the full Networks Extension Dictionary