Skip to content

Commit

Permalink
Optimization: any? breed-on ... -> _anybreedon
Browse files Browse the repository at this point in the history
  • Loading branch information
LaCuneta committed Apr 8, 2024
1 parent 8865f89 commit d52a070
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
21 changes: 20 additions & 1 deletion compiler/shared/src/main/scala/Optimizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import

import
org.nlogo.core.{ prim, AstTransformer, ProcedureDefinition, ReporterApp, Statement, NetLogoCore, CommandBlock, ReporterBlock },
prim.{ _any, _const, _count, _createorderedturtles, _createturtles, _equal, _fd, _greaterthan, _hatch, _lessthan,
prim.{ _any, _breedon, _const, _count, _createorderedturtles, _createturtles, _equal, _fd, _greaterthan, _hatch, _lessthan,
_neighbors, _neighbors4, _not, _notequal, _observervariable, _of, _oneof, _other, _patchat, _patches, _patchvariable,
_procedurevariable, _random, _sprout, _sum, _turtleson, _with }

Expand Down Expand Up @@ -351,6 +351,24 @@ object Optimizer {
}
}

class _anybreedon(val breedName: String) extends Reporter {
override def syntax: Syntax =
Syntax.reporterSyntax(right = List(Syntax.AgentsetType | Syntax.AgentType), ret = Syntax.BooleanType)
}

// _any(_breedon) => _anybreedon
object AnyBreedOnTransformer extends AstTransformer {
override def visitReporterApp(ra: ReporterApp): ReporterApp = {
ra match {
case ReporterApp(_: _any, Seq(ReporterApp(to: _breedon, breedonArgs, _)), _) =>
val r = new _anybreedon(to.breedName)
r.token = to.token
ra.copy(reporter = r, args = breedonArgs)
case _ => super.visitReporterApp(ra)
}
}
}

class NeighborTransformer extends AstTransformer {
def visitNeighbor(ra: ReporterApp, check: Reporter => Boolean, make: String => Reporter): ReporterApp = {
ra match {
Expand Down Expand Up @@ -514,6 +532,7 @@ object Optimizer {
AnyWith4Transformer .visitProcedureDefinition andThen
AnyWith5Transformer .visitProcedureDefinition andThen
AnyTurtlesOnTransformer .visitProcedureDefinition andThen
AnyBreedOnTransformer .visitProcedureDefinition andThen
OptimizeCountTransformer .visitProcedureDefinition andThen
RandomConstTransformer .visitProcedureDefinition
)(pd)
Expand Down
1 change: 1 addition & 0 deletions compiler/shared/src/main/scala/Prims.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ trait ReporterPrims extends PrimUtils {
case b: prim.etc._breedat => s"SelfManager.self().breedAt(${jsString(b.breedName)}, ${args.get(0)}, ${args.get(1)})"
case b: prim.etc._breedhere => s"SelfManager.self().breedHere(${jsString(b.breedName)})"
case b: prim._breedon => s"PrimChecks.agentset.breedOn(${jsString(b.breedName)}, ${args.makeCheckedOp(0)})"
case b: Optimizer._anybreedon => s"PrimChecks.agentset.anyBreedOn(${jsString(b.breedName)}, ${args.makeCheckedOp(0)})"
case b: prim.etc._isbreed => s"NLType.checks.isBreed(${jsString(b.breedName)}, ${args.get(0)})"

// List prims
Expand Down
8 changes: 8 additions & 0 deletions engine/src/main/coffee/engine/core/patch.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ module.exports =
breedHereArray: (breedName) ->
filter((turtle) -> turtle.getBreedName() is breedName)(@_turtles)

# (String) => Boolean
anyBreedHere: (breedName) ->
for turtle in @_turtles
if turtle.getBreedName() is breedName
return true

false

# (Number, Number) => TurtleSet
turtlesAt: (dx, dy) ->
@patchAt(dx, dy).turtlesHere()
Expand Down
4 changes: 4 additions & 0 deletions engine/src/main/coffee/engine/core/turtle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ module.exports =
breedHereArray: (breedName) ->
@getPatchHere().breedHereArray(breedName)

# (String) => Boolean
anyBreedHere: (breedName) ->
@getPatchHere().anyBreedHere(breedName)

# (Number, String) => TurtleSet
hatch: (n, breedName) ->
num = if n >= 0 then n else 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class AgentSetChecks
else
@prims.breedOnAgent(breedName, target)

# (String, Patch | Turtle | PatchSet | TurtleSet) => Boolean
anyBreedOn: (breedName, target) ->
if checks.isAgentSet(target)
@prims.anyBreedOnAgentSet(breedName, target)
else
@prims.anyBreedOnAgent(breedName, target)

# (AgentSet[T]) => Number
count: (agentset) ->
agentset.size()
Expand Down
22 changes: 22 additions & 0 deletions engine/src/main/coffee/engine/prim/prims.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,33 @@ module.exports =
turtles = agent.breedHereArray(breedName)
new TurtleSet(turtles, @_world)

# (String, Turtle | Patch) -> TurtleSet
anyBreedOnAgent: (breedName, agent) ->
agent.anyBreedHere(breedName)

# (String, AgentSet) -> TurtleSet
breedOnAgentSet: (breedName, agents) ->
turtles = flatMap((p) -> p.breedHereArray(breedName))(agents.toArray())
new TurtleSet(turtles, @_world)

# (String, TurtleSet | PatchSet) -> Boolean
anyBreedOnAgentSet: (breedName, agents) ->
if checks.isPatchSet(agents)
for patch in agents.toArray()
if patch.anyBreedHere(breedName)
return true

else
seenPatches = []
for turtle in agents.toArray()
patch = turtle.getPatchHere()
if not seenPatches.includes(patch)
seenPatches.push(patch)
if patch.anyBreedHere(breedName)
return true

false

# (Any, String) => Boolean
booleanCheck: (b, primName) ->
if checks.isBoolean(b)
Expand Down

0 comments on commit d52a070

Please sign in to comment.