[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Questions on probes and inheritance
From: |
Marcus G. Daniels |
Subject: |
Re: Questions on probes and inheritance |
Date: |
14 Oct 2000 12:39:35 -0700 |
User-agent: |
Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.4 |
>>>>> "DA" == David Aliaga <address@hidden> writes:
DA> How can I make that the probe displays these superclass instance
DA> variables? (this probe is generated by right clicking, not by
DA> writing code)
Here's an example of adding superclass instance variables to
a ProbeMap.
SuperProbeMap is a subclass of EmptyProbeMap that adds a method
called addProbeForVariable that will search up the class hierarchy
for the requested variable.
In turn, MySubClassProbeMap is a subclass of SuperProbeMap that adds
the specific variables from MySubClass. MySubClassProbeMap
is then instantiated and registered as the canonical ProbeMap for
MySubClass, so then when a ProbeDisplay is created (e.g. via a
right click on an agent), that we'll have all the variables
in the initial panel.
import swarm.Globals;
import swarm.objectbase.VarProbe;
import swarm.objectbase.EmptyProbeMapImpl;
import swarm.objectbase.ProbeMap;
import swarm.defobj.Zone;
import swarm.simtoolsgui.GUISwarm;
import swarm.simtoolsgui.GUISwarmImpl;
class BaseClass {
public double val1;
public String val2;
}
class MySubClass extends BaseClass {
public int val3;
MySubClass (double val1, String val2, int val3) {
super ();
this.val1 = val1;
this.val2 = val2;
this.val3 = val3;
}
}
class SuperProbeMap extends EmptyProbeMapImpl {
SuperProbeMap (Zone aZone, Class aClass) {
super (aZone, aClass);
}
VarProbe getProbeForVariable (String name, Class clas) {
VarProbe vp =
Globals.env.probeLibrary.getProbeForVariable$inClass (name, clas);
if (vp == null)
{
clas = clas.getSuperclass ();
if (clas != null)
vp = getProbeForVariable (name, clas);
}
return vp;
}
void addProbeForVariable (String name) {
addProbe (getProbeForVariable (name, getProbedClass ()));
}
}
class MySubClassProbeMap extends SuperProbeMap {
MySubClassProbeMap (Zone aZone) {
super (aZone, MySubClass.class);
addProbeForVariable ("val1");
addProbeForVariable ("val2");
addProbeForVariable ("val3");
}
}
public class SuperProbeMapTest extends GUISwarmImpl {
SuperProbeMapTest (Zone aZone) {
super (aZone);
}
public Object buildObjects () {
super.buildObjects ();
MySubClass subClass = new MySubClass (1.0, "Hello World", 2);
MySubClassProbeMap probeMap = new MySubClassProbeMap (getZone ());
Globals.env.probeLibrary.setProbeMap$For ((Object) probeMap,
subClass.getClass ());
Globals.env.createProbeDisplay (subClass);
return this;
}
public Object buildActions () {
super.buildActions ();
return this;
}
public static void main(String[] args) {
Globals.env.initSwarm ("SuperProbeMap", "0.0","address@hidden", args);
GUISwarm guiSwarm = new SuperProbeMapTest (Globals.env.globalZone);
guiSwarm.buildObjects ();
guiSwarm.buildActions ();
guiSwarm.activateIn (null);
guiSwarm.go ();
}
}
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.