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:32:32 UTC
svn commit: r1556717 - 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:32:32 2014
New Revision: 1556717
URL: http://svn.apache.org/r1556717
Log:
UIMA-3303 Add support to import packages using "IMPORT PACKAGE pkg" or "IMPORT PACKAGE pkg AS alias"
Added:
uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportPackageFromCasTypeSystem.ruta
Modified:
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
uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java
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=1556717&r1=1556716&r2=1556717&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:32:32 2014
@@ -58,6 +58,7 @@ import org.apache.uima.ruta.condition.Ab
import org.apache.uima.ruta.condition.ConditionFactory;
import org.apache.uima.ruta.RutaAutomataBlock;
import org.apache.uima.ruta.RutaBlock;
+import org.apache.uima.ruta.RutaEnvironment;
import org.apache.uima.ruta.RutaModule;
import org.apache.uima.ruta.RutaScriptBlock;
import org.apache.uima.ruta.RutaScriptFactory;
@@ -239,13 +240,18 @@ public void setExternalFactory(RutaExter
* Import a package from a type system.
*
* @param parent Block where the type should be imported.
- * @param tsd Typesystem from which to import the type.
+ * @param tsd Typesystem from which to import the package or null to import it from the CAS typesystem.
* @param qualifiedPackage Package to import from the typesystem.
* @param alias Package alias.
*/
- public void importPackageFromTypeSystem(RutaBlock parent, String typesystem, String qualifiedPackage, Token alias) {
- parent.getEnvironment()
- .importPackageFromTypeSystem(typesystem, qualifiedPackage, alias != null? alias.getText() : null);
+ public void importPackage(RutaBlock parent, String typesystem, String qualifiedPackage, Token alias) {
+ RutaEnvironment env = parent.getEnvironment();
+ String aliasText = alias != null? alias.getText() : null;
+ if (typesystem != null) {
+ env.importPackageFromTypeSystem(typesystem, qualifiedPackage, aliasText);
+ } else {
+ env.importPackage(qualifiedPackage, aliasText);
+ }
}
protected static final int[] getBounds(Token t) {
@@ -449,7 +455,7 @@ importStatement returns [RutaStatement s
| UimafitString ns = dottedIdentifier2{addImportUimafitEngine($blockDeclaration::env, ns);} SEMI
| ImportString type = dottedIdentifier (FromString ts = dottedIdentifier2)? (AsString alias = Identifier)? SEMI{importTypeFromTypeSystem($blockDeclaration::env, ts, type, alias);}
| ImportString STAR FromString ts = dottedIdentifier2 SEMI{addImportTypeSystem($blockDeclaration::env, ts);}
- | ImportString PackageString pkg = dottedIdentifier FromString ts = dottedIdentifier2 (AsString alias = Identifier)? SEMI{importPackageFromTypeSystem($blockDeclaration::env, ts, pkg, alias);}
+ | ImportString PackageString pkg = dottedIdentifier (FromString ts = dottedIdentifier2)? (AsString alias = Identifier)? SEMI{importPackage($blockDeclaration::env, ts, pkg, alias);}
;
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=1556717&r1=1556716&r2=1556717&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:32:32 2014
@@ -99,6 +99,14 @@ public class RutaEnvironment {
private Map<String, List<Alias>> typeImports;
/**
+ * Packages that are imported in the environment without a typesystem specification.
+ *
+ * Keys are package names and values are aliases. An empty string as alias means that all types from the package
+ * should be imported in the default namespace.
+ */
+ private Map<String, List<String>> packageImports;
+
+ /**
* Set of types that are declared in the script.
*/
private Set<String> declaredAnnotationTypes;
@@ -125,6 +133,7 @@ public class RutaEnvironment {
ambiguousTypeAlias = new HashMap<String, Set<String>>();
typesystems = new HashSet<String>();
typeImports = new HashMap<String, List<Alias>>();
+ packageImports = new HashMap<String, List<String>>();
declaredAnnotationTypes = new HashSet<String>();
wordLists = new HashMap<String, RutaWordList>();
tables = new HashMap<String, CSVTable>();
@@ -173,6 +182,7 @@ public class RutaEnvironment {
try {
if (strictImport) {
importDeclaredTypes(cas.getTypeSystem());
+ importDeclaredPackages(cas.getTypeSystem());
} else {
// import all types known to the cas
importAllTypes(cas.getTypeSystem());
@@ -253,6 +263,30 @@ public class RutaEnvironment {
}
}
+ /**
+ * Import all packages that are imported by the script.
+ *
+ * @param casTS Type system containing all known types.
+ */
+ private void importDeclaredPackages(TypeSystem casTS) {
+ Iterator<Type> iter = casTS.getTypeIterator();
+ while(iter.hasNext()) {
+ Type type = iter.next();
+ String name = type.getName();
+ String pkg = name.substring(0, Math.max(name.lastIndexOf('.'), 0));
+ List<String> aliases = packageImports.get(pkg);
+ if (aliases != null) {
+ for (String alias : aliases) {
+ if (alias.isEmpty()) {
+ addType(type);
+ } else {
+ addType(alias + "." + type.getShortName(), type);
+ }
+ }
+ }
+ }
+ }
+
public String[] getResourcePaths() {
if (resourcePaths == null) {
RutaBlock parent = owner.getParent();
@@ -433,6 +467,22 @@ public class RutaEnvironment {
}
}
+ /**
+ * Import all the types from a package that are available at runtime.
+ *
+ * @param packageName Package to load.
+ * @param alias Alias of the package. Null or empty string to use no alias.
+ */
+ public void importPackage(String packageName, String alias) {
+ List<String> aliases = packageImports.get(packageName);
+ if (aliases == null) {
+ aliases = new ArrayList<String>(1);
+ packageImports.put(packageName, aliases);
+ }
+
+ aliases.add(alias == null? "" : alias);
+ }
+
public RutaWordList getWordList(String list) {
RutaWordList result = wordLists.get(list);
if (result == null) {
Modified: 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=1556717&r1=1556716&r2=1556717&view=diff
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java (original)
+++ uima/ruta/trunk/ruta-core/src/test/java/org/apache/uima/ruta/ImportStatementsTest.java Thu Jan 9 04:32:32 2014
@@ -159,6 +159,21 @@ public class ImportStatementsTest {
}
+ @Test
+ public void testImportPackageFromCasTypeSystem() throws Exception {
+ AnalysisEngine ae = createAE(NAMESPACE + "/" + NAME + "ImportPackageFromCasTypeSystem", true);
+ try {
+ CAS cas = ae.newCAS();
+ cas.setDocumentText("First Second");
+ ae.process(cas);
+
+ assertEquals(Arrays.asList("First"), selectText(cas, "org.apache.uima.ruta.other.Type1"));
+ assertEquals(Arrays.asList("Second"), selectText(cas, "org.apache.uima.ruta.other2.Type1"));
+ } 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))) {
Added: uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportPackageFromCasTypeSystem.ruta
URL: http://svn.apache.org/viewvc/uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportPackageFromCasTypeSystem.ruta?rev=1556717&view=auto
==============================================================================
--- uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportPackageFromCasTypeSystem.ruta (added)
+++ uima/ruta/trunk/ruta-core/src/test/resources/org/apache/uima/ruta/ImportStatementsTestImportPackageFromCasTypeSystem.ruta Thu Jan 9 04:32:32 2014
@@ -0,0 +1,7 @@
+PACKAGE org.apache.uima.ruta;
+
+IMPORT PACKAGE org.apache.uima.ruta.other;
+IMPORT PACKAGE org.apache.uima.ruta.other2 AS other2;
+
+W{REGEXP("First") -> MARK(Type1)};
+W{REGEXP("Second") -> MARK(other2.Type1)};