Apache UIMA 配置参数详解

详解 Apache UIMA 3.6.1 描述符与配置参数体系:TypeSystem 与 AE 描述符关键元素、配置参数声明注入、JUL 日志、CPE 参数与 JVM 堆设置,均附实测回显与报错定位。

Apache UIMAApache UIMA2026/10/12

Apache UIMA 配置参数详解

一、配置体系总览

Apache UIMA 的运行行为由一组 XML 描述符驱动,各描述符分工如下:

描述符根元素作用
类型系统描述符typeSystemDescription声明分析中用到的类型与特征
原语 AE 描述符analysisEngineDescription(primitive=true)绑定注解器实现类、声明配置参数
聚合 AE 描述符analysisEngineDescription(primitive=false)组合多个委托组件并定义执行流
Collection Reader 描述符collectionReaderDescription数据源接入,产出 CAS
CAS Consumer 描述符casConsumerDescription消费处理后的 CAS(落盘、上报等)
CPE 描述符cpeDescription编排 Collection Reader 与 CAS 处理器的批处理

本文基于 uimaj-core / uimaj-cpe / uimaj-tools 3.6.1(Maven Central 最新稳定版),全部条目在 OpenJDK 17 环境实测验证。

二、TypeSystem 描述符关键元素

TypeSystem 描述符的核心是 types/typeDescription,每个类型声明四要素:类型名、说明、父类型、特征列表。实测样例(descriptors/TypeSystem.xml):

<types>
  <typeDescription>
    <name>com.example.uima.RoomNumber</name>
    <description>A room number such as 9244-108</description>
    <supertypeName>uima.tcas.Annotation</supertypeName>
    <features>
      <featureDescription>
        <name>pattern</name>
        <description>The raw matched pattern text</description>
        <rangeTypeName>uima.cas.String</rangeTypeName>
      </featureDescription>
    </features>
  </typeDescription>
</types>

要点:

  • supertypeName 常用 uima.tcas.Annotation(带起止位置的文本注解);不关心位置时可用 uima.cas.TOP。
  • rangeTypeName 可取 uima.cas.String、uima.cas.Integer、uima.cas.Boolean、uima.cas.Float 及数组类型 uima.cas.StringArray 等。
  • TypeSystem 可通过 imports 复用:<import location="OtherTypes.xml"/>(相对路径)或 <import name="com.example.Types"/>(类路径资源)。
  • 类型声明完成后必须用 JCasGen 生成绑定类,编译期才能以强类型方式访问特征。实测生成命令:
java -cp uimaj-tools.jar:uimaj-core.jar:slf4j-api.jar \
  org.apache.uima.tools.jcasgen.Jg -jcasgeninput descriptors/TypeSystem.xml -jcasgenoutput src/main/java

三、AnalysisEngine 描述符关键元素

3.1 原语 AE 描述符

实测样例中的关键结构(节选):

<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
  <frameworkImplementation>org.apache.uima.java</frameworkImplementation>
  <primitive>true</primitive>
  <annotatorImplementationName>com.example.uima.RoomNumberAnnotator</annotatorImplementationName>
  <analysisEngineMetaData>
    <name>RoomNumberAnnotator</name>
    <version>1.0</version>
    <configurationParameters searchStrategy="language_fallback">...</configurationParameters>
    <configurationParameterSettings>...</configurationParameterSettings>
    <typeSystemDescription>
      <imports><import location="TypeSystem.xml"/></imports>
    </typeSystemDescription>
    <capabilities>...</capabilities>
    <operationalProperties>
      <modifiesCas>true</modifiesCas>
      <multipleDeploymentAllowed>true</multipleDeploymentAllowed>
      <outputsNewCASes>false</outputsNewCASes>
    </operationalProperties>
  </analysisEngineMetaData>
</analysisEngineDescription>

逐项说明:

  • frameworkImplementation:Java 实现固定为 org.apache.uima.java。
  • annotatorImplementationName:注解器全限定类名,需继承 JCasAnnotator_ImplBase 或 CasAnnotator_ImplBase。
  • capabilities:声明输入/输出类型,供聚合 AE 做兼容性检查;allAnnotatorFeatures="true" 表示连同全部特征。
  • operationalProperties:modifiesCas 声明是否修改 CAS(影响并行部署),multipleDeploymentAllowed 声明是否可多实例并行。

3.2 聚合 AE 描述符

聚合 AE 组合委托组件并用 fixedFlow 定义线性执行流:

<analysisEngineDescription xmlns="http://uima.apache.org/resourceSpecifier">
  <frameworkImplementation>org.apache.uima.java</frameworkImplementation>
  <primitive>false</primitive>
  <delegateAnalysisEngineSpecifiers>
    <delegateAnalysisEngine key="RoomNumberAnnotator">
      <import location="RoomNumberAnnotator.xml"/>
    </delegateAnalysisEngine>
    <delegateAnalysisEngine key="RoomNumberCounter">
      <import location="RoomNumberCounter.xml"/>
    </delegateAnalysisEngine>
  </delegateAnalysisEngineSpecifiers>
  <analysisEngineMetaData>
    <name>RoomNumber Aggregate AE</name>
    <version>1.0</version>
    <configurationParameters searchStrategy="language_fallback"/>
    <configurationParameterSettings/>
    <capabilities>...</capabilities>
    <operationalProperties>
      <modifiesCas>true</modifiesCas>
      <multipleDeploymentAllowed>true</multipleDeploymentAllowed>
      <outputsNewCASes>false</outputsNewCASes>
    </operationalProperties>
    <flowConstraints>
      <fixedFlow>
        <node>RoomNumberAnnotator</node>
        <node>RoomNumberCounter</node>
      </fixedFlow>
    </flowConstraints>
  </analysisEngineMetaData>
</analysisEngineDescription>

实测确认的两条硬性规则(违反即初始化失败):

  1. flowConstraints 必须写在 analysisEngineMetaData 内部。放在描述符顶层会报 InvalidXMLException: The XML parser encountered an unknown element type: flowConstraints。
  2. fixedFlow 的 node 用元素文本承载组件 key,不是 key 属性。实测用 jshell 调用 FixedFlow_impl.toXML() 得到的规范序列化输出为:
<fixedFlow xmlns="http://uima.apache.org/resourceSpecifier">
    <node>RoomNumberAnnotator</node>
    <node>RoomNumberCounter</node>
</fixedFlow>

写成 <node key="RoomNumberAnnotator"/> 时解析出的 key 为空串,运行报 The key "" was used in the flow but is not defined as a key in the <delegateAnalysisEngineSpecifiers> element。

  1. 聚合 AE 不得显式声明 TypeSystem。类型系统由全部委托组件合并推导,写 <typeSystemDescription> 会报 The Analysis Engine Descriptor is invalid - a Type System may not be explicitly set for an aggregate Analysis Engine。

四、ConfigurationParameter 声明与注入

4.1 声明语法:子元素,不是属性

实测踩坑确认:<configurationParameter> 的名称、类型、是否必填必须用子元素声明,写成属性会被解析器静默忽略,导致后续报 No configuration parameter with name "Pattern" is declared in component "RoomNumberAnnotator"。正确写法:

<configurationParameters searchStrategy="language_fallback">
  <configurationParameter>
    <name>Pattern</name>
    <description>Regular expression for room numbers</description>
    <type>String</type>
    <multiValued>false</multiValued>
    <mandatory>false</mandatory>
  </configurationParameter>
</configurationParameters>
<configurationParameterSettings>
  <nameValuePair>
    <name>Pattern</name>
    <value><string>(\d{3,4})\s*-\s*\d{1,4}</string></value>
  </nameValuePair>
</configurationParameterSettings>

参数类型支持 String、Integer、Float、Boolean;multiValued=true 时值元素为 <array><string>a</string><string>b</string></array>。mandatory=true 且未提供设置值时,实例化直接抛 ResourceInitializationException。

4.2 代码中读取参数

注解器在 initialize 中通过 UimaContext 读取:

String regex = (String) context.getConfigParameterValue("Pattern");

4.3 运行期注入:setConfigParameterValue + reconfigure(实测有效)

对已实例化的 AE 覆盖参数后重配置,实测验证:

AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(specifier);
ae.setConfigParameterValue("Pattern", "2244-3A");
ae.reconfigure();

实测输出(只命中字面量 2244-3A):

[ae] parameter override applied: Pattern="2244-3A"
[jcas] RoomNumber #1 begin=96 end=103 covered="2244-3A" pattern="2244-3A"
[jcas] total RoomNumber = 1

4.4 两条实测确认的限制

  1. produceAnalysisEngine 的 Map 参数不注入配置参数。UIMAFramework.produceAnalysisEngine(specifier, params) 的 Map 用于资源初始化参数,实测传入 Map.of("Pattern", "5117-C") 后注解器仍使用描述符默认值(输出 9244-108 而非 5117-C)。注入请用 4.3 节方式。
  2. 聚合 AE 上调用 setConfigParameterValue 不会下发给委托组件。对聚合 AE 分别用 Pattern 与 RoomNumberAnnotator.Pattern 两种写法覆盖后 reconfigure,实测计数器统计到的仍按默认正则命中 3 个(values=[9244-108, 3116-2, 2244-3])。聚合层参数覆盖的正确做法是在聚合描述符中声明同名参数并提供设置值,或直接修改委托描述符后重启管线。

4.5 searchStrategy 说明

searchStrategy="language_fallback" 表示查询先找当前文档语言的设置(如 Pattern/en),找不到再退回无语言设置;默认值 none 只查无语言设置。多语言场景保持 language_fallback 即可。

五、日志配置

5.1 UIMA Logger 与 JUL 绑定

UIMA 框架与注解器统一通过 org.apache.uima.util.Logger 打日志,默认实现 JSR47Logger_impl 桥接到 java.util.logging(JUL)。获取方式两种等价:

UIMAFramework.getLogger();                 // 框架级
context.getLogger();                       // 组件级(注解器内)

日志级别用 UIMA 自己的 org.apache.uima.util.Level(INFO/WARN/FINE 等),注意不要导入 java.util.logging.Level,实测会因类型不匹配编译失败。

5.2 消息是 MessageFormat 模板(实测坑)

UIMA Logger 的消息串会作为 java.text.MessageFormat 模式解析。实测把正则直接拼进日志:

context.getLogger().info("... regex=" + regex);   // regex = (\d{3,4})\s*-\s*\d{1,4}

运行即抛:

Caused by: java.lang.IllegalArgumentException: unknown format type: 4
    at java.text.MessageFormat.makeFormat(MessageFormat.java:1562)
    at org.apache.uima.util.impl.JSR47Logger_impl.log(JSR47Logger_impl.java:263)

{3,4} 被当作"参数 3,格式类型 4"。正确写法是参数化日志:

context.getLogger().log(Level.INFO, "RoomNumberAnnotator initialized, regex={0}", regex);

5.3 用 logging.properties 控制 JUL 输出

新建 logging.properties:

handlers=java.util.logging.ConsoleHandler
.level=INFO
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$s] %5$s%6$s%n
org.apache.uima.level=INFO

启动时注入:

java -Djava.util.logging.config.file=logging.properties -Xms256m -Xmx512m \
  -cp "target/classes:$(cat target/cp.txt)" com.example.uima.RunPipeline

实测日志行从默认格式变为:

[2026-10-12 14:52:18] [INFO] RoomNumberAnnotator initialized, regex=(\d{3,4})\s*-\s*\d{1,4}

5.4 slf4j 绑定

uimaj-cpe 传递依赖 slf4j-api 1.7.36。未提供绑定时运行会打印 SLF4J: Defaulting to no-operation (NOP) logger implementation 告警(功能不受影响)。工程引入 org.slf4j:slf4j-jdk14:1.7.36(runtime)即可把 slf4j 日志汇入 JUL,与 UIMA 日志同通道。

六、CPE 参数

6.1 CPE 描述符结构

CPE(集合处理引擎)把 Collection Reader 与若干 CAS 处理器编排成批处理。实测用 CpeDescriptorFactory 编程式构建并序列化出的完整描述符(descriptors/RoomNumberCPE.xml,可直接复用为模板):

<?xml version="1.0" encoding="UTF-8"?>
<cpeDescription xmlns="http://uima.apache.org/resourceSpecifier">
    <collectionReader>
        <collectionIterator>
            <descriptor>
                <include href="descriptors/TextCollectionReader.xml"/>
            </descriptor>
        </collectionIterator>
    </collectionReader>
    <casProcessors casPoolSize="1" processingUnitThreadCount="1">
        <casProcessor deployment="integrated" name="RoomNumberAnnotator">
            <descriptor>
                <include href="descriptors/RoomNumberAnnotator.xml"/>
            </descriptor>
            <deploymentParameters/>
            <errorHandling>
                <errorRateThreshold action="terminate" value="100/1000"/>
                <maxConsecutiveRestarts action="terminate" value="30"/>
                <timeout max="100000" default="-1"/>
            </errorHandling>
            <checkpoint batch="1" time="1000ms"/>
        </casProcessor>
    </casProcessors>
    <cpeConfig>
        <numToProcess>-1</numToProcess>
        <deployAs>immediate</deployAs>
        <checkpoint batch="0" time="300000ms"/>
        <timerImpl/>
    </cpeConfig>
</cpeDescription>

6.2 关键参数说明

参数含义实测取值
casPoolSizeCAS 对象池大小,即同时在内存中的 CAS 数1
processingUnitThreadCount处理线程数,多文档并行时调大1
deploymentintegrated 进程内运行;远程部署用其他取值integrated
errorRateThreshold错误率阈值,超限执行 action(terminate/continue)100/1000
maxConsecutiveRestarts处理器连续重启上限30
timeout max单文档处理超时(毫秒),-1 不限时100000
numToProcess最多处理的文档数,-1 表示全部-1
checkpoint批次检查点(断点续跑)频率1000ms

6.3 编程式构建与运行

CpeDescription cpeDesc = CpeDescriptorFactory.produceDescriptor();
cpeDesc.addCollectionReader("descriptors/TextCollectionReader.xml");
CpeCasProcessor annotator = CpeDescriptorFactory.produceCasProcessor("RoomNumberAnnotator");
annotator.setDescriptor("descriptors/RoomNumberAnnotator.xml");
cpeDesc.addCasProcessor(annotator);
cpeDesc.setNumToProcess(-1);

CollectionProcessingEngine engine =
        UIMAFramework.produceCollectionProcessingEngine(cpeDesc, Collections.emptyMap());
engine.addStatusCallbackListener(listener);
engine.process();   // 异步执行,回调里结束 JVM

注意工厂类是 org.apache.uima.collection.impl.metadata.cpe.CpeDescriptorFactory(位于 uimaj-cpe 构件),回调接口 org.apache.uima.collection.StatusCallbackListener 除 entityProcessComplete 外还要求实现 initializationComplete/batchProcessComplete/collectionProcessComplete/paused/resumed/aborted(实测未实现会编译失败)。实测运行回显:

[cpe] generated CPE descriptor: /opt/uima-demo/descriptors/RoomNumberCPE.xml
INFO: RoomNumberAnnotator initialized, regex=(\d{3,4})\s*-\s*\d{1,4}
[reader] input directory=/opt/uima-demo/data/in, files found=1
[cpe] initializationComplete - CPE is running
[cpe] document processed, length=130, errors=0
[cpe] collectionProcessComplete - finished

七、JVM 参数

7.1 堆限定

UIMA 框架本身很轻,单机示例与中小规模服务把堆限制在 256-512 MB 即可稳定运行:

java -Xms256m -Xmx512m -cp "target/classes:$(cat target/cp.txt)" com.example.uima.RunPipeline

实测在程序入口打印 Runtime.getRuntime().maxMemory():

[heap] effective max heap = 512 MB

7.2 MAVEN_OPTS / JAVA_OPTS 修改位置

  • Maven 进程:MAVEN_OPTS,写入 /etc/profile.d/uima-build.sh:
export MAVEN_OPTS="-Xms256m -Xmx512m"
  • java 直跑进程:直接在命令行加 -Xms256m -Xmx512m;固定脚本化运行时可写 JAVA_OPTS 并在启动脚本中引用。

7.3 实测验证

构建期间用 jps -v 查看 Maven 进程(实测回显):

4887 Launcher -Xms256m -Xmx512m -Dclassworlds.conf=/usr/share/maven/bin/m2.conf ...

运行期对 java 进程验证(实测回显):

7236 RunAE -Xms256m -Xmx512m

八、常见问题

Q1:报 No configuration parameter with name "Pattern" is declared?

实测原因:<configurationParameter> 用了 name/type 属性写法,声明被解析器忽略。解决:改用 <name>/<type>/<multiValued>/<mandatory> 子元素声明。

Q2:聚合 AE 初始化报 unknown element type: flowConstraints?

实测原因:flowConstraints 写在了描述符顶层。解决:移入 analysisEngineMetaData 元素内部。

Q3:聚合 AE 报 The key "" was used in the flow?

实测原因:fixedFlow 节点写成了 <node key="X"/> 属性形式。解决:改为 <node>X</node> 文本形式(见 3.2 节规范输出)。

Q4:聚合 AE 报 a Type System may not be explicitly set for an aggregate Analysis Engine?

实测原因:聚合描述符里写了 <typeSystemDescription>。解决:删除该元素,类型系统由委托组件合并推导。

Q5:聚合层 setConfigParameterValue 不生效?

实测确认聚合上下文的参数设置不下发给委托组件(Pattern 与 RoomNumberAnnotator.Pattern 两种写法均无效)。解决:在聚合描述符中声明同名参数并给设置值,或修改委托描述符后重建实例。

Q6:日志抛 unknown format type: 4?

实测原因:UIMA Logger 将消息串按 MessageFormat 解析,内容含未转义花括号。解决:改用 log(Level, String, Object) 参数化写法,用 {0} 占位。

九、参考链接