@Override
    protected void writeChildren(XMLStreamWriter out)
        throws XMLStreamException {
        super.writeChildren(out);

        // Using a copy of the objects defensively against races.
        for (AIObject aio : new ArrayList<AIObject>(aiObjects.values())) {
            if (!aio.checkIntegrity()) {
                logger.warning("Integrity failure: " + aio);
                continue;
            }
            if (aio instanceof Wish) {
                Wish wish = (Wish)aio;
                if (!wish.shouldBeStored()) continue;
            }

            try {
                if (aio.getId() == null) {
                    logger.warning("Null AI identifier for: "
                        + aio.getClass().getName());
                } else {
                    aio.toXML(out);
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Failed to write AI object: " + aio,
                    e);
            }
        }
    }