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 2016/01/04 15:07:55 UTC
svn commit: r1722875 - in /uima/ruta/trunk/ruta-core/src:
main/java/org/apache/uima/ruta/action/SetFeatureAction.java
test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
Author: pkluegl
Date: Mon Jan 4 14:07:55 2016
New Revision: 1722875
URL: http://svn.apache.org/viewvc?rev=1722875&view=rev
Log:
UIMA-4408
- reuse RutaStream method
- extended test
Modified:
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/SetFeatureAction.java
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/SetFeatureAction.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/SetFeatureAction.java?rev=1722875&r1=1722874&r2=1722875&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/SetFeatureAction.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/action/SetFeatureAction.java Mon Jan 4 14:07:55 2016
@@ -22,20 +22,13 @@ package org.apache.uima.ruta.action;
import java.util.List;
import org.apache.uima.cas.Feature;
-import org.apache.uima.cas.Type;
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.IRutaExpression;
-import org.apache.uima.ruta.expression.bool.IBooleanExpression;
-import org.apache.uima.ruta.expression.feature.GenericFeatureExpression;
-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;
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 SetFeatureAction extends AbstractRutaAction {
@@ -63,71 +56,9 @@ public class SetFeatureAction extends Ab
List<AnnotationFS> matchedAnnotations = match.getMatchedAnnotationsOfElement(element);
for (AnnotationFS annotationFS : matchedAnnotations) {
Feature feature = annotationFS.getType().getFeatureByBaseName(featureString);
-
if (feature != null) {
- Type range = feature.getRange();
- String rangeName = range.getName();
stream.getCas().removeFsFromIndexes(annotationFS);
- if (rangeName.equals(UIMAConstants.TYPE_STRING) && expr instanceof IStringExpression) {
- IStringExpression stringExpr = (IStringExpression) expr;
- String string = stringExpr.getStringValue(context, stream);
- annotationFS.setStringValue(feature, string);
- } else if (rangeName.equals(UIMAConstants.TYPE_INTEGER)
- && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- int v = numberExpr.getIntegerValue(context, stream);
- annotationFS.setIntValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_DOUBLE) && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- double v = numberExpr.getDoubleValue(context, stream);
- annotationFS.setDoubleValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_FLOAT) && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- float v = numberExpr.getFloatValue(context, stream);
- annotationFS.setFloatValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_BYTE) && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- byte v = (byte) numberExpr.getIntegerValue(context, stream);
- annotationFS.setByteValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_SHORT) && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- short v = (short) numberExpr.getIntegerValue(context, stream);
- annotationFS.setShortValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_LONG) && expr instanceof INumberExpression) {
- INumberExpression numberExpr = (INumberExpression) expr;
- long v = numberExpr.getIntegerValue(context, stream);
- annotationFS.setLongValue(feature, v);
- } else if (rangeName.equals(UIMAConstants.TYPE_BOOLEAN)
- && expr instanceof IBooleanExpression) {
- IBooleanExpression booleanExpr = (IBooleanExpression) expr;
- boolean v = booleanExpr.getBooleanValue(context, stream);
- annotationFS.setBooleanValue(feature, v);
- } else if (expr instanceof ITypeExpression) {
- ITypeExpression typeExpr = (ITypeExpression) expr;
- Type t = typeExpr.getType(context, stream);
- List<AnnotationFS> inWindow = stream.getAnnotationsInWindow(annotationFS, t);
- if (feature.getRange().isArray()) {
- annotationFS.setFeatureValue(feature, UIMAUtils.toFSArray(stream.getJCas(), inWindow));
- } else {
- if (inWindow != null) {
- AnnotationFS annotation = inWindow.get(0);
- annotationFS.setFeatureValue(feature, annotation);
- } else {
- annotationFS.setFeatureValue(feature, null);
- }
- }
- } else if (expr instanceof GenericFeatureExpression) {
- ITypeExpression typeExpr = ((GenericFeatureExpression) expr).getFeatureExpression()
- .getTypeExpr(context, stream);
- Type t = typeExpr.getType(context, stream);
- List<AnnotationFS> inWindow = stream.getAnnotationsInWindow(annotationFS, t);
- if (feature.getRange().isArray()) {
- annotationFS.setFeatureValue(feature, UIMAUtils.toFSArray(stream.getJCas(), inWindow));
- } else {
- AnnotationFS annotation = inWindow.get(0);
- annotationFS.setFeatureValue(feature, annotation);
- }
- }
+ stream.assignFeatureValue(annotationFS, feature, expr, context);
stream.getCas().addFsToIndexes(annotationFS);
} else {
throw new IllegalArgumentException("Not able to assign feature value (e.g., coveredText).");
Modified: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java?rev=1722875&r1=1722874&r2=1722875&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/expression/annotation/AnnotationLabelExpressionTest.java Mon Jan 4 14:07:55 2016
@@ -41,7 +41,7 @@ import org.junit.Test;
public class AnnotationLabelExpressionTest {
@Test
- public void test() {
+ public void testSimple() {
String document = "Some text.";
String script = "a:W W{-> CREATE(Struct, \"a\"=a)};";
@@ -78,7 +78,142 @@ public class AnnotationLabelExpressionTe
AnnotationFS a = (AnnotationFS) next.getFeatureValue(f1);
assertNotNull("Feature value is null!", a);
assertEquals("Some", a.getCoveredText());
+ }
+
+ @Test
+ public void testLayers() {
+ String document = "Some text.";
+ String script = "d:(a:W b:W{-> CREATE(Struct, \"a\"=a, \"b\"=b, \"c\"=c, \"d\"=d)} c:PERIOD);";
+
+ Map<String, String> typeMap = new TreeMap<String, String>();
+ String typeName = "Struct";
+ typeMap.put(typeName, "uima.tcas.Annotation");
+
+ Map<String, List<TestFeature>> featureMap = new TreeMap<String, List<TestFeature>>();
+ List<TestFeature> list = new ArrayList<RutaTestUtils.TestFeature>();
+ featureMap.put(typeName, list);
+ list.add(new TestFeature("a", "", "uima.tcas.Annotation"));
+ list.add(new TestFeature("b", "", "uima.tcas.Annotation"));
+ list.add(new TestFeature("c", "", "uima.tcas.Annotation"));
+ list.add(new TestFeature("d", "", "uima.tcas.Annotation"));
+
+ CAS cas = null;
+ try {
+ cas = RutaTestUtils.getCAS(document, typeMap, featureMap);
+ Ruta.apply(cas, script);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ Type t = null;
+ AnnotationIndex<AnnotationFS> ai = null;
+ FSIterator<AnnotationFS> iterator = null;
+
+ t = cas.getTypeSystem().getType(typeName);
+ ai = cas.getAnnotationIndex(t);
+ assertEquals(1, ai.size());
+ iterator = ai.iterator();
+
+ AnnotationFS a = null;
+ AnnotationFS next = null;
+ Feature f = null;
+
+ next = iterator.next();
+ assertEquals("text", next.getCoveredText());
+
+
+ f = t.getFeatureByBaseName("a");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("Some", a.getCoveredText());
+
+ f = t.getFeatureByBaseName("b");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("text", a.getCoveredText());
+
+ f = t.getFeatureByBaseName("c");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals(".", a.getCoveredText());
+
+ f = t.getFeatureByBaseName("d");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("Some text.", a.getCoveredText());
}
+
+ @Test
+ public void testActions() {
+ String document = "Some text.";
+ String script = "a:W W{-> CREATE(Struct1, \"a\"=a)};";
+ script += "W W{-> Struct2, Struct3};";
+ script += "a:W Struct2{-> SETFEATURE(\"a\", a)};";
+ script += "a:W Struct3{-> Struct3.a=a};";
+
+ Map<String, String> typeMap = new TreeMap<String, String>();
+ typeMap.put("Struct1", "uima.tcas.Annotation");
+ typeMap.put("Struct2", "uima.tcas.Annotation");
+ typeMap.put("Struct3", "uima.tcas.Annotation");
+ typeMap.put("Struct4", "uima.tcas.Annotation");
+
+ Map<String, List<TestFeature>> featureMap = new TreeMap<String, List<TestFeature>>();
+ List<TestFeature> list = new ArrayList<RutaTestUtils.TestFeature>();
+ featureMap.put("Struct1", list);
+ featureMap.put("Struct2", list);
+ featureMap.put("Struct3", list);
+ featureMap.put("Struct4", list);
+ list.add(new TestFeature("a", "", "uima.tcas.Annotation"));
+
+ CAS cas = null;
+ try {
+ cas = RutaTestUtils.getCAS(document, typeMap, featureMap);
+ Ruta.apply(cas, script);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ Type t = null;
+ AnnotationIndex<AnnotationFS> ai = null;
+ FSIterator<AnnotationFS> iterator = null;
+ AnnotationFS a = null;
+ AnnotationFS next = null;
+ Feature f = null;
+
+ t = cas.getTypeSystem().getType("Struct1");
+ ai = cas.getAnnotationIndex(t);
+ assertEquals(1, ai.size());
+ iterator = ai.iterator();
+ next = iterator.next();
+ assertEquals("text", next.getCoveredText());
+ f = t.getFeatureByBaseName("a");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("Some", a.getCoveredText());
+
+ t = cas.getTypeSystem().getType("Struct2");
+ ai = cas.getAnnotationIndex(t);
+ assertEquals(1, ai.size());
+ iterator = ai.iterator();
+ next = iterator.next();
+ assertEquals("text", next.getCoveredText());
+ f = t.getFeatureByBaseName("a");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("Some", a.getCoveredText());
+
+ t = cas.getTypeSystem().getType("Struct3");
+ ai = cas.getAnnotationIndex(t);
+ assertEquals(1, ai.size());
+ iterator = ai.iterator();
+ next = iterator.next();
+ assertEquals("text", next.getCoveredText());
+ f = t.getFeatureByBaseName("a");
+ a = (AnnotationFS) next.getFeatureValue(f);
+ assertNotNull("Feature value is null!", a);
+ assertEquals("Some", a.getCoveredText());
+
+ }
}