org.nlogo.headless
Class HeadlessWorkspace

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

public class HeadlessWorkspace
extends AbstractWorkspace
implements Controllable, WorldLoaderInterface

The main 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
 
Field Summary
 LogoException lastLogoException
          Internal use only.
 StringBuffer outputAreaBuffer
           
 String[] protocolLines
           
 org.nlogo.render.Renderer renderer
           
static int SPREADSHEET
          constant for specifying format of BehaviorSpace output
static int TABLE
          constant for specifying format of BehaviorSpace output
static String TEST_DECLARATIONS
          Internal use only.
 
Fields inherited from class org.nlogo.workspace.AbstractWorkspace
aggregateManager, DEFAULT_PREVIEW_COMMANDS, jobManager, previewCommands, TYPE_CONVERTED, TYPE_LIBRARY, TYPE_NEW, TYPE_NORMAL, world, worldLoader
 
Fields inherited from interface org.nlogo.nvm.Workspace
CONTINUOUS_UPDATES, OUTPUT_NORMAL, OUTPUT_TO_FILE, OUTPUT_TO_OUTPUT_AREA, TICK_BASED_UPDATES
 
Constructor Summary
HeadlessWorkspace()
          Makes a new instance of NetLogo capable of running a model "headless", with no GUI.
 
Method Summary
 int calculateHeight(int worldHeight, double patchSize)
           
 int calculateWidth(int worldWidth, double patchSize)
           
 void changeTopology(boolean wrapX, boolean wrapY)
           
 void clearDrawing()
           
 void clearOutput()
          Internal use only.
 void clearTurtles()
           
 void command(String source)
          Runs NetLogo commands and waits for them to complete.
 double computePatchSize(int width, int numPatches)
           
 void createPatchesNotify()
          Internal use only.
 void deleteLogFiles()
          Internal use only.
 void ensureRenderer()
          Internal use only.
 void exportDrawing(String filename, String format)
           
 void exportDrawingToCSV(PrintWriter writer)
           
 void exportGraphics(String filename, String format)
           
 void exportInterface(String filename)
          Not implemented.
 void exportOutput(String filename)
           
 void exportOutputAreaToCSV(PrintWriter writer)
           
 void fontSize(int fontSize)
           
 void generateModelTest(String model)
           
static void generateModelTests()
          Internal use only.
 BufferedImage getAndCreateDrawing()
           
 org.nlogo.compiler.AutoConverter.ErrorHandler getAutoConverterErrorHandler()
           
 Shape getDefaultLinkShape()
           
 Shape getDefaultShape()
           
 BufferedImage getGraphics()
          Get a snapshot of the 2D view.
 void getGraphics(BufferedImage image)
          Get a snapshot of the 2D view, using an existing BufferedImage object.
 float[] getHSB(int[] rgb)
           
 int getMinimumWidth()
           
 void halt()
          Halts all running NetLogo code in this workspace.
 void importDrawing(org.nlogo.util.File file)
           
 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(WorldDimensions d)
          Internal use only.
 void initForTesting(WorldDimensions d, String source)
           
 void initModelForChecksumming()
           
 int insetWidth()
           
 void inspectAgent(Class agentClass, Agent agent)
          Internal use only.
 boolean isCompilerTestingMode()
          Internal use only.
 boolean isHeadless()
          Internal use only.
 void magicOpen(String name)
          Internal use only.
static void main(String[] args)
           
 void makePreviews(boolean allModels, boolean overwrite)
           
 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 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.
 org.nlogo.lab.Protocol protocolFromModel(String name)
           
 void refreshShaped(boolean shapesOn)
           
 Object report(String source)
          Runs a NetLogo reporter.
 void requestDisplayUpdate(boolean force)
          Internal use only.
 void resizeWorld()
           
 void runExperiment(File file, int format, PrintWriter writer)
          Runs a BehaviorSpace experiment.
 void runExperiment(File file, String name, int format, PrintWriter writer)
          Runs a BehaviorSpace experiment.
 void runExperiment(String xml, int format, PrintWriter writer)
          Runs a BehaviorSpace experiment.
 org.nlogo.lab.Experiment runExperimentFromModel(String name)
           
 void runExperimentFromModel(String name, int format, PrintWriter writer)
          Runs a BehaviorSpace experiment.
 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 shapesOn()
           
 void shapesOn(boolean shapesOn)
           
 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(int updateMode)
           
 void waitFor(CommandRunnable runnable)
          Internal use only.
 void waitForQueuedEvents()
          Internal use only.
 Object waitForResult(ReporterRunnable 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, clearAll, compileCommands, compileForRun, compileReporter, computerHubNetRunning, computerHubNetRunning, convertToNormal, currentPlot, dispose, evaluateCommands, evaluateCommands, evaluateCommands, evaluateReporter, evaluateReporter, exportAllPlots, exportBehaviors, exportPlot, exportWorld, exportWorld, fileManager, forceSaveAs, getExtensionManager, getExternalFileSource, getHubNetManager, getIsApplet, getModelDir, getModelFileName, getModelPath, getModelType, getPlot, guessExportName, hubnetManager, importDrawing, importWorld, init, isApp, isApp, isApplet, isApplet, isConstant, joinForeverButtons, makeThunk, modelNameForDisplay, outputObject, plotManager, readFromString, resetTimer, runCompiledCommands, runCompiledReporter, setOutputAreaContents, updateChecksum, warningMessage, world
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

protocolLines

public String[] protocolLines

TABLE

public static final int TABLE
constant for specifying format of BehaviorSpace output

See Also:
Constant Field Values

SPREADSHEET

public static final int SPREADSHEET
constant for specifying format of BehaviorSpace output

See Also:
Constant Field Values

renderer

public org.nlogo.render.Renderer renderer

outputAreaBuffer

public final StringBuffer 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()
Makes a new instance of NetLogo capable of running a model "headless", with no GUI.

Method Detail

isHeadless

public boolean isHeadless()
Internal use only.

Specified by:
isHeadless in class AbstractWorkspace

ensureRenderer

public void ensureRenderer()
Internal use only.


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 Object waitForResult(ReporterRunnable 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 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
Throws:
CompilerException

initForTesting

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

initForTesting

public void initForTesting(WorldDimensions d)
Description copied from class: AbstractWorkspace
Internal use only.

Overrides:
initForTesting in class AbstractWorkspace

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 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

resizeWorld

public void resizeWorld()
Specified by:
resizeWorld 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

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 Workspace

changeTopology

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

refreshShaped

public void refreshShaped(boolean shapesOn)
Specified by:
refreshShaped in interface WorldLoaderInterface

updateMode

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

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(Class agentClass,
                         Agent agent)
Internal use only.

Specified by:
inspectAgent in interface Workspace

getAndCreateDrawing

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

importDrawing

public void importDrawing(org.nlogo.util.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

getAutoConverterErrorHandler

public org.nlogo.compiler.AutoConverter.ErrorHandler getAutoConverterErrorHandler()
Specified by:
getAutoConverterErrorHandler in interface Workspace

setImporterErrorHandler

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


getGraphics

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


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.


exportGraphics

public void exportGraphics(String filename,
                           String format)
                    throws IOException
Specified by:
exportGraphics 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

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 Controllable
Specified by:
open in class AbstractWorkspace
Parameters:
path - the path (absolute or relative) of the NetLogo model to open.
Throws:
IOException
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

runExperiment

public void runExperiment(String xml,
                          int format,
                          PrintWriter writer)
                   throws CompilerException,
                          SAXException
Runs a BehaviorSpace experiment.

Parameters:
xml - a single experiment setup represented in XML
format - TABLE or SPREADSHEET
writer - where to send the results Writes the results of the experiment to the given writer in CSV (comma separated values) format.
Throws:
CompilerException
SAXException

runExperiment

public void runExperiment(File file,
                          int format,
                          PrintWriter writer)
                   throws IOException,
                          CompilerException,
                          SAXException
Runs a BehaviorSpace experiment.

Parameters:
file - file containing a single experiment setup represented in XML
format - TABLE or SPREADSHEET
writer - where to send the results Writes the results of the experiment to the given writer in CSV (comma separated values) format.
Throws:
IOException
CompilerException
SAXException

runExperiment

public void runExperiment(File file,
                          String name,
                          int format,
                          PrintWriter writer)
                   throws IOException,
                          CompilerException,
                          SAXException
Runs a BehaviorSpace experiment.

Parameters:
file - file containing one or more experiment setups represented in XML
name - name of the experiment setup to run; must match the name of one of the setups in the XML file
format - TABLE or SPREADSHEET
writer - where to send the results Writes the results of the experiment to the given writer in CSV (comma separated values) format.
Throws:
IOException
CompilerException
SAXException

protocolFromModel

public org.nlogo.lab.Protocol protocolFromModel(String name)
                                         throws SAXException
Throws:
SAXException

runExperimentFromModel

public org.nlogo.lab.Experiment runExperimentFromModel(String name)
                                                throws CompilerException,
                                                       SAXException
Throws:
CompilerException
SAXException

runExperimentFromModel

public void runExperimentFromModel(String name,
                                   int format,
                                   PrintWriter writer)
                            throws CompilerException,
                                   SAXException
Runs a BehaviorSpace experiment.

Parameters:
name - the name of an experiment setup which is stored in the currently loaded model file
format - TABLE or SPREADSHEET
writer - where to send the results Writes the results of the experiment to the given writer in CSV (comma separated values) format.
Throws:
CompilerException
SAXException

main

public static void main(String[] args)
                 throws InterruptedException
Throws:
InterruptedException

shapesOn

public void shapesOn(boolean shapesOn)
Specified by:
shapesOn in class AbstractWorkspace

shapesOn

public boolean shapesOn()
Specified by:
shapesOn in class AbstractWorkspace

generateModelTest

public void generateModelTest(String model)
                       throws IOException,
                              CompilerException,
                              LogoException
Throws:
IOException
CompilerException
LogoException

initModelForChecksumming

public void initModelForChecksumming()
                              throws LogoException,
                                     CompilerException
Specified by:
initModelForChecksumming in interface Workspace
Throws:
LogoException
CompilerException

generateModelTests

public static void generateModelTests()
                               throws IOException
Internal use only.

Throws:
IOException

makePreviews

public void makePreviews(boolean allModels,
                         boolean overwrite)
Specified by:
makePreviews in interface Workspace

getHSB

public float[] getHSB(int[] rgb)
Specified by:
getHSB in interface Workspace

getDefaultShape

public Shape getDefaultShape()
Overrides:
getDefaultShape in class AbstractWorkspace

getDefaultLinkShape

public Shape getDefaultLinkShape()
Overrides:
getDefaultLinkShape in class AbstractWorkspace