Skip to content

Instantly share code, notes, and snippets.

@SethTisue
Created November 20, 2012 18:06
Show Gist options
  • Save SethTisue/4119699 to your computer and use it in GitHub Desktop.
Save SethTisue/4119699 to your computer and use it in GitHub Desktop.
NetLogo: old and new plotting architecture
// The idea: instead of PlotListener with lots of methods,
// we have a PlotAction trait with lots of subclasses.
// what can we do with plot actions?
// - we can respond to them by mutating a plot on-screen
// but also:
// - we can write tests (are the right actions generated?)
// - we can record them for later replay (in e.g. recordable model runs)
// - we can broadcast them to a remote client
// old code (NetLogo 5.0)
package prim.plot
class _plotxy extends PlotCommand(...) {
override def perform(...) {
...
currentPen(context).plot(x, y)
...
}
}
package plot
def plot(x: Double, y: Double) {
this.x = x
points += PlotPoint(x, y, isDown, color)
plot.plotListener.foreach(_.plot(x, y))
}
trait PlotListener {
def plot(x: Double, y: Double)
...lots more methods...
}
package hubnet.server
class Server(...) extends PlotListener {
...
for(plot <- plots)
plot.setPlotListener(this)
...
def plot(x: Double, y: Double) {broadcastToClients(new HubNetPlotPoint(x, y))
...
}
class HubNetPlotPoint(...)
// new code (NetLogo 5.1)
package prim.plot
class _plotxy extends ... {
override def perform(...) {
plotManager.run(
PlotAction.PlotXY(
currentPlot(context),
currentPen(context),
x, y)
...
}
}
package plot
sealed trait PlotAction
case class PlotXY(plot: Plot, pen: PlotPen, x: Double, y: Double)
extends PlotAction
...lots more actions...
trait PlotRunner {
def run(action: PlotAction)
}
trait LocalPlotRunner {
def run(action: PlotAction) = action match {
...
case PlotAction.PlotXY(plot, pen, x, y) =>
plot.plot(pen, x, y)
...
}
}
package hubnet.server
class Server(...) {
def run(action: PlotAction) {
broadcastToClients(action)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment