org.nlogo.headless
Class HeadlessWorkspace

java.lang.Object
  extended by org.nlogo.workspace.AbstractWorkspace
      extended by org.nlogo.headless.HeadlessWorkspace
All Implemented Interfaces:
Evaluator, org.nlogo.api.CompilerServices, HubNetWorkspaceInterface, ImporterUser, RandomServices, ViewSettings, WorldResizer, JobManagerOwner, Workspace, Controllable, WorldLoaderInterface

public class HeadlessWorkspace
extends AbstractWorkspace
implements Controllable, WorldLoaderInterface, ViewSettings

The primary class for headless (no GUI) operation of NetLogo.

You may create more than one HeadlessWorkspace object. Multiple instances can operate separately and independently. (Behind the scenes, this is supported by creating a separate thread for each instance.)

When you are done using a HeadlessWorkspace, you should call its dispose() method. This will shut down the thread associated with the workspace and allow resources to be freed.

See the "Controlling" section of the NetLogo User Manual for example code.


Nested Class Summary
 
Nested classes/interfaces inherited from class org.nlogo.workspace.AbstractWorkspace
AbstractWorkspace.HubNetManagerFactory
 
Nested classes/interfaces inherited from interface org.nlogo.nvm.Workspace
Workspace.BehaviorSpaceFormat, Workspace.OutputDestination, Workspace.UpdateMode
 
Field Summary
 LogoException lastLogoException
          Internal use only.
 boolean modelOpened
          Has a model been opened in this workspace?
 StringBuilder outputAreaBuffer
           
static String TEST_DECLARATIONS
          Internal use only.
 
Fields inherited from class org.nlogo.workspace.AbstractWorkspace
codeBits, DEFAULT_PREVIEW_COMMANDS, jobManager, previewCommands, world
 
Constructor Summary
HeadlessWorkspace(World world, CompilerInterface compiler, RendererInterface renderer, org.nlogo.api.AggregateManagerInterface aggregateManager)
          Internal use only.
 
Method Summary
 org.nlogo.api.AggregateManagerInterface aggregateManager()
           
 int calculateHeight(int worldHeight, double patchSize)
           
 int calculateWidth(int worldWidth, double patchSize)
           
 void changeTopology(boolean wrapX, boolean wrapY)
           
 void clearDrawing()
           
 void clearLastLogoException()
          Internal use only.
 void clearOutput()
          Internal use only.
 void clearTurtles()
           
 void command(String source)
          Runs NetLogo commands and waits for them to complete.
 CompilerInterface compiler()
           
 double computePatchSize(int width, int numPatches)
           
 void createPatchesNotify()
          Internal use only.
 void deleteLogFiles()
          Internal use only.
 boolean drawSpotlight()
           
 void exportDrawing(String filename, String format)
           
 void exportDrawingToCSV(PrintWriter writer)
           
 void exportInterface(String filename)
          Not implemented.
 void exportOutput(String filename)
           
 void exportOutputAreaToCSV(PrintWriter writer)
           
 BufferedImage exportView()
          Get a snapshot of the 2D view.
 void exportView(String filename, String format)
           
 int fontSize()
           
 void fontSize(int fontSize)
           
 BufferedImage getAndCreateDrawing()
           
 void getGraphics(BufferedImage image)
          Get a snapshot of the 2D view, using an existing BufferedImage object.
 int getHeight()
           
 int getMinimumWidth()
           
 int getWidth()
           
 void halt()
          Halts all running NetLogo code in this workspace.
 void importDrawing(File file)
           
 void initForTesting(int worldSize)
          Internal use only.
 void initForTesting(int minPxcor, int maxPxcor, int minPycor, int maxPycor)
           
 void initForTesting(int minPxcor, int maxPxcor, int minPycor, int maxPycor, String source)
          Internal use only.
 void initForTesting(int worldSize, String modelString)
          Internal use only.
 void initForTesting(WorldDimensions d)
          Internal use only.
 void initForTesting(WorldDimensions d, String source)
          Internal use only.
 int insetWidth()
           
 void inspectAgent(Agent agent, double radius)
          Internal use only.
 void inspectAgent(Class<? extends Agent> agentClass, Agent agent, double radius)
          Internal use only.
 boolean isCompilerTestingMode()
          Internal use only.
 boolean isHeadless()
          Internal use only.
 LogoException lastLogoException()
          Internal use only.
 void magicOpen(String name)
          Internal use only.
static HeadlessWorkspace newInstance()
          Makes a new instance of NetLogo capable of running a model "headless", with no GUI.
static HeadlessWorkspace newInstance(Class<? extends HeadlessWorkspace> subclass)
          If you derive your own subclass of HeadlessWorkspace, you can use this method to instantiate it.
static org.nlogo.nvm.LabInterface newLab()
           
 void open(String path)
          Opens a model stored in a file.
 void openFromSource(String source)
          Opens a model stored in a string.
 void openIndex()
          Internal use only.
 void openNext()
          Internal use only.
 void openPrevious()
          Internal use only.
 void openString(String modelContents)
          Opens a model stored in a string
 void ownerFinished(JobOwner owner)
          Internal use only.
 void paint(Graphics2D g)
          Internal use only.
 double patchSize()
           
 void patchSize(double patchSize)
           
 void perhapsUpdateDisplay()
          Internal use only.
 void periodicUpdate()
          Internal use only.
 Perspective perspective()
           
 RendererInterface renderer()
           
 boolean renderPerspective()
           
 Object report(String source)
          Runs a NetLogo reporter.
 void requestDisplayUpdate(boolean force)
          Internal use only.
 void resizeView()
           
 void runtimeError(JobOwner owner, Context context, Instruction instruction, Exception ex)
          Internal use only.
 void setCompilerTestingMode(boolean testing)
          Internal use only.
 void setDimensions(WorldDimensions d)
          Kills all turtles, clears all patch variables, and makes a new patch grid.
 void setDimensions(WorldDimensions d, double patchSize)
           
 void setImporterErrorHandler(Importer.ErrorHandler importErrorHandler)
          Internal use only.
 void setSize(int x, int y)
           
 boolean showTickCounter()
           
 void showTickCounter(boolean visible)
           
 void startLogging(String properties)
          Internal use only.
 String tickCounterLabel()
           
 void tickCounterLabel(String label)
           
 void updateDisplay(boolean haveWorldLockAlready)
          Internal use only.
 void updateMode(Workspace.UpdateMode updateMode)
           
 double viewHeight()
           
 double viewOffsetX()
           
 double viewOffsetY()
           
 double viewWidth()
           
 void waitFor(CommandRunnable runnable)
          Internal use only.
 void waitForQueuedEvents()
          Internal use only.
<T> T
waitForResult(ReporterRunnable<T> runnable)
          Internal use only.
 void writeGraphicsData(PrintWriter writer)
           
 void zipLogFiles(String filename)
          Internal use only.
 
Methods inherited from class org.nlogo.workspace.AbstractWorkspace
addJobFromJobThread, attachModelDir, autoConvert, auxRNG, behaviorSpaceRunNumber, behaviorSpaceRunNumber, checkCommandSyntax, checkReporterSyntax, clearAll, compileCommands, compileCommands, compileForRun, compileReporter, computerHubNetRunning, computerHubNetRunning, convertToNormal, currentPlot, dispose, evaluateCommands, evaluateCommands, evaluateCommands, evaluateReporter, evaluateReporter, exportAllPlots, exportBehaviors, exportPlot, exportWorld, exportWorld, fileManager, findProcedurePositions, forceSaveAs, getExtensionManager, getHubNetManager, getIsApplet, getModelDir, getModelFileName, getModelPath, getModelType, getPlot, getProcedures, getPropertiesInterface, getSource, getTokenAtPosition, guessExportName, hubnetManager, importDrawing, importExtensionData, importWorld, init, isApp, isApp, isApplet, isApplet, isConstant, isExtensionName, isValidIdentifier, joinForeverButtons, lastRunTimes, loadWorld, mainRNG, makeThunk, modelNameForDisplay, outputObject, plotManager, readFromString, readNumberFromString, runCompiledCommands, runCompiledReporter, runCompiledReporter, setModelPath, setOutputAreaContents, setProcedures, tokenizeForColorization, updateMode, warningMessage, world
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

modelOpened

public boolean modelOpened
Has a model been opened in this workspace?


outputAreaBuffer

public final StringBuilder outputAreaBuffer

TEST_DECLARATIONS

public static final String TEST_DECLARATIONS
Internal use only.

See Also:
Constant Field Values

lastLogoException

public LogoException lastLogoException
Internal use only.

Constructor Detail

HeadlessWorkspace

public HeadlessWorkspace(World world,
                         CompilerInterface compiler,
                         RendererInterface renderer,
                         org.nlogo.api.AggregateManagerInterface aggregateManager)
Internal use only. Use newInstance() instead.

Method Detail

newInstance

public static HeadlessWorkspace newInstance()
Makes a new instance of NetLogo capable of running a model "headless", with no GUI.


newInstance

public static HeadlessWorkspace newInstance(Class<? extends HeadlessWorkspace> subclass)
If you derive your own subclass of HeadlessWorkspace, you can use this method to instantiate it.


newLab

public static org.nlogo.nvm.LabInterface newLab()

aggregateManager

public org.nlogo.api.AggregateManagerInterface aggregateManager()
Specified by:
aggregateManager in interface Workspace

renderer

public RendererInterface renderer()

compiler

public CompilerInterface compiler()
Specified by:
compiler in interface Workspace
Specified by:
compiler in class AbstractWorkspace

isHeadless

public boolean isHeadless()
Internal use only.

Specified by:
isHeadless in interface ViewSettings
Specified by:
isHeadless in interface Workspace
Specified by:
isHeadless in class AbstractWorkspace

setCompilerTestingMode

public void setCompilerTestingMode(boolean testing)
Internal use only.


isCompilerTestingMode

public boolean isCompilerTestingMode()
Internal use only.

Overrides:
isCompilerTestingMode in class AbstractWorkspace

waitFor

public void waitFor(CommandRunnable runnable)
             throws LogoException
Internal use only.

Specified by:
waitFor in interface Workspace
Throws:
LogoException

waitForResult

public <T> T waitForResult(ReporterRunnable<T> runnable)
                throws LogoException
Internal use only.

Specified by:
waitForResult in interface Workspace
Throws:
LogoException

waitForQueuedEvents

public void waitForQueuedEvents()
Internal use only.

Specified by:
waitForQueuedEvents in interface Workspace

initForTesting

public void initForTesting(int worldSize)
                    throws CompilerException
Internal use only.

Throws:
CompilerException

initForTesting

public void initForTesting(int worldSize,
                           String modelString)
                    throws CompilerException
Internal use only.

Throws:
CompilerException

initForTesting

public void initForTesting(int minPxcor,
                           int maxPxcor,
                           int minPycor,
                           int maxPycor,
                           String source)
                    throws CompilerException
Internal use only.

Throws:
CompilerException

initForTesting

public void initForTesting(WorldDimensions d,
                           String source)
                    throws CompilerException
Internal use only.

Throws:
CompilerException

initForTesting

public void initForTesting(int minPxcor,
                           int maxPxcor,
                           int minPycor,
                           int maxPycor)

initForTesting

public void initForTesting(WorldDimensions d)
Internal use only.


setDimensions

public void setDimensions(WorldDimensions d)
Kills all turtles, clears all patch variables, and makes a new patch grid.

Specified by:
setDimensions in interface WorldResizer

setDimensions

public void setDimensions(WorldDimensions d,
                          double patchSize)
Specified by:
setDimensions in interface WorldResizer
Specified by:
setDimensions in interface WorldLoaderInterface

getMinimumWidth

public int getMinimumWidth()
Specified by:
getMinimumWidth in interface WorldLoaderInterface

insetWidth

public int insetWidth()
Specified by:
insetWidth in interface WorldLoaderInterface

computePatchSize

public double computePatchSize(int width,
                               int numPatches)
Specified by:
computePatchSize in interface WorldLoaderInterface

calculateHeight

public int calculateHeight(int worldHeight,
                           double patchSize)
Specified by:
calculateHeight in interface WorldLoaderInterface

calculateWidth

public int calculateWidth(int worldWidth,
                          double patchSize)
Specified by:
calculateWidth in interface WorldLoaderInterface

resizeView

public void resizeView()
Specified by:
resizeView in interface WorldResizer

tickCounterLabel

public void tickCounterLabel(String label)
Specified by:
tickCounterLabel in interface WorldLoaderInterface

tickCounterLabel

public String tickCounterLabel()
Specified by:
tickCounterLabel in interface WorldLoaderInterface

showTickCounter

public void showTickCounter(boolean visible)
Specified by:
showTickCounter in interface WorldLoaderInterface

showTickCounter

public boolean showTickCounter()
Specified by:
showTickCounter in interface WorldLoaderInterface

getWidth

public int getWidth()

getHeight

public int getHeight()

viewWidth

public double viewWidth()
Specified by:
viewWidth in interface ViewSettings

viewHeight

public double viewHeight()
Specified by:
viewHeight in interface ViewSettings

patchSize

public void patchSize(double patchSize)
Specified by:
patchSize in interface WorldResizer
Specified by:
patchSize in interface WorldLoaderInterface

patchSize

public double patchSize()
Specified by:
patchSize in interface ViewSettings
Specified by:
patchSize in interface Workspace

changeTopology

public void changeTopology(boolean wrapX,
                           boolean wrapY)
Specified by:
changeTopology in interface Workspace
Specified by:
changeTopology in interface WorldLoaderInterface

perspective

public Perspective perspective()
Specified by:
perspective in interface ViewSettings

drawSpotlight

public boolean drawSpotlight()
Specified by:
drawSpotlight in interface ViewSettings

renderPerspective

public boolean renderPerspective()
Specified by:
renderPerspective in interface ViewSettings

viewOffsetX

public double viewOffsetX()
Specified by:
viewOffsetX in interface ViewSettings

viewOffsetY

public double viewOffsetY()
Specified by:
viewOffsetY in interface ViewSettings

updateMode

public void updateMode(Workspace.UpdateMode updateMode)
Specified by:
updateMode in interface WorldLoaderInterface
Overrides:
updateMode in class AbstractWorkspace

fontSize

public int fontSize()
Specified by:
fontSize in interface ViewSettings

fontSize

public void fontSize(int fontSize)
Specified by:
fontSize in interface WorldLoaderInterface

setSize

public void setSize(int x,
                    int y)
Specified by:
setSize in interface WorldLoaderInterface

clearTurtles

public void clearTurtles()
Specified by:
clearTurtles in interface WorldLoaderInterface

createPatchesNotify

public void createPatchesNotify()
Internal use only.


inspectAgent

public void inspectAgent(Agent agent,
                         double radius)
Internal use only.

Specified by:
inspectAgent in interface Workspace

inspectAgent

public void inspectAgent(Class<? extends Agent> agentClass,
                         Agent agent,
                         double radius)
Internal use only.

Specified by:
inspectAgent in interface Workspace

getAndCreateDrawing

public BufferedImage getAndCreateDrawing()
Specified by:
getAndCreateDrawing in interface Workspace

importDrawing

public void importDrawing(File file)
                   throws IOException
Throws:
IOException

clearDrawing

public void clearDrawing()
Specified by:
clearDrawing in interface Workspace
Specified by:
clearDrawing in class AbstractWorkspace

exportDrawing

public void exportDrawing(String filename,
                          String format)
                   throws IOException
Specified by:
exportDrawing in interface Workspace
Throws:
IOException

exportDrawingToCSV

public void exportDrawingToCSV(PrintWriter writer)
Specified by:
exportDrawingToCSV in class AbstractWorkspace

exportOutput

public void exportOutput(String filename)
Specified by:
exportOutput in interface Workspace

exportOutputAreaToCSV

public void exportOutputAreaToCSV(PrintWriter writer)
Specified by:
exportOutputAreaToCSV in class AbstractWorkspace

clearOutput

public void clearOutput()
Internal use only.

Specified by:
clearOutput in interface Workspace

setImporterErrorHandler

public void setImporterErrorHandler(Importer.ErrorHandler importErrorHandler)
Internal use only.


exportView

public BufferedImage exportView()
Get a snapshot of the 2D view.

Specified by:
exportView in interface Workspace

getGraphics

public void getGraphics(BufferedImage image)
Get a snapshot of the 2D view, using an existing BufferedImage object.


paint

public void paint(Graphics2D g)
Internal use only.


exportView

public void exportView(String filename,
                       String format)
                throws IOException
Specified by:
exportView in interface Workspace
Throws:
IOException

exportInterface

public void exportInterface(String filename)
Not implemented.

Specified by:
exportInterface in interface Workspace

writeGraphicsData

public void writeGraphicsData(PrintWriter writer)
Specified by:
writeGraphicsData in class AbstractWorkspace

ownerFinished

public void ownerFinished(JobOwner owner)
Internal use only.

Specified by:
ownerFinished in interface JobManagerOwner

updateDisplay

public void updateDisplay(boolean haveWorldLockAlready)
Internal use only.

Specified by:
updateDisplay in interface JobManagerOwner

requestDisplayUpdate

public void requestDisplayUpdate(boolean force)
Internal use only.

Specified by:
requestDisplayUpdate in interface Workspace
Specified by:
requestDisplayUpdate in class AbstractWorkspace

perhapsUpdateDisplay

public void perhapsUpdateDisplay()
Internal use only.

Specified by:
perhapsUpdateDisplay in interface Workspace
Specified by:
perhapsUpdateDisplay in class AbstractWorkspace

periodicUpdate

public void periodicUpdate()
Internal use only.

Specified by:
periodicUpdate in interface JobManagerOwner

magicOpen

public void magicOpen(String name)
Internal use only.

Specified by:
magicOpen in interface Workspace
Specified by:
magicOpen in class AbstractWorkspace

openIndex

public void openIndex()
Internal use only.

Specified by:
openIndex in interface Workspace

openNext

public void openNext()
Internal use only.

Specified by:
openNext in interface Workspace

openPrevious

public void openPrevious()
Internal use only.

Specified by:
openPrevious in interface Workspace

startLogging

public void startLogging(String properties)
Internal use only.

Specified by:
startLogging in interface Workspace

zipLogFiles

public void zipLogFiles(String filename)
Internal use only.

Specified by:
zipLogFiles in interface Workspace

deleteLogFiles

public void deleteLogFiles()
Internal use only.

Specified by:
deleteLogFiles in interface Workspace

lastLogoException

public LogoException lastLogoException()
Internal use only.

Specified by:
lastLogoException in interface Workspace
Overrides:
lastLogoException in class AbstractWorkspace

clearLastLogoException

public void clearLastLogoException()
Internal use only.

Specified by:
clearLastLogoException in interface Workspace
Overrides:
clearLastLogoException in class AbstractWorkspace

runtimeError

public void runtimeError(JobOwner owner,
                         Context context,
                         Instruction instruction,
                         Exception ex)
Internal use only.

Specified by:
runtimeError in interface JobManagerOwner

open

public void open(String path)
          throws IOException,
                 CompilerException,
                 LogoException
Opens a model stored in a file.

Specified by:
open in interface Workspace
Specified by:
open in interface Controllable
Specified by:
open in class AbstractWorkspace
Parameters:
path - the path (absolute or relative) of the NetLogo model to open.
Throws:
IOException
CompilerException
LogoException

openString

public void openString(String modelContents)
                throws CompilerException,
                       LogoException
Opens a model stored in a string

Specified by:
openString in interface Workspace
Specified by:
openString in class AbstractWorkspace
Parameters:
modelContents -
Throws:
CompilerException
LogoException

openFromSource

public void openFromSource(String source)
                    throws CompilerException,
                           LogoException
Opens a model stored in a string. Can only be called once per instance of HeadlessWorkspace

Parameters:
source - The complete model, including widgets and so forth, in the same format as it would be stored in a file.
Throws:
CompilerException
LogoException

command

public void command(String source)
             throws CompilerException,
                    LogoException
Runs NetLogo commands and waits for them to complete.

Specified by:
command in interface Controllable
Parameters:
source - The command or commands to run
Throws:
CompilerException - if the code fails to compile
LogoException - if the code fails to run

report

public Object report(String source)
              throws CompilerException,
                     LogoException
Runs a NetLogo reporter.

Specified by:
report in interface Controllable
Parameters:
source - The reporter to run
Returns:
the result reported; may be of type java.lang.Integer, java.lang.Double, java.lang.Boolean, java.lang.String, LogoList, Agent, AgentSet, or Nobody
Throws:
CompilerException - if the code fails to compile
LogoException - if the code fails to run

halt

public void halt()
Halts all running NetLogo code in this workspace.

Overrides:
halt in class AbstractWorkspace