Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
primitive: nw:get-context
Networks Extension Dictionary
  • No items found

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.

Networks Extension Dictionary: generate-wheel

Documentation for the generate-wheel primitive.

Networks Extension Dictionary: load

Documentation for the load primitive.


NetLogo is a programmable modeling environment for simulating natural and social phenomena. It was authored by Uri Wilensky in 1999 and has been in continuous development ever since at the Center for Connected Learning and Computer-Based Modeling.

Related Links
  • NetLogo Home
  • CCL Home
  • NetLogo Web
  • NetTango Web
  • NetLogo 3D
  • BehaviorSearch
  • Contact Us

Copyright © 1999-2025 Uri Wilensky and the Center for Connected Learning and Computer-Based Modeling at Northwestern University . All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License , or (at your option) any later version.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at netlogo-commercial-admin@ccl.northwestern.edu .

For more information, visit the NetLogo website .