Class SimulationRecorder

java.lang.Object
com.eco.bio7.spatial.SimulationRecorder

public class SimulationRecorder extends Object
Records a simulation written in Java (e.g., a Bio7 model) step by step and exports it to an HTML page which plays the recording: Play/Pause, Step, Reset, the speed, a timeline to jump to a step, a loop and the chart (see HtmlSimulation for the views). The model runs in Java (any model, the exact results), the page only plays the recorded frames (the settings of the model cannot be changed in the page).

Each step adds a frame with the data of the view: a grid of cells (GRID_2D, with optional agents), points and lines (SCENE_3D) or a surface (SURFACE_3D) and the values of the chart.

SimulationRecorder recorder = new SimulationRecorder("Game of Life").view(HtmlSimulation.View.GRID_2D)
                .palette(0xFFFFFF, 0x1F3A93).chart("Population");
for (int t = 0; t < 300; t++) {
        step(); // the model
        recorder.frame().grid(width, height, cells).chart("Population", alive);
}
recorder.export("/path/life.html");
In a Bio7 model the frame can be recorded in the run method (e.g., if SpatialUtil.canStep() is true) and exported after a number of frames.
  • Constructor Details

    • SimulationRecorder

      public SimulationRecorder(String title)
      Creates a recorder with the title of the page.
  • Method Details

    • description

      public SimulationRecorder description(String description)
      A description below the title.
    • view

      The view (default GRID_2D).
    • palette

      public SimulationRecorder palette(int... colors)
      The colors (0xRRGGBB) of the integer states of a grid (state 0, 1, 2, ...).
    • colorMap

      public SimulationRecorder colorMap(int[] ramp)
      The color map for continuous values of a grid or a surface (e.g., PlotLegend.VIRIDIS).
    • range

      public SimulationRecorder range(double min, double max)
      The range of the values of the color map (and the height of a surface), default: the range of all frames.
    • box

      public SimulationRecorder box(double width, double height, double depth)
      The size of the box of the 3D views (the points are in these coordinates, default 1, 1, 1).
    • points

      public SimulationRecorder points(double radius, int color)
      The radius of the points (in the coordinates of the box) and their color (if not each point has a color).
    • lineColor

      public SimulationRecorder lineColor(int color)
      The color of the lines (if not each line has a color).
    • agents

      public SimulationRecorder agents(double radius, int color)
      The radius (in cells) and the color of the agents of a grid (if not each agent has a color).
    • speed

      public SimulationRecorder speed(double framesPerSecond)
      The default speed in frames per second (default 20).
    • chart

      public SimulationRecorder chart(String title)
      Displays a chart of the recorded values (see Frame.chart).
    • loop

      public SimulationRecorder loop(boolean loop)
      Plays the recording again from the start at the end (default true, a check box of the page).
    • autoplay

      public SimulationRecorder autoplay(boolean autoplay)
      Plays the recording when the page is opened (default false).
    • background

      public SimulationRecorder background(String color)
      The background color of the view (e.g., "#ffffff").
    • maxFrames

      public SimulationRecorder maxFrames(int maxFrames)
      The maximum number of the frames (default 100000, further frames are ignored).
    • frame

      public SimulationRecorder.Frame frame()
      Adds a new frame (a step of the simulation) and returns it for the data.
      Returns:
      the frame
    • frames

      public int frames()
      Returns the number of the recorded frames.
    • clear

      public SimulationRecorder clear()
      Removes the recorded frames.
    • record

      public SimulationRecorder record(int steps, Consumer<SimulationRecorder.Frame> step)
      Records a number of steps: the function is called for each step with a new frame (it computes the step of the model and adds the data to the frame).
      Parameters:
      steps - the number of the steps
      step - the step of the model and the data of the frame
      Returns:
      the recorder
    • toHtml

      public String toHtml()
      Returns the HTML page of the recording.
    • export

      public SimulationRecorder export(String file) throws IOException
      Writes the HTML page of the recording.
      Parameters:
      file - the HTML file
      Returns:
      the recorder
      Throws:
      IOException - if the file cannot be written