Ejemplo n.º 1
0
 /**
  * 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);
 }
Ejemplo n.º 2
0
  /**
   * 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;
  }