Author: pkluegl
Date: Wed Feb  6 12:27:44 2013
New Revision: 1442926

URL: http://svn.apache.org/viewvc?rev=1442926&view=rev
Log:
[UIMA-2640]
- fixed SHIFT action
- added test
- extended documentation

Added:
    
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/action/ShiftTest2.java
    
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.tm
    
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.txt
Modified:
    
uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/action/ShiftAction.java
    
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/AllActionsTest.java
    
uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.language.actions.xml

Modified: 
uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/action/ShiftAction.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/action/ShiftAction.java?rev=1442926&r1=1442925&r2=1442926&view=diff
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/action/ShiftAction.java
 (original)
+++ 
uima/sandbox/textmarker/trunk/textmarker-core/src/main/java/org/apache/uima/textmarker/action/ShiftAction.java
 Wed Feb  6 12:27:44 2013
@@ -20,11 +20,16 @@
 package org.apache.uima.textmarker.action;
 
 import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
 
+import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.jcas.tcas.Annotation;
 import org.apache.uima.textmarker.TextMarkerStream;
 import org.apache.uima.textmarker.expression.number.NumberExpression;
 import org.apache.uima.textmarker.expression.type.TypeExpression;
+import org.apache.uima.textmarker.rule.AnnotationComparator;
 import org.apache.uima.textmarker.rule.RuleElement;
 import org.apache.uima.textmarker.rule.RuleMatch;
 import org.apache.uima.textmarker.visitor.InferenceCrowd;
@@ -38,15 +43,28 @@ public class ShiftAction extends MarkAct
   @Override
   public void execute(RuleMatch match, RuleElement element, TextMarkerStream 
stream,
           InferenceCrowd crowd) {
+    Type targetType = type.getType(element.getParent());
     List<Integer> indexList = getIndexList(element, list);
-    List<AnnotationFS> matchedAnnotations = 
match.getMatchedAnnotations(stream, indexList,
+    List<AnnotationFS> destinationAnnotationSpans = 
match.getMatchedAnnotations(stream, indexList,
             element.getContainer());
-    for (AnnotationFS matchedAnnotation : matchedAnnotations) {
-      List<AnnotationFS> matchedAnnotationsOf = 
match.getMatchedAnnotationsOf(element, stream);
-      for (AnnotationFS annotationFS : matchedAnnotationsOf) {
-        stream.removeAnnotation(annotationFS, annotationFS.getType());
-        createAnnotation(matchedAnnotation, element, stream, match);
+    List<AnnotationFS> annotationsMatchedByRuleElementofAction = 
match.getMatchedAnnotationsOf(element, stream);
+    int size = Math.min(annotationsMatchedByRuleElementofAction.size(), 
destinationAnnotationSpans.size());
+    for (int i = 0; i < size; i++) {
+      AnnotationFS eachMatched = 
annotationsMatchedByRuleElementofAction.get(i);
+      AnnotationFS eachDestination = destinationAnnotationSpans.get(i);
+      Set<AnnotationFS> allAnchoredAnnotations = new TreeSet<AnnotationFS>(new 
AnnotationComparator());
+      Set<AnnotationFS> beginAnchors = 
stream.getBeginAnchor(eachMatched.getBegin()).getBeginAnchors(targetType);
+      Set<AnnotationFS> endAnchors = 
stream.getEndAnchor(eachMatched.getEnd()).getEndAnchors(targetType);
+      allAnchoredAnnotations.addAll(beginAnchors);
+      allAnchoredAnnotations.addAll(endAnchors);
+      for (AnnotationFS eachAnchored : allAnchoredAnnotations) {
+        Annotation a = (Annotation) eachAnchored;
+        stream.removeAnnotation(a);
+        a.setBegin(eachDestination.getBegin());
+        a.setEnd(eachDestination.getEnd());
+        stream.addAnnotation(a, true, match);
       }
     }
   }
+
 }

Modified: 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/AllActionsTest.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/AllActionsTest.java?rev=1442926&r1=1442925&r2=1442926&view=diff
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/AllActionsTest.java
 (original)
+++ 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/AllActionsTest.java
 Wed Feb  6 12:27:44 2013
@@ -41,6 +41,7 @@ import org.apache.uima.textmarker.action
 import org.apache.uima.textmarker.action.RetainTypeTest;
 import org.apache.uima.textmarker.action.SetFeatureTest;
 import org.apache.uima.textmarker.action.ShiftTest;
+import org.apache.uima.textmarker.action.ShiftTest2;
 import org.apache.uima.textmarker.action.TransferTest;
 import org.apache.uima.textmarker.action.TrieTest;
 import org.apache.uima.textmarker.action.TrimTest;
@@ -56,7 +57,7 @@ import org.junit.runners.Suite.SuiteClas
     MarkLastTest.class, MarkOnceTest.class, MarkScoreTest.class, 
MarkTableTest.class,
     MarkTest.class, MatchedTextTest.class, MergeTest.class, 
RemoveDuplicateTest.class,
     RemoveTest.class, ReplaceTest.class, RetainTypeTest.class, 
SetFeatureTest.class,
-    ShiftTest.class, TransferTest.class, TrieTest.class, TrimTest.class, 
UnmarkAllTest.class,
+    ShiftTest.class, ShiftTest2.class, TransferTest.class, TrieTest.class, 
TrimTest.class, UnmarkAllTest.class,
     UnmarkTest.class })
 public class AllActionsTest {
 

Added: 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/action/ShiftTest2.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/action/ShiftTest2.java?rev=1442926&view=auto
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/action/ShiftTest2.java
 (added)
+++ 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/java/org/apache/uima/textmarker/action/ShiftTest2.java
 Wed Feb  6 12:27:44 2013
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.uima.textmarker.action;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.textmarker.TextMarkerTestUtils;
+import org.apache.uima.textmarker.TextMarkerTestUtils.TestFeature;
+import org.junit.Test;
+
+public class ShiftTest2 {
+
+  @Test
+  public void test() {
+    String name = this.getClass().getSimpleName();
+    String namespace = 
this.getClass().getPackage().getName().replaceAll("\\.", "/");
+    
+    Map<String, String> complexTypes = new HashMap<String, String>();
+    String typeName = "org.apache.uima.FS";
+    complexTypes.put(typeName, "uima.tcas.Annotation");
+    
+    Map<String, List<TestFeature>> features = new TreeMap<String, 
List<TestFeature>>();
+    List<TestFeature> list = new ArrayList<TextMarkerTestUtils.TestFeature>();
+    features.put(typeName, list);
+    String fn1 = "doc";
+    list.add(new TestFeature(fn1, "", "uima.tcas.Annotation"));
+    String fn2 = "lang";
+    list.add(new TestFeature(fn2, "", "uima.cas.String"));
+    
+    CAS cas = null;
+    try {
+      cas = TextMarkerTestUtils.process(namespace + "/" + name + ".tm", 
namespace + "/" + name
+              + ".txt", 50, false, false, complexTypes, features, namespace + 
"/");
+    } catch (Exception e) {
+      e.printStackTrace();
+      assert (false);
+    }
+    AnnotationIndex<AnnotationFS> ai = null;
+    FSIterator<AnnotationFS> iterator = null;
+
+    Type type = cas.getTypeSystem().getType(typeName);
+    Feature f1 = type.getFeatureByBaseName(fn1);
+    Feature f2 = type.getFeatureByBaseName(fn2);
+    ai = cas.getAnnotationIndex(type);
+    iterator = ai.iterator();
+    assertEquals(1, ai.size());
+    AnnotationFS next = iterator.next();
+    AnnotationFS v1 = (AnnotationFS) next.getFeatureValue(f1);
+    String v2 = next.getStringValue(f2); 
+    assertEquals("only some text<br/>", v1.getCoveredText());
+    assertEquals("unknown", v2);
+    assertEquals("only some text", next.getCoveredText());
+    cas.release();
+  }
+}

Added: 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.tm
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.tm?rev=1442926&view=auto
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.tm
 (added)
+++ 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.tm
 Wed Feb  6 12:27:44 2013
@@ -0,0 +1,9 @@
+PACKAGE org.apache.uima;
+
+DECLARE T1, T2, T3, T4, T5, T6, T7, T8;
+
+DECLARE Annotation FS (Annotation doc, STRING lang);
+
+Document{-> CREATE(FS, "doc" = Document, "lang" = "unknown")};
+Document{ -> RETAINTYPE(MARKUP)};
+W{STARTSWITH(FS) -> SHIFT(FS, 1, 2)} W+ MARKUP;
\ No newline at end of file

Added: 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.txt
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.txt?rev=1442926&view=auto
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.txt
 (added)
+++ 
uima/sandbox/textmarker/trunk/textmarker-core/src/test/resources/org/apache/uima/textmarker/action/ShiftTest2.txt
 Wed Feb  6 12:27:44 2013
@@ -0,0 +1 @@
+only some text<br/>
\ No newline at end of file

Modified: 
uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.language.actions.xml
URL: 
http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.language.actions.xml?rev=1442926&r1=1442925&r2=1442926&view=diff
==============================================================================
--- 
uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.language.actions.xml
 (original)
+++ 
uima/sandbox/textmarker/trunk/textmarker-docbook/src/docbook/tools.textmarker.language.actions.xml
 Wed Feb  6 12:27:44 2013
@@ -1072,8 +1072,10 @@ Document{-> MARKTABLE(Struct, 1, TestTab
     <section id="ugr.tools.tm.language.actions.shift">
       <title>SHIFT</title>
       <para>
-        The SHIFT action can be used to change the offsets of an annotation. 
The optional number expression,
-        which points the the rule elements of the rule, specify the new 
offsets of the annotation.
+        The SHIFT action can be used to change the offsets of an annotation. 
The optional number expressions,
+        which point the rule elements of the rule, specify the new offsets of 
the annotation. The annotations that
+        will be modified have to start or end at the match of the rule element 
of the action. This means that the action 
+        has to be placed at a matching condition, which will be used to 
specify the annotations to be changed.
       </para>
       <section>
         <title>
@@ -1094,6 +1096,13 @@ Document{-> MARKTABLE(Struct, 1, TestTab
           In this example, an annotation of the type <quote>Author</quote> is 
expanded 
           in order to cover the following punctation mark.
         </para>
+        <para>
+          <programlisting><![CDATA[W{STARTSWITH(FS) -> SHIFT(FS, 1, 2)} W+ 
MARKUP;]]></programlisting>
+        </para>
+        <para>
+          In this example, an annotation of the type <quote>FS</quote> that 
consists mostly of words 
+          is shrinked by removing the last MARKUP annotation. 
+        </para>
       </section>
     </section>
 


Reply via email to