You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by ap...@apache.org on 2014/01/09 05:31:38 UTC
svn commit: r1556712 - in /uima/ruta/trunk/ruta-core/src:
main/antlr3/org/apache/uima/ruta/parser/ main/java/org/apache/uima/ruta/
test/java/org/apache/uima/ruta/ test/resources/org/apache/uima/ruta/
Author: apatry
Date: Thu Jan 9 04:31:37 2014
New Revision: 1556712
URL: http://svn.apache.org/r1556712
Log:
UIMA-3303 Add support for "IMPORT type (FROM typesystem)?" statements
Added:
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java
uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportType.ruta
uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportTypeFromDefaultTypeSystem.ruta
uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestTypeSystem.xml
uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestUnimportedTypesAreNotResolved.ruta
Modified:
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaLexer.g
uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
Modified: uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaLexer.g
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaLexer.g?rev=1556712&r1=1556711&r2=1556712&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaLexer.g (original)
+++ uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaLexer.g Thu Jan 9 04:31:37 2014
@@ -339,7 +339,9 @@ DoubleString : 'DOUBLE';
FloatString : 'FLOAT';
StringString : 'STRING';
BooleanString : 'BOOLEAN';
-TypeSystemString: 'TYPESYSTEM';
+TypeSystemString: 'TYPESYSTEM';
+ImportString : 'IMPORT';
+FromString : 'FROM';
SymbolString : 'SYMBOL';
CONDITION : 'CONDITION';
ACTION : 'ACTION';
Modified: uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g?rev=1556712&r1=1556711&r2=1556712&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g (original)
+++ uima/ruta/trunk/ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g Thu Jan 9 04:31:37 2014
@@ -219,8 +219,18 @@ public void setExternalFactory(RutaExter
public void addImportUimafitEngine(RutaBlock parent, String namespace) {
parent.getScript().addUimafitEngine(namespace, null);
}
-
-
+
+ /**
+ * Import a type from a type system.
+ *
+ * @param parent Block where the type should be imported.
+ * @param tsd Typesystem from which to import the type.
+ * @param qualifiedType Type to import from the typesystem.
+ */
+ public void importTypeFromTypeSystem(RutaBlock parent, String typesystem, String qualifiedType) {
+ parent.getEnvironment().importTypeFromTypeSystem(typesystem, qualifiedType);
+ }
+
protected static final int[] getBounds(Token t) {
if (t instanceof CommonToken) {
CommonToken ct = (CommonToken) t;
@@ -420,6 +430,7 @@ importStatement returns [RutaStatement s
| ScriptString ns = dottedIdentifier2{addImportScript($blockDeclaration::env, ns);} SEMI
| EngineString ns = dottedIdentifier2{addImportEngine($blockDeclaration::env, ns);} SEMI
| UimafitString ns = dottedIdentifier2{addImportUimafitEngine($blockDeclaration::env, ns);} SEMI
+ | ImportString type = dottedIdentifier (FromString ts = dottedIdentifier2)? SEMI{importTypeFromTypeSystem($blockDeclaration::env, ts, type);}
;
declaration returns [RutaStatement stmt = null]
Modified: uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java?rev=1556712&r1=1556711&r2=1556712&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java (original)
+++ uima/ruta/trunk/ruta-core/src/main/java/org/apache/uima/ruta/RutaEnvironment.java Thu Jan 9 04:31:37 2014
@@ -22,14 +22,8 @@ package org.apache.uima.ruta;
import static org.apache.uima.util.Level.SEVERE;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
-import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -85,6 +79,25 @@ public class RutaEnvironment {
* Set of imported typesystems.
*/
private Set<String> typesystems;
+
+ /**
+ * An alias from a long to a short name.
+ */
+ private static class Alias {
+ final String longName;
+ final String shortName;
+
+ Alias(String longName, String shortName) {
+ this.longName = longName;
+ this.shortName = shortName;
+ }
+ }
+
+ /**
+ * Types that are imported in the environment. Keys are type system descriptors and values are aliased types.
+ */
+ private Map<String, List<Alias>> typeImports;
+
/**
* Set of types that are declared in the script.
*/
@@ -111,6 +124,7 @@ public class RutaEnvironment {
namespaces = new HashMap<String, String>();
ambiguousTypeAlias = new HashMap<String, Set<String>>();
typesystems = new HashSet<String>();
+ typeImports = new HashMap<String, List<Alias>>();
declaredAnnotationTypes = new HashSet<String>();
wordLists = new HashMap<String, RutaWordList>();
tables = new HashMap<String, CSVTable>();
@@ -217,6 +231,13 @@ public class RutaEnvironment {
}
}
+ // Add types that are imported explicitly
+ for (List<Alias> aliases : typeImports.values()) {
+ for (Alias alias : aliases) {
+ addType(alias.shortName, casTS.getType(alias.longName));
+ }
+ }
+
// Add declared types
for (String name : declaredAnnotationTypes) {
Type type = casTS.getType(name);
@@ -346,6 +367,26 @@ public class RutaEnvironment {
}
}
+ /**
+ * Import a type from a type system.
+ *
+ * @param typesystem Typesystem from which to import the type or null.
+ * @param longName Type to import.
+ */
+ public void importTypeFromTypeSystem(String typesystem, String longName) {
+ String key = typesystem != null? typesystem : "";
+ List<Alias> aliases = typeImports.get(key);
+
+ if (aliases == null) {
+ // we keep aliases sorted by key for
+ aliases = new ArrayList<Alias>();
+ typeImports.put(key, aliases);
+ }
+
+ String shortName = longName.substring(longName.lastIndexOf('.') + 1);
+ aliases.add(new Alias(longName, shortName));
+ }
+
public RutaWordList getWordList(String list) {
RutaWordList result = wordLists.get(list);
if (result == null) {
Added: uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java?rev=1556712&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java (added)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java Thu Jan 9 04:31:37 2014
@@ -0,0 +1,105 @@
+package org.apache.uima.ruta;
+
+import org.apache.uima.analysis_engine.AnalysisEngine;
+import org.apache.uima.analysis_engine.AnalysisEngineDescription;
+import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
+import org.apache.uima.cas.CAS;
+import org.apache.uima.cas.text.AnnotationFS;
+import org.apache.uima.fit.factory.AnalysisEngineFactory;
+import org.apache.uima.fit.factory.TypeSystemDescriptionFactory;
+import org.apache.uima.fit.util.CasUtil;
+import org.apache.uima.resource.ResourceInitializationException;
+import org.apache.uima.resource.metadata.TypeSystemDescription;
+import org.apache.uima.ruta.engine.RutaEngine;
+import org.apache.uima.util.InvalidXMLException;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * Test various import statements syntaxes (see UIMA-3303).
+ */
+public class ImportStatementsTest {
+ private final String NAME = this.getClass().getSimpleName();
+ private final String NAMESPACE = this.getClass().getPackage().getName().replaceAll("\\.", "/");
+
+ /**
+ * Create an analysis engine for a Ruta script.
+ *
+ * @param script Script path.
+ * @param strictImport {@link org.apache.uima.ruta.engine.RutaEngine#PARAM_STRICT_IMPORTS} value.
+ * @return Analysis engine.
+ */
+ private AnalysisEngine createAE(String script, boolean strictImport) throws ResourceInitializationException, IOException, InvalidXMLException {
+ final TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription(
+ "org.apache.uima.ruta.BasicTypeSystem",
+ "org.apache.uima.ruta.ImportStatementsTestTypeSystem");
+ final AnalysisEngineDescription ruta = AnalysisEngineFactory.createEngineDescription(
+ "org.apache.uima.ruta.engine.BasicEngine",
+ RutaEngine.PARAM_MAIN_SCRIPT, script,
+ RutaEngine.PARAM_STRICT_IMPORTS, strictImport);
+
+ ruta.getAnalysisEngineMetaData().setTypeSystem(tsd);
+
+ return AnalysisEngineFactory.createEngine(ruta);
+ }
+
+ @Test
+ public void testImportType() throws Exception {
+ AnalysisEngine ae = createAE(NAMESPACE + "/" + NAME + "ImportType", true);
+ try {
+ CAS cas = ae.newCAS();
+ cas.setDocumentText("Word");
+ ae.process(cas);
+
+ assertEquals(Arrays.asList("Word"), selectText(cas, "org.apache.uima.ruta.Type1"));
+ } finally {
+ ae.destroy();
+ }
+ }
+
+ @Test
+ public void testImportTypeFromDefaultTypesystem() throws Exception {
+ AnalysisEngine ae = createAE(NAMESPACE + "/" + NAME + "ImportTypeFromDefaultTypeSystem", true);
+ try {
+ CAS cas = ae.newCAS();
+ cas.setDocumentText("Word");
+ ae.process(cas);
+
+ assertEquals(Arrays.asList("Word"), selectText(cas, "org.apache.uima.ruta.Type1"));
+ } finally {
+ ae.destroy();
+ }
+ }
+
+ @Test
+ public void testUnimportedTypesAreNotResolved() throws Exception {
+ AnalysisEngine ae = createAE(NAMESPACE + "/" + NAME + "UnimportedTypesAreNotResolved", true);
+ try {
+ CAS cas = ae.newCAS();
+ cas.setDocumentText("Word");
+ ae.process(cas);
+
+ fail("Type2 should not be resolved because it is not imported.");
+ } catch (AnalysisEngineProcessException e) {
+ // success, Type2 should not be resolved
+ } finally {
+ ae.destroy();
+ }
+ }
+
+ private List<String> selectText(CAS cas, String type) {
+ List<String> values = new ArrayList<String>();
+ for (AnnotationFS annotation : CasUtil.select(cas, cas.getTypeSystem().getType(type))) {
+ values.add(annotation.getCoveredText());
+ }
+
+ return values;
+ }
+}
Added: uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportType.ruta
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportType.ruta?rev=1556712&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportType.ruta (added)
+++ uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportType.ruta Thu Jan 9 04:31:37 2014
@@ -0,0 +1,5 @@
+PACKAGE org.apache.uima.ruta;
+
+IMPORT org.apache.uima.ruta.Type1 FROM org.apache.uima.ruta.ImportStatementsTestTypeSystem;
+
+W{-> MARK(Type1)};
\ No newline at end of file
Added: uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportTypeFromDefaultTypeSystem.ruta
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportTypeFromDefaultTypeSystem.ruta?rev=1556712&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportTypeFromDefaultTypeSystem.ruta (added)
+++ uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportTypeFromDefaultTypeSystem.ruta Thu Jan 9 04:31:37 2014
@@ -0,0 +1,5 @@
+PACKAGE org.apache.uima.ruta;
+
+IMPORT org.apache.uima.ruta.Type1;
+
+W{-> MARK(Type1)};
\ No newline at end of file
Added: uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestTypeSystem.xml
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestTypeSystem.xml?rev=1556712&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestTypeSystem.xml (added)
+++ uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestTypeSystem.xml Thu Jan 9 04:31:37 2014
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+
+<typeSystemDescription xmlns="http://uima.apache.org/resourceSpecifier">
+ <name>ImportStatementsTestTypeSystem</name>
+ <description>
+ A type system to test ruta engine's import statements.
+ </description>
+ <version>1.0</version>
+ <vendor/>
+ <types>
+ <typeDescription>
+ <name>org.apache.uima.ruta.Type1</name>
+ <description/>
+ <supertypeName>uima.tcas.Annotation</supertypeName>
+ </typeDescription>
+ <typeDescription>
+ <name>org.apache.uima.ruta.Type2</name>
+ <description/>
+ <supertypeName>uima.tcas.Annotation</supertypeName>
+ </typeDescription>
+ </types>
+</typeSystemDescription>
Added: uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestUnimportedTypesAreNotResolved.ruta
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestUnimportedTypesAreNotResolved.ruta?rev=1556712&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestUnimportedTypesAreNotResolved.ruta (added)
+++ uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestUnimportedTypesAreNotResolved.ruta Thu Jan 9 04:31:37 2014
@@ -0,0 +1,6 @@
+PACKAGE org.apache.uima.ruta;
+
+IMPORT org.apache.uima.ruta.Type1 FROM org.apache.uima.ruta.ImportStatementsTestTypeSystem;
+
+// Type2 is in org.apache.uima.ruta.ImportStatementsTestTypeSystem, but it is not imported. It should not be resolved.
+W{-> MARK(Type2)};
\ No newline at end of file