diff --git a/README.md b/README.md index 9b216be..42a7c1d 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ A much shorter version of this documentation, that can be useful as a cheat shee [Path and Distance](#path-and-distance) -- [turtles-in-radius, turtles-in-undirected-radius, turtles-in-out-radius, turtles-in-in-radius](#turtles-in-radius-turtles-in-undirected-radius-turtles-in-out-radius-turtles-in-in-radius), [distance-to, weighted-distance-to](#distance-to-weighted-distance-to), [path-to, turtles-on-path-to, weighted-path-to, turtles-on-weighted-path-to](#path-to-turtles-on-path-to-weighted-path-to-turtles-on-weighted-path-to), [mean-path-length, mean-weighted-path-length](#mean-path-length-mean-weighted-path-length) +- [turtles-in-radius, turtles-in-reverse-radius](#turtles-in-radius-turtles-in-reverse-radius), [distance-to, weighted-distance-to](#distance-to-weighted-distance-to), [path-to, turtles-on-path-to, weighted-path-to, turtles-on-weighted-path-to](#path-to-turtles-on-path-to-weighted-path-to-turtles-on-weighted-path-to), [mean-path-length, mean-weighted-path-length](#mean-path-length-mean-weighted-path-length) [Centrality](#centrality) @@ -272,19 +272,17 @@ observer: [[(turtle 0) (turtle 1)] [(link 0 1)]] ### Path and Distance -#### turtles-in-radius, turtles-in-undirected-radius, turtles-in-out-radius, turtles-in-in-radius +#### turtles-in-radius, turtles-in-reverse-radius ![turtle][turtle] `nw:turtles-in-radius` _radius_ -![turtle][turtle] `nw:turtles-in-undirected-radius` _radius_ +![turtle][turtle] `nw:turtles-in-reverse-radius` _radius_ -![turtle][turtle] `nw:turtles-in-out-radius` _radius_ +Returns the set of turtles within the given distance (number of links followed) of the calling turtle in the current context. Both forms include the calling turtle, whom you can exclude with `other` if need be. -![turtle][turtle] `nw:turtles-in-in-radius` _radius_ +The `turtles-in-radius` form will follow both undirected links and directed **out** links. The `turtles-in-reverse-radius` form will follow both undirected links and directed **in** links. You can think of `turtles-in-radius` as "turtles **who I can get to** in _radius_ steps" and of `turtles-in-reverse-radius` as "turtles **who can get to me** in _radius_ steps". -Returns the set of turtles within the given distance (number of links followed) of the calling turtle in the current current. - -The `turtles-in-radius` form will follow both undirected links and directed out links. The `turtles-in-undirected-radius` form works only with undirected links. The other two forms work with directed links; `out` or `in` specifies whether links are followed in the normal direction (`out`), or in reverse (`in`). +If you want the primitive to follow only undirected links or only directed links, you can do it by setting the context appropriately. For example: `nw:set-context turtles undir-links` (assuming `undir-links` is an undirected link breed) or `nw:set-context turtles dir-links` (assuming `dir-links` is a directed link breed). ##### Example: diff --git a/src/main/org/nlogo/extensions/nw/NetworkExtension.scala b/src/main/org/nlogo/extensions/nw/NetworkExtension.scala index b2d8b14..4953834 100644 --- a/src/main/org/nlogo/extensions/nw/NetworkExtension.scala +++ b/src/main/org/nlogo/extensions/nw/NetworkExtension.scala @@ -42,9 +42,7 @@ class NetworkExtension extends api.DefaultClassManager { add("get-context", new prim.GetContext(getGraphContext)) add("turtles-in-radius", new org.nlogo.extensions.nw.prim.TurtlesInRadius(getGraphContext)) - add("turtles-in-undirected-radius", new org.nlogo.extensions.nw.prim.TurtlesInUndirectedRadius(getGraphContext)) - add("turtles-in-out-radius", new org.nlogo.extensions.nw.prim.TurtlesInOutRadius(getGraphContext)) - add("turtles-in-in-radius", new org.nlogo.extensions.nw.prim.TurtlesInInRadius(getGraphContext)) + add("turtles-in-reverse-radius", new org.nlogo.extensions.nw.prim.TurtlesInReverseRadius(getGraphContext)) add("mean-path-length", new prim.MeanPathLength(getGraphContext)) add("mean-weighted-path-length", new prim.jung.MeanWeightedPathLength(getGraphContext)) diff --git a/src/main/org/nlogo/extensions/nw/prim/InRadius.scala b/src/main/org/nlogo/extensions/nw/prim/InRadius.scala index 334ce5d..8cd7022 100644 --- a/src/main/org/nlogo/extensions/nw/prim/InRadius.scala +++ b/src/main/org/nlogo/extensions/nw/prim/InRadius.scala @@ -13,7 +13,7 @@ import org.nlogo.agent.AgentSet trait InRadiusPrim extends api.DefaultReporter { val getGraphContext: api.World => GraphContext - val followUnLinks: Boolean + val followUnLinks: Boolean = true val followInLinks: Boolean val followOutLinks: Boolean override def getSyntax = reporterSyntax( @@ -33,31 +33,13 @@ trait InRadiusPrim extends api.DefaultReporter { class TurtlesInRadius( override val getGraphContext: api.World => GraphContext) extends InRadiusPrim { - override val followUnLinks = true override val followInLinks = false override val followOutLinks = true } -class TurtlesInUndirectedRadius( +class TurtlesInReverseRadius( override val getGraphContext: api.World => GraphContext) extends InRadiusPrim { - override val followUnLinks = true - override val followInLinks = false - override val followOutLinks = false -} - -class TurtlesInInRadius( - override val getGraphContext: api.World => GraphContext) - extends InRadiusPrim { - override val followUnLinks = false override val followInLinks = true override val followOutLinks = false } - -class TurtlesInOutRadius( - override val getGraphContext: api.World => GraphContext) - extends InRadiusPrim { - override val followUnLinks = false - override val followInLinks = false - override val followOutLinks = true -} diff --git a/tests.txt b/tests.txt index 085b748..7500b12 100644 --- a/tests.txt +++ b/tests.txt @@ -204,13 +204,17 @@ mixed-turtles-in-radius O> ask turtle 3 [ create-directed-link-to turtle 11 ] O> ask turtle 3 [ create-directed-link-from turtle 12 ] sort [who] of [nw:turtles-in-radius 1] of turtle 0 => [0 1 2] - sort [who] of [nw:turtles-in-undirected-radius 1] of turtle 0 => [0 1] - sort [who] of [nw:turtles-in-out-radius 1] of turtle 0 => [0 2] - sort [who] of [nw:turtles-in-in-radius 1] of turtle 0 => [0 3] + sort [who] of [nw:turtles-in-reverse-radius 1] of turtle 0 => [0 1 3] sort [who] of [nw:turtles-in-radius 2] of turtle 0 => [0 1 2 4 5 7 8] - sort [who] of [nw:turtles-in-undirected-radius 2] of turtle 0 => [0 1 4] - sort [who] of [nw:turtles-in-out-radius 2] of turtle 0 => [0 2 8] - sort [who] of [nw:turtles-in-in-radius 2] of turtle 0 => [0 3 12] + sort [who] of [nw:turtles-in-reverse-radius 2] of turtle 0 => [0 1 3 4 6 10 12] + O> nw:set-context turtles undirected-links + sort [who] of [nw:turtles-in-radius 1] of turtle 0 => [0 1] + sort [who] of [nw:turtles-in-radius 2] of turtle 0 => [0 1 4] + O> nw:set-context turtles directed-links + sort [who] of [nw:turtles-in-radius 1] of turtle 0 => [0 2] + sort [who] of [nw:turtles-in-radius 2] of turtle 0 => [0 2 8] + sort [who] of [nw:turtles-in-reverse-radius 1] of turtle 0 => [0 3] + sort [who] of [nw:turtles-in-reverse-radius 2] of turtle 0 => [0 3 12] ### turtles-in-out-radius, turtles-in-in-radius @@ -218,15 +222,15 @@ turtles-in-out-radius extensions [nw] O> crt 2 O> ask turtle 0 [ create-link-to turtle 1 ] - sort [ who ] of [ nw:turtles-in-out-radius 1 ] of turtle 0 => [0 1] - sort [ who ] of [ nw:turtles-in-out-radius 1 ] of turtle 1 => [1] + sort [ who ] of [ nw:turtles-in-radius 1 ] of turtle 0 => [0 1] + sort [ who ] of [ nw:turtles-in-radius 1 ] of turtle 1 => [1] turtles-in-in-radius extensions [nw] O> crt 2 O> ask turtle 0 [ create-link-to turtle 1 ] - sort [who] of [ nw:turtles-in-in-radius 1 ] of turtle 0 => [0] - sort [who] of [ nw:turtles-in-in-radius 1 ] of turtle 1 => [0 1] + sort [who] of [ nw:turtles-in-reverse-radius 1 ] of turtle 0 => [0] + sort [who] of [ nw:turtles-in-reverse-radius 1 ] of turtle 1 => [0 1] ### distance-to