in ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java [1201:1419]
public void assignFeatureValue(FeatureStructure annotation, Feature feature,
IRutaExpression value, MatchContext context) {
if (feature == null || feature instanceof CoveredTextFeature
|| feature instanceof TypeFeature) {
throw new IllegalArgumentException(
"Not able to assign feature value (e.g., coveredText, type) in script "
+ context.getParent().getName());
}
if (feature instanceof LazyFeature) {
LazyFeature lazyFeature = (LazyFeature) feature;
feature = lazyFeature.initialize(annotation);
}
CAS cas = annotation.getCAS();
TypeSystem typeSystem = cas.getTypeSystem();
Type range = feature.getRange();
String rangeName = range.getName();
if (typeSystem.subsumes(typeSystem.getType(CAS.TYPE_NAME_STRING), range)) {
if (value instanceof IStringExpression) {
IStringExpression stringExpr = (IStringExpression) value;
String string = stringExpr.getStringValue(context, this);
annotation.setStringValue(feature, string);
}
} else if (rangeName.equals(CAS.TYPE_NAME_STRING_ARRAY)) {
if (value instanceof IStringListExpression) {
IStringListExpression stringListExpr = (IStringListExpression) value;
List<String> stringList = stringListExpr.getStringList(context, this);
StringArrayFS stringArray = FSCollectionFactory.createStringArrayFS(cas, stringList);
annotation.setFeatureValue(feature, stringArray);
} else if (value instanceof IStringExpression) {
IStringExpression stringExpr = (IStringExpression) value;
String string = stringExpr.getStringValue(context, this);
StringArrayFS array = FSCollectionFactory.createStringArrayFS(cas, string);
annotation.setFeatureValue(feature, array);
}
} else if (rangeName.equals(CAS.TYPE_NAME_INTEGER) || rangeName.equals(CAS.TYPE_NAME_LONG)
|| rangeName.equals(CAS.TYPE_NAME_SHORT) || rangeName.equals(CAS.TYPE_NAME_BYTE)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
int v = numberExpr.getIntegerValue(context, this);
if (annotation instanceof AnnotationFS
&& StringUtils.equals(feature.getShortName(), CAS.FEATURE_BASE_NAME_BEGIN)) {
changeBegin((AnnotationFS) annotation, v, context.getRuleMatch());
} else if (annotation instanceof AnnotationFS
&& StringUtils.equals(feature.getShortName(), CAS.FEATURE_BASE_NAME_END)) {
changeEnd((AnnotationFS) annotation, v, context.getRuleMatch());
} else {
annotation.setIntValue(feature, v);
}
}
} else if (rangeName.equals(CAS.TYPE_NAME_INTEGER_ARRAY)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
int v = numberExpr.getIntegerValue(context, this);
IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
List<Number> list = expr.getNumberList(context, this);
IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas,
RutaListUtils.toIntArray(list));
annotation.setFeatureValue(feature, array);
}
} else if (rangeName.equals(CAS.TYPE_NAME_DOUBLE)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
double v = numberExpr.getDoubleValue(context, this);
annotation.setDoubleValue(feature, v);
}
} else if (rangeName.equals(CAS.TYPE_NAME_DOUBLE_ARRAY)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
double v = numberExpr.getDoubleValue(context, this);
DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
List<Number> list = expr.getNumberList(context, this);
DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas,
RutaListUtils.toDoubleArray(list));
annotation.setFeatureValue(feature, array);
}
} else if (rangeName.equals(CAS.TYPE_NAME_FLOAT)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
float v = numberExpr.getFloatValue(context, this);
annotation.setFloatValue(feature, v);
}
} else if (rangeName.equals(CAS.TYPE_NAME_FLOAT_ARRAY)) {
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
float v = numberExpr.getFloatValue(context, this);
FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
List<Number> list = expr.getNumberList(context, this);
FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas,
RutaListUtils.toFloatArray(list));
annotation.setFeatureValue(feature, array);
}
} else if (rangeName.equals(CAS.TYPE_NAME_BOOLEAN)) {
if (value instanceof IBooleanExpression) {
IBooleanExpression expr = (IBooleanExpression) value;
Boolean v = expr.getBooleanValue(context, this);
annotation.setBooleanValue(feature, v);
} else if (value instanceof IStringExpression) {
String stringValue = ((IStringExpression) value).getStringValue(context, this);
annotation.setBooleanValue(feature, Boolean.parseBoolean(stringValue));
}
} else if (rangeName.equals(CAS.TYPE_NAME_BOOLEAN_ARRAY)) {
if (value instanceof IBooleanListExpression) {
IBooleanListExpression expr = (IBooleanListExpression) value;
List<Boolean> list = expr.getBooleanList(context, this);
BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, list);
annotation.setFeatureValue(feature, array);
} else if (value instanceof IBooleanExpression) {
IBooleanExpression expr = (IBooleanExpression) value;
Boolean v = expr.getBooleanValue(context, this);
BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
}
} else if (value instanceof AnnotationTypeExpression && !range.isPrimitive()) {
AnnotationTypeExpression ate = (AnnotationTypeExpression) value;
if (range.isArray()) {
List<AnnotationFS> annotations = ate.getAnnotationList(context, this);
annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), annotations));
} else {
AnnotationFS a = ate.getAnnotation(context, this);
annotation.setFeatureValue(feature, a);
}
} else if (value instanceof IAnnotationListExpression && !range.isPrimitive()) {
IAnnotationListExpression ale = (IAnnotationListExpression) value;
List<? extends FeatureStructure> list = null;
boolean rangeSubsumesAnnotation = cas.getTypeSystem().subsumes(cas.getAnnotationType(),
range);
if (range.isArray()) {
boolean componentSubsumesAnnotation = cas.getTypeSystem().subsumes(cas.getAnnotationType(),
range.getComponentType());
if (componentSubsumesAnnotation) {
list = ale.getAnnotationList(context, this);
} else {
list = ale.getFeatureStructureList(context, this);
}
} else {
if (rangeSubsumesAnnotation) {
list = ale.getAnnotationList(context, this);
} else {
list = ale.getFeatureStructureList(context, this);
}
}
if (list != null) {
if (range.isArray()) {
annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), list));
} else {
if (list.isEmpty()) {
annotation.setFeatureValue(feature, null);
} else {
annotation.setFeatureValue(feature, list.get(0));
}
}
} else {
annotation.setFeatureValue(feature, null);
}
} else if (value instanceof IAnnotationExpression && !range.isPrimitive()) {
IAnnotationExpression ae = (IAnnotationExpression) value;
boolean rangeSubsumesAnnotation = cas.getTypeSystem().subsumes(cas.getAnnotationType(),
range);
FeatureStructure a = null;
if (rangeSubsumesAnnotation) {
a = ae.getAnnotation(context, this);
} else {
a = ae.getFeatureStructure(context, this);
}
if (range.isArray()) {
List<FeatureStructure> c = new ArrayList<>();
c.add(a);
annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), c));
} else {
annotation.setFeatureValue(feature, a);
}
} else if (value instanceof ITypeExpression && !range.isPrimitive()) {
ITypeExpression typeExpr = (ITypeExpression) value;
Type t = typeExpr.getType(context, this);
assignAnnotationByTypeInWindow(annotation, feature, context, t);
} else if (value instanceof GenericFeatureExpression && !range.isPrimitive()) {
FeatureExpression fe = ((GenericFeatureExpression) value).getFeatureExpression();
Type t = fe.getInitialType(context, this);
List<AnnotationFS> inWindow = getAnnotationsInWindow(context.getAnnotation(), t);
if (fe instanceof SimpleFeatureExpression) {
SimpleFeatureExpression sfe = (SimpleFeatureExpression) fe;
List<? extends FeatureStructure> featureAnnotations = null;
if (fe.getFeatures(context, this) != null) {
featureAnnotations = new ArrayList<>(
sfe.getFeatureStructures(inWindow, false, context, this));
} else {
featureAnnotations = inWindow;
}
if (range.isArray()) {
annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), featureAnnotations));
} else if (!featureAnnotations.isEmpty()) {
FeatureStructure a = featureAnnotations.get(0);
annotation.setFeatureValue(feature, a);
}
} else {
if (range.isArray()) {
annotation.setFeatureValue(feature, UIMAUtils.toFSArray(getJCas(), inWindow));
} else {
AnnotationFS a = inWindow.get(0);
annotation.setFeatureValue(feature, a);
}
}
}
}