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 2017/01/05 12:55:02 UTC

svn commit: r1777465 - in /uima/ruta/trunk: example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ ruta-core-ext/src/main/java/org/apache/uima/ruta/block/ ruta-core-ext/src/main/java/org/apache/uima/ruta/bloc...

Author: pkluegl
Date: Thu Jan  5 12:55:02 2017
New Revision: 1777465

URL: http://svn.apache.org/viewvc?rev=1777465&view=rev
Log:
UIMA-4978
- added RutaAnnotationTypeMatcher as new default matcher
- allow label expressions as match conditions

Added:
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java   (with props)
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java   (with props)
Modified:
    uima/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleBlock.java
    uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java
    uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/fst/FSTBlock.java
    uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/GetFeatureAction.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/block/RutaScriptBlock.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/PositionCondition.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationMatcher.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.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/rule/WildCardRuleElement.java
    uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationFeatureExpressionTest.java
    uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java

Modified: uima/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleBlock.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleBlock.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleBlock.java (original)
+++ uima/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleBlock.java Thu Jan  5 12:55:02 2017
@@ -63,17 +63,15 @@ public class ExampleBlock extends RutaBl
         if (each == null) {
           continue;
         }
-        List<Type> types = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher().getTypes(
-                getParent() == null ? this : getParent(), stream);
-        for (Type eachType : types) {
-          RutaStream window = stream.getWindowStream(each, eachType);
+        Type type = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher()
+                .getType(getParent() == null ? this : getParent(), stream);
+          RutaStream window = stream.getWindowStream(each, type);
           List<RutaStatement> elements = new ArrayList<RutaStatement>(getElements());
           Collections.reverse(elements);
           for (RutaStatement element : elements) {
             if (element != null) {
               element.apply(window, crowd);
             }
-          }
         }
       }
     }

Modified: uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java (original)
+++ uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java Thu Jan  5 12:55:02 2017
@@ -61,10 +61,9 @@ public class OnlyFirstBlock extends Ruta
           continue;
         }
         boolean stop = false;
-        List<Type> types = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher().getTypes(
-                getParent() == null ? this : getParent(), stream);
-        for (Type eachType : types) {
-          RutaStream window = stream.getWindowStream(each, eachType);
+        Type type = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher()
+                .getType(getParent() == null ? this : getParent(), stream);
+          RutaStream window = stream.getWindowStream(each, type);
           for (RutaStatement element : getElements()) {
             if (stop)
               break;
@@ -82,7 +81,6 @@ public class OnlyFirstBlock extends Ruta
                 }
               }
             }
-          }
         }
       }
     }

Modified: uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/fst/FSTBlock.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/fst/FSTBlock.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/fst/FSTBlock.java (original)
+++ uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/fst/FSTBlock.java Thu Jan  5 12:55:02 2017
@@ -42,8 +42,8 @@ public class FSTBlock extends RutaBlock
 
   public FSTBlock(String name, RutaRule rule, List<RutaStatement> elements, RutaBlock parent,
           String defaultNamespace) {
-    super(name, rule, elements, parent, defaultNamespace, parent != null ? parent.getContext()
-            : null);
+    super(name, rule, elements, parent, defaultNamespace,
+            parent != null ? parent.getContext() : null);
   }
 
   @Override
@@ -62,12 +62,10 @@ public class FSTBlock extends RutaBlock
         if (each == null) {
           continue;
         }
-        List<Type> types = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher().getTypes(
-                getParent() == null ? this : getParent(), stream);
-        for (Type eachType : types) {
-          RutaStream window = stream.getWindowStream(each, eachType);
-          auto.apply(window, crowd, this);
-        }
+        Type type = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher()
+                .getType(getParent() == null ? this : getParent(), stream);
+        RutaStream window = stream.getWindowStream(each, type);
+        auto.apply(window, crowd, this);
       }
     }
     crowd.endVisit(this, result);

Modified: uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g (original)
+++ uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g Thu Jan  5 12:55:02 2017
@@ -70,6 +70,7 @@ import org.apache.uima.ruta.RutaScriptFa
 import org.apache.uima.ruta.RutaStatement;
 import org.apache.uima.ruta.block.RutaBlock;
 import org.apache.uima.ruta.block.RutaScriptBlock;
+import org.apache.uima.ruta.expression.AnnotationTypeExpression;
 import org.apache.uima.ruta.expression.ExpressionFactory;
 import org.apache.uima.ruta.expression.IRutaExpression;
 import org.apache.uima.ruta.expression.MatchReference;
@@ -948,9 +949,7 @@ String label = null;
 	:
 	(l = Identifier {label = l.getText();} COLON)?
 	start = STARTANCHOR? (
-	rea = ruleElementAnnotation[container]{re = rea;}
-	| 
-	re1 = ruleElementType[container] {re = re1;}
+	re1 = ruleElementAnnotationType[container] {re = re1;}
 	| re2 = ruleElementLiteral[container] {re = re2;}
 	| (ruleElementComposed[null])=>re3 = ruleElementComposed[container] {re = re3;}
 	| (ruleElementWildCard[null])=> re5 = ruleElementWildCard[container] {re = re5;}
@@ -1068,6 +1067,47 @@ ruleElementAnnotation [RuleElementContai
 	}
 	}
     ;
+    
+ ruleElementAnnotationType [RuleElementContainer container] returns [RutaRuleElement re = null]
+    :
+    (
+    (annotationExpression2)=>aExpr = annotationExpression2 
+     {re = factory.createRuleElement(aExpr, null, null, null, container, $blockDeclaration::env);} 
+     
+    |
+    (typeFunction)=> tf = typeFunction 
+    {re = factory.createRuleElement(tf, null, null, null, container, $blockDeclaration::env);}
+    
+    |
+     match = dottedIdWithIndex2 ((comp = LESS | comp = GREATER | comp = GREATEREQUAL | comp = LESSEQUAL |comp =  EQUAL | comp = NOTEQUAL) arg = argument)?
+     {
+	     MatchReference mr = ExpressionFactory.createMatchReference(match);
+	     if(comp == null) {
+	     AnnotationTypeExpression ate = ExpressionFactory.createAnnotationTypeExpression(mr);
+	     re = factory.createRuleElement(ate, null, null, null, container, $blockDeclaration::env);
+	     } else {
+	     FeatureMatchExpression fme = ExpressionFactory.createFeatureMatchExpression(mr, comp, arg, $blockDeclaration::env);
+	     re = factory.createRuleElement(fme, null, null, null, container, $blockDeclaration::env);
+	     } 
+     }
+     
+     
+     )
+     
+    q = quantifierPart? 
+        (LCURLY c = conditions? (THEN a = actions)? RCURLY)?
+   {
+	if(q != null) {
+		re.setQuantifier(q);
+	}
+	if(c!= null) {
+		re.setConditions(c);
+	}
+	if(a != null) {
+		re.setActions(a);
+	}
+	}
+    ;
 
 ruleElementLiteral [RuleElementContainer container] returns [RutaRuleElement re = null]
     :
@@ -2357,6 +2397,9 @@ annotationExpression2 returns [IRutaExpr
 	{isVariableOfType($blockDeclaration::env,input.LT(1).getText(), "ANNOTATION")}? 
 	id = Identifier {expr = ExpressionFactory.createAnnotationVariableExpression(id);} 
 	|
+	{isVariableOfType($blockDeclaration::env,input.LT(1).getText(), "ANNOTATIONLIST")}? 
+	id = Identifier {expr = ExpressionFactory.createAnnotationListVariableExpression(id);} 
+	|
 	//(annotationListIndexExpression)=> alie = annotationListIndexExpression {expr = alie;}
 	//|
 	ale = annotationListExpression {expr = ale;}
@@ -2490,6 +2533,20 @@ dottedIdWithIndex returns [Token token =
 	{token = ct; return token;}
 	;
 
+dottedIdWithIndex2 returns [Token token = null ]
+@init {CommonToken ct = null;}
+	:
+	id = Identifier {ct = new CommonToken(id);}
+	(
+	lb =  LBRACK {ct.setText(ct.getText() + lb.getText());}
+	index = DecimalLiteral {ct.setText(ct.getText() + index.getText());}
+	rb = RBRACK {ct.setStopIndex(getBounds(rb)[1]);ct.setText(ct.getText() + rb.getText());}
+	|
+	dot = DOT {ct.setText(ct.getText() + dot.getText());}
+	id = Identifier {ct.setStopIndex(getBounds(id)[1]);ct.setText(ct.getText() + id.getText());}
+	)*
+	{token = ct; return token;}
+	;
 
 annotationType returns [Token ref = null]
 	: 

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java Thu Jan  5 12:55:02 2017
@@ -31,6 +31,7 @@ import org.apache.uima.ruta.block.ForEac
 import org.apache.uima.ruta.block.RutaBlock;
 import org.apache.uima.ruta.block.RutaScriptBlock;
 import org.apache.uima.ruta.condition.AbstractRutaCondition;
+import org.apache.uima.ruta.expression.AnnotationTypeExpression;
 import org.apache.uima.ruta.expression.IRutaExpression;
 import org.apache.uima.ruta.expression.annotation.IAnnotationExpression;
 import org.apache.uima.ruta.expression.annotation.IAnnotationListExpression;
@@ -48,6 +49,7 @@ import org.apache.uima.ruta.rule.RuleEle
 import org.apache.uima.ruta.rule.RuleElementContainer;
 import org.apache.uima.ruta.rule.RuleElementIsolator;
 import org.apache.uima.ruta.rule.RutaAnnotationMatcher;
+import org.apache.uima.ruta.rule.RutaAnnotationTypeMatcher;
 import org.apache.uima.ruta.rule.RutaLiteralMatcher;
 import org.apache.uima.ruta.rule.RutaMatcher;
 import org.apache.uima.ruta.rule.RutaRule;
@@ -159,7 +161,9 @@ public class RutaScriptFactory {
           RuleElementQuantifier quantifier, List<AbstractRutaCondition> conditions,
           List<AbstractRutaAction> actions, RuleElementContainer container, RutaBlock parent) {
     RutaMatcher matcher = null;
-    if (expression instanceof ITypeExpression) {
+    if (expression instanceof AnnotationTypeExpression) {
+      matcher = new RutaAnnotationTypeMatcher((AnnotationTypeExpression) expression);
+    }  else if (expression instanceof ITypeExpression) {
       matcher = new RutaTypeMatcher((ITypeExpression) expression);
     } else if (expression instanceof FeatureExpression) {
       matcher = new RutaTypeMatcher((FeatureExpression) expression);

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java Thu Jan  5 12:55:02 2017
@@ -1290,4 +1290,11 @@ public class RutaStream extends FSIterat
       }
     }
   }
+
+  public Type getSharedParentType(List<Type> types) {
+    TypeSystem typeSystem = cas.getTypeSystem();
+    Type parentType = null;
+    // TODO 
+    return types.get(0);
+  }
 }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/GetFeatureAction.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/GetFeatureAction.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/GetFeatureAction.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/GetFeatureAction.java Thu Jan  5 12:55:02 2017
@@ -19,7 +19,6 @@
 
 package org.apache.uima.ruta.action;
 
-import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.uima.cas.Feature;
@@ -52,58 +51,55 @@ public class GetFeatureAction extends Ab
   public void execute(MatchContext context, RutaStream stream, InferenceCrowd crowd) {
     RuleMatch match = context.getRuleMatch();
     RuleElement element = context.getElement();
-    List<Type> types = new ArrayList<Type>();
+    // TODO refactor
     RutaBlock parent = element.getParent();
+
+    Type type = null;
     if (element instanceof RutaRuleElement) {
-      types = ((RutaRuleElement) element).getMatcher().getTypes(parent, stream);
+      type = ((RutaRuleElement) element).getMatcher().getType(parent, stream);
     }
-    if (types == null)
-      return;
+    String stringValue = featureStringExpression.getStringValue(context, stream);
+    Feature featureByBaseName = type.getFeatureByBaseName(stringValue);
+    RutaEnvironment environment = parent.getEnvironment();
+    List<AnnotationFS> matchedAnnotations = match.getMatchedAnnotationsOfElement(element);
+    for (AnnotationFS annotationFS : matchedAnnotations) {
+      if (annotationFS.getType().getFeatureByBaseName(stringValue) == null) {
+        System.out.println("Can't access feature " + stringValue
+                + ", because it's not defined in the matched type: " + annotationFS.getType());
+        return;
+      }
 
-    for (Type type : types) {
-      String stringValue = featureStringExpression.getStringValue(context, stream);
-      Feature featureByBaseName = type.getFeatureByBaseName(stringValue);
-      RutaEnvironment environment = parent.getEnvironment();
-      List<AnnotationFS> matchedAnnotations = match.getMatchedAnnotationsOfElement(element);
-      for (AnnotationFS annotationFS : matchedAnnotations) {
-        if (annotationFS.getType().getFeatureByBaseName(stringValue) == null) {
-          System.out.println("Can't access feature " + stringValue
-                  + ", because it's not defined in the matched type: " + annotationFS.getType());
-          return;
+      String featName = featureByBaseName.getRange().getName();
+      if (environment.getVariableType(variable).equals(String.class)
+              && featName.equals(UIMAConstants.TYPE_STRING)) {
+        Object value = annotationFS.getStringValue(featureByBaseName);
+        environment.setVariableValue(variable, value);
+      } else if (Number.class.isAssignableFrom(environment.getVariableType(variable))) {
+        Number value = 0;
+        if (featName.equals(UIMAConstants.TYPE_INTEGER)) {
+          value = annotationFS.getIntValue(featureByBaseName);
+        } else if (featName.equals(UIMAConstants.TYPE_DOUBLE)) {
+          value = annotationFS.getDoubleValue(featureByBaseName);
+        } else if (featName.equals(UIMAConstants.TYPE_FLOAT)) {
+          value = annotationFS.getFloatValue(featureByBaseName);
+        } else if (featName.equals(UIMAConstants.TYPE_BYTE)) {
+          value = annotationFS.getByteValue(featureByBaseName);
+        } else if (featName.equals(UIMAConstants.TYPE_SHORT)) {
+          value = annotationFS.getShortValue(featureByBaseName);
+        } else if (featName.equals(UIMAConstants.TYPE_LONG)) {
+          value = annotationFS.getLongValue(featureByBaseName);
         }
-
-        String featName = featureByBaseName.getRange().getName();
-        if (environment.getVariableType(variable).equals(String.class)
-                && featName.equals(UIMAConstants.TYPE_STRING)) {
-          Object value = annotationFS.getStringValue(featureByBaseName);
-          environment.setVariableValue(variable, value);
-        } else if (Number.class.isAssignableFrom(environment.getVariableType(variable))) {
-          Number value = 0;
-          if (featName.equals(UIMAConstants.TYPE_INTEGER)) {
-            value = annotationFS.getIntValue(featureByBaseName);
-          } else if (featName.equals(UIMAConstants.TYPE_DOUBLE)) {
-            value = annotationFS.getDoubleValue(featureByBaseName);
-          } else if (featName.equals(UIMAConstants.TYPE_FLOAT)) {
-            value = annotationFS.getFloatValue(featureByBaseName);
-          } else if (featName.equals(UIMAConstants.TYPE_BYTE)) {
-            value = annotationFS.getByteValue(featureByBaseName);
-          } else if (featName.equals(UIMAConstants.TYPE_SHORT)) {
-            value = annotationFS.getShortValue(featureByBaseName);
-          } else if (featName.equals(UIMAConstants.TYPE_LONG)) {
-            value = annotationFS.getLongValue(featureByBaseName);
-          }
-          environment.setVariableValue(variable, value);
-        } else if (environment.getVariableType(variable).equals(Boolean.class)
-                && featName.equals(UIMAConstants.TYPE_BOOLEAN)) {
-          Object value = annotationFS.getBooleanValue(featureByBaseName);
-          environment.setVariableValue(variable, value);
-        } else if (environment.getVariableType(variable).equals(Type.class)
-                && featName.equals(UIMAConstants.TYPE_STRING)) {
-          Object value = annotationFS.getStringValue(featureByBaseName);
-          Type t = stream.getCas().getTypeSystem().getType((String) value);
-          if (t != null) {
-            environment.setVariableValue(variable, t);
-          }
+        environment.setVariableValue(variable, value);
+      } else if (environment.getVariableType(variable).equals(Boolean.class)
+              && featName.equals(UIMAConstants.TYPE_BOOLEAN)) {
+        Object value = annotationFS.getBooleanValue(featureByBaseName);
+        environment.setVariableValue(variable, value);
+      } else if (environment.getVariableType(variable).equals(Type.class)
+              && featName.equals(UIMAConstants.TYPE_STRING)) {
+        Object value = annotationFS.getStringValue(featureByBaseName);
+        Type t = stream.getCas().getTypeSystem().getType((String) value);
+        if (t != null) {
+          environment.setVariableValue(variable, t);
         }
       }
     }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/block/RutaScriptBlock.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/block/RutaScriptBlock.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/block/RutaScriptBlock.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/block/RutaScriptBlock.java Thu Jan  5 12:55:02 2017
@@ -38,7 +38,8 @@ public class RutaScriptBlock extends Rut
 
   public RutaScriptBlock(String id, RutaRule rule, List<RutaStatement> elements, RutaBlock parent,
           String defaultNamespace) {
-    super(id, rule, elements, parent, defaultNamespace, parent != null ? parent.getContext() : null);
+    super(id, rule, elements, parent, defaultNamespace,
+            parent != null ? parent.getContext() : null);
   }
 
   @Override
@@ -57,14 +58,13 @@ public class RutaScriptBlock extends Rut
         if (each == null) {
           continue;
         }
-        List<Type> types = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher().getTypes(
-                getParent() == null ? this : getParent(), stream);
-        for (Type eachType : types) {
-          RutaStream window = stream.getWindowStream(each, eachType);
-          for (RutaStatement element : getElements()) {
-            if (element != null) {
-              element.apply(window, crowd);
-            }
+        // TODO refactor!!
+        Type type = ((RutaRuleElement) rule.getRuleElements().get(0)).getMatcher()
+                .getType(getParent() == null ? this : getParent(), stream);
+        RutaStream window = stream.getWindowStream(each, type);
+        for (RutaStatement element : getElements()) {
+          if (element != null) {
+            element.apply(window, crowd);
           }
         }
       }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/PositionCondition.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/PositionCondition.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/PositionCondition.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/condition/PositionCondition.java Thu Jan  5 12:55:02 2017
@@ -85,7 +85,7 @@ public class PositionCondition extends T
     List<Type> targetTypes = new ArrayList<Type>();
     if (element instanceof RutaRuleElement) {
       RutaRuleElement re = (RutaRuleElement) element;
-      targetTypes.addAll(re.getMatcher().getTypes(element.getParent(), stream));
+      targetTypes.add(re.getMatcher().getType(element.getParent(), stream));
     } else {
       targetTypes.add(annotation.getType());
     }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/AnnotationTypeExpression.java Thu Jan  5 12:55:02 2017
@@ -217,4 +217,8 @@ public class AnnotationTypeExpression ex
     return featureExpression;
   }
 
+  public String toString() {
+      return reference.toString();
+  }
+  
 }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/ExpressionFactory.java Thu Jan  5 12:55:02 2017
@@ -370,6 +370,11 @@ public class ExpressionFactory {
     MatchReference match = new MatchReference(ref.getText());
     return new AnnotationTypeExpression(match);
   }
+  
+  
+  public static AnnotationTypeExpression createAnnotationTypeExpression(MatchReference mr) {
+    return new AnnotationTypeExpression(mr);
+  }
 
   public static IStringExpression createStringListIndexExpression(AbstractStringListExpression sl,
           INumberExpression index) {

Added: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java?rev=1777465&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java (added)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java Thu Jan  5 12:55:02 2017
@@ -0,0 +1,92 @@
+/*
+ * 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.ruta.rule;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.FeatureStructure;
+import org.apache.uima.cas.impl.FSIteratorImplBase;
+import org.apache.uima.cas.text.AnnotationFS;
+
+public class AnnotationListFSIterator extends FSIteratorImplBase<AnnotationFS> {
+
+  private final List<AnnotationFS> list;
+
+  private int pointer = 0;
+
+  public AnnotationListFSIterator(List<AnnotationFS> list) {
+    super();
+    this.list = list;
+  }
+
+  @Override
+  public boolean isValid() {
+
+    return pointer >= 0 && pointer < list.size();
+  }
+
+  @Override
+  public AnnotationFS get() throws NoSuchElementException {
+    return list.get(pointer);
+  }
+
+  @Override
+  public void moveToNext() {
+    pointer++;
+
+  }
+
+  @Override
+  public void moveToPrevious() {
+    pointer--;
+
+  }
+
+  @Override
+  public void moveToFirst() {
+    pointer = 0;
+
+  }
+
+  @Override
+  public void moveToLast() {
+    pointer = list.size()-1;
+
+  }
+
+  @Override
+  public void moveTo(FeatureStructure fs) {
+    for (int i = 0; i < list.size(); i++) {
+      AnnotationFS each = list.get(i);
+      if(each.equals(fs)) {
+        pointer = i;
+        break;
+      }
+    }
+  }
+
+  @Override
+  public FSIterator<AnnotationFS> copy() {
+    
+    return new AnnotationListFSIterator(list);
+  }
+
+}

Propchange: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/AnnotationListFSIterator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationMatcher.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationMatcher.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationMatcher.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationMatcher.java Thu Jan  5 12:55:02 2017
@@ -111,7 +111,8 @@ public class RutaAnnotationMatcher imple
           return result;
         }
       } else if (annotationListExpression != null) {
-        List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context, stream);
+        List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context,
+                stream);
         Collection<AnnotationFS> result = new ArrayList<>();
         for (AnnotationFS each : annotations) {
           boolean beginsWith = nextBasic.getBegin() == each.getBegin();
@@ -160,7 +161,8 @@ public class RutaAnnotationMatcher imple
           return result;
         }
       } else if (annotationListExpression != null) {
-        List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context, stream);
+        List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context,
+                stream);
         for (AnnotationFS each : annotations) {
           boolean endsWith = nextBasic.getEnd() == each.getEnd();
           if (endsWith) {
@@ -175,28 +177,27 @@ public class RutaAnnotationMatcher imple
   }
 
   @Override
-  public List<Type> getTypes(RutaBlock parent, RutaStream stream) {
+  public Type getType(RutaBlock parent, RutaStream stream) {
     MatchContext context = new MatchContext(parent);
-    if(annotationExpression != null) {
-      
-    AnnotationFS ref = annotationExpression.getAnnotation(context, stream);
-    if (ref == null) {
-      return Collections.emptyList();
-    }
-    List<Type> result = new ArrayList<Type>(1);
-    result.add(ref.getType());
-    return result;
-    } else if(annotationListExpression != null) {
+    if (annotationExpression != null) {
+      AnnotationFS ref = annotationExpression.getAnnotation(context, stream);
+      if (ref == null) {
+        return null;
+      }
+      return ref.getType();
+    } else if (annotationListExpression != null) {
       List<AnnotationFS> annotations = annotationListExpression.getAnnotationList(context, stream);
-      List<Type> result = new ArrayList<Type>();
+      List<Type> types = new ArrayList<Type>();
       for (AnnotationFS each : annotations) {
-        result.add(each.getType());
+        types.add(each.getType());
       }
-      return result;
+      return stream.getSharedParentType(types);
     }
-    return Collections.emptyList();
+    return null;
   }
 
+  
+  
   @Override
   public IRutaExpression getExpression() {
     if (annotationExpression != null) {
@@ -211,7 +212,7 @@ public class RutaAnnotationMatcher imple
   public long estimateAnchors(RutaBlock parent, RutaStream stream) {
     return 1;
   }
-  
+
   @Override
   public String toString() {
     IRutaExpression expression = getExpression();
@@ -222,5 +223,4 @@ public class RutaAnnotationMatcher imple
     }
   }
 
-
 }

Added: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java?rev=1777465&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java (added)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java Thu Jan  5 12:55:02 2017
@@ -0,0 +1,296 @@
+/*
+ * 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.ruta.rule;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.block.RutaBlock;
+import org.apache.uima.ruta.expression.AnnotationTypeExpression;
+import org.apache.uima.ruta.expression.IRutaExpression;
+import org.apache.uima.ruta.type.RutaBasic;
+
+public class RutaAnnotationTypeMatcher implements RutaMatcher {
+
+  private static final boolean CHECK_ON_FEATURE = true;
+
+  private AnnotationTypeExpression expression;
+
+  public RutaAnnotationTypeMatcher(AnnotationTypeExpression expression) {
+    super();
+    this.expression = expression;
+  }
+
+  @Override
+  public Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent,
+          RutaStream stream) {
+    Collection<AnnotationFS> result = null;
+    MatchContext context = new MatchContext(parent);
+    // just for forcing expression top initialize
+    expression.getType(context, stream);
+    if (expression.getAnnotationExpression() != null) {
+      result = new ArrayList<>(1);
+      result.add(expression.getAnnotation(context, stream));
+    } else if (expression.getAnnotationListExpression() != null) {
+      result = expression.getAnnotationList(context, stream);
+    } else {
+      Type type = getType(parent, stream);
+      if (type == null) {
+        return Collections.emptyList();
+      }
+      Type overallDAType = stream.getCas().getDocumentAnnotation().getType();
+      String name = type.getName();
+      if (StringUtils.equals(CAS.TYPE_NAME_DOCUMENT_ANNOTATION, name)
+              || "org.apache.uima.ruta.type.Document".equals(name) || overallDAType.equals(type)) {
+        // TODO what about dynamic windowing?
+        result = new ArrayList<>(1);
+        result.add(stream.getDocumentAnnotation());
+      } else {
+        result = stream.getAnnotations(type);
+      }
+      if (expression.getFeatureExpression() != null) {
+        return expression.getFeatureExpression().getAnnotations(result, CHECK_ON_FEATURE, context,
+                stream);
+      }
+    }
+    return result;
+  }
+
+  @Override
+  public Collection<? extends AnnotationFS> getAnnotationsAfter(RutaRuleElement ruleElement,
+          AnnotationFS annotation, RutaBlock parent, RutaStream stream) {
+    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 (annotation.getEnd() > 0) {
+      stream.moveTo(lastBasic);
+      if (stream.isVisible(lastBasic) && stream.isValid()
+              && stream.get().getEnd() == lastBasic.getEnd()) {
+        stream.moveToNext();
+      }
+    } else {
+      stream.moveToFirst();
+    }
+
+    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;
+          }
+        }
+      } 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;
+      } else {
+        Type type = getType(parent, stream);
+        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) {
+          return expression.getFeatureExpression().getAnnotations(anchors, CHECK_ON_FEATURE,
+                  context, stream);
+        } else {
+          return anchors;
+        }
+
+      }
+
+    }
+    return Collections.emptyList();
+  }
+
+  @Override
+  public Collection<? extends AnnotationFS> getAnnotationsBefore(RutaRuleElement ruleElement,
+          AnnotationFS annotation, RutaBlock parent, RutaStream stream) {
+    if (annotation.getBegin() == stream.getDocumentAnnotation().getBegin()) {
+      return Collections.emptyList();
+    }
+    RutaBasic firstBasic = stream.getBeginAnchor(annotation.getBegin());
+    if (firstBasic == null) {
+      return Collections.emptyList();
+    }
+    stream.moveTo(firstBasic);
+    if (stream.isVisible(firstBasic)) {
+      stream.moveToPrevious();
+    }
+
+    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();
+        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<>();
+            result.add(each);
+            return result;
+          }
+        }
+      } else {
+        Type type = getType(parent, stream);
+        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) {
+          return expression.getFeatureExpression().getAnnotations(anchors, CHECK_ON_FEATURE,
+                  context, stream);
+        } else {
+          return anchors;
+        }
+      }
+    }
+    return Collections.emptyList();
+  }
+
+  @Override
+  public String toString() {
+    IRutaExpression expression = getExpression();
+    if (expression != null) {
+      return expression.toString();
+    } else {
+      return "";
+    }
+  }
+
+  @Override
+  public IRutaExpression getExpression() {
+    return expression;
+  }
+
+  protected Type getType(AnnotationTypeExpression expression, RutaBlock parent, RutaStream stream,
+          boolean resolveDocumentAnnotation) {
+    MatchContext context = new MatchContext(parent);
+    Type type = expression.getType(context, stream);
+    if (resolveDocumentAnnotation && type != null
+            && StringUtils.equals(CAS.TYPE_NAME_DOCUMENT_ANNOTATION, type.getName())) {
+      return stream.getDocumentAnnotationType();
+    }
+    return type;
+  }
+
+  @Override
+  public long estimateAnchors(RutaBlock parent, RutaStream stream) {
+    if (expression.getAnnotationExpression() != null) {
+      return 1;
+    } else if (expression.getAnnotationListExpression() != null) {
+      return 1;
+    } else {
+      return stream.getHistogram(getType(expression, parent, stream, true));
+    }
+  }
+
+  @Override
+  public Type getType(RutaBlock parent, RutaStream stream) {
+    MatchContext context = new MatchContext(parent);
+    if (expression.getAnnotationExpression() != null) {
+      AnnotationFS annotation = expression.getAnnotationExpression().getAnnotation(context, stream);
+      if (annotation != null) {
+        return annotation.getType();
+      }
+    } else if (expression.getAnnotationListExpression() != null) {
+      List<AnnotationFS> annotations = expression.getAnnotationListExpression()
+              .getAnnotationList(context, stream);
+      List<Type> types = new ArrayList<Type>();
+      for (AnnotationFS each : annotations) {
+        types.add(each.getType());
+      }
+      return stream.getSharedParentType(types);
+    } else {
+      return getType(expression, parent, stream, false);
+    }
+
+    return null;
+  }
+
+}

Propchange: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaAnnotationTypeMatcher.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaLiteralMatcher.java Thu Jan  5 12:55:02 2017
@@ -99,8 +99,8 @@ public class RutaLiteralMatcher implemen
   }
 
   @Override
-  public List<Type> getTypes(RutaBlock parent, RutaStream stream) {
-    return new ArrayList<Type>();
+  public Type getType(RutaBlock parent, RutaStream stream) {
+    return stream.getCas().getAnnotationType();
   }
 
 }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaMatcher.java Thu Jan  5 12:55:02 2017
@@ -20,7 +20,6 @@
 package org.apache.uima.ruta.rule;
 
 import java.util.Collection;
-import java.util.List;
 
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
@@ -32,7 +31,7 @@ public interface RutaMatcher {
 
   Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent, RutaStream stream);
 
-  List<Type> getTypes(RutaBlock parent, RutaStream stream);
+  Type getType(RutaBlock parent, RutaStream stream);
 
   IRutaExpression getExpression();
 

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=1777465&r1=1777464&r2=1777465&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 Thu Jan  5 12:55:02 2017
@@ -22,7 +22,6 @@ package org.apache.uima.ruta.rule;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.List;
 
 import org.apache.uima.cas.Type;
 import org.apache.uima.cas.text.AnnotationFS;
@@ -52,30 +51,26 @@ public class RutaTypeMatcher implements
   }
 
   @Override
-  public Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent, RutaStream stream) {
+  public Collection<? extends AnnotationFS> getMatchingAnnotations(RutaBlock parent,
+          RutaStream stream) {
     // TODO what about the matching direction?
     Collection<AnnotationFS> annotations = new ArrayList<>();
-    List<Type> types = getTypes(parent, stream);
-    for (Type type : types) {
-      if (type == null) {
-        continue;
-      }
-      Type overallDAType = stream.getCas().getDocumentAnnotation().getType();
-      String name = type.getName();
-      if ("uima.tcas.DocumentAnnotation".equals(name)
-              || "org.apache.uima.ruta.type.Document".equals(name)
-              || overallDAType.equals(type)
-              ) {
-        // TODO what about dynamic windowing?
-        annotations.add(stream.getDocumentAnnotation());
-      } else {
-        annotations.addAll(stream.getAnnotations(type));
-      }
+    Type type = getType(parent, stream);
+    if (type == null) {
+      return annotations;
+    }
+    Type overallDAType = stream.getCas().getDocumentAnnotation().getType();
+    String name = type.getName();
+    if ("uima.tcas.DocumentAnnotation".equals(name)
+            || "org.apache.uima.ruta.type.Document".equals(name) || overallDAType.equals(type)) {
+      // TODO what about dynamic windowing?
+      annotations.add(stream.getDocumentAnnotation());
+    } else {
+      annotations.addAll(stream.getAnnotations(type));
     }
     MatchContext context = new MatchContext(parent);
     if (featureExpression != null) {
-      return featureExpression
-              .getAnnotations(annotations, CHECK_ON_FEATURE, context, stream);
+      return featureExpression.getAnnotations(annotations, CHECK_ON_FEATURE, context, stream);
     } else {
       return annotations;
     }
@@ -115,16 +110,14 @@ public class RutaTypeMatcher implements
           nextBasic = (RutaBasic) stream.get();
         }
       }
-      List<Type> reTypes = getTypes(parent, stream);
+      Type type = getType(parent, stream);
       Collection<AnnotationFS> anchors = new ArrayList<>();
-      for (Type eachMatchType : reTypes) {
-        Collection<AnnotationFS> beginAnchors = nextBasic.getBeginAnchors(eachMatchType);
-        if (beginAnchors != null) {
-          for (AnnotationFS afs : beginAnchors) {
-            if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()
-                    && afs.getEnd() <= stream.getDocumentAnnotation().getEnd()) {
-              anchors.add(afs);
-            }
+      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);
           }
         }
       }
@@ -163,15 +156,13 @@ public class RutaTypeMatcher implements
           nextBasic = (RutaBasic) stream.get();
         }
       }
-      List<Type> reTypes = getTypes(parent, stream);
+      Type type = getType(parent, stream);
       Collection<AnnotationFS> anchors = new ArrayList<>();
-      for (Type eachMatchType : reTypes) {
-        Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(eachMatchType);
-        if (endAnchors != null) {
-          for (AnnotationFS afs : endAnchors) {
-            if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
-              anchors.add(afs);
-            }
+      Collection<AnnotationFS> endAnchors = nextBasic.getEndAnchors(type);
+      if (endAnchors != null) {
+        for (AnnotationFS afs : endAnchors) {
+          if (afs.getBegin() >= stream.getDocumentAnnotation().getBegin()) {
+            anchors.add(afs);
           }
         }
       }
@@ -186,8 +177,6 @@ public class RutaTypeMatcher implements
     return Collections.emptyList();
   }
 
-
-
   @Override
   public String toString() {
     IRutaExpression expression = getExpression();
@@ -208,10 +197,12 @@ public class RutaTypeMatcher implements
     return null;
   }
 
-  protected Type getType(ITypeExpression expression, RutaBlock parent, RutaStream stream, boolean resolveDocumentAnnotation) {
+  protected Type getType(ITypeExpression expression, RutaBlock parent, RutaStream stream,
+          boolean resolveDocumentAnnotation) {
     MatchContext context = new MatchContext(parent);
     Type type = expression.getType(context, stream);
-    if (resolveDocumentAnnotation && type != null && "uima.tcas.DocumentAnnotation".equals(type.getName())) {
+    if (resolveDocumentAnnotation && type != null
+            && "uima.tcas.DocumentAnnotation".equals(type.getName())) {
       return stream.getDocumentAnnotationType();
     }
     return type;
@@ -223,16 +214,15 @@ public class RutaTypeMatcher implements
   }
 
   @Override
-  public List<Type> getTypes(RutaBlock parent, RutaStream stream) {
-    List<Type> result = new ArrayList<Type>(1);
+  public Type getType(RutaBlock parent, RutaStream stream) {
     if (typeExpression != null) {
       Type type = getType(typeExpression, parent, stream, false);
-      result.add(type);
+      return type;
     } else if (featureExpression != null) {
       Type type = featureExpression.getInitialType(new MatchContext(parent), stream);
-      result.add(type);
+      return type;
     }
-    return result;
+    return null;
   }
 
 }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/WildCardRuleElement.java Thu Jan  5 12:55:02 2017
@@ -35,6 +35,7 @@ import org.apache.uima.ruta.action.Abstr
 import org.apache.uima.ruta.block.RutaBlock;
 import org.apache.uima.ruta.condition.AbstractRutaCondition;
 import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.ruta.expression.AnnotationTypeExpression;
 import org.apache.uima.ruta.expression.string.IStringExpression;
 import org.apache.uima.ruta.type.RutaBasic;
 import org.apache.uima.ruta.type.RutaFrame;
@@ -98,14 +99,19 @@ public class WildCardRuleElement extends
     } else if (nextElement instanceof RutaRuleElement) {
       RutaRuleElement re = (RutaRuleElement) nextElement;
       RutaMatcher matcher = re.getMatcher();
-      if (matcher instanceof RutaTypeMatcher) {
-        result = tryWithNextType(after, annotation, nextElement, null, ruleMatch, ruleApply,
-                containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+      if (matcher instanceof RutaAnnotationTypeMatcher) {
+        result = tryWithNextAnnotationType(after, annotation, nextElement, null, ruleMatch,
+                ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+      } else if (matcher instanceof RutaTypeMatcher) {
+        result = tryWithNextAnnotationType(after, annotation, nextElement, null, ruleMatch,
+                ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+      } else if (matcher instanceof RutaAnnotationMatcher) {
+        result = tryWithNextAnnotationType(after, annotation, nextElement, null, ruleMatch,
+                ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
       } else if (matcher instanceof RutaLiteralMatcher) {
         result = tryWithNextLiteral(after, annotation, re, ruleMatch, ruleApply, containerMatch,
                 nextDepth, sideStepOrigin, stream, crowd);
       }
-
     } else if (nextElement instanceof ComposedRuleElement) {
       ComposedRuleElement cre = ((ComposedRuleElement) nextElement);
       result = tryWithNextComposed(after, annotation, cre, ruleMatch, ruleApply, containerMatch,
@@ -113,8 +119,9 @@ public class WildCardRuleElement extends
     } else if (nextElement instanceof WildCardRuleElement) {
       // another wildcard? seriously? then just assume its an "Annotation" type
       CAS cas = stream.getCas();
-      result = tryWithNextType(after, annotation, nextElement, cas.getAnnotationType(), ruleMatch,
-              ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream, crowd);
+      result = tryWithNextAnnotationType(after, annotation, nextElement, cas.getAnnotationType(),
+              ruleMatch, ruleApply, containerMatch, nextDepth, sideStepOrigin, entryPoint, stream,
+              crowd);
     }
     return result;
   }
@@ -234,11 +241,28 @@ public class WildCardRuleElement extends
           RutaStream stream, RuleElement element, AnnotationFS result) {
     RutaRuleElement re = (RutaRuleElement) element;
     RutaMatcher matcher = re.getMatcher();
-    if (matcher instanceof RutaTypeMatcher) {
+      
+    if (matcher instanceof RutaLiteralMatcher) {
+      RutaLiteralMatcher lm = (RutaLiteralMatcher) matcher;
+      IStringExpression expression = lm.getExpression();
+      MatchContext context = new MatchContext(this, null, after);
+      String stringValue = expression.getStringValue(context, stream);
+      AnnotationFS documentAnnotation = stream.getDocumentAnnotation();
+      int delta = documentAnnotation.getBegin();
+      String document = documentAnnotation.getCoveredText();
+      int pointer = annotation.getEnd() - delta;
+      int indexOf = document.indexOf(stringValue, pointer);
+      if (indexOf < 0) {
+        return null;
+      } else {
+        return stream.getAnchor(after, indexOf);
+      }
+    } else {
       FSIterator<AnnotationFS> iterator = getIterator(after, annotation, re, null, stream);
       if (iterator == null) {
         return null;
       }
+      
       if (iterator.isValid()) {
         result = iterator.get();
         if (annotation != null && (after && result.getEnd() == annotation.getEnd())
@@ -252,21 +276,6 @@ public class WildCardRuleElement extends
         }
 
       }
-    } else if (matcher instanceof RutaLiteralMatcher) {
-      RutaLiteralMatcher lm = (RutaLiteralMatcher) matcher;
-      IStringExpression expression = lm.getExpression();
-      MatchContext context = new MatchContext(this, null, after);
-      String stringValue = expression.getStringValue(context, stream);
-      AnnotationFS documentAnnotation = stream.getDocumentAnnotation();
-      int delta = documentAnnotation.getBegin();
-      String document = documentAnnotation.getCoveredText();
-      int pointer = annotation.getEnd() - delta;
-      int indexOf = document.indexOf(stringValue, pointer);
-      if (indexOf < 0) {
-        return null;
-      } else {
-        return stream.getAnchor(after, indexOf);
-      }
     }
     return result;
   }
@@ -289,7 +298,7 @@ public class WildCardRuleElement extends
     }
   }
 
-  private List<RuleMatch> tryWithNextType(boolean after, AnnotationFS annotation,
+  private List<RuleMatch> tryWithNextAnnotationType(boolean after, AnnotationFS annotation,
           RuleElement nextElement, Type defaultType, RuleMatch ruleMatch, RuleApply ruleApply,
           ComposedRuleElementMatch containerMatch, int nextDepth, RutaRuleElement sideStepOrigin,
           RuleElement entryPoint, RutaStream stream, InferenceCrowd crowd) {
@@ -385,15 +394,29 @@ public class WildCardRuleElement extends
           RuleElement nextElement, Type defaultType, RutaStream stream) {
     FSIterator<AnnotationFS> iterator = null;
     if (defaultType == null) {
+      MatchContext context = new MatchContext(nextElement.getParent());
       RutaRuleElement re = (RutaRuleElement) nextElement;
       RutaMatcher matcher = re.getMatcher();
-      if (matcher instanceof RutaTypeMatcher) {
-        RutaTypeMatcher typeMatcher = (RutaTypeMatcher) re.getMatcher();
-        List<Type> types = typeMatcher.getTypes(parent, stream);
-        Type type = types.get(0);
-        iterator = getIteratorOfType(after, type, annotation, stream);
+      if (matcher instanceof RutaAnnotationTypeMatcher) {
+        RutaAnnotationTypeMatcher atm = (RutaAnnotationTypeMatcher) matcher;
+        AnnotationTypeExpression expression = (AnnotationTypeExpression) atm.getExpression();
+        if (expression.getAnnotationExpression() != null
+                || expression.getAnnotationListExpression() != null) {
+          iterator = new AnnotationListFSIterator(expression.getAnnotationList(context, stream));
+          iterator.moveTo(annotation);
+        } else {
+          Type type = matcher.getType(parent, stream);
+          iterator = getIteratorOfType(after, type, annotation, stream);
+        }
+      } else if (matcher instanceof RutaAnnotationMatcher) {
+        RutaAnnotationMatcher am = (RutaAnnotationMatcher) matcher;
+        Collection<AnnotationFS> matchingAnnotations = am.getMatchingAnnotations(parent, stream);
+        iterator = new AnnotationListFSIterator(new ArrayList<>(matchingAnnotations));
+        iterator.moveTo(annotation);
       } else {
-        // should not happen
+        // fallback
+        Type type = matcher.getType(parent, stream);
+        iterator = getIteratorOfType(after, type, annotation, stream);
       }
     } else {
       iterator = getIteratorOfType(after, defaultType, annotation, stream);
@@ -649,7 +672,7 @@ public class WildCardRuleElement extends
         EvaluatedCondition eval = condition.eval(context, stream, crowd);
         crowd.endVisit(condition, null);
         evaluatedConditions.add(eval);
-        if(!eval.isValue()) {
+        if (!eval.isValue()) {
           break;
         }
       }

Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/verbalize/ScriptVerbalizer.java Thu Jan  5 12:55:02 2017
@@ -44,7 +44,6 @@ import org.apache.uima.ruta.rule.RuleEle
 import org.apache.uima.ruta.rule.RutaMatcher;
 import org.apache.uima.ruta.rule.RutaRule;
 import org.apache.uima.ruta.rule.RutaRuleElement;
-import org.apache.uima.ruta.rule.RutaTypeMatcher;
 import org.apache.uima.ruta.rule.WildCardRuleElement;
 import org.apache.uima.ruta.rule.quantifier.MinMaxGreedy;
 import org.apache.uima.ruta.rule.quantifier.MinMaxReluctant;
@@ -154,8 +153,7 @@ public class ScriptVerbalizer {
       RutaRuleElement tmre = (RutaRuleElement) re;
       RutaMatcher matcher = tmre.getMatcher();
       // action-only rule
-      if (matcher instanceof RutaTypeMatcher) {
-        IRutaExpression expression = ((RutaTypeMatcher) matcher).getExpression();
+        IRutaExpression expression = matcher.getExpression();
         if (expression != null) {
           result.append(verbalizer.verbalize(expression));
         } else {
@@ -168,9 +166,6 @@ public class ScriptVerbalizer {
             }
           }
         }
-      } else {
-        result.append(verbalizeMatcher(tmre));
-      }
     } else if (re instanceof WildCardRuleElement) {
       result.append("#");
     }

Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationFeatureExpressionTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationFeatureExpressionTest.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationFeatureExpressionTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationFeatureExpressionTest.java Thu Jan  5 12:55:02 2017
@@ -192,16 +192,16 @@ public class AnnotationFeatureExpression
     cas.addFsToIndexes(a2);
 
     StringBuilder script = new StringBuilder();
-    script.append("A1.fs.s==\"1\"{-> T1};");
-    script.append("A1.fs.s==\"2\"{-> T2};");
-    script.append("A1.fs{-> T3};");
-    script.append("A1.fss{-> T4};");
-    script.append("A1.fss.fs{-> T5};");
-    script.append("A1.fss.fs.fs{-> T6};");
-    script.append("A1.fss.fs.fs.s == \"1\"{-> T7};");
-    script.append("A1.fss.fss.s == \"1\"{-> T8};");
-    script.append("a:A1 {a.fss.fss.s == \"1\" -> T9};");
-    script.append("a:A1 {a.fs.s==\"1\" -> T10};");
+    script.append("A1.fs.s==\"1\"{-> T1};\n");
+    script.append("A1.fs.s==\"2\"{-> T2};\n");
+    script.append("A1.fs{-> T3};\n");
+    script.append("A1.fss{-> T4};\n");
+    script.append("A1.fss.fs{-> T5};\n");
+    script.append("A1.fss.fs.fs{-> T6};\n");
+    script.append("A1.fss.fs.fs.s == \"1\"{-> T7};\n");
+    script.append("A1.fss.fss.s == \"1\"{-> T8};\n");
+    script.append("a:A1 {a.fss.fss.s == \"1\" -> T9};\n");
+    script.append("a:A1 {a.fs.s==\"1\" -> T10};\n");
 
     Ruta.apply(cas, script.toString());
 

Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java?rev=1777465&r1=1777464&r2=1777465&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java Thu Jan  5 12:55:02 2017
@@ -315,7 +315,7 @@ public class AnnotationLabelExpressionTe
     Ruta.matches(cas.getJCas(), "a:W b:W{a.x == (b.y-1)-> T1};");
   }
 
-  @Test(expected = AnalysisEngineProcessException.class)
+  @Test
   public void testSequentialLabelSelfMatch() throws ResourceInitializationException,
           InvalidXMLException, IOException, AnalysisEngineProcessException, CASException {
     CAS cas = RutaTestUtils.getCAS("Some text.");
@@ -613,6 +613,17 @@ public class AnnotationLabelExpressionTe
     RutaTestUtils.assertAnnotationsEquals(cas, 1, 0);
   }
   
+  @Test
+  public void testInMatchCondition() throws Exception {
+    String script= "";
+    script += "CW{-> Struct1, Struct1.a=sw} sw:SW;\n";
+    script += "s:Struct1 s.a{-> T1};\n";
+//    script += "(s:Struct1 SW){->T2}<-{s W{REGEXP(\"text\")};};\n";
+    CAS cas = applyOnStruct4Cas(script);
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 1, "text");
+//    RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "Some text");
+  }
+  
   
   private CAS applyOnStruct4Cas(String script) throws Exception {
     String document = "Some text.";