|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.ascape.util.data.StatCollector
public class StatCollector
Collects and stores aggregate statistics on some data source for every specified measurement type
of that source, and for each succesive measurement of that source.
These stats, when added to a scape, are available for observation by charting tools,
data export, etc..
Statistics can be automatically collect by the framework, or collected manually.
While this class is not abstract, it should be considered so when collecting statistics automatically.
This class and its subclasses are intended to collaborate with classes that gather
statistics on some collection of objects, by calling clear()
to set
all values to the appropriate 'zero' value, then addValue()
on each object.
Pick the stat collector class that provides all the measurements you will want to use.
StatCollectorCSA provides count, sum and average measures.
StatCollectorCSAMM also provides minimum and maximum measures.
StatCollectorCSAMMVar also provides variance and standard deviation measures.
Let us know if there are other measurements that you might want to use. (Median is being considered, but will be relativly costly.)
The following example creates a class that gathers a count, sum of ages, and average age for supplied
objects.
To use within ascape, you could create a new class that overrides the getValue
and getName
methods.
class AgeStatCollector extends StatCollectorCSA {
public double getValue(Object object) {
return ((AnObjectWithAge) object).getAge();
}
public String getName() {
return "Age";
}
}
and then add it to your scape.
myScape.addStatCollector(new AgeStatCollector());
In practice, we usually just do something like:
scape.addStatCollector(new StatCollectorCSA("Age") {
public double getValue(Object object) {
return ((AnObjectWithAge) object).getAge();
}
}
If you only want to collect statistics on a subpopulation of a scape (i.e. "Wealth of all agents over 30"), use a StatCollectorCond.
public void scapeCreated() {
...
myScape.addStatCollector(new StatCollectorCSA("Death Wealth", false));
...
}
...
public void die() {
super.die();
scape.getData().getStatCollector("Death Wealth").addValue(wealth);
...
}
Performance note: This manual method requires a string comparison in the model to compare the
name of the stat collector to the list of stat collectors. We usually try to avoid this kind of thing inside
the body of a running model but for typical usage this hit is probably minimal.
To avoid it, you could create a static class member for the stat, or make the stat a member of the parent scape,
and access it directly.
StatCollectorCond
,
StatCollectorCSA
,
StatCollectorCSAMM
,
StatCollectorCSAMMVar
,
StatCollectorCondCSA
,
StatCollectorCondCSAMM
,
StatCollectorCondCSAMMVar
,
Serialized FormField Summary | |
---|---|
protected static java.lang.String[] |
allMeasureNames
Names for all statistical measures implemented. |
protected static java.lang.String[] |
allMeasureNamesShort
Short names for all statistical measures implemented. |
protected boolean |
autoCollect
Should the stat be collected automatically? Default is true. |
static int |
COLLECTING
|
protected int |
count
The number of values collected. |
protected java.lang.String |
name
The name of the stat. |
static int |
NOT_COLLECTING
|
static int |
SET_BY_DATAGROUP
|
Constructor Summary | |
---|---|
StatCollector()
Constructs a new StatCollector. |
|
StatCollector(java.lang.String name)
Constructs a new StatCollector. |
|
StatCollector(java.lang.String name,
boolean autoCollect)
Constructs a new StatCollector. |
Method Summary | |
---|---|
void |
addValue(double value)
Add the value, incrementing count. |
void |
addValueFor(java.lang.Object object)
|
void |
calculateCollection(java.util.Collection collection)
Calcualtes the statcollector across the provided list. |
void |
calculateIterator(java.util.Iterator iter)
Calcualtes the statcollector across the provided iterator. |
double |
calculateValue()
Calculate the value to be added (once) to the statistic. |
void |
clear()
Override to clear all values related to this stat. |
int |
compareTo(java.lang.Object o)
Compares one stat collector to another by name. |
void |
createDataSeries(boolean collectingLongitudinalData)
Creates a data series for every measure of this statistic collector. |
DataSeries[] |
getAllDataSeries()
Returns a data series for every statistical measure this collector is capable of generating. |
static java.lang.String[] |
getAllMeasureNames()
Returns all names for measures that statistics collectors are capable of generating. |
static java.lang.String[] |
getAllMeasureNamesShort()
Returns all short names for measures that statistics collectors are capable of generating. |
int |
getCollectingLongitudinalDataMode()
|
int |
getCount()
Returns the current number of values added. |
DataGroup |
getDataGroup()
|
DataSeries |
getDataSeries(java.lang.String measureName)
Returns a data series for the measure name provided. |
java.lang.String |
getName()
Override to provide a short name, or set a name, in the constructor or using this method. |
double |
getValue(java.lang.Object object)
Override to return the value being used to calculate this statistic. |
boolean |
isAutoCollect()
Is value collected automatically by iterating over a scape? Default is true; override to set false; |
boolean |
isCalculated()
Is value collected automatically by iterating over a scape? Default is true; override to set false; |
boolean |
isCollectingLongitudinalData()
|
boolean |
isPhase2()
|
void |
setAutoCollect(boolean autoCollect)
Set whether the value is collected automatically. |
void |
setCollectingLongitudinalDataMode(int mode)
|
void |
setDataGroup(DataGroup dataGroup)
|
void |
setName(java.lang.String name)
Sets the name of this statistic. |
java.lang.String |
toString()
Returns a short desription of this stat collector. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected int count
protected java.lang.String name
protected boolean autoCollect
public static final int SET_BY_DATAGROUP
public static final int COLLECTING
public static final int NOT_COLLECTING
protected static java.lang.String[] allMeasureNames
protected static java.lang.String[] allMeasureNamesShort
Constructor Detail |
---|
public StatCollector()
public StatCollector(java.lang.String name, boolean autoCollect)
name
- the name of the stat collector.autoCollect
- should the stat be collected automatically?public StatCollector(java.lang.String name)
name
- the name of the stat collector.Method Detail |
---|
public void clear()
public void addValueFor(java.lang.Object object)
public void addValue(double value)
public double getValue(java.lang.Object object)
public double getValue(Object object) { return ((MyClass) object).getMyInterestingValue(); }
getValue
in interface DataPoint
object
- an object that contains the value of interest, ignored in base casepublic double calculateValue()
public boolean isCalculated()
public boolean isPhase2()
public boolean isAutoCollect()
public void setAutoCollect(boolean autoCollect)
autoCollect
- true to collect statistic automatically.public boolean isCollectingLongitudinalData()
public void setCollectingLongitudinalDataMode(int mode)
public int getCollectingLongitudinalDataMode()
public int getCount()
public static java.lang.String[] getAllMeasureNames()
public static java.lang.String[] getAllMeasureNamesShort()
public DataSeries[] getAllDataSeries()
createDataSeries(boolean)
public DataSeries getDataSeries(java.lang.String measureName)
public void calculateCollection(java.util.Collection collection)
collection
- public void calculateIterator(java.util.Iterator iter)
iter
- the list to calcualte statistics on.public void createDataSeries(boolean collectingLongitudinalData)
public void setDataGroup(DataGroup dataGroup)
public DataGroup getDataGroup()
public java.lang.String getName()
public double getName(Object object) { return "Interesting Value"; }Or simply: myStat.setName("Interesting Value");
getName
in interface DataPoint
getName
in interface HasName
public void setName(java.lang.String name)
public java.lang.String toString()
toString
in class java.lang.Object
public int compareTo(java.lang.Object o)
compareTo
in interface java.lang.Comparable
|
![]() |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |