Introduction

Small knight's blog.

Blog

ConcurrentLinkedQueue + saving HashMap in XML with JAXB 2012/01/17

Problems with concurrency exceptions when accessing your ArrayLists? Well ConcurrentLinkedQueue solves your problems! Now another funny thing - ever used serializing to XML with JAXB? The thing is JAXB does not serialize HashMap annotated by XMLElement annotation AND it does not even let you know by raising some kind of exception. You will realize your hashmap is not saved when you try to load it in the future! To fix this, you need to create own adapter for each HashMap<OBJECT1, OBJECT2> … Example for HashMap<Double,String>:

/** My XML serialized object using HashMaps of type Double,String... */
@XmlRootElement
public class EmohawkAgentHistorySave {
 
    @XmlElement
    public double startTime = 0;
    @XmlElement
    public double endTime = 0;
    @XmlElement
    public String agentName = "";
 
    @XmlJavaTypeAdapter(XMLHashMapDoubleStringAdapter.class)
    public HashMap<Double,String> myCurrentGoalHistory = new HashMap();
    @XmlJavaTypeAdapter(XMLHashMapDoubleStringAdapter.class)
    public HashMap<Double,String> myGoalBecomeActiveHistory = new HashMap();
    @XmlJavaTypeAdapter(XMLHashMapDoubleStringAdapter.class)
    public HashMap<Double,String> myGoalFinishedHistory = new HashMap();
 
    @XmlElement
    public ArrayList<RecordAgentStateSave> myAgentStateHistory = new ArrayList();
 
}
 
/**
 * Helper class that you need in order for your hashmap to be properly serialized.
 * @author knight
 */
public class XMLHashMapDoubleStringAdapter extends XmlAdapter<MatTypeDS, HashMap<Double, String>> {
 
    @XmlRootElement
    public static class MapEntryDS {
        @XmlElement
        public Double key;
        @XmlElement
        public String value;
    }
 
    @XmlRootElement
    public static class MatTypeDS {
        @XmlElement(name ="entryDS")
        public List<MapEntryDS> entryList = new ArrayList<MapEntryDS>();
    }
 
    @Override
    public MatTypeDS marshal(HashMap<Double, String> map) {
        MatTypeDS MatTypeDS = new MatTypeDS();
        for (Entry<Double, String> entry : map.entrySet()) {
            MapEntryDS mapEntry = new MapEntryDS();
            mapEntry.key = entry.getKey();
            mapEntry.value = entry.getValue();
            MatTypeDS.entryList.add(mapEntry);
        }
        return MatTypeDS;
    }
 
    @Override
    public HashMap<Double, String> unmarshal(MatTypeDS type) throws Exception {
        HashMap<Double, String> map = new HashMap<Double, String>();
        for (MapEntryDS entry : type.entryList) {
            map.put(entry.key, entry.value);
        }
        return map;
    }
}

UE2, UT + Pogamut - character's are not disappearing from the game when .stop or .kill method is called in Pogamut 2011/09/08

I've tried to solve this problem for a long time. It seems there is some problem in Java part that cause the actual connection to UT is preserved even after the methods .stop or .kill are called. I've created a disconnect command that destroyes the bot. With this solution there was another problem. It seems that when issuing this command and calling .stop or .kill immediatelly after, the command did not reached the GameBots! So it seems the only sure method of quitting/killing bot is to issue Disconnect command ONLY and NOT call any .kill or .stop methods on agent. The agent will be quit when the connection will terminate (and this will happen as an effect of Disconnect command),