|
From: North, M. <no...@an...> - 2001-09-04 15:38:52
|
Nick and Skye:
The deep copy code that I mentioned follows.
Mike
/*
* Import the required classes.
*
*/
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
/**
* Deep copies the given source object using serialization to and
* form a memory buffer. All of the contained objects must
* implement java.io.Serializable. Deep copying can fail for
* several reasons. The two most common causes of deep copy failure
* are attempting to copy objects that are not serializable and a
* lack of memory.
*
* Please note that there are limitations on serializing the
* Java Foundation Classes as noted in following entries from the
* Java Bug Database:
*
* 4379839: "Some tests fail with NotSerializableException."
* 4368050: "JToolBar is throwing a NotSerializableException."
* 4374993: "JSpinner is throwing NotSerializableException."
* 4375004: "TransferHandler is throwing
NotSerializableException."
* 4081159: "JFC components cannot be serialized as Java Beans"
*
* @param source - the element to duplicated.
* @return a deep copy of the source element or null if there is a
failure.
*/
public static Object deepCopy(Object source) {
// Declare the needed local variables.
ByteArrayOutputStream ostream = new
ByteArrayOutputStream();
ByteArrayInputStream istream = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Object destination = null;
// Attempt to duplicate the given item.
try {
// Attempt to serialize the given item to memory.
oos = new ObjectOutputStream(ostream);
oos.writeObject(source);
oos.flush();
ostream.close();
// Attempt to deserialize the given item from
memory.
istream = new
ByteArrayInputStream(ostream.toByteArray());
ois = new ObjectInputStream(istream);
destination = ois.readObject();
istream.close();
// Return the results.
return destination;
// Note errors.
} catch (Exception e) {
// Signal an error.
return null;
}
}
-----Original Message-----
From: Skye Bender-deMoll [mailto:sky...@sa...]
Sent: Friday, August 31, 2001 11:56 AM
To: ni...@sr...; rep...@li...
Subject: Re: [Repast-developer] interesting copy question
The way I've been doing it is to assume that people are either working
with multiplex networks, or they arn't (hows that for logic!). So, for
ex., all of the generation classes are either designed so that they will
not create duplicate ties, or (I belive random net does this) you must
explicity allow the possiblity of duplicate ties and self-loops. Many
network statistics not meaningful if duplicate ties are allowed, but
there are definitly some models where that is the way to go. So Density
(the ratio of existing arcs to possible arcs in the network) will still
return a reasonable value, but it is not really defined if you have
multiplex ties, as you don't know what the max possbile num ties is.
My soln. was to have calcDensity(ArrayList network) check if the network
was multiplex, and throw a message. However this takes time, so I was
also creating a method calcDensity(ArrayList network, boolean
isMultiplx) If false, it would calc the density and return a value
without checking for multplex (expensive) Method won't crash if the net
was multi, just would return funny value. if isMultiplex is true, I was
going to have it call a method, collapseMulti(ArralyList network) which
would return the network with multiplex ties removed and calc the
density. This pattern could be extened to other methods as well. Idea
is that if they don't know what they are doing, things will be safe and
slow, or if they know they've generated a network with no multiple ties,
they can be fast.
But, I realized that it would be dangerous to call collapseMulti from
within another method, as it would delete the ties in the original
network, hence the disire for a deep copy method. But it made me
realise that there may be other (non-network) cases were people might
want to perform operations on agents without actually modifying the
"real" agents in the agent list. So perhaps we should implement Mikes
suggestion, but as RepastUtilties.getDeepCopy(Object agent)? The other
tricky thing is that getting a deep copy of a node would have to return
a copy of the entire network it is connected to, otherwise if it still
has the original edges, changing them would still change the original
network.
So, as I doubt we will figure all this out before i leave, I'll make
sure collapseMulti is not called from within other methods, that it
doesn't return (to make it clear that it is chaging the pased network
rather than returning a copy), and it is documented as dangerous!
BTW, NetUtilities.isMultiplex(ArrayList network) is a great way to check
if a construction algorithm is working correctly!
best skye
Nick Collier wrote:
>
> I think Mike's suggestion here is a good one. But a question, under what
> conditions will there be duplicate ties. Is it something we can protect
> against when ties are created, a switch to all multiplex networks, or an
> exception is thrown?
>
> nick
>
>
> --
> Nick Collier
> Social Science Research Computing
> University of Chicago
> http://repast.sourceforge.net
_______________________________________________
Repast-developer mailing list
Rep...@li...
http://lists.sourceforge.net/lists/listinfo/repast-developer
|