in ruta-core/src/main/java/org/apache/uima/ruta/rule/RuleMatch.java [98:201]
public List<AnnotationFS> getMatchedAnnotations(List<Integer> indexes,
RuleElementContainer container) {
List<AnnotationFS> result = new ArrayList<>();
indexes = extendIndexes(indexes);
if (container == null) {
container = rule.getRoot();
}
// TODO refactor this!
if (indexes == null) {
List<RuleElement> ruleElements = container.getRuleElements();
indexes = new ArrayList<>();
for (RuleElement ruleElement : ruleElements) {
indexes.add(ruleElements.indexOf(ruleElement) + 1);
}
}
// TODO refactor this
// this method should not be called by elements that need the actual annotation, e.g, for
// accessing theirs features.
// this is a hotfix for that case...
if (indexes.size() == 1) {
RuleElement ruleElement = container.getRuleElements().get(indexes.get(0) - 1);
List<List<RuleElementMatch>> matchInfo = getMatchInfo(ruleElement);
if (matchInfo.size() == 1) {
List<RuleElementMatch> list = matchInfo.get(0);
if (list != null && list.size() == 1) {
List<AnnotationFS> textsMatched = list.get(0).getTextsMatched();
if (textsMatched.size() == 1) {
AnnotationFS firstNormal = getFirstNormal(textsMatched);
if (firstNormal != null) {
return textsMatched;
}
}
}
}
}
// TODO rethink the reverse
List<List<List<RuleElementMatch>>> reverseList = new ArrayList<>();
for (Integer index : indexes) {
if (index > container.getRuleElements().size()) {
continue;
}
RuleElement ruleElement = container.getRuleElements().get(index - 1);
List<List<RuleElementMatch>> matchInfo = getMatchInfo(ruleElement);
int i = 0;
for (List<RuleElementMatch> list : matchInfo) {
if (reverseList.size() <= i) {
reverseList.add(new ArrayList<List<RuleElementMatch>>());
}
List<List<RuleElementMatch>> l = reverseList.get(i);
l.add(list);
i++;
}
}
CAS cas = null;
for (List<List<RuleElementMatch>> list : reverseList) {
int begin = Integer.MAX_VALUE;
int end = 0;
AnnotationFS singleAnnotation = null;
boolean mergingRequired = list.size() > 1;
for (List<RuleElementMatch> list2 : list) {
if (list2 != null) {
if (list2.size() > 1) {
mergingRequired = true;
}
if (list2.size() == 1 && !mergingRequired) {
RuleElementMatch ruleElementMatch = list2.get(0);
List<AnnotationFS> textsMatched = ruleElementMatch.getTextsMatched();
if (textsMatched != null && textsMatched.size() == 1) {
singleAnnotation = textsMatched.get(0);
} else {
mergingRequired = true;
}
}
if (mergingRequired) {
for (RuleElementMatch ruleElementMatch : list2) {
List<AnnotationFS> textsMatched = ruleElementMatch.getTextsMatched();
if (textsMatched != null && !textsMatched.isEmpty()) {
AnnotationFS first = getFirstNormal(textsMatched);
if (first != null) {
begin = Math.min(first.getBegin(), begin);
}
AnnotationFS last = getLastNormal(textsMatched);
if (last != null) {
end = Math.max(last.getEnd(), end);
}
if (cas == null && first != null) {
cas = first.getCAS();
}
}
}
}
}
}
if (singleAnnotation != null && !(singleAnnotation instanceof RutaOptional)) {
result.add(singleAnnotation);
} else if (cas != null && end != 0) {
AnnotationFS annotation = cas.createAnnotation(cas.getAnnotationType(), begin, end);
result.add(annotation);
}
}
return result;
}