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 2014/01/07 17:52:23 UTC

svn commit: r1556271 - in /uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta: resource/MultiTreeWordList.java rule/ComposedRuleElementMatch.java rule/RutaTypeMatcher.java type/RutaBasic.java

Author: pkluegl
Date: Tue Jan  7 16:52:22 2014
New Revision: 1556271

URL: http://svn.apache.org/r1556271
Log:
UIMA-2332
some more speed improvements
- mtwl: remember found types
- composed rule element matches: cache matched annotations
- type matcher: exploit caching in RutaBasic
- RutaBasic: do not cache TOP and AnnotationBase

Modified:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/resource/MultiTreeWordList.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/type/RutaBasic.java

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/resource/MultiTreeWordList.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/resource/MultiTreeWordList.java?rev=1556271&r1=1556270&r2=1556271&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/resource/MultiTreeWordList.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/resource/MultiTreeWordList.java Tue Jan  7 16:52:22 2014
@@ -744,7 +744,7 @@ public class MultiTreeWordList implement
             // ignoreCase,
             // ignoreLength, edit, distance, ignoreToken);
 
-            tryToCreateAnnotation(stream, results, basicsToAdd, candidate.toString(), interResults,
+            tryToCreateAnnotation(types, stream, results, basicsToAdd, candidate.toString(), interResults,
                     ignoreCase, ignoreLength, edit, distance, ignoreToken, typeMap);
             //
             lastCandidate = candidate.toString();
@@ -753,7 +753,7 @@ public class MultiTreeWordList implement
 
           } else {
             // !streamPointer.isValid();
-            tryToCreateAnnotation(stream, results, basicsToAdd, lastCandidate, interResults,
+            tryToCreateAnnotation(types, stream, results, basicsToAdd, lastCandidate, interResults,
                     ignoreCase, ignoreLength, edit, distance, ignoreToken, typeMap);
           }
         } else {
@@ -782,15 +782,12 @@ public class MultiTreeWordList implement
     return new ArrayList<AnnotationFS>();
   }
 
-  private void tryToCreateAnnotation(RutaStream stream, Collection<AnnotationFS> results,
+  private void tryToCreateAnnotation(List<String> types, RutaStream stream, Collection<AnnotationFS> results,
           List<RutaBasic> basicsToAdd, String lastCandidate, List<AnnotationFS> interResult,
           boolean ignoreCase, int ignoreLength, boolean edit, double distance, String ignoreToken,
           Map<String, Type> map) {
-
-    List<String> contains = contains(lastCandidate, ignoreCase, ignoreLength, edit, distance,
-            ignoreToken);
-    if (basicsToAdd.size() >= 1 || contains.isEmpty()) {
-      for (String each : contains) {
+    if (basicsToAdd.size() >= 1 && types != null) {
+      for (String each : types) {
         Type type = map.get(each);
         if (type != null) {
           int begin = basicsToAdd.get(0).getBegin();

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java?rev=1556271&r1=1556270&r2=1556271&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/ComposedRuleElementMatch.java Tue Jan  7 16:52:22 2014
@@ -20,7 +20,6 @@
 package org.apache.uima.ruta.rule;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -36,6 +35,10 @@ public class ComposedRuleElementMatch ex
 
   private Map<RuleElement, List<RuleElementMatch>> innerMatches;
 
+  private boolean textMatchedUpdated = false;
+
+  private List<AnnotationFS> textMatched = null;
+
   public ComposedRuleElementMatch(ComposedRuleElement ruleElement,
           ComposedRuleElementMatch containerMatch) {
     super(ruleElement, containerMatch);
@@ -70,8 +73,8 @@ public class ComposedRuleElementMatch ex
     }
     list.add(ruleElementMatch);
     textsMatched.addAll(ruleElementMatch.getTextsMatched());
-
     evaluateInnerMatches(included, stream);
+    textMatchedUpdated = false;
   }
 
   public void evaluateInnerMatches(boolean included, RutaStream stream) {
@@ -189,6 +192,7 @@ public class ComposedRuleElementMatch ex
         }
       }
     }
+    textMatchedUpdated = false;
   }
 
   public String toString() {
@@ -196,17 +200,20 @@ public class ComposedRuleElementMatch ex
   }
 
   public List<AnnotationFS> getTextsMatched() {
-    // TODO find a better solution for this
-    Collection<AnnotationFS> set = new TreeSet<AnnotationFS>(new AnnotationComparator());
-    Collection<List<RuleElementMatch>> values = innerMatches.values();
-    for (List<RuleElementMatch> list : values) {
-      if (list != null) {
-        for (RuleElementMatch ruleElementMatch : list) {
-          set.addAll(ruleElementMatch.getTextsMatched());
+    if (!textMatchedUpdated || textMatched == null) {
+      Collection<AnnotationFS> set = new TreeSet<AnnotationFS>(new AnnotationComparator());
+      Collection<List<RuleElementMatch>> values = innerMatches.values();
+      for (List<RuleElementMatch> list : values) {
+        if (list != null) {
+          for (RuleElementMatch ruleElementMatch : list) {
+            set.addAll(ruleElementMatch.getTextsMatched());
+          }
         }
       }
+      textMatched = new ArrayList<AnnotationFS>(set);
+      textMatchedUpdated = true;
     }
-    return Arrays.asList(set.toArray(new AnnotationFS[0]));
+    return textMatched;
   }
 
   public void setConditionInfo(List<EvaluatedCondition> evaluatedConditions) {

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java?rev=1556271&r1=1556270&r2=1556271&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java Tue Jan  7 16:52:22 2014
@@ -23,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Set;
 import java.util.TreeSet;
 
 import org.apache.uima.cas.ConstraintFactory;
@@ -115,15 +116,11 @@ public class RutaTypeMatcher implements 
       List<Type> reTypes = getTypes(parent, stream);
       Collection<AnnotationFS> anchors = new TreeSet<AnnotationFS>(comparator);
       for (Type eachMatchType : reTypes) {
-        List<Type> types = stream.getCas().getTypeSystem().getProperlySubsumedTypes(eachMatchType);
-        types.add(eachMatchType);
-        for (Type eachType : types) {
-          Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(eachType);
-          if (beginAnchors != null) {
-            for (AnnotationFS afs : beginAnchors) {
-              if (afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) {
-                anchors.add(afs);
-              }
+        Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(eachMatchType);
+        if (beginAnchors != null) {
+          for (AnnotationFS afs : beginAnchors) {
+            if (afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) {
+              anchors.add(afs);
             }
           }
         }
@@ -160,15 +157,11 @@ public class RutaTypeMatcher implements 
       List<Type> reTypes = getTypes(parent, stream);
       Collection<AnnotationFS> anchors = new TreeSet<AnnotationFS>(comparator);
       for (Type eachMatchType : reTypes) {
-        List<Type> types = stream.getCas().getTypeSystem().getProperlySubsumedTypes(eachMatchType);
-        types.add(eachMatchType);
-        for (Type eachType : types) {
-          Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(eachType);
-          if (endAnchors != null) {
-            for (AnnotationFS afs : endAnchors) {
-              if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
-                anchors.add(afs);
-              }
+        Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(eachMatchType);
+        if (endAnchors != null) {
+          for (AnnotationFS afs : endAnchors) {
+            if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
+              anchors.add(afs);
             }
           }
         }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/type/RutaBasic.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/type/RutaBasic.java?rev=1556271&r1=1556270&r2=1556271&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/type/RutaBasic.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/type/RutaBasic.java Tue Jan  7 16:52:22 2014
@@ -28,6 +28,10 @@ public class RutaBasic extends Annotatio
 
   private static final int INITIAL_CAPACITY = 2;
 
+  private static final String ROOT_TYPE1 = "uima.tcas.Annotation";
+
+  private static final String ROOT_TYPE2 = "uima.cas.AnnotationBase";
+
   private boolean lowMemoryProfile = false;
 
   private Map<String, Integer> partOf = new TreeMap<String, Integer>();
@@ -53,7 +57,8 @@ public class RutaBasic extends Annotatio
     }
     count++;
     partOf.put(type.getName(), count);
-    if (!lowMemoryProfile) {
+    if (!lowMemoryProfile && !type.getName().equals(ROOT_TYPE1)
+            && !type.getName().equals(ROOT_TYPE2)) {
       TypeSystem typeSystem = getCAS().getTypeSystem();
       Type parent = typeSystem.getParent(type);
       if (parent != null) {
@@ -181,7 +186,8 @@ public class RutaBasic extends Annotatio
       beginMap.put(type, list);
     }
     list.add(annotation);
-    if (!lowMemoryProfile) {
+    if (!lowMemoryProfile && !type.getName().equals(ROOT_TYPE1)
+            && !type.getName().equals(ROOT_TYPE2)) {
       TypeSystem typeSystem = getCAS().getTypeSystem();
       Type parent = typeSystem.getParent(type);
       if (parent != null) {
@@ -197,7 +203,8 @@ public class RutaBasic extends Annotatio
       endMap.put(type, list);
     }
     list.add(annotation);
-    if (!lowMemoryProfile) {
+    if (!lowMemoryProfile && !type.getName().equals(ROOT_TYPE1)
+            && !type.getName().equals(ROOT_TYPE2)) {
       TypeSystem typeSystem = getCAS().getTypeSystem();
       Type parent = typeSystem.getParent(type);
       if (parent != null) {