You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by pk...@apache.org on 2019/11/23 13:34:17 UTC
svn commit: r1870237 - in /uima/uv3/ruta-v3/trunk/ruta-core/src:
main/java/org/apache/uima/ruta/ main/java/org/apache/uima/ruta/rule/
test/java/org/apache/uima/ruta/rule/
Author: pkluegl
Date: Sat Nov 23 13:34:17 2019
New Revision: 1870237
URL: http://svn.apache.org/viewvc?rev=1870237&view=rev
Log:
UIMA-6150: fix right to left match in window by simplifying code, avoid using subiterators in uima v3
Modified:
uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java
uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java
uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/RuleInference3Test.java
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java?rev=1870237&r1=1870236&r2=1870237&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java Sat Nov 23 13:34:17 2019
@@ -199,7 +199,9 @@ public class RutaStream {
private void updateIterators(CAS cas, Type basicType, FilterManager filter,
AnnotationFS additionalWindow) {
if (additionalWindow != null) {
- this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
+ this.basicIt = cas.getAnnotationIndex(basicType).select().coveredBy(additionalWindow)
+ .fsIterator();
+ // was: this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
} else {
this.basicIt = cas.getAnnotationIndex(basicType).iterator();
}
@@ -690,7 +692,10 @@ public class RutaStream {
}
FSMatchConstraint defaultConstraint = filter.getDefaultConstraint();
FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(
- cas.getAnnotationIndex(basicType).subiterator(windowAnnotation), defaultConstraint);
+ cas.getAnnotationIndex(basicType).select().coveredBy(windowAnnotation).fsIterator(),
+ defaultConstraint);
+// FSIterator<AnnotationFS> iterator = cas.createFilteredIterator(
+// cas.getAnnotationIndex(basicType).subiterator(windowAnnotation), defaultConstraint);
while (iterator.isValid()) {
result.add((RutaBasic) iterator.get());
@@ -936,9 +941,13 @@ public class RutaStream {
|| windowAnnotation.getEnd() != cas.getDocumentAnnotation().getEnd())) {
AnnotationFS frame = cas.createAnnotation(cas.getTypeSystem().getType(RutaEngine.FRAME_TYPE),
windowAnnotation.getBegin(), windowAnnotation.getEnd());
- FSIterator<AnnotationFS> subiterator = cas.getAnnotationIndex(type).subiterator(frame);
- while (subiterator.hasNext()) {
- AnnotationFS each = subiterator.next();
+ FSIterator<AnnotationFS> iterator = cas.getAnnotationIndex(type).select().coveredBy(frame)
+ .fsIterator();
+ // was: FSIterator<AnnotationFS> subiterator =
+ // cas.getAnnotationIndex(type).subiterator(frame);
+
+ while (iterator.hasNext()) {
+ AnnotationFS each = iterator.next();
if (isVisible(each)) {
result.add(each);
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java?rev=1870237&r1=1870236&r2=1870237&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java Sat Nov 23 13:34:17 2019
@@ -106,95 +106,67 @@ public class RutaAnnotationTypeMatcher i
if (annotation.getEnd() == stream.getDocumentAnnotation().getEnd()) {
return Collections.emptyList();
}
- RutaBasic lastBasic = stream.getEndAnchor(annotation.getEnd());
- int end = 0;
- if (lastBasic == null) {
- if (annotation.getEnd() != 0) {
- return Collections.emptyList();
- }
- } else {
- end = lastBasic.getEnd();
- }
- if (end == stream.getDocumentAnnotation().getBegin()) {
- // non existing wildcard match
- stream.moveToFirst();
- } else if (annotation.getEnd() > 0) {
- stream.moveTo(lastBasic);
- if (stream.isVisible(lastBasic) && stream.isValid()
- && stream.get().getEnd() == lastBasic.getEnd()) {
- stream.moveToNext();
- }
- } else {
- stream.moveToFirst();
+ RutaBasic nextBasic = stream.getBasicNextTo(false, annotation);
+ if (nextBasic == null) {
+ return Collections.emptyList();
}
- if (stream.isValid()) {
- RutaBasic nextBasic = (RutaBasic) stream.get();
- // TODO HOTFIX for annotation of length 0
- while (stream.isValid() && nextBasic.getBegin() < end) {
- stream.moveToNext();
- if (stream.isValid()) {
- nextBasic = (RutaBasic) stream.get();
+ MatchContext context = new MatchContext(parent);
+ // just for forcing expression top initialize
+ expression.getType(context, stream);
+ if (expression.getAnnotationExpression() != null) {
+
+ AnnotationFS ref = expression.getAnnotation(context, stream);
+ if (ref != null) {
+ boolean beginsWith = nextBasic.getBegin() == ref.getBegin();
+ if (beginsWith) {
+ Collection<AnnotationFS> result = new ArrayList<>(1);
+ result.add(ref);
+ return result;
}
}
-
- MatchContext context = new MatchContext(parent);
- // just for forcing expression top initialize
- expression.getType(context, stream);
- if (expression.getAnnotationExpression() != null) {
-
- AnnotationFS ref = expression.getAnnotation(context, stream);
- if (ref != null) {
- boolean beginsWith = nextBasic.getBegin() == ref.getBegin();
- if (beginsWith) {
- Collection<AnnotationFS> result = new ArrayList<>(1);
- result.add(ref);
- return result;
- }
- }
- } else if (expression.getAnnotationListExpression() != null) {
- List<AnnotationFS> annotations = expression.getAnnotationList(context, stream);
- Collection<AnnotationFS> result = new ArrayList<>();
- for (AnnotationFS each : annotations) {
- boolean beginsWith = nextBasic.getBegin() == each.getBegin();
- if (beginsWith) {
- result.add(each);
- }
+ } else if (expression.getAnnotationListExpression() != null) {
+ List<AnnotationFS> annotations = expression.getAnnotationList(context, stream);
+ Collection<AnnotationFS> result = new ArrayList<>();
+ for (AnnotationFS each : annotations) {
+ boolean beginsWith = nextBasic.getBegin() == each.getBegin();
+ if (beginsWith) {
+ result.add(each);
}
- return result;
+ }
+ return result;
+ } else {
+ List<Type> types = null;
+ if (expression.getTypeListExpression() != null) {
+ types = expression.getTypeListExpression().getTypeList(context, stream);
} else {
- List<Type> types = null;
- if (expression.getTypeListExpression() != null) {
- types = expression.getTypeListExpression().getTypeList(context, stream);
- } else {
- Type type = getType(context.getParent(), stream);
- types = new ArrayList<>(1);
- types.add(type);
- }
- List<AnnotationFS> annotations = new ArrayList<>();
- for (Type type : types) {
- Collection<AnnotationFS> anchors = new ArrayList<>();
- Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(type);
- if (beginAnchors != null) {
- for (AnnotationFS afs : beginAnchors) {
- if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()
- && afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) {
- anchors.add(afs);
- }
+ Type type = getType(context.getParent(), stream);
+ types = new ArrayList<>(1);
+ types.add(type);
+ }
+ List<AnnotationFS> annotations = new ArrayList<>();
+ for (Type type : types) {
+ Collection<AnnotationFS> anchors = new ArrayList<>();
+ Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(type);
+ if (beginAnchors != null) {
+ for (AnnotationFS afs : beginAnchors) {
+ if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()
+ && afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) {
+ anchors.add(afs);
}
}
- if (expression.getFeatureExpression() != null) {
- annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors,
- CHECK_ON_FEATURE, context, stream));
- } else {
- annotations.addAll(anchors);
- }
-
}
- return annotations;
- }
+ if (expression.getFeatureExpression() != null) {
+ annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors,
+ CHECK_ON_FEATURE, context, stream));
+ } else {
+ annotations.addAll(anchors);
+ }
+ }
+ return annotations;
}
+
return Collections.emptyList();
}
@@ -204,80 +176,61 @@ public class RutaAnnotationTypeMatcher i
if (annotation.getBegin() == stream.getDocumentAnnotation().getBegin()) {
return Collections.emptyList();
}
- RutaBasic firstBasic = stream.getBeginAnchor(annotation.getBegin());
- if (firstBasic == null) {
+ RutaBasic nextBasic = stream.getBasicNextTo(true, annotation);
+ if (nextBasic == null) {
return Collections.emptyList();
}
- stream.moveTo(firstBasic);
- if (stream.isVisible(firstBasic)) {
- stream.moveToPrevious();
- }
- if (firstBasic.getBegin() == stream.getDocumentAnnotation().getEnd()) {
- // non existing wildcard match
- stream.moveToLast();
- }
-
- if (stream.isValid()) {
- RutaBasic nextBasic = (RutaBasic) stream.get();
- // TODO HOTFIX for annotation of length 0
- while (stream.isValid() && nextBasic.getEnd() > firstBasic.getBegin()) {
- stream.moveToPrevious();
- if (stream.isValid()) {
- nextBasic = (RutaBasic) stream.get();
- }
- }
- MatchContext context = new MatchContext(parent);
- // just for forcing expression top initialize
- expression.getType(context, stream);
- if (expression.getAnnotationExpression() != null) {
- AnnotationFS ref = expression.getAnnotationExpression().getAnnotation(context, stream);
- boolean endsWith = nextBasic.getEnd() == ref.getEnd();
+ MatchContext context = new MatchContext(parent);
+ // just for forcing expression top initialize
+ expression.getType(context, stream);
+ if (expression.getAnnotationExpression() != null) {
+ AnnotationFS ref = expression.getAnnotationExpression().getAnnotation(context, stream);
+ boolean endsWith = nextBasic.getEnd() == ref.getEnd();
+ if (endsWith) {
+ Collection<AnnotationFS> result = new ArrayList<>(1);
+ result.add(ref);
+ return result;
+ }
+ } else if (expression.getAnnotationListExpression() != null) {
+ List<AnnotationFS> annotations = expression.getAnnotationListExpression()
+ .getAnnotationList(context, stream);
+ for (AnnotationFS each : annotations) {
+ boolean endsWith = nextBasic.getEnd() == each.getEnd();
if (endsWith) {
- Collection<AnnotationFS> result = new ArrayList<>(1);
- result.add(ref);
+ Collection<AnnotationFS> result = new ArrayList<>();
+ result.add(each);
return result;
}
- } else if (expression.getAnnotationListExpression() != null) {
- List<AnnotationFS> annotations = expression.getAnnotationListExpression()
- .getAnnotationList(context, stream);
- for (AnnotationFS each : annotations) {
- boolean endsWith = nextBasic.getEnd() == each.getEnd();
- if (endsWith) {
- Collection<AnnotationFS> result = new ArrayList<>();
- result.add(each);
- return result;
- }
- }
+ }
+ } else {
+ List<Type> types = null;
+ if (expression.getTypeListExpression() != null) {
+ types = expression.getTypeListExpression().getTypeList(context, stream);
} else {
- List<Type> types = null;
- if (expression.getTypeListExpression() != null) {
- types = expression.getTypeListExpression().getTypeList(context, stream);
- } else {
- Type type = getType(context.getParent(), stream);
- types = new ArrayList<>(1);
- types.add(type);
- }
- List<AnnotationFS> annotations = new ArrayList<>();
- for (Type type : types) {
- Collection<AnnotationFS> anchors = new ArrayList<>();
- Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(type);
- if (endAnchors != null) {
- for (AnnotationFS afs : endAnchors) {
- if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
- anchors.add(afs);
- }
+ Type type = getType(context.getParent(), stream);
+ types = new ArrayList<>(1);
+ types.add(type);
+ }
+ List<AnnotationFS> annotations = new ArrayList<>();
+ for (Type type : types) {
+ Collection<AnnotationFS> anchors = new ArrayList<>();
+ Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(type);
+ if (endAnchors != null) {
+ for (AnnotationFS afs : endAnchors) {
+ if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
+ anchors.add(afs);
}
}
- if (expression.getFeatureExpression() != null) {
- annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors,
- CHECK_ON_FEATURE, context, stream));
- } else {
- annotations.addAll(anchors);
- }
}
- return annotations;
+ if (expression.getFeatureExpression() != null) {
+ annotations.addAll(expression.getFeatureExpression().getAnnotations(anchors,
+ CHECK_ON_FEATURE, context, stream));
+ } else {
+ annotations.addAll(anchors);
+ }
}
+ return annotations;
}
return Collections.emptyList();
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java?rev=1870237&r1=1870236&r2=1870237&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/ManualAnchoringTest.java Sat Nov 23 13:34:17 2019
@@ -33,11 +33,34 @@ public class ManualAnchoringTest {
script += "CW{-> T1};\n";
script += "\"and\"{-> T2};\n";
script += "T1 (COMMA T1)* @T2 T1 {->MARK(T3,1,4)};\n";
+
CAS cas = RutaTestUtils.getCAS(document);
Ruta.apply(cas, script);
RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "A, B and C");
+ }
- cas.release();
+ @Test
+ public void testInWindow() throws Exception {
+ String document = "A, B and C. A, B and C.";
+ String script = "";
+ script += "ANY+{-PARTOF({PERIOD,T1}) -> T1};\n";
+ script += "BLOCK(block) T1{}{\n";
+ script += "SW CW{-> T2};\n";
+ script += "SW @CW{-> T3};\n";
+ script += "}\n";
+ script += "T1->{\n";
+ script += "SW CW{-> T4};\n";
+ script += "SW @CW{-> T5};\n";
+ script += "};\n";
+
+ CAS cas = RutaTestUtils.getCAS(document);
+ Ruta.apply(cas, script);
+
+ RutaTestUtils.assertAnnotationsEquals(cas, 2, 2, "C", "C");
+ RutaTestUtils.assertAnnotationsEquals(cas, 3, 2, "C", "C");
+ RutaTestUtils.assertAnnotationsEquals(cas, 4, 2, "C", "C");
+ RutaTestUtils.assertAnnotationsEquals(cas, 5, 2, "C", "C");
}
+
}
Modified: uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/RuleInference3Test.java
URL: http://svn.apache.org/viewvc/uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/RuleInference3Test.java?rev=1870237&r1=1870236&r2=1870237&view=diff
==============================================================================
--- uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/RuleInference3Test.java (original)
+++ uima/uv3/ruta-v3/trunk/ruta-core/src/test/java/org/apache/uima/ruta/rule/RuleInference3Test.java Sat Nov 23 13:34:17 2019
@@ -31,7 +31,5 @@ public class RuleInference3Test {
CAS cas = RutaTestUtils.processTestScript(this.getClass());
RutaTestUtils.assertAnnotationsEquals(cas, 4, 4, "[1]", "[2]", "[3]", "[4]");
-
- cas.release();
}
}