Skip to content

Commit

Permalink
reorganize in-radius primitives - close #94
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Oct 28, 2013
1 parent 40b8964 commit 5862ca4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:

Expand Down
4 changes: 1 addition & 3 deletions src/main/org/nlogo/extensions/nw/NetworkExtension.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
22 changes: 2 additions & 20 deletions src/main/org/nlogo/extensions/nw/prim/InRadius.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
24 changes: 14 additions & 10 deletions tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,33 @@ 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

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

Expand Down

0 comments on commit 5862ca4

Please sign in to comment.