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/04/26 16:02:32 UTC

svn commit: r1476208 - in /uima/sandbox/textmarker/trunk: example-projects/textmarker-ep-example-extensions/ example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ textmarker-ep-ide/src/main/java/...

Author: pkluegl
Date: Fri Apr 26 14:02:31 2013
New Revision: 1476208

URL: http://svn.apache.org/r1476208
Log:
UIMA-2834
- fixed some smaller bugs (expression type)
- added example condition extension

Added:
    uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleCondition.java
    uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionExtension.java
    uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionIDEExtension.java
Modified:
    uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/plugin.xml
    uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerCondition.java
    uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerStringExpression.java
    uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/validator/TextMarkerCheckerProblemFactory.java

Modified: uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/plugin.xml
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/plugin.xml?rev=1476208&r1=1476207&r2=1476208&view=diff
==============================================================================
--- uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/plugin.xml (original)
+++ uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/plugin.xml Fri Apr 26 14:02:31 2013
@@ -26,5 +26,12 @@ under the License.
             engine="org.apache.uima.textmarker.example.extensions.ExampleActionExtension">
       </action>
    </extension>
+   <extension
+         point="org.apache.uima.textmarker.ide.conditionExtension">
+      <condition
+            class="org.apache.uima.textmarker.example.extensions.ExampleConditionIDEExtension"
+            engine="org.apache.uima.textmarker.example.extensions.ExampleConditionExtension">
+      </condition>
+   </extension>
 
 </plugin>

Added: uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleCondition.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleCondition.java?rev=1476208&view=auto
==============================================================================
--- uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleCondition.java (added)
+++ uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleCondition.java Fri Apr 26 14:02:31 2013
@@ -0,0 +1,73 @@
+/*
+ * 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.textmarker.example.extensions;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.textmarker.TextMarkerStream;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.rule.EvaluatedCondition;
+import org.apache.uima.textmarker.rule.RuleElement;
+import org.apache.uima.textmarker.visitor.InferenceCrowd;
+
+public class ExampleCondition extends AbstractTextMarkerCondition {
+  
+  private final StringExpression dateExpr;
+
+  private final StringExpression formatExpr;
+  
+  public ExampleCondition(StringExpression expr, StringExpression format) {
+    super();
+    this.dateExpr = expr;
+    this.formatExpr = format;
+  }
+  
+  @Override
+  public EvaluatedCondition eval(AnnotationFS annotation, RuleElement element,
+          TextMarkerStream stream, InferenceCrowd crowd) {
+    String coveredText = annotation.getCoveredText();
+    String dateValue = dateExpr.getStringValue(element.getParent());
+    String formatValue = formatExpr.getStringValue(element.getParent());
+    SimpleDateFormat dateFormat = new SimpleDateFormat(formatValue); 
+    boolean result = false;
+    try {
+      Date matchedDate = dateFormat.parse(coveredText); 
+      Date givenDate = dateFormat.parse(dateValue); 
+      int compareTo = matchedDate.compareTo(givenDate);
+      result = compareTo < 0;
+    } catch (Exception e) {
+    }
+    return new EvaluatedCondition(this, result);
+  }
+
+  public StringExpression getExpr() {
+    return dateExpr;
+  }
+
+  public StringExpression getFormatExpr() {
+    return formatExpr;
+  }
+
+ 
+
+}

Added: uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionExtension.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionExtension.java?rev=1476208&view=auto
==============================================================================
--- uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionExtension.java (added)
+++ uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionExtension.java Fri Apr 26 14:02:31 2013
@@ -0,0 +1,75 @@
+/*
+ * 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.textmarker.example.extensions;
+
+import java.util.List;
+
+import org.apache.uima.textmarker.TextMarkerElement;
+import org.apache.uima.textmarker.condition.AbstractTextMarkerCondition;
+import org.apache.uima.textmarker.expression.TextMarkerExpression;
+import org.apache.uima.textmarker.expression.string.StringExpression;
+import org.apache.uima.textmarker.extensions.ITextMarkerConditionExtension;
+import org.apache.uima.textmarker.verbalize.TextMarkerVerbalizer;
+
+import antlr.ANTLRException;
+
+public class ExampleConditionExtension implements ITextMarkerConditionExtension {
+
+  private final String[] knownExtensions = new String[] { "ExampleCondition" };
+
+  private final Class<?>[] extensions = new Class[] { ExampleCondition.class };
+
+  public String verbalize(TextMarkerElement element, TextMarkerVerbalizer verbalizer) {
+    if (element instanceof ExampleCondition) {
+      ExampleCondition c = (ExampleCondition) element;
+      return verbalizeName(element) + "(" + verbalizer.verbalize(c.getExpr()) + ", "
+              + verbalizer.verbalize(c.getFormatExpr()) + ")";
+    } else {
+      return "UnknownAction";
+    }
+  }
+
+  public AbstractTextMarkerCondition createCondition(String name, List<TextMarkerExpression> args)
+          throws ANTLRException {
+    if (args != null && args.size() == 2) {
+      if (!(args.get(0) instanceof StringExpression)) {
+      }
+      if (!(args.get(1) instanceof StringExpression)) {
+      }
+    } else {
+      throw new ANTLRException(
+              "ExampleCondition accepts exactly two StringExpressions as arguments");
+    }
+    return new ExampleCondition((StringExpression) args.get(0), (StringExpression) args.get(1));
+  }
+
+  public String verbalizeName(TextMarkerElement element) {
+    return knownExtensions[0];
+  }
+
+  public String[] getKnownExtensions() {
+    return knownExtensions;
+  }
+
+  public Class<?>[] extensions() {
+    return extensions;
+  }
+
+}

Added: uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionIDEExtension.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionIDEExtension.java?rev=1476208&view=auto
==============================================================================
--- uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionIDEExtension.java (added)
+++ uima/sandbox/textmarker/trunk/example-projects/textmarker-ep-example-extensions/src/main/java/org/apache/uima/textmarker/example/extensions/ExampleConditionIDEExtension.java Fri Apr 26 14:02:31 2013
@@ -0,0 +1,71 @@
+/*
+ * 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.textmarker.example.extensions;
+
+import java.util.List;
+
+import org.antlr.runtime.RecognitionException;
+import org.apache.uima.textmarker.ide.core.extensions.IIDEConditionExtension;
+import org.apache.uima.textmarker.ide.parser.ast.TMTypeConstants;
+import org.apache.uima.textmarker.ide.parser.ast.TextMarkerCondition;
+import org.apache.uima.textmarker.ide.validator.TextMarkerCheckerProblemFactory;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.compiler.problem.IProblem;
+import org.eclipse.dltk.compiler.problem.IProblemReporter;
+
+
+
+public class ExampleConditionIDEExtension implements IIDEConditionExtension {
+  private final String[] strings = new String[] { "ExampleCondition" };
+
+  public String[] getKnownExtensions() {
+    return strings;
+  }
+
+  public boolean checkSyntax(Expression element, TextMarkerCheckerProblemFactory problemFactory,
+          IProblemReporter rep) throws RecognitionException {
+    if (element instanceof TextMarkerCondition) {
+      TextMarkerCondition a = (TextMarkerCondition) element;
+      String name = a.getName();
+      if (!name.equals(strings[0])) {
+        IProblem problem = problemFactory.createUnknownConditionProblem(a);
+        rep.reportProblem(problem);
+        return false;
+      }
+      boolean ok = true;
+      List<Expression> childs = a.getChilds();
+      if(childs.size() != 2) {
+        IProblem problem = problemFactory.createWrongNumberOfArgumentsProblem(name, element, 2);
+        rep.reportProblem(problem);
+        ok = false;
+      }
+      for (Expression expression : childs) {
+        if (expression.getKind() != TMTypeConstants.TM_TYPE_S) {
+          IProblem problem = problemFactory.createWrongArgumentTypeProblem(expression, "StringExpression");
+          rep.reportProblem(problem);
+          ok = false;
+        }
+      }
+      return ok;
+    }
+    return false;
+  }
+
+}

Modified: uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerCondition.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerCondition.java?rev=1476208&r1=1476207&r2=1476208&view=diff
==============================================================================
--- uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerCondition.java (original)
+++ uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerCondition.java Fri Apr 26 14:02:31 2013
@@ -65,8 +65,8 @@ public class TextMarkerCondition extends
   @Override
   public void traverse(ASTVisitor visitor) throws Exception {
     if (visitor.visit(this)) {
-      for (Iterator iterator = exprs.iterator(); iterator.hasNext();) {
-        Expression expr = (Expression) iterator.next();
+      for (Iterator<Expression> iterator = exprs.iterator(); iterator.hasNext();) {
+        Expression expr = iterator.next();
         if (expr != null) {
           expr.traverse(visitor);
         }
@@ -76,7 +76,7 @@ public class TextMarkerCondition extends
   }
 
   @Override
-  public List getChilds() {
+  public List<Expression> getChilds() {
     return exprs;
   }
 

Modified: uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerStringExpression.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerStringExpression.java?rev=1476208&r1=1476207&r2=1476208&view=diff
==============================================================================
--- uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerStringExpression.java (original)
+++ uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/parser/ast/TextMarkerStringExpression.java Fri Apr 26 14:02:31 2013
@@ -24,7 +24,6 @@ import java.util.List;
 import org.eclipse.dltk.ast.ASTListNode;
 import org.eclipse.dltk.ast.ASTVisitor;
 import org.eclipse.dltk.ast.expressions.Expression;
-import org.eclipse.dltk.ast.expressions.ExpressionConstants;
 import org.eclipse.dltk.utils.CorePrinter;
 
 /**
@@ -41,11 +40,6 @@ public class TextMarkerStringExpression 
   }
 
   @Override
-  public int getKind() {
-    return ExpressionConstants.E_CONCAT;
-  }
-
-  @Override
   public void traverse(ASTVisitor visitor) throws Exception {
     if (visitor.visit(this)) {
       this.exprs.traverse(visitor);

Modified: uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/validator/TextMarkerCheckerProblemFactory.java
URL: http://svn.apache.org/viewvc/uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/validator/TextMarkerCheckerProblemFactory.java?rev=1476208&r1=1476207&r2=1476208&view=diff
==============================================================================
--- uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/validator/TextMarkerCheckerProblemFactory.java (original)
+++ uima/sandbox/textmarker/trunk/textmarker-ep-ide/src/main/java/org/apache/uima/textmarker/ide/validator/TextMarkerCheckerProblemFactory.java Fri Apr 26 14:02:31 2013
@@ -154,17 +154,17 @@ public class TextMarkerCheckerProblemFac
       }
     }
     String message = "error: Feature \"" + feat + "\" is not defined.";
-    if(matchedType != null) {
-      message = "error: Feature \"" + feat + "\" is not defined for type \""+matchedType+"\".";
+    if (matchedType != null) {
+      message = "error: Feature \"" + feat + "\" is not defined for type \"" + matchedType + "\".";
     }
     return new TextMarkerCheckerDefaultProblem(this.fileName, message, var, getLine(var));
   }
 
-  public IProblem createWrongArgumentTypeProblem(Expression was,String expected) {
-    String message = "Wrong kind of argument: expected "+expected;
+  public IProblem createWrongArgumentTypeProblem(Expression was, String expected) {
+    String message = "Wrong kind of argument: expected " + expected;
     return new TextMarkerCheckerDefaultProblem(this.fileName, message, was, getLine(was));
   }
-  
+
   public IProblem createInheritenceFinalProblem(TextMarkerVariableReference parent) {
     String message = "Type \"" + parent.getName()
             + "\" is final and cannot be used as a parent type.";
@@ -181,4 +181,9 @@ public class TextMarkerCheckerProblemFac
     return new TextMarkerCheckerDefaultProblem(this.fileName, message, action, getLine(action));
   }
 
+  public IProblem createWrongNumberOfArgumentsProblem(String name, Expression element, int expected) {
+    String message = "error: The element " + name + " expects " + expected + " arguments.";
+    return new TextMarkerCheckerDefaultProblem(this.fileName, message, element, getLine(element));
+  }
+
 }