swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: java question


From: Marcus G. Daniels
Subject: Re: java question
Date: 18 Jul 2001 14:27:14 -0600
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.7

>>>>> "MA" == Myriam Abramson <address@hidden> writes:

MA> How do I clone a SwarmObjectImpl object?

A simple question... with a wonderfully perverse answer!

Seriously, I can't see any reason to do this because you can subclass
passive or reactive agents from ordinary Java objects.  For active
agents (Swarms), this kind of approach might make sense if you really
need a copy of toplevel Java ivars.  Note that Java `clone' is
shallow; if there are component objects in the Object, those won't be
copied, although the references will be preserved.

The trick here is to setup the phase switch with the pre-cloned object
on the Java side, and then follow through with the Objective C object
creation (or copy).  The createEnd that gets called by create, will
end up returning the cloned Java object when the phase switch occurs.

There won't be anything on the Objective C side to copy if you are
using Java and SwarmObject, so a creation is just as good...

Sigh, I suppose I'm the only one that will be amused by this.  ;-)

import swarm.Globals;
import swarm.objectbase.SwarmObjectImpl;
import swarm.objectbase.SwarmObjectCImpl;
import swarm.objectbase.SwarmObjectC;
import swarm.defobj.Zone;

class MySwarmObjectCImpl extends SwarmObjectCImpl {
    MySwarmObjectCImpl (MySwarmObjectImpl nextPhase) {
        super (nextPhase);
    }
}

class MySwarmObjectImpl extends SwarmObjectImpl implements Cloneable {
    public int val1;
    public double val2;

    MySwarmObjectImpl (Zone aZone) {
        super (aZone);
    }

    protected Object clone () {
        try {
            return super.clone ();
        }
        catch (CloneNotSupportedException e) {
            // won't happen
            e.printStackTrace (System.err);
        }
        return null;
    }
}

public class CopySwarmObject {
    static void main (String args[]) {
        Globals.env.initSwarm ("CopySwarmObject", "0.0", "address@hidden",
                               args);

        new CopySwarmObject ();
    }

    CopySwarmObject () {
        MySwarmObjectImpl obj = new MySwarmObjectImpl (Globals.env.globalZone);

        obj.val1 = 1;
        obj.val2 = 2.0;

        MySwarmObjectCImpl newObjC =
            new MySwarmObjectCImpl ((MySwarmObjectImpl) (obj.clone ()));
        MySwarmObjectImpl newObj =
            (MySwarmObjectImpl) newObjC.create (obj.getZone ());
        
        System.out.println ("val1 " + newObj.val1 + " val2: " + newObj.val2);
        
    }
}

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]