Author: pkluegl
Date: Fri Nov  8 14:48:06 2019
New Revision: 1869567

URL: http://svn.apache.org/viewvc?rev=1869567&view=rev
Log:
UIMA-4676: support type feature expression

Added:
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/TypeFeature.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/type/TypeFeatureExpression.java
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/
    
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/TypeFeatureTest.java
Modified:
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.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/expression/feature/FeatureMatchExpression.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/GenericFeatureExpression.java
    
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java?rev=1869567&r1=1869566&r2=1869567&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java 
(original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/UIMAConstants.java 
Fri Nov  8 14:48:06 2019
@@ -20,9 +20,11 @@
 package org.apache.uima.ruta;
 
 public class UIMAConstants {
-  
+
   public static final String FEATURE_COVERED_TEXT = "coveredText";
 
   public static final String FEATURE_COVERED_TEXT_SHORT = "ct";
-  
+
+  public static final String FEATURE_TYPE = "type";
+
 }

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=1869567&r1=1869566&r2=1869567&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
 Fri Nov  8 14:48:06 2019
@@ -25,6 +25,7 @@ import java.util.List;
 import org.antlr.runtime.Token;
 import org.apache.uima.cas.CAS;
 import org.apache.uima.ruta.TypeUsageInformation;
+import org.apache.uima.ruta.UIMAConstants;
 import org.apache.uima.ruta.block.RutaBlock;
 import org.apache.uima.ruta.condition.AbstractRutaCondition;
 import org.apache.uima.ruta.condition.AndCondition;
@@ -210,12 +211,34 @@ public class ExpressionFactory {
   }
 
   public ITypeExpression createSimpleTypeExpression(String typeString, 
RutaBlock parent) {
+    if (typeString.endsWith("." + UIMAConstants.FEATURE_TYPE)) {
+      return createTypeFeatureExpression(typeString, parent);
+    }
+
     if (typeUsage != null) {
       typeUsage.addMentionedType(typeString);
     }
     return new SimpleTypeExpression(typeString);
   }
 
+  public ITypeExpression createTypeFeatureExpression(String typeString, 
RutaBlock parent) {
+    MatchReference matchReference = createMatchReference(typeString);
+    return createTypeFeatureExpression(matchReference);
+  }
+
+  public ITypeExpression createTypeFeatureExpression(FeatureExpression 
featureExpression) {
+    if (featureExpression instanceof SimpleFeatureExpression) {
+      return createTypeFeatureExpression(
+              ((SimpleFeatureExpression) 
featureExpression).getMatchReference());
+    }
+    return null;
+  }
+
+  private ITypeExpression createTypeFeatureExpression(MatchReference 
matchReference) {
+    AnnotationTypeExpression typeExpression = 
createAnnotationTypeExpression(matchReference);
+    return typeExpression;
+  }
+
   public IBooleanExpression createBooleanFunction(Token op, IBooleanExpression 
e1,
           IBooleanExpression e2) {
     return new SimpleBooleanFunction(op.getText(), e1, e2);

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java?rev=1869567&r1=1869566&r2=1869567&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/FeatureMatchExpression.java
 Fri Nov  8 14:48:06 2019
@@ -41,6 +41,7 @@ import org.apache.uima.ruta.expression.a
 import org.apache.uima.ruta.expression.bool.IBooleanExpression;
 import org.apache.uima.ruta.expression.number.INumberExpression;
 import org.apache.uima.ruta.expression.string.IStringExpression;
+import org.apache.uima.ruta.expression.type.ITypeExpression;
 import org.apache.uima.ruta.rule.MatchContext;
 
 public class FeatureMatchExpression extends SimpleFeatureExpression {
@@ -88,7 +89,15 @@ public class FeatureMatchExpression exte
           RutaStream stream) {
     Type featureRangeType = null;
     TypeSystem typeSystem = stream.getCas().getTypeSystem();
-    if (feature instanceof CoveredTextFeature) {
+    if (feature instanceof TypeFeature) {
+      // TODO
+      if (getArg() instanceof ITypeExpression) {
+        Type t1 = fs.getType();
+        ITypeExpression expr = (ITypeExpression) getArg();
+        Type t2 = expr.getType(context, stream);
+        return compare(t1, t2);
+      }
+    } else if (feature instanceof CoveredTextFeature) {
       featureRangeType = typeSystem.getType(CAS.TYPE_NAME_STRING);
     } else if (feature != null) {
       featureRangeType = feature.getRange();

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/GenericFeatureExpression.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/GenericFeatureExpression.java?rev=1869567&r1=1869566&r2=1869567&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/GenericFeatureExpression.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/GenericFeatureExpression.java
 Fri Nov  8 14:48:06 2019
@@ -49,6 +49,8 @@ public class GenericFeatureExpression ex
 
   private FeatureExpression featureExpression;
 
+  private ITypeExpression typeExpression;
+
   private INumberExpression numberExpression;
 
   private IStringExpression stringExpression;
@@ -66,7 +68,7 @@ public class GenericFeatureExpression ex
   private IAnnotationListExpression annotationListExpression;
 
   private ExpressionFactory expressionFactory;
-  
+
   public GenericFeatureExpression(FeatureExpression fe) {
     super();
     this.featureExpression = fe;
@@ -128,14 +130,19 @@ public class GenericFeatureExpression ex
     }
     return annotationExpression.getFeatureStructure(context, stream);
   }
-  
-  
+
   @Override
   public Type getType(MatchContext context, RutaStream stream) {
+    if (typeExpression == null) {
+      typeExpression = 
expressionFactory.createTypeFeatureExpression(featureExpression);
+    }
+    if (typeExpression != null) {
+      return typeExpression.getType(context, stream);
+    }
     // special case where an argument is interpreted as a type expression
     return featureExpression.getInitialType(context, stream);
   }
-  
+
   public FeatureExpression getFeatureExpression() {
     return featureExpression;
   }
@@ -200,5 +207,4 @@ public class GenericFeatureExpression ex
     return result;
   }
 
-
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java?rev=1869567&r1=1869566&r2=1869567&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/SimpleFeatureExpression.java
 Fri Nov  8 14:48:06 2019
@@ -100,6 +100,8 @@ public class SimpleFeatureExpression ext
           // also allow for unknown types
           feature = new CoveredTextFeature();
         }
+      } else if (StringUtils.equals(each, UIMAConstants.FEATURE_TYPE)) {
+        feature = new TypeFeature();
       } else if (type == null || type.isArray()) {
         // lazy check of range
         feature = new LazyFeature(each, context.getParent());
@@ -229,8 +231,8 @@ public class SimpleFeatureExpression ext
     }
 
     if (currentFeature == null || currentFeature instanceof CoveredTextFeature
-            || currentFeature.getRange().isPrimitive()) {
-      // feature == null -> this is the coveredText "feature"
+            || currentFeature instanceof TypeFeature || 
currentFeature.getRange().isPrimitive()) {
+      // feature == null -> this is not a real feature
       if (this instanceof FeatureMatchExpression) {
         FeatureMatchExpression fme = (FeatureMatchExpression) this;
         if (checkOnFeatureValue) {

Added: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/TypeFeature.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/TypeFeature.java?rev=1869567&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/TypeFeature.java
 (added)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/feature/TypeFeature.java
 Fri Nov  8 14:48:06 2019
@@ -0,0 +1,60 @@
+/*
+ * 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.expression.feature;
+
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.Type;
+
+public class TypeFeature implements Feature {
+
+  public TypeFeature() {
+    super();
+  }
+  
+  @Override
+  public int compareTo(Feature o) {
+    return 0;
+  }
+
+  @Override
+  public Type getDomain() {
+    return null;
+  }
+
+  @Override
+  public Type getRange() {
+    return null;
+  }
+
+  @Override
+  public String getName() {
+    return null;
+  }
+
+  @Override
+  public String getShortName() {
+    return null;
+  }
+
+  @Override
+  public boolean isMultipleReferencesAllowed() {
+    return false;
+  }
+
+}

Added: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/type/TypeFeatureExpression.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/type/TypeFeatureExpression.java?rev=1869567&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/type/TypeFeatureExpression.java
 (added)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/expression/type/TypeFeatureExpression.java
 Fri Nov  8 14:48:06 2019
@@ -0,0 +1,62 @@
+/*
+ * 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.expression.type;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.expression.feature.FeatureExpression;
+import org.apache.uima.ruta.rule.MatchContext;
+
+public class TypeFeatureExpression extends AbstractTypeExpression {
+
+  private FeatureExpression fe;
+
+  public TypeFeatureExpression(FeatureExpression fe) {
+
+    super();
+    this.fe = fe;
+  }
+
+  @Override
+  public Type getType(MatchContext context, RutaStream stream) {
+
+    return null;
+  }
+
+  public FeatureExpression getFe() {
+
+    return this.fe;
+  }
+
+  public void setFe(FeatureExpression fe) {
+
+    this.fe = fe;
+  }
+
+  @Override
+  public String getStringValue(MatchContext context, RutaStream stream) {
+    Type type = getType(context, stream);
+    if (type != null) {
+      return type.getName();
+    }
+    return null;
+  }
+
+}

Added: 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/TypeFeatureTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/TypeFeatureTest.java?rev=1869567&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/TypeFeatureTest.java
 (added)
+++ 
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/type/TypeFeatureTest.java
 Fri Nov  8 14:48:06 2019
@@ -0,0 +1,48 @@
+/*
+ * 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.expression.type;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.ruta.engine.Ruta;
+import org.apache.uima.ruta.engine.RutaTestUtils;
+import org.junit.Test;
+
+public class TypeFeatureTest {
+
+  @Test
+  public void test() throws Exception {
+
+    String document = "This is a test.";
+
+    String script = "CW{-> T1};\n";
+    script += "SW{-> T2};\n";
+    script += "T1{-> MARK(T1.type)};\n";
+    script += "t2:T2{-> t2.type};\n";
+    script += "(a1:ANY a2:ANY){a1.type==a2.type -> T3};\n";
+
+    CAS cas = RutaTestUtils.getCAS(document);
+    Ruta.apply(cas, script);
+
+    RutaTestUtils.assertAnnotationsEquals(cas, 1, 2, "This", "This");
+    RutaTestUtils.assertAnnotationsEquals(cas, 2, 6, "is", "is", "a", "a", 
"test", "test");
+    RutaTestUtils.assertAnnotationsEquals(cas, 3, 2, "is a", "a test");
+
+  }
+
+}


Reply via email to