blob: 714e2c3fde9bba3c314a313e0bd3d428e2d7ec7b [file] [log] [blame]
/*
* 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.expression.string;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.StringArrayFS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.ruta.RutaStream;
import org.apache.uima.ruta.expression.feature.FeatureExpression;
import org.apache.uima.ruta.rule.MatchContext;
/**
* An expression referring to strings stored in a feature.
*
*/
public class StringListFeatureExpression extends AbstractStringListExpression {
private FeatureExpression fe;
public StringListFeatureExpression(FeatureExpression fe) {
super();
this.fe = fe;
}
@Override
public List<String> getList(MatchContext context, RutaStream stream) {
AnnotationFS annotation = context.getAnnotation();
Feature feature = this.fe.getFeature(context, stream);
if (feature == null || !feature.getRange().isArray()
|| !StringUtils.equals(feature.getRange().getName(), CAS.TYPE_NAME_STRING_ARRAY)) {
// throw runtime exception?
return Collections.emptyList();
}
List<AnnotationFS> list = this.getTargetAnnotation(annotation, this.fe, context, stream);
Collection<? extends FeatureStructure> featureStructures = this.fe.getFeatureStructures(list,
false, context, stream);
List<String> result = new ArrayList<>();
for (FeatureStructure each : featureStructures) {
if (each instanceof AnnotationFS && !each.getType().equals(annotation.getType())) {
feature = this.fe.getFeature(new MatchContext((AnnotationFS) each, context.getElement(),
context.getRuleMatch(), context.getDirection()), stream);
}
FeatureStructure featureValue = each.getFeatureValue(feature);
if (featureValue instanceof StringArrayFS) {
StringArrayFS array = (StringArrayFS) featureValue;
for (int i = 0; i < array.size(); i++) {
String b = array.get(i);
result.add(b);
}
}
}
return result;
}
public FeatureExpression getFeatureExpression() {
return this.fe;
}
public void setFeatureExpression(FeatureExpression fe) {
this.fe = fe;
}
}