Author: pkluegl
Date: Wed May 15 11:18:39 2013
New Revision: 1482766

URL: http://svn.apache.org/r1482766
Log:
UIMA-2904
- added implicit action implementation

Added:
    
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitFeatureAction.java
    
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitMarkAction.java
Modified:
    
uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
    
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ActionFactory.java
    
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g
    
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ActionFactory.java
    
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java

Modified: 
uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g?rev=1482766&r1=1482765&r2=1482766&view=diff
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
 (original)
+++ 
uima/sandbox/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
 Wed May 15 11:18:39 2013
@@ -918,6 +918,14 @@ featureMatchExpression2 returns [Feature
        {fme = ExpressionFactory.createFeatureMatchExpression(f, op, arg, 
$blockDeclaration::env);}
        ;
 
+
+featureAssignmentExpression returns [FeatureMatchExpression fme = null]
+       :
+       f = featureExpression op = ASSIGN_EQUAL arg = primitiveArgument
+       {fme = ExpressionFactory.createFeatureMatchExpression(f, op, arg, 
$blockDeclaration::env);}
+       ;
+       
+       
 variable returns [Token var = null]
        :
        {isVariable($blockDeclaration::env, input.LT(1).getText())}? v = 
Identifier {var = v;}
@@ -1241,7 +1249,10 @@ action  returns [AbstractRutaAction resu
        | a = actionRemoveRetainType
        | a = actionAddFilterType
        | a = actionRemoveFilterType
-       | (a = externalAction)=> a = externalAction
+       | (externalAction)=> a = externalAction
+       | (featureAssignmentExpression)=> fae = featureAssignmentExpression {a 
= ActionFactory.createAction(fae);}
+       | (typeExpression)=> te = typeExpression {a = 
ActionFactory.createAction(te);}
+       
 //     | a = variableAction
        ) {result = a;}
        ;

Modified: 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ActionFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ActionFactory.java?rev=1482766&r1=1482765&r2=1482766&view=diff
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ActionFactory.java
 (original)
+++ 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ActionFactory.java
 Wed May 15 11:18:39 2013
@@ -27,6 +27,7 @@ import org.antlr.runtime.Token;
 import org.apache.uima.ruta.RutaBlock;
 import org.apache.uima.ruta.expression.RutaExpression;
 import org.apache.uima.ruta.expression.bool.BooleanExpression;
+import org.apache.uima.ruta.expression.feature.FeatureMatchExpression;
 import org.apache.uima.ruta.expression.list.ListExpression;
 import org.apache.uima.ruta.expression.list.StringListExpression;
 import org.apache.uima.ruta.expression.list.TypeListExpression;
@@ -264,6 +265,14 @@ public class ActionFactory {
     return new TrimAction(types, typeList);
   }
 
+  public static AbstractRutaAction createAction(FeatureMatchExpression fae) {
+    return new ImplicitFeatureAction(fae);
+  }
+
+  public static AbstractRutaAction createAction(TypeExpression te) {
+    return new ImplicitMarkAction(te);
+  }
+
   
 
 }

Added: 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitFeatureAction.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitFeatureAction.java?rev=1482766&view=auto
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitFeatureAction.java
 (added)
+++ 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitFeatureAction.java
 Wed May 15 11:18:39 2013
@@ -0,0 +1,154 @@
+/*
+ * 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.action;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.uima.cas.Feature;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.TypeSystem;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.UIMAConstants;
+import org.apache.uima.ruta.expression.RutaExpression;
+import org.apache.uima.ruta.expression.bool.BooleanExpression;
+import org.apache.uima.ruta.expression.feature.FeatureMatchExpression;
+import org.apache.uima.ruta.expression.number.NumberExpression;
+import org.apache.uima.ruta.expression.string.StringExpression;
+import org.apache.uima.ruta.expression.type.TypeExpression;
+import org.apache.uima.ruta.rule.AnnotationComparator;
+import org.apache.uima.ruta.rule.RuleElement;
+import org.apache.uima.ruta.rule.RuleMatch;
+import org.apache.uima.ruta.utils.UIMAUtils;
+import org.apache.uima.ruta.visitor.InferenceCrowd;
+
+public class ImplicitFeatureAction extends AbstractRutaAction {
+
+  private FeatureMatchExpression expr;
+
+  private Comparator<? super AnnotationFS> comp = new AnnotationComparator();
+
+  public ImplicitFeatureAction(FeatureMatchExpression expr) {
+    super();
+    this.expr = expr;
+  }
+
+  @Override
+  public void execute(RuleMatch match, RuleElement element, RutaStream stream, 
InferenceCrowd crowd) {
+    TypeExpression typeExpr = expr.getTypeExpr();
+    Type type = typeExpr.getType(element.getParent());
+    List<AnnotationFS> matchedAnnotations = 
match.getMatchedAnnotationsOf(element, stream);
+    Collection<AnnotationFS> annotations = new TreeSet<AnnotationFS>(comp);
+    for (AnnotationFS annotation : matchedAnnotations) {
+      annotations.addAll(getAnnotations(annotation, type, expr, stream));
+    }
+    for (AnnotationFS each : annotations) {
+      stream.getCas().removeFsFromIndexes(each);
+    }
+    Collection<AnnotationFS> featureAnnotations = 
expr.getFeatureAnnotations(annotations, stream,
+            element.getParent(), false);
+    Feature feature = expr.getFeature(element.getParent());
+    RutaExpression arg = expr.getArg();
+    for (AnnotationFS each : featureAnnotations) {
+      setFeatureValue(each, feature, arg, element, stream);
+    }
+    for (AnnotationFS each : annotations) {
+      stream.getCas().addFsToIndexes(each);
+    }
+  }
+
+  private void setFeatureValue(AnnotationFS a, Feature feature, RutaExpression 
argExpr,
+          RuleElement element, RutaStream stream) {
+    String range = feature.getRange().getName();
+    if (range.equals(UIMAConstants.TYPE_STRING)) {
+      if (argExpr instanceof StringExpression) {
+        StringExpression stringExpr = (StringExpression) argExpr;
+        String string = stringExpr.getStringValue(element.getParent());
+        a.setStringValue(feature, string);
+      }
+    } else if (argExpr instanceof NumberExpression
+            && (range.equals(UIMAConstants.TYPE_INTEGER) || 
range.equals(UIMAConstants.TYPE_LONG)
+                    || range.equals(UIMAConstants.TYPE_SHORT) || range
+                      .equals(UIMAConstants.TYPE_BYTE))) {
+      NumberExpression numberExpr = (NumberExpression) argExpr;
+      int v = numberExpr.getIntegerValue(element.getParent());
+      a.setIntValue(feature, v);
+    } else if (argExpr instanceof NumberExpression && 
(range.equals(UIMAConstants.TYPE_DOUBLE))) {
+      NumberExpression numberExpr = (NumberExpression) argExpr;
+      double v = numberExpr.getDoubleValue(element.getParent());
+      a.setDoubleValue(feature, v);
+    } else if (argExpr instanceof NumberExpression && 
(range.equals(UIMAConstants.TYPE_FLOAT))) {
+      NumberExpression numberExpr = (NumberExpression) argExpr;
+      float v = numberExpr.getFloatValue(element.getParent());
+      a.setFloatValue(feature, v);
+    } else if (argExpr instanceof BooleanExpression && 
(range.equals(UIMAConstants.TYPE_BOOLEAN))) {
+      BooleanExpression booleanExpr = (BooleanExpression) argExpr;
+      boolean v = booleanExpr.getBooleanValue(element.getParent());
+      a.setBooleanValue(feature, v);
+    } else if (argExpr instanceof BooleanExpression && 
(range.equals(UIMAConstants.TYPE_BOOLEAN))) {
+      BooleanExpression booleanExpr = (BooleanExpression) argExpr;
+      boolean v = booleanExpr.getBooleanValue(element.getParent());
+      a.setBooleanValue(feature, v);
+    } else if (argExpr instanceof TypeExpression && 
!feature.getRange().isPrimitive()) {
+      TypeExpression typeExpr = (TypeExpression) argExpr;
+      Type t = typeExpr.getType(element.getParent());
+      List<AnnotationFS> inWindow = stream.getAnnotationsInWindow(a, t);
+      if (feature.getRange().isArray()) {
+        a.setFeatureValue(feature, UIMAUtils.toFSArray(stream.getJCas(), 
inWindow));
+      } else {
+        AnnotationFS annotation = inWindow.get(0);
+        a.setFeatureValue(feature, annotation);
+      }
+    }
+  }
+
+  private List<AnnotationFS> getAnnotations(AnnotationFS annotation, Type type,
+          FeatureMatchExpression fme, RutaStream stream) {
+    List<AnnotationFS> result = new ArrayList<AnnotationFS>();
+    TypeSystem typeSystem = stream.getCas().getTypeSystem();
+    if (typeSystem.subsumes(type, annotation.getType())) {
+      result.add(annotation);
+    } else {
+      Set<AnnotationFS> beginAnchors = 
stream.getBeginAnchor(annotation.getBegin())
+              .getBeginAnchors(type);
+      Set<AnnotationFS> endAnchors = 
stream.getEndAnchor(annotation.getEnd()).getEndAnchors(type);
+      @SuppressWarnings("unchecked")
+      Collection<AnnotationFS> intersection = CollectionUtils
+              .intersection(beginAnchors, endAnchors);
+      result.addAll(intersection);
+    }
+    return result;
+  }
+
+  public FeatureMatchExpression getExpr() {
+    return expr;
+  }
+
+  public void setExpr(FeatureMatchExpression expr) {
+    this.expr = expr;
+  }
+
+}

Added: 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitMarkAction.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitMarkAction.java?rev=1482766&view=auto
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitMarkAction.java
 (added)
+++ 
uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/ImplicitMarkAction.java
 Wed May 15 11:18:39 2013
@@ -0,0 +1,50 @@
+/*
+ * 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.action;
+
+import java.util.List;
+
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.expression.type.TypeExpression;
+import org.apache.uima.ruta.rule.RuleElement;
+import org.apache.uima.ruta.rule.RuleMatch;
+import org.apache.uima.ruta.visitor.InferenceCrowd;
+
+public class ImplicitMarkAction extends AbstractMarkAction {
+
+  public ImplicitMarkAction(TypeExpression te) {
+    super(te);
+  }
+
+  @Override
+  public void execute(RuleMatch match, RuleElement element, RutaStream stream, 
InferenceCrowd crowd) {
+    List<AnnotationFS> matchedAnnotations = 
match.getMatchedAnnotations(stream, null,
+            element.getContainer());
+    for (AnnotationFS matchedAnnotation : matchedAnnotations) {
+      if (matchedAnnotation == null) {
+        return;
+      }
+      createAnnotation(matchedAnnotation, element, stream, match);
+    }
+
+  }
+
+}

Modified: 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g?rev=1482766&r1=1482765&r2=1482766&view=diff
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g
 (original)
+++ 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/antlr3/org/apache/uima/ruta/ide/core/parser/RutaParser.g
 Wed May 15 11:18:39 2013
@@ -903,6 +903,11 @@ externalTypeFunction returns [Expression
        }
        ;
 
+featureAssignmentExpression returns [Expression expr = null]
+       :
+       feature = dottedId comp = ASSIGN_EQUAL value = primitiveArgument {expr 
= ExpressionFactory.createFeatureMatch(feature, comp, value);}
+       ;
+
 featureTypeExpression returns [Expression expr = null]
        :
        feature = dottedId (comp = EQUAL | comp = NOTEQUAL) value = 
primitiveArgument {expr = ExpressionFactory.createFeatureMatch(feature, comp, 
value);}
@@ -1288,12 +1293,17 @@ result = ActionFactory.createEmptyAction
        | a = actionAddRetainType
        | a = actionRemoveFilterType
        | a = actionRemoveRetainType
-       | a = externalAction
+       | (externalAction)=> a = externalAction
+       | (featureAssignmentExpression)=> fae = featureAssignmentExpression {a 
= ActionFactory.createAction(fae);}
+       | (typeExpression)=> te = typeExpression {a = 
ActionFactory.createAction(te);}
+       
        // | a = variableAction
        ) {result = a;}
        ;
 
 
+
+
 variableAction returns [RutaAction action = null]
        :
        // also create an dummy action for auto-completion

Modified: 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ActionFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ActionFactory.java?rev=1482766&r1=1482765&r2=1482766&view=diff
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ActionFactory.java
 (original)
+++ 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ActionFactory.java
 Wed May 15 11:18:39 2013
@@ -235,4 +235,20 @@ public class ActionFactory extends Abstr
             nameEnd, assignments, structure);
   }
 
+  public static RutaAction createAction(Expression... exprsArray) {
+    List<Expression> exprL = new ArrayList<Expression>();
+    if (exprsArray != null) {
+      for (int i = 0; i < exprsArray.length; i++) {
+        Expression expression = exprsArray[i];
+        if (expression != null) {
+          exprL.add(expression);
+        }
+      }
+    }
+    int[] bounds = getBounds(exprL.get(0), exprL.get(exprL.size()-1));
+    return new RutaAction(bounds[0], bounds[1], exprL,
+            ExpressionConstants.USER_EXPRESSION_START, "", bounds[0],
+            bounds[0]);
+  }
+
 }

Modified: 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java
URL: 
http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java?rev=1482766&r1=1482765&r2=1482766&view=diff
==============================================================================
--- 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java
 (original)
+++ 
uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/parser/ast/ExpressionFactory.java
 Wed May 15 11:18:39 2013
@@ -110,7 +110,10 @@ public class ExpressionFactory extends A
 
   // =====> TYPE-EXPRESSIONS <======
   public static Expression createTypeExpression(Expression e) {
-    return new RutaExpression(e.sourceStart(), e.sourceEnd(), e, 
RutaTypeConstants.RUTA_TYPE_AT);
+    if(e != null) {
+      return new RutaExpression(e.sourceStart(), e.sourceEnd(), e, 
RutaTypeConstants.RUTA_TYPE_AT);
+    }
+    return null;
   }
 
   public static Expression createEmptyTypeExpression(Token token) {


Reply via email to