Carbon Bathtub Groovy

Ken Webb 2010-06-14T20:25:59Z

This page describes the Groovy version of the Carbon Bathtub Xholon model. The Groovy programming language is an extension of Java. The code on this page does not take advantage of any of the Groovy extensions, so it's basically just Java. But it does use the Groovy compiler.

<CarbonBathtub implName="lang:groovy:inline:"><![CDATA[
package org.primordion.user.app.Chameleon.CarbonBathtub;

import org.primordion.xholon.app.Application;
import org.primordion.xholon.base.IMessage;
import org.primordion.xholon.base.Xholon;

public class CarbonBathtub extends Xholon {
  
  private boolean acting = false;
  private int startYear = 2008;
  private int endYear   = 2070;
  private int currentYear = 0;
  private String dataPlotterTitleExtra = "";
  
  public void act() {
    if (acting) {
      currentYear++;
      if (currentYear == endYear) {
        ((Application)getApp()).invokeDataPlotter();
        ((Application)getApp()).setUseDataPlotter("none");
        acting = false;
      }
      super.act();
    }
  }
  
  private static final String start = "Start";
  
  private String[] actions = new String[1];
  
  public String[] getActionList() {
    if (actions[0] == null) {
      actions[0] = start;
    }
    return actions;
  }
  
  public void setActionList(String[] actionList) {
    actions = actionList;
  }
  
  public void doAction(String action) {
    if (start.equals(action)) {
      start();
    }
  }
  
  public void processReceivedMessage(IMessage msg) {
    doAction((String)msg.getData());
  }
  
  public IMessage processReceivedSyncMessage(IMessage msg) {
    dataPlotterTitleExtra += (String)msg.getData();
    return null;
  }
  
  protected void start() {
    if (!acting) {
      if (currentYear == endYear) {
        // this is a restart
        getFirstChild().reconfigure();
      }
      currentYear = startYear;
      getApp().setTimeStep(startYear);
      generateDataPlotter();
      dataPlotterTitleExtra = "";
      acting = true;
    }
  }

  protected void generateDataPlotter() {
    Application app = (Application)getApp();
    app.setUseDataPlotter("JFreeChart"); // or "google"
    app.setDataPlotterParams("CO2 in the Atmosphere "
        + dataPlotterTitleExtra
        + ",Year,MTons CO2,./statistics/,stats,1,WRITE_AS_LONG");
    app.createChart();
  }
  
  public boolean isActing() {
    return acting;
  }

  public void setActing(boolean acting) {
    this.acting = acting;
  }

  public int getStartYear() {
    return startYear;
  }

  public void setStartYear(int startYear) {
    this.startYear = startYear;
  }

  public int getEndYear() {
    return endYear;
  }

  public void setEndYear(int endYear) {
    this.endYear = endYear;
  }

  public int getCurrentYear() {
    return currentYear;
  }

  public void setCurrentYear(int currentYear) {
    this.currentYear = currentYear;
  }
  
}
new CarbonBathtub();
]]></CarbonBathtub>
<_-.carbonbathtub>
<Atmosphere implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.base.Xholon;
import org.primordion.xholon.base.IXholonClass;

public class Atmosphere extends Xholon
{
  private static final double INITIAL_CARBON_MTONS = 820000000000.0;
  private double carbonMTons = INITIAL_CARBON_MTONS; // 2008 average
  
  public void reconfigure() {
    carbonMTons = INITIAL_CARBON_MTONS;
    super.reconfigure();
  }
  
  public double getVal() {
    return carbonMTons;
  }
  
  public void setVal(double val) {
    this.carbonMTons = val;
  }
  
  public int getXhType() {
    return IXholonClass.XhtypePurePassiveObject;
  }
  
}
new Atmosphere();
]]></Atmosphere>
<InAllHumanMadeCo2 implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class InAllHumanMadeCo2 extends Xholon {
  
  private static final double INITIAL_CARBON_PERYEAR = 9100000000.0;
  private double carbonPerYear = INITIAL_CARBON_PERYEAR; // rate
  private double rateChange    = 1.02; // 1.01 is +1%, 1.00 is steady, 0.99 is -1%
  private IXholon atmosphere = null;
  
  public void postConfigure() {
    getParentNode().sendSyncMessage(101, "[Rate of change = " + rateChange + "]", this);
    super.postConfigure();
  }
  
  public void reconfigure() {
    carbonPerYear = INITIAL_CARBON_PERYEAR;
    rateChange -= 0.01;
    //getParentNode().sendMessage(101, Double.toString(rateChange), this);
    getParentNode().sendSyncMessage(101, "[Rate of change = " + rateChange + "]", this);
    super.reconfigure();
  }
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() + carbonPerYear;
    carbonPerYear *= rateChange;
    atmosphere.setVal(newVal);
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
  public double getRateChange() {return rateChange;}
  public void setRateChange(double rateChange) {this.rateChange = rateChange;}
  
}
new InAllHumanMadeCo2();
]]></InAllHumanMadeCo2>
<OutAbsorbedByPlantsAndSoils implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedByPlantsAndSoils extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.3;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(newVal);
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedByPlantsAndSoils();
]]></OutAbsorbedByPlantsAndSoils>
<OutAbsorbedByOceans implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedByOceans extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.25;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(newVal);
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedByOceans();
]]></OutAbsorbedByOceans>
<OutAbsorbedBySedimentsAndRocks implName="lang:groovy:inline:"><![CDATA[
import org.primordion.xholon.base.IXholon;
import org.primordion.xholon.base.Xholon;

public class OutAbsorbedBySedimentsAndRocks extends Xholon {
  
  private double carbonPerYear = 9100000000.0 * 0.009;
  private IXholon atmosphere = null;
  
  public void act() {
    if (atmosphere == null) {
      atmosphere = getXPath().evaluate("../Atmosphere", this);
    }
    double newVal = atmosphere.getVal() - carbonPerYear;
    atmosphere.setVal(newVal);
    super.act();
  }
  
  public IXholon getAtmosphere() {return atmosphere;}
  public void setAtmosphere(IXholon atmosphere) {this.atmosphere = atmosphere;}
  
}
new OutAbsorbedBySedimentsAndRocks();
]]></OutAbsorbedBySedimentsAndRocks>
</_-.carbonbathtub>

return to main page