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 2013/05/03 17:46:23 UTC

svn commit: r1478849 - in /uima/sandbox/ruta/trunk: example-projects/ruta-ep-example-extensions/ example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ ruta-core/src/main/java/org/apache/uima/ruta/rule/ ruta-...

Author: pkluegl
Date: Fri May  3 15:46:23 2013
New Revision: 1478849

URL: http://svn.apache.org/r1478849
Log:
UIMA-2834
- added example for type function extensions
- added some checks on null and empty list

Added:
    uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunction.java
    uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionExtension.java
    uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionIDEExtension.java
Modified:
    uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/plugin.xml
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java
    uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
    uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaCheckerProblemFactory.java

Modified: uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/plugin.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/plugin.xml?rev=1478849&r1=1478848&r2=1478849&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/plugin.xml (original)
+++ uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/plugin.xml Fri May  3 15:46:23 2013
@@ -33,5 +33,12 @@ under the License.
             engine="org.apache.uima.ruta.example.extensions.ExampleConditionExtension">
       </condition>
    </extension>
+   <extension
+         point="org.apache.uima.ruta.ide.typeFunctionExtension">
+      <condition
+            class="org.apache.uima.ruta.example.extensions.ExampleTypeFunctionIDEExtension"
+            engine="org.apache.uima.ruta.example.extensions.ExampleTypeFunctionExtension">
+      </condition>
+   </extension>
 
 </plugin>

Added: uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunction.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunction.java?rev=1478849&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunction.java (added)
+++ uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunction.java Fri May  3 15:46:23 2013
@@ -0,0 +1,52 @@
+/*
+ * 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.example.extensions;
+
+import org.apache.uima.cas.Type;
+import org.apache.uima.ruta.RutaStatement;
+import org.apache.uima.ruta.expression.string.StringExpression;
+import org.apache.uima.ruta.expression.type.TypeFunctionExpression;
+
+public class ExampleTypeFunction extends TypeFunctionExpression {
+
+  private final StringExpression expr;
+
+  public ExampleTypeFunction(StringExpression expr) {
+    super();
+    this.expr = expr;
+  }
+
+  public StringExpression getExpr() {
+    return expr;
+  }
+
+  @Override
+  public Type getType(RutaStatement parent) {
+    String stringValue = expr.getStringValue(parent);
+    return parent.getEnvironment().getType(stringValue);
+  }
+
+  @Override
+  public String getStringValue(RutaStatement parent) {
+    return expr.getStringValue(parent);
+  }
+
+
+}

Added: uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionExtension.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionExtension.java?rev=1478849&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionExtension.java (added)
+++ uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionExtension.java Fri May  3 15:46:23 2013
@@ -0,0 +1,72 @@
+/*
+ * 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.example.extensions;
+
+import java.util.List;
+
+import org.apache.uima.ruta.RutaElement;
+import org.apache.uima.ruta.expression.RutaExpression;
+import org.apache.uima.ruta.expression.string.StringExpression;
+import org.apache.uima.ruta.expression.type.TypeFunctionExpression;
+import org.apache.uima.ruta.extensions.IRutaTypeFunctionExtension;
+import org.apache.uima.ruta.verbalize.RutaVerbalizer;
+
+import antlr.ANTLRException;
+
+public class ExampleTypeFunctionExtension implements IRutaTypeFunctionExtension {
+
+  private final String[] knownExtensions = new String[] { "ExampleTypeFunction" };
+
+  private final Class<?>[] extensions = new Class[] { ExampleTypeFunction.class };
+
+  public String verbalize(RutaElement element, RutaVerbalizer verbalizer) {
+    if (element instanceof ExampleTypeFunction) {
+      return verbalizeName(element) + "("
+              + verbalizer.verbalize(((ExampleTypeFunction)element).getExpr()) + ")";
+    } else {
+      return "UnknownTypeFunction";
+    }
+  }
+
+  public TypeFunctionExpression createTypeFunction(String name, List<RutaExpression> args)
+          throws ANTLRException {
+    if(args == null || args.size() != 1 || !(args.get(0) instanceof StringExpression)) {
+      throw new ANTLRException("ExampleTypeFunction accepts only one StringExpression as argument!");
+    }
+    return new ExampleTypeFunction((StringExpression) args.get(0));
+  }
+
+  public String verbalizeName(RutaElement element) {
+    return knownExtensions[0];
+  }
+
+  public String[] getKnownExtensions() {
+    return knownExtensions;
+  }
+
+  public Class<?>[] extensions() {
+    return extensions;
+  }
+
+
+
+
+
+}

Added: uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionIDEExtension.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionIDEExtension.java?rev=1478849&view=auto
==============================================================================
--- uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionIDEExtension.java (added)
+++ uima/sandbox/ruta/trunk/example-projects/ruta-ep-example-extensions/src/main/java/org/apache/uima/ruta/example/extensions/ExampleTypeFunctionIDEExtension.java Fri May  3 15:46:23 2013
@@ -0,0 +1,68 @@
+/*
+ * 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.example.extensions;
+
+import java.util.List;
+
+import org.antlr.runtime.RecognitionException;
+import org.apache.uima.ruta.ide.core.extensions.IIDETypeFunctionExtension;
+import org.apache.uima.ruta.ide.parser.ast.RutaFunction;
+import org.apache.uima.ruta.ide.parser.ast.RutaTypeConstants;
+import org.apache.uima.ruta.ide.validator.RutaCheckerProblemFactory;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.compiler.problem.IProblem;
+import org.eclipse.dltk.compiler.problem.IProblemReporter;
+
+public class ExampleTypeFunctionIDEExtension implements IIDETypeFunctionExtension {
+  private final String[] strings = new String[] { "ExampleTypeFunction" };
+
+  public String[] getKnownExtensions() {
+    return strings;
+  }
+
+  public boolean checkSyntax(Expression element, RutaCheckerProblemFactory problemFactory,
+          IProblemReporter rep) throws RecognitionException {
+    if (element instanceof RutaFunction) {
+      RutaFunction f = (RutaFunction) element;
+      String name = f.getName();
+      if (!name.equals(strings[0])) {
+        IProblem problem = problemFactory.createUnknownFunctionProblem(f);
+        rep.reportProblem(problem);
+        return false;
+      }
+      boolean ok = true;
+      List<Expression> childs = f.getChilds();
+      if(childs.size() != 1) {
+        IProblem problem = problemFactory.createWrongNumberOfArgumentsProblem(name, element, 1);
+        rep.reportProblem(problem);
+        ok = false;
+      }
+      Expression expr = childs.get(0);
+      if(expr.getKind() != RutaTypeConstants.RUTA_TYPE_S) {
+        IProblem problem = problemFactory.createWrongArgumentTypeProblem(expr, "NumberExpression");
+        rep.reportProblem(problem);
+        ok = false;
+      }
+      return ok;
+    }
+    return false;
+  }
+
+}

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java?rev=1478849&r1=1478848&r2=1478849&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaTypeMatcher.java Fri May  3 15:46:23 2013
@@ -54,6 +54,9 @@ public class RutaTypeMatcher implements 
     Collection<AnnotationFS> result = new TreeSet<AnnotationFS>(comparator);
     List<Type> types = getTypes(parent, stream);
     for (Type type : types) {
+      if(type == null) {
+        continue;
+      }
       String name = type.getName();
       RutaBasic firstBasicOfAll = stream.getFirstBasicOfAll();
       if ("uima.tcas.DocumentAnnotation".equals(name)
@@ -201,7 +204,7 @@ public class RutaTypeMatcher implements 
 
   protected Type getType(TypeExpression expression, RutaBlock parent, RutaStream stream) {
     Type type = expression.getType(parent);
-    if ("uima.tcas.DocumentAnnotation".equals(type.getName())) {
+    if (type != null && "uima.tcas.DocumentAnnotation".equals(type.getName())) {
       return stream.getDocumentAnnotationType();
     }
     return type;

Modified: uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java?rev=1478849&r1=1478848&r2=1478849&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java (original)
+++ uima/sandbox/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/visitor/DebugInfoFactory.java Fri May  3 15:46:23 2013
@@ -103,7 +103,8 @@ public class DebugInfoFactory {
       }
       counter = 0;
       for (List<ScriptApply> list : loops) {
-        AbstractRuleMatch<? extends AbstractRule> ruleMatch = blockApply.getRuleApply().getList().get(counter);
+        AbstractRuleMatch<? extends AbstractRule> ruleMatch = blockApply.getRuleApply().getList()
+                .get(counter);
         DebugBlockApply dummyBlockApply = createDummyBlockApply((RuleMatch) ruleMatch, stream,
                 addToIndex, withMatches, timeInfo);
         List<DebugRuleMatch> ruleMatches = new ArrayList<DebugRuleMatch>();
@@ -197,8 +198,9 @@ public class DebugInfoFactory {
     return dra;
   }
 
-  public DebugRuleMatch createDebugRuleMatch(AbstractRuleMatch<? extends AbstractRule> match, RutaStream stream,
-          boolean addToIndex, boolean withMatches, Map<RutaElement, Long> timeInfo) {
+  public DebugRuleMatch createDebugRuleMatch(AbstractRuleMatch<? extends AbstractRule> match,
+          RutaStream stream, boolean addToIndex, boolean withMatches,
+          Map<RutaElement, Long> timeInfo) {
     JCas cas = stream.getJCas();
     DebugRuleMatch drm = null;
     if (match.matchedCompletely()) {
@@ -217,7 +219,7 @@ public class DebugInfoFactory {
         }
         drm.setDelegates(UIMAUtils.toFSArray(cas, delegates));
       }
-    } else if(match instanceof RegExpRuleMatch) {
+    } else if (match instanceof RegExpRuleMatch) {
       RegExpRuleMatch rerm = (RegExpRuleMatch) match;
       Map<Integer, List<AnnotationFS>> map = rerm.getMap();
       Set<Entry<Integer, List<AnnotationFS>>> entrySet = map.entrySet();
@@ -235,7 +237,7 @@ public class DebugInfoFactory {
 
             DebugEvaluatedCondition base = new DebugEvaluatedCondition(cas);
             base.setValue(true);
-            String baseString = "Group "+key ;
+            String baseString = "Group " + key;
             base.setElement(baseString);
             drem.setBaseCondition(base);
 
@@ -253,25 +255,28 @@ public class DebugInfoFactory {
         }
         ruleElementMatches.add(drems);
       }
-      
+
       drm.setElements(UIMAUtils.toFSArray(cas, ruleElementMatches));
     }
     if (timeInfo != null) {
       long time = timeInfo.get(match.getRule());
       drm.setTime(time);
     }
-    AnnotationFS matchedAnnotation = match.getMatchedAnnotationsOfRoot(stream).get(0);
-    if (matchedAnnotation != null) {
-      drm.setBegin(matchedAnnotation.getBegin());
-      drm.setEnd(matchedAnnotation.getEnd());
-      if (addToIndex || withMatches)
-        drm.addToIndexes();
+    List<AnnotationFS> matchedAnnotationsOfRoot = match.getMatchedAnnotationsOfRoot(stream);
+    if (!matchedAnnotationsOfRoot.isEmpty()) {
+      AnnotationFS matchedAnnotation = matchedAnnotationsOfRoot.get(0);
+      if (matchedAnnotation != null) {
+        drm.setBegin(matchedAnnotation.getBegin());
+        drm.setEnd(matchedAnnotation.getEnd());
+        if (addToIndex || withMatches)
+          drm.addToIndexes();
+      }
     }
     return drm;
   }
 
-  private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas,
-          DebugRuleMatch drm, ComposedRuleElementMatch rootMatch) {
+  private void setInnerMatches(RutaStream stream, boolean addToIndex, JCas cas, DebugRuleMatch drm,
+          ComposedRuleElementMatch rootMatch) {
     Set<Entry<RuleElement, List<RuleElementMatch>>> entrySet = rootMatch.getInnerMatches()
             .entrySet();
     List<DebugRuleElementMatches> ruleElementMatches = new ArrayList<DebugRuleElementMatches>();
@@ -346,8 +351,8 @@ public class DebugInfoFactory {
     return drem;
   }
 
-  public DebugRuleElementMatch createDebugRuleElementMatch(RuleElementMatch rem,
-          RutaStream stream, boolean addToIndex) {
+  public DebugRuleElementMatch createDebugRuleElementMatch(RuleElementMatch rem, RutaStream stream,
+          boolean addToIndex) {
     JCas cas = stream.getJCas();
     DebugRuleElementMatch drem = new DebugRuleElementMatch(cas);
 

Modified: uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaCheckerProblemFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaCheckerProblemFactory.java?rev=1478849&r1=1478848&r2=1478849&view=diff
==============================================================================
--- uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaCheckerProblemFactory.java (original)
+++ uima/sandbox/ruta/trunk/ruta-ep-ide/src/main/java/org/apache/uima/ruta/ide/validator/RutaCheckerProblemFactory.java Fri May  3 15:46:23 2013
@@ -25,6 +25,7 @@ import org.apache.uima.ruta.ide.parser.a
 import org.apache.uima.ruta.ide.parser.ast.RutaAction;
 import org.apache.uima.ruta.ide.parser.ast.RutaCondition;
 import org.apache.uima.ruta.ide.parser.ast.RutaFeatureDeclaration;
+import org.apache.uima.ruta.ide.parser.ast.RutaFunction;
 import org.apache.uima.ruta.ide.parser.ast.RutaVariableReference;
 import org.eclipse.dltk.ast.ASTListNode;
 import org.eclipse.dltk.ast.ASTNode;
@@ -186,4 +187,9 @@ public class RutaCheckerProblemFactory {
     return new RutaCheckerDefaultProblem(this.fileName, message, element, getLine(element));
   }
 
+  public IProblem createUnknownFunctionProblem(RutaFunction f) {
+    String message = "error: Function \"" + f.getName() + "\" is not defined.";
+    return new RutaCheckerDefaultProblem(this.fileName, message, f, getLine(f));
+  }
+
 }