/** * 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); }
public static <TARGET_TYPE, CACHE_TYPE extends CachedData<TARGET_TYPE, CACHE_TYPE>> void checkDepthLabelling(AbstractLearnerGraph<TARGET_TYPE, CACHE_TYPE> coregraph) { int coreGraphStateNumber = coregraph.getStateNumber(); Map<CmpVertex, Integer> stateToDepth = coregraph.config.getTransitionMatrixImplType() == STATETREE.STATETREE_ARRAY ? new ArrayMapWithSearch<CmpVertex, Integer>(coreGraphStateNumber) : new HashMapWithSearch<CmpVertex, Integer>(coreGraphStateNumber); CmpVertex from = coregraph.getInit(); stateToDepth.put(from, 0); Queue<CmpVertex> fringe = new LinkedList<CmpVertex>(); Map<CmpVertex, CmpVertex> statesInFringe = coregraph.config.getTransitionMatrixImplType() == STATETREE.STATETREE_ARRAY ? new ArrayMapWithSearch<CmpVertex, CmpVertex>(coreGraphStateNumber) : new HashMapWithSearch<CmpVertex, CmpVertex>( coreGraphStateNumber); // in order not to iterate through the list all the time. fringe.add(from); statesInFringe.put(from, from); while (!fringe.isEmpty()) { CmpVertex currentState = fringe.remove(); Integer currentDepth = stateToDepth.get(currentState); Map<Label, TARGET_TYPE> targets = coregraph.transitionMatrix.get(currentState); if (targets != null && !targets.isEmpty()) for (Entry<Label, TARGET_TYPE> labelstate : targets.entrySet()) for (CmpVertex target : coregraph.getTargets(labelstate.getValue())) { if (!statesInFringe.containsKey( target)) // put returns the old value, so if it returned null, it means that target // was not already in the list (but it has since been added) { int newDepth = currentDepth + 1; stateToDepth.put(target, newDepth); Assert.assertEquals("state " + target, newDepth, target.getDepth()); fringe.offer(target); statesInFringe.put(target, target); } } } }