/** * Loads the initial data from the supplied XML element. * * @param elem where to load from * @return initial data */ public InitialData readInitialData(Element elem) { if (!elem.getNodeName().equals(StatechumXML.ELEM_INIT.name())) throw new IllegalArgumentException( "expecting to load learner initial data " + elem.getNodeName()); NodeList children = elem.getChildNodes(); InitialData result = new InitialData(); for (int i = 0; i < children.getLength(); ++i) if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) children.item(i); if (e.getNodeName().equals(StatechumXML.graphmlNodeNameNS.toString())) { if (result.graph != null) throw new IllegalArgumentException("duplicate graph element"); result.graph = new LearnerGraph(config); AbstractPersistence.loadGraph(e, result.graph, decoratedLearner.getLabelConverter()); } else if (e.getNodeName().equals(StatechumXML.ELEM_SEQ.name())) { String sequenceName = e.getAttribute(StatechumXML.ATTR_SEQ.name()); if (sequenceName.equals(StatechumXML.ATTR_POSITIVE_SEQUENCES.name())) { if (result.plus != null) throw new IllegalArgumentException("duplicate positive element"); result.plus = labelio.readSequenceList(e, StatechumXML.ATTR_POSITIVE_SEQUENCES.name()); if (!e.hasAttribute(StatechumXML.ATTR_POSITIVE_SIZE.name())) throw new IllegalArgumentException("missing positive size"); String size = e.getAttribute(StatechumXML.ATTR_POSITIVE_SIZE.name()); try { result.plusSize = Integer.valueOf(size); } catch (NumberFormatException ex) { statechum.Helper.throwUnchecked("positive value is not an integer " + size, ex); } } else if (sequenceName.equals(StatechumXML.ATTR_NEGATIVE_SEQUENCES.name())) { if (result.minus != null) throw new IllegalArgumentException("duplicate negative element"); result.minus = labelio.readSequenceList(e, StatechumXML.ATTR_NEGATIVE_SEQUENCES.name()); if (!e.hasAttribute(StatechumXML.ATTR_NEGATIVE_SIZE.name())) throw new IllegalArgumentException("missing negative size"); String size = e.getAttribute(StatechumXML.ATTR_NEGATIVE_SIZE.name()); try { result.minusSize = Integer.valueOf(size); } catch (NumberFormatException ex) { statechum.Helper.throwUnchecked("negative value is not an integer " + size, ex); } } else throw new IllegalArgumentException("unexpected kind of sequences: " + sequenceName); } else throw new IllegalArgumentException("unexpected element " + e.getNodeName()); } if (result.graph == null) throw new IllegalArgumentException("missing graph"); if (result.plus == null) throw new IllegalArgumentException("missing positive sequences"); if (result.minus == null) throw new IllegalArgumentException("missing negative sequences"); return result; }
/** * This method stores all the details of initialisation of a learner. * * @param initialData data to store * @return the constructed XML element */ protected Element writeInitialData(InitialData initialData) { Element elemInit = doc.createElement(StatechumXML.ELEM_INIT.name()); Element positive = labelio.writeSequenceList(StatechumXML.ATTR_POSITIVE_SEQUENCES.name(), initialData.plus); positive.setAttribute( StatechumXML.ATTR_POSITIVE_SIZE.name(), Integer.toString(initialData.plusSize)); Element negative = labelio.writeSequenceList(StatechumXML.ATTR_NEGATIVE_SEQUENCES.name(), initialData.minus); negative.setAttribute( StatechumXML.ATTR_NEGATIVE_SIZE.name(), Integer.toString(initialData.minusSize)); elemInit.appendChild(initialData.graph.storage.createGraphMLNode(doc)); elemInit.appendChild(AbstractPersistence.endl(doc)); elemInit.appendChild(positive); elemInit.appendChild(AbstractPersistence.endl(doc)); elemInit.appendChild(negative); elemInit.appendChild(AbstractPersistence.endl(doc)); return elemInit; }