Author: pkluegl
Date: Wed May 21 13:06:36 2014
New Revision: 1596568

URL: http://svn.apache.org/r1596568
Log:
UIMA-3833
- added new extension: ONLYONCE block
- added test
- added ide extension

Added:
    
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java
   (with props)
    
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java
   (with props)
    
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java
   (with props)
    
uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java
   (with props)
Modified:
    
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyFirstBlock.java
    
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyFirstBlockTest.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/rule/RutaRuleElement.java
    uima/ruta/trunk/ruta-ep-core-ext/plugin.xml

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=1596568&r1=1596567&r2=1596568&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
 Wed May 21 13:06:36 2014
@@ -32,7 +32,6 @@ import org.apache.uima.ruta.rule.Abstrac
 import org.apache.uima.ruta.rule.AbstractRuleMatch;
 import org.apache.uima.ruta.rule.RuleApply;
 import org.apache.uima.ruta.rule.RuleMatch;
-import org.apache.uima.ruta.rule.RutaRule;
 import org.apache.uima.ruta.rule.RutaRuleElement;
 import org.apache.uima.ruta.visitor.InferenceCrowd;
 

Added: 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java?rev=1596568&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java
 (added)
+++ 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java
 Wed May 21 13:06:36 2014
@@ -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.block;
+
+import org.apache.uima.ruta.RutaBlock;
+import org.apache.uima.ruta.RutaScriptBlock;
+import org.apache.uima.ruta.RutaStream;
+import org.apache.uima.ruta.ScriptApply;
+import org.apache.uima.ruta.visitor.InferenceCrowd;
+
+/**
+ * A block construct in UIMA Ruta, in which each rule matches only once and 
additional positions are
+ * skipped.
+ * 
+ */
+public class OnlyOnceBlock extends RutaScriptBlock {
+
+  public OnlyOnceBlock(RutaBlock parent, String defaultNamespace) {
+    super(null, null, null, parent, defaultNamespace);
+  }
+
+  @Override
+  public ScriptApply apply(RutaStream stream, InferenceCrowd crowd) {
+    boolean oldSetting = stream.isOnlyOnce();
+    stream.setOnlyOnce(true);
+    ScriptApply result = super.apply(stream, crowd);
+    stream.setOnlyOnce(oldSetting);
+    return result;
+  }
+
+}

Propchange: 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlock.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java?rev=1596568&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java
 (added)
+++ 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java
 Wed May 21 13:06:36 2014
@@ -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.block;
+
+import java.util.List;
+
+import org.apache.uima.ruta.RutaBlock;
+import org.apache.uima.ruta.RutaElement;
+import org.apache.uima.ruta.expression.RutaExpression;
+import org.apache.uima.ruta.extensions.IRutaBlockExtension;
+import org.apache.uima.ruta.extensions.RutaParseException;
+import org.apache.uima.ruta.verbalize.RutaVerbalizer;
+
+public class OnlyOnceBlockExtension implements IRutaBlockExtension {
+
+  private final String[] knownExtensions = new String[] { "ONLYONCE" };
+
+  private final Class<?>[] extensions = new Class[] { OnlyOnceBlock.class };
+
+  public String verbalize(RutaElement element, RutaVerbalizer verbalizer) {
+    if (element instanceof OnlyOnceBlock) {
+      OnlyOnceBlock b = (OnlyOnceBlock) element;
+      String verbalize = verbalizer.verbalize(b, false);
+      verbalize = verbalize.replaceFirst("BLOCK", verbalizeName(element));
+      return verbalize;
+    } else {
+      return "UnknownAction";
+    }
+  }
+
+  @Override
+  public RutaBlock createBlock(String name, List<RutaExpression> args, 
RutaBlock env)
+          throws RutaParseException {
+    return new OnlyOnceBlock(env, name);
+  }
+
+
+  public String verbalizeName(RutaElement element) {
+    return knownExtensions[0];
+  }
+
+  public String[] getKnownExtensions() {
+    return knownExtensions;
+  }
+
+  public Class<?>[] extensions() {
+    return extensions;
+  }
+
+
+}

Propchange: 
uima/ruta/trunk/ruta-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockExtension.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyFirstBlockTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyFirstBlockTest.java?rev=1596568&r1=1596567&r2=1596568&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyFirstBlockTest.java
 (original)
+++ 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyFirstBlockTest.java
 Wed May 21 13:06:36 2014
@@ -38,15 +38,17 @@ public class OnlyFirstBlockTest {
 
   @Test
   public void test() {
-    String document = "some text";
-    String script = "ONLYFIRST Document{}{\n";
-    script += "Document{-> T1};\n";
-    script += "Document{-> T2};\n";
+    String document = "some text with numbers: 1 2 3 4";
+    String script = "ONLYONCE Document{}{\n";
+    script += "SW{-> T1};\n";
+    script += "NUM{-> T2};\n";
+    script += "SW+{-> T1};\n";
+    script += "NUM+{-> T4};\n";
     script += "}";
 
     Map<String, Object> parameters = new HashMap<String, Object>();
     parameters.put(RutaEngine.PARAM_ADDITIONAL_EXTENSIONS,
-            new String[] { OnlyFirstBlockExtension.class.getName() });
+            new String[] { OnlyOnceBlockExtension.class.getName() });
     CAS cas = null;
     try {
       cas = RutaTestUtils.getCAS(document);
@@ -63,12 +65,26 @@ public class OnlyFirstBlockTest {
     ai = cas.getAnnotationIndex(t);
     assertEquals(1, ai.size());
     iterator = ai.iterator();
-    assertEquals("some text", iterator.next().getCoveredText());
+    assertEquals("some", iterator.next().getCoveredText());
 
     t = RutaTestUtils.getTestType(cas, 2);
     ai = cas.getAnnotationIndex(t);
-    assertEquals(0, ai.size());
+    assertEquals(1, ai.size());
+    iterator = ai.iterator();
+    assertEquals("1", iterator.next().getCoveredText());
+    
+    t = RutaTestUtils.getTestType(cas, 3);
+    ai = cas.getAnnotationIndex(t);
+    assertEquals(1, ai.size());
+    iterator = ai.iterator();
+    assertEquals("some text with numbers", iterator.next().getCoveredText());
 
+    t = RutaTestUtils.getTestType(cas, 2);
+    ai = cas.getAnnotationIndex(t);
+    assertEquals(1, ai.size());
+    iterator = ai.iterator();
+    assertEquals("1 2 3 4", iterator.next().getCoveredText());
+    
     if (cas != null) {
       cas.release();
     }

Added: 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java?rev=1596568&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java
 (added)
+++ 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java
 Wed May 21 13:06:36 2014
@@ -0,0 +1,77 @@
+/*
+ * 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.block;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.FSIterator;
+import org.apache.uima.cas.Type;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.cas.text.AnnotationIndex;
+import org.apache.uima.ruta.engine.Ruta;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.ruta.engine.RutaTestUtils;
+import org.junit.Test;
+
+public class OnlyOnceBlockTest {
+
+  @Test
+  public void test() {
+    String document = "some text";
+    String script = "ONLYFIRST Document{}{\n";
+    script += "Document{-> T1};\n";
+    script += "Document{-> T2};\n";
+    script += "}";
+
+    Map<String, Object> parameters = new HashMap<String, Object>();
+    parameters.put(RutaEngine.PARAM_ADDITIONAL_EXTENSIONS,
+            new String[] { OnlyFirstBlockExtension.class.getName() });
+    CAS cas = null;
+    try {
+      cas = RutaTestUtils.getCAS(document);
+      Ruta.apply(cas, script, parameters);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+
+    Type t = null;
+    AnnotationIndex<AnnotationFS> ai = null;
+    FSIterator<AnnotationFS> iterator = null;
+
+    t = RutaTestUtils.getTestType(cas, 1);
+    ai = cas.getAnnotationIndex(t);
+    assertEquals(1, ai.size());
+    iterator = ai.iterator();
+    assertEquals("some text", iterator.next().getCoveredText());
+
+    t = RutaTestUtils.getTestType(cas, 2);
+    ai = cas.getAnnotationIndex(t);
+    assertEquals(0, ai.size());
+
+    if (cas != null) {
+      cas.release();
+    }
+
+  }
+}

Propchange: 
uima/ruta/trunk/ruta-core-ext/src/test/java/org/apache/uima/ruta/block/OnlyOnceBlockTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1596568&r1=1596567&r2=1596568&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 
Wed May 21 13:06:36 2014
@@ -86,6 +86,8 @@ public class RutaStream extends FSIterat
 
   private Boolean greedyRule;
 
+  private boolean onlyOnce = false;
+
   public RutaStream(CAS cas, Type basicType, FilterManager filter, boolean 
lowMemoryProfile,
           boolean simpleGreedyForComposed, InferenceCrowd crowd) {
     super();
@@ -359,6 +361,7 @@ public class RutaStream extends FSIterat
     stream.setDynamicAnchoring(dynamicAnchoring);
     stream.setGreedyRuleElement(greedyRuleElement);
     stream.setGreedyRule(greedyRule);
+    stream.setGreedyRule(greedyRule);
     return stream;
   }
 
@@ -752,6 +755,19 @@ public class RutaStream extends FSIterat
     this.simpleGreedyForComposed = simpleGreedyForComposed;
   }
 
+  public boolean isGreedyAnchoring() {
+    return greedyRule || greedyRuleElement;
+  }
+
+  public boolean isOnlyOnce() {
+    return onlyOnce;
+  }
+  
+  public void setOnlyOnce(Boolean onlyOnce) {
+    this.onlyOnce = onlyOnce;
+  }
+  
+  
   public boolean isVisible(AnnotationFS annotationFS) {
     if (annotationFS == null) {
       return false;
@@ -802,10 +818,6 @@ public class RutaStream extends FSIterat
     }
   }
 
-  public boolean isGreedyAnchoring() {
-    return greedyRule || greedyRuleElement;
-  }
-
   public Collection<AnnotationFS> getAnnotations(Type type) {
     Collection<AnnotationFS> result = new LinkedList<AnnotationFS>();
     AnnotationFS windowAnnotation = filter.getWindowAnnotation();
@@ -832,4 +844,6 @@ public class RutaStream extends FSIterat
     return result;
   }
 
+
+
 }

Modified: 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java?rev=1596568&r1=1596567&r2=1596568&view=diff
==============================================================================
--- 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
 (original)
+++ 
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/rule/RutaRuleElement.java
 Wed May 21 13:06:36 2014
@@ -60,8 +60,14 @@ public class RutaRuleElement extends Abs
     boolean useAlternatives = anchors.size() != 1;
     for (AnnotationFS eachAnchor : anchors) {
       if (stream.isGreedyAnchoring() && isAlreadyCovered(eachAnchor, 
ruleApply, stream)) {
+        // skip if next matched should not overlap
         continue;
       }
+      if (stream.isOnlyOnce() && ruleApply.getApplied() > 0) {
+        // skip if the rule should only be applied once, on the first 
successful match
+        continue;
+      }
+      
       ComposedRuleElementMatch extendedContainerMatch = containerMatch;
       RuleMatch extendedMatch = ruleMatch;
       if (useAlternatives) {

Modified: uima/ruta/trunk/ruta-ep-core-ext/plugin.xml
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-ep-core-ext/plugin.xml?rev=1596568&r1=1596567&r2=1596568&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-ep-core-ext/plugin.xml (original)
+++ uima/ruta/trunk/ruta-ep-core-ext/plugin.xml Wed May 21 13:06:36 2014
@@ -26,5 +26,12 @@ under the License.
             engine="org.apache.uima.ruta.block.OnlyFirstBlockExtension">
       </block>
    </extension>
+   <extension
+         point="org.apache.uima.ruta.ide.blockExtension">
+      <block
+            class="org.apache.uima.ruta.block.OnlyOnceBlockIDEExtension"
+            engine="org.apache.uima.ruta.block.OnlyOnceBlockExtension">
+      </block>
+   </extension>
 
 </plugin>

Added: 
uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java
URL: 
http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java?rev=1596568&view=auto
==============================================================================
--- 
uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java
 (added)
+++ 
uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java
 Wed May 21 13:06:36 2014
@@ -0,0 +1,41 @@
+/*
+ * 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.block;
+
+import org.antlr.runtime.RecognitionException;
+import org.apache.uima.ruta.ide.core.extensions.IIDEConditionExtension;
+import org.apache.uima.ruta.ide.core.extensions.IRutaCheckerProblemFactory;
+import org.eclipse.dltk.ast.expressions.Expression;
+import org.eclipse.dltk.compiler.problem.IProblemReporter;
+
+public class OnlyOnceBlockIDEExtension implements IIDEConditionExtension {
+  private final String[] strings = new String[] { "ONLYONCE" };
+
+  public String[] getKnownExtensions() {
+    return strings;
+  }
+
+  public boolean checkSyntax(Expression element, IRutaCheckerProblemFactory 
problemFactory,
+          IProblemReporter rep) throws RecognitionException {
+    // do not add additional checks yet
+    return true;
+  }
+
+}

Propchange: 
uima/ruta/trunk/ruta-ep-core-ext/src/main/java/org/apache/uima/ruta/block/OnlyOnceBlockIDEExtension.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to