/** Checks if a PTA constructed is consistent with provided sequences. */ private void checkPTAConsistency( PTASequenceEngine engine, Set<List<Label>> sequences, boolean accept) { SequenceSet initSeq = engine.new SequenceSet(); initSeq.setIdentity(); int leafNumber = engine.getDebugDataMapDepth(null).size(); // Now we check the consistency for (List<Label> seq : sequences) { SequenceSet endOfSeq = initSeq.crossWithSequence(seq); // this is aimed to follow a path in a PTA // to a state which is not necessarily a tail-state (hence no use calling // getDebugDataMapDepth(null), // but can inadvertently add a new sequence, hence we have to check that it has to happened at // the end. Map<String, String> map = engine.getDebugDataMapDepth(endOfSeq); assert map.size() == 1 : "expected size of 1, got " + map.size(); String attrs = map.values().iterator().next(); // For reject-sequences, // If the end of the sequence is not a leaf, it should be considered an accept-sequence, hence // throw. // If the end of the sequence is a leaf but should be returned, this is also an // accept-sequence (see the sequence engine construction above) throw. // The only remaining case is that the end is a leaf and should not be returned - this is ok. if (!accept && !attrs.equals(DebugDataValues.booleanToString(true, false))) throw new IllegalArgumentException( "reject-sequence " + seq + " is present in PTA with a positive ending"); // For accept-sequences, the only erroneous case is when a leaf is not returned. // (we assume that non-leaf is always returned.) if (accept && attrs.equals(DebugDataValues.booleanToString(true, false))) throw new IllegalArgumentException( "reject-sequence " + seq + " is present in PTA with a negative ending"); } Assert.assertEquals(leafNumber, engine.getDebugDataMapDepth(null).size()); }
public static PTASequenceEngine buildPTA( Set<List<Label>> plusStrings, Set<List<Label>> minusStrings) { final Boolean accept = new Boolean(true), reject = new Boolean(false); boolean theOnlyStateReject = false; for (List<Label> seq : minusStrings) if (seq.isEmpty()) { theOnlyStateReject = true; break; } final Boolean rejectAllStates = new Boolean(theOnlyStateReject); final AtomicBoolean statesAccept = new AtomicBoolean(true); PTASequenceEngine allSequences = new PTASequenceEngine(); // Here we are building a PTA which consists of accept states for plusStrings and reject-states // for minusStrings. This way if a plus string is a prefix of a plusString, the prefix will // be labelled with Boolean(true) states and the suffix - with Boolean(false) states. // Note that the suffix may contain many states. allSequences.init( new PTASequenceSetAutomaton() { @Override public Object getTheOnlyState() { return statesAccept.get() ? accept : reject; } @Override public boolean shouldBeReturned(Object elem) { return elem != null && ((Boolean) elem).booleanValue(); } @Override public boolean isAccept(@SuppressWarnings("unused") Object elem) { return !rejectAllStates.booleanValue(); } }); SequenceSet initSeq = allSequences.new SequenceSet(); initSeq.setIdentity(); initSeq.cross(plusStrings); statesAccept.getAndSet(false); initSeq.cross(minusStrings); return allSequences; }