/** * Loads a pair from the supplied XML element. * * @param graph the graph which elements to load Ideally, I need to match string IDs loaded to * those of the graph but this is not done because * <ul> * <li>graphseries used to mangle names of vertices, now this should not happen often (and * if I use relabelling when loading graphs, this would never happen). * <li>this method is expected to be general - purpose hence we do not expect a matching * graph to be present. * <li>matching was only needed when loading a compatibility table from a graph - graph * loader now does the matching after reading pairs. * </ul> * * @param elem element to load from * @return loaded state pair. */ public static <TARGET_TYPE, CACHE_TYPE extends CachedData<TARGET_TYPE, CACHE_TYPE>> PairScore readPair(AbstractLearnerGraph<TARGET_TYPE, CACHE_TYPE> graph, Element elem) { if (!elem.getNodeName().equals(StatechumXML.ELEM_PAIR.name())) throw new IllegalArgumentException("expected to load a pair but got " + elem.getNodeName()); if (!elem.hasAttribute(StatechumXML.ATTR_Q.name()) || !elem.hasAttribute(StatechumXML.ATTR_R.name())) throw new IllegalArgumentException("missing attribute in a pair"); String q = elem.getAttribute(StatechumXML.ATTR_Q.name()), r = elem.getAttribute(StatechumXML.ATTR_R.name()), score = elem.getAttribute(StatechumXML.ATTR_SCORE.name()), otherscore = elem.getAttribute(StatechumXML.ATTR_OTHERSCORE.name()); int scoreInt = JUConstants.intUNKNOWN, otherScoreInt = JUConstants.intUNKNOWN; if (score != null && score.length() > 0) try { scoreInt = Integer.valueOf(score); } catch (NumberFormatException ex) { statechum.Helper.throwUnchecked("failed to read a score in a pair", ex); } if (otherscore != null && otherscore.length() > 0) try { otherScoreInt = Integer.valueOf(otherscore); } catch (NumberFormatException ex) { statechum.Helper.throwUnchecked("failed to read a anotherscore in a pair", ex); } return new PairScore( AbstractLearnerGraph.generateNewCmpVertex(VertexID.parseID(q), graph.config), AbstractLearnerGraph.generateNewCmpVertex(VertexID.parseID(r), graph.config), scoreInt, otherScoreInt); }
/** * Writes the supplied element into XML. * * @param element to write * @return the constructed XML element. */ public static Element writePair(PairScore element, Document doc) { Element pairElement = doc.createElement(StatechumXML.ELEM_PAIR.name()); pairElement.setAttribute(StatechumXML.ATTR_Q.name(), element.getQ().getStringId()); pairElement.setAttribute(StatechumXML.ATTR_R.name(), element.getR().getStringId()); if (element.getScore() != JUConstants.intUNKNOWN) pairElement.setAttribute(StatechumXML.ATTR_SCORE.name(), Long.toString(element.getScore())); if (element.getAnotherScore() != JUConstants.intUNKNOWN) pairElement.setAttribute( StatechumXML.ATTR_OTHERSCORE.name(), Long.toString(element.getAnotherScore())); return pairElement; }