Here we give an example of using poly-morphic collections with s.im.pl serialization. In this example we will use GameData as our case study. There are situations when building multi-player games the client and the server needs to communicated with each other by transferring game data from client to server and vice versa. We will show here how to annotate java class which has poly-morphic collections.
To view this tutorial you will need to download the entire simplTutorials,
simplTranslators and
ecologylabFundamental projects.
Access to the ecologylab fundamental project source is available
through anonymous SVN access (user: anonymous, password: anonymous).
The source for this tutorial is
located under
/trunk/simplTutorials/trunk/src/tutorials/
Below is a set of annotated Java classes similar to the one presented in the Java "getting started" tutorial. The major difference is that we are now working with polymorphic collections.
Below is a code excerpt from game data which defines fields we want to translate as attributes of game data in an XML file.
public class GameData<G extends Goal, T extends Threat> extends
|
Below we annotate further GameData with some more attributes and a polymorphic collections of "threats". This array list can hold threat objects and subclasses of threats.
/**
|
Here we also annotate a threat class which itself does not hold any attributes. However, some attributes are derived from the "Targetter" class. We do not present the "Targetter" class here for simplicity.
@simpl_inherit
|
Here we derive the "Threat" class to "OrbitingThreat" class. It does not define any new new attributes but it defines a new tag "ot" as a class attribute.
@simpl_inherit
|
Here we show how to create a translation scope by providing the translation scope class with the set of classes the xml file will bind to.
/*
|
In the code below we de-serialize a sample XML file containing game data. We call the static function on ElementState class, "translateFromXML" and provide the file path of the sample file.
We again serialize the GameData object and verify the produced XML file is the same as we supplied as input.
try
|
Below is the sample XML file we have used as our GameData. We can see that there are three different types of threats. Which are subclasses of "Threat" and belongs to the same collection
<game_data timestamp="1234399958508" cyc_rem="8078" loaded="true"
running="true" score="-28.066665835678577">
<threats>
<nt t_val="-1.0" id="_t12" ord="12">
<dir x="-1" y="-0.09" />
<vel x="-2.88" y="-0.26" />
<pos x="171.46" y="904.72" />
</nt>
<ot t_val="-1.0" id="_t13" ord="13">
<dir x="0.4" y="-0.92" />
<vel x="0.24" y="-0.38" />
<pos x="925.46" y="114.26" />
</ot>
<ot t_val="-1.0" id="_t14" ord="14">
<dir x="0.82" y="-0.58" />
<vel x="0.78" y="-0.58" />
<pos x="88.1" y="542.74" />
</ot>
<ot t_val="-1.0" id="_t15" ord="15">
<dir x="0.94" y="-0.35" />
<vel x="1.87" y="-0.62" />
<pos x="530.98" y="549.86" />
</ot>
<pt t_val="-1.0" id="_t16" ord="16">
<dir x="0.98" y="0.21" />
<vel x="2.02" y="0.46" />
<pos x="580.81" y="702.55" />
</pt>
<pt t_val="-1.0" id="_t17" ord="17">
<dir x="-0.99" y="-0.1" />
<vel x="-2.37" y="-0.24" />
<pos x="122.95" y="900.95" />
</pt>
<pt t_val="-5.300337003285493" id="_t18" ord="18">
<dir x="-0.81" y="-0.59" />
<vel x="-1.22" y="-0.77" />
<pos x="334.58" y="734.4" />
</pt>
<pt t_val="-1.0" id="_t19" ord="19">
<dir x="-1" y="-0.04" />
<vel x="-2.43" y="-0.14" />
<pos x="863.92" y="750.44" />
</pt>
</threats>
</game_data>
So that's it! We have successfully serialized and deserialized polymorphic collections in java.