public SequenceMatchResult<T> apply(SequenceMatchResult<T> seqMatchResult, int... groups) { if (seqMatchResult instanceof SequenceMatcher) { SequenceMatcher<T> matcher = (SequenceMatcher<T>) seqMatchResult; if (matcher.find()) { return matcher; } else { return null; } } else { return null; } } }
/** * Searches for pattern in the region starting * at the next index * @return true if a match is found (false otherwise) */ private boolean findNextNonOverlapping() { if (nextMatchStart < 0) { return false; } return find(nextMatchStart, false); }
private SequenceMatchResult<T> getNext() { boolean found = find(); if (found) { return toBasicSequenceMatchResult(); } else { return null; } }
/** * Reset the matcher and then searches for pattern at the specified start index * @param start - Index at which to start the search * @return true if a match is found (false otherwise) * @throws IndexOutOfBoundsException if start is {@literal <} 0 or larger then the size of the sequence * @see #find() */ public boolean find(int start) { if (start < 0 || start > elements.size()) { throw new IndexOutOfBoundsException("Invalid region start=" + start + ", need to be between 0 and " + elements.size()); } reset(); return find(start, false); }
public SequenceMatchResult<T> apply(SequenceMatchResult<T> seqMatchResult, int... groups) { SequenceMatcher<T> matcher = pattern.getMatcher(seqMatchResult.elements()); if (matcher.find()) { return matcher; } else { return null; } } }
private boolean findNextAll() { if (curMatchIter != null && curMatchIter.hasNext()) { while (curMatchIter.hasNext()) { int next = curMatchIter.next(); curMatchStates.setMatchedGroups(next); String sig = getMatchedSignature(); if (!prevMatchedSignatures.contains(sig)) { prevMatchedSignatures.add(sig); return true; } } } if (nextMatchStart < 0) { return false; } prevMatchedSignatures.clear(); boolean matched = find(nextMatchStart, false); if (matched) { Collection<Integer> matchedBranches = curMatchStates.getMatchIndices(); curMatchIter = matchedBranches.iterator(); int next = curMatchIter.next(); curMatchStates.setMatchedGroups(next); prevMatchedSignatures.add(getMatchedSignature()); } return matched; }
@Override public boolean extract(List<? extends T> seq, List<O> out) { if (seq == null) return false; boolean extracted = false; SequenceMatcher<T> m = pattern.getMatcher(seq); if (findType != null) { m.setFindType(findType); } m.setMatchWithResult(matchWithResult); while (m.find()) { out.add(extractor.apply(m)); extracted = true; } // System.err.println("SequencePattern " + pattern + " of type " + pattern.getClass() + " matched on " + extracted); return extracted; }
/** * Replaces all occurrences of the pattern with the specified list. * Use {@link #replaceAllExtended(java.util.List)} to replace with matched groups. * @param replacement What to replace the matched sequence with * @return New list with all occurrences of the pattern replaced * @see #replaceAllExtended(java.util.List) * @see #replaceFirst(java.util.List) * @see #replaceFirstExtended(java.util.List) */ public List<T> replaceAll(List<T> replacement) { List<T> res = new ArrayList<>(); FindType oldFindType = findType; findType = FindType.FIND_NONOVERLAPPING; int index = 0; while (find()) { // Copy from current index to found index res.addAll(elements().subList(index, start())); res.addAll(replacement); index = end(); } res.addAll(elements().subList(index, elements().size())); findType = oldFindType; return res; }
/** * Replaces the first occurrence of the pattern with the specified list. * Use {@link #replaceFirstExtended(java.util.List)} to replace with matched groups. * @param replacement What to replace the matched sequence with * @return New list with the first occurrence of the pattern replaced * @see #replaceAll(java.util.List) * @see #replaceAllExtended(java.util.List) * @see #replaceFirstExtended(java.util.List) */ public List<T> replaceFirst(List<T> replacement) { List<T> res = new ArrayList<>(); FindType oldFindType = findType; findType = FindType.FIND_NONOVERLAPPING; int index = 0; if (find()) { // Copy from current index to found index res.addAll(elements().subList(index, start())); res.addAll(replacement); index = end(); } res.addAll(elements().subList(index, elements().size())); findType = oldFindType; return res; }
/** * Replaces the first occurrence of the pattern with the specified list * of replacement items (can include matched groups). * @param replacement What to replace the matched sequence with * @return New list with the first occurrence of the pattern replaced * @see #replaceFirst(java.util.List) * @see #replaceAll(java.util.List) * @see #replaceAllExtended(java.util.List) */ public List<T> replaceFirstExtended(List<MatchReplacement<T>> replacement) { List<T> res = new ArrayList<>(); FindType oldFindType = findType; findType = FindType.FIND_NONOVERLAPPING; int index = 0; if (find()) { // Copy from current index to found index res.addAll(elements().subList(index, start())); for (MatchReplacement<T> r:replacement) { r.append(this, res); } index = end(); } res.addAll(elements().subList(index, elements().size())); findType = oldFindType; return res; }
/** * Replaces all occurrences of the pattern with the specified list * of replacement items (can include matched groups). * @param replacement What to replace the matched sequence with * @return New list with all occurrences of the pattern replaced * @see #replaceFirst(java.util.List) * @see #replaceFirstExtended(java.util.List) * @see #replaceAllExtended(java.util.List) */ public List<T> replaceAllExtended(List<MatchReplacement<T>> replacement) { List<T> res = new ArrayList<>(); FindType oldFindType = findType; findType = FindType.FIND_NONOVERLAPPING; int index = 0; while (find()) { // Copy from current index to found index res.addAll(elements().subList(index, start())); for (MatchReplacement<T> r:replacement) { r.append(this, res); } index = end(); } res.addAll(elements().subList(index, elements().size())); findType = oldFindType; return res; }
/** * Given a sequence, applies our patterns over the sequence and returns * all non overlapping matches. When multiple patterns overlaps, * matched patterns are selected to give the overall maximum score. * * @param elements input sequence to match against * @param scorer scorer for scoring each match * @return list of match results that are non-overlapping */ public List<SequenceMatchResult<T>> findNonOverlappingMaxScore(List<? extends T> elements, ToDoubleFunction<? super SequenceMatchResult> scorer) { Collection<SequencePattern<T>> triggered = getTriggeredPatterns(elements); List<SequenceMatchResult<T>> all = new ArrayList<>(); int i = 0; for (SequencePattern<T> p:triggered) { SequenceMatcher<T> m = p.getMatcher(elements); m.setMatchWithResult(matchWithResult); m.setOrder(i); while (m.find()) { all.add(m.toBasicSequenceMatchResult()); } i++; } List<SequenceMatchResult<T>> res = IntervalTree.getNonOverlappingMaxScore( all, SequenceMatchResult.TO_INTERVAL, scorer); res.sort(SequenceMatchResult.OFFSET_COMPARATOR); return res; }
while (matcher.find()) { List<? super IN> nodes = matcher.groupNodes(); if (nodes != null && ! nodes.isEmpty()) {
/** * Given a sequence, applies our patterns over the sequence and returns * all non overlapping matches. When multiple patterns overlaps, * matched patterns are selected by order specified by the comparator * @param elements input sequence to match against * @param cmp comparator indicating order that overlapped sequences should be selected. * @return list of match results that are non-overlapping */ public List<SequenceMatchResult<T>> findNonOverlapping(List<? extends T> elements, Comparator<? super SequenceMatchResult> cmp) { Collection<SequencePattern<T>> triggered = getTriggeredPatterns(elements); List<SequenceMatchResult<T>> all = new ArrayList<>(); int i = 0; for (SequencePattern<T> p:triggered) { if (Thread.interrupted()) { // Allow interrupting throw new RuntimeInterruptedException(); } SequenceMatcher<T> m = p.getMatcher(elements); m.setMatchWithResult(matchWithResult); m.setOrder(i); while (m.find()) { all.add(m.toBasicSequenceMatchResult()); } i++; } List<SequenceMatchResult<T>> res = IntervalTree.getNonOverlapping( all, SequenceMatchResult.TO_INTERVAL, cmp); res.sort(SequenceMatchResult.OFFSET_COMPARATOR); return res; }
/** * Given a sequence, applies our patterns over the sequence and returns * all matches, depending on the findType. When multiple patterns overlaps, * matched patterns are selected by order specified by the comparator * @param elements input sequence to match against * @param findType whether FindType.FIND_ALL or FindType.FIND_NONOVERLAPPING * @return list of match results */ public List<SequenceMatchResult<T>> find(List<? extends T> elements, SequenceMatcher.FindType findType) { Collection<SequencePattern<T>> triggered = getTriggeredPatterns(elements); List<SequenceMatchResult<T>> all = new ArrayList<>(); int i = 0; for (SequencePattern<T> p:triggered) { if (Thread.interrupted()) { // Allow interrupting throw new RuntimeInterruptedException(); } SequenceMatcher<T> m = p.getMatcher(elements); m.setMatchWithResult(matchWithResult); m.setFindType(findType); m.setOrder(i); while (m.find()) { all.add(m.toBasicSequenceMatchResult()); } i++; } List<SequenceMatchResult<T>> res = IntervalTree.getNonOverlapping( all, SequenceMatchResult.TO_INTERVAL, SequenceMatchResult.DEFAULT_COMPARATOR); res.sort(SequenceMatchResult.OFFSET_COMPARATOR); return res; }
/** * Searches for pattern in the region starting * at the next index * @return true if a match is found (false otherwise) */ private boolean findNextNonOverlapping() { if (nextMatchStart < 0) { return false; } return find(nextMatchStart, false); }
private SequenceMatchResult<T> getNext() { boolean found = find(); if (found) { return toBasicSequenceMatchResult(); } else { return null; } }
private SequenceMatchResult<T> getNext() { boolean found = find(); if (found) { return toBasicSequenceMatchResult(); } else { return null; } }
private SequenceMatchResult<T> getNext() { boolean found = find(); if (found) { return toBasicSequenceMatchResult(); } else { return null; } }
public SequenceMatchResult<T> apply(SequenceMatchResult<T> seqMatchResult, int... groups) { SequenceMatcher<T> matcher = pattern.getMatcher(seqMatchResult.elements()); if (matcher.find()) { return matcher; } else { return null; } } }