Skip to content

Commit

Permalink
Issue #130: Improve support for feature assignments
Browse files Browse the repository at this point in the history
- support more generic string assignments
- improved boolean assignment
  • Loading branch information
pkluegl committed Oct 11, 2023
1 parent 0133fec commit 46eb095
Show file tree
Hide file tree
Showing 20 changed files with 523 additions and 280 deletions.
18 changes: 15 additions & 3 deletions ruta-core/src/main/antlr3/org/apache/uima/ruta/parser/RutaParser.g
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ options {
| match = dottedIdWithIndex2 (comp = LESS | comp = GREATER | comp = GREATEREQUAL | comp = LESSEQUAL |comp = EQUAL | comp = NOTEQUAL) arg = argument
{MatchReference mr = expressionFactory.createMatchReference(match, comp, arg);
expr = expressionFactory.createAnnotationTypeExpression(mr);}

| (complexStringExpression) => cse = complexStringExpression {expr = cse;}
| (featureExpression)=> fe = featureExpression {expr = expressionFactory.createGenericFeatureExpression(fe);}
| a2 = booleanExpression {expr = a2;}
| a3 = numberExpression {expr = a3;}
Expand All @@ -2455,8 +2455,8 @@ options {
}
:
(featureExpression)=> fe = featureExpression {expr = expressionFactory.createGenericFeatureExpression(fe);}
| a2 = booleanExpression {expr = a2;}
| a3 = numberExpression {expr = a3;}
| a2 = simpleBooleanExpression {expr = a2;}
| a3 = simpleNumberExpression {expr = a3;}
| a4 = stringExpression {expr = a4;}
| (listExpression)=> l = listExpression {expr = l;}
| a5 = nullExpression {expr = a5;}
Expand Down Expand Up @@ -2778,6 +2778,18 @@ List<IStringExpression> exprs = new ArrayList<IStringExpression>();
|(e = stringFunction)=> e = stringFunction{expr = e;}
;

complexStringExpression returns [IStringExpression expr = null]
options {
backtrack = true;
}
@init {List<IRutaExpression> list = new ArrayList<IRutaExpression>();}
:
a1 = simpleArgument {list.add(a1);}
((PLUS)=>PLUS an = simpleArgument {list.add(an);})+
{expr = expressionFactory.createGenericComposedStringExpression(list);}
;


// not checked
stringFunction returns [IStringExpression expr = null]
@init {List<IStringExpression> list = new ArrayList<IStringExpression>();}
Expand Down
22 changes: 11 additions & 11 deletions ruta-core/src/main/java/org/apache/uima/ruta/RutaScriptFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -106,7 +106,7 @@ public RutaScriptBlock createScriptBlock(String text, RutaRuleElement ruleElemen
if (ruleElement != null) {
rule = createRule(ruleElement, parent);
}
List<RutaStatement> elements = new ArrayList<RutaStatement>();
List<RutaStatement> elements = new ArrayList<>();
if (body != null) {
for (RutaStatement each : body) {
if (each != null) {
Expand All @@ -121,7 +121,7 @@ public RutaScriptBlock createRootScriptBlock(String module, String pack) {
String defaultNamespace = getDefaultNamespace(module, pack);

RutaScriptBlock result = createScriptBlock(module, null, null, null, defaultNamespace);
List<RuleElement> ruleElements = new ArrayList<RuleElement>();
List<RuleElement> ruleElements = new ArrayList<>();
RuleElementIsolator container = new RuleElementIsolator();
ITypeExpression documentExpression = expressionFactory
.createSimpleTypeExpression(CAS.TYPE_NAME_DOCUMENT_ANNOTATION, null);
Expand Down Expand Up @@ -167,13 +167,13 @@ public RutaBlock createForEachBlock(Token varToken, IBooleanExpression direction
}

public RutaRule createRule(RuleElement element, RutaBlock parent) {
List<RuleElement> elements = new ArrayList<RuleElement>();
List<RuleElement> elements = new ArrayList<>();
elements.add(element);
return createRule(elements, parent);
}

public RutaStatement createImplicitRule(List<AbstractRutaAction> actions, RutaBlock parent) {
List<RuleElement> elements = new ArrayList<RuleElement>();
List<RuleElement> elements = new ArrayList<>();
ITypeExpression documentExpression = expressionFactory.createSimpleTypeExpression("Document",
parent);
MatchReference mr = expressionFactory.createMatchReference(documentExpression);
Expand Down Expand Up @@ -293,15 +293,15 @@ public List<RuleElement> processConjunctRules(List<RuleElement> reList, List<Tok
if (!isConjunct) {
return reList;
}
Map<Integer, List<RuleElement>> map = new TreeMap<Integer, List<RuleElement>>();
List<String> connectors = new ArrayList<String>();
Map<Integer, List<RuleElement>> map = new TreeMap<>();
List<String> connectors = new ArrayList<>();
int reCounter = 0;
int conCounter = 0;
for (Token token : conList) {
if (token == null) {
List<RuleElement> list = map.get(conCounter);
if (list == null) {
list = new ArrayList<RuleElement>();
list = new ArrayList<>();
map.put(conCounter, list);
}
RuleElement e = reList.get(reCounter);
Expand All @@ -312,7 +312,7 @@ public List<RuleElement> processConjunctRules(List<RuleElement> reList, List<Tok
conCounter++;
}
}
List<RuleElement> elements = new ArrayList<RuleElement>();
List<RuleElement> elements = new ArrayList<>();

ConjunctRulesRuleElement cr = new ConjunctRulesRuleElement(null, container, env);
for (List<RuleElement> each : map.values()) {
Expand All @@ -324,7 +324,7 @@ public List<RuleElement> processConjunctRules(List<RuleElement> reList, List<Tok
}
cr.setElements(elements);
cr.setContainer(null);
List<RuleElement> result = new ArrayList<RuleElement>();
List<RuleElement> result = new ArrayList<>();
result.add(cr);
return result;
}
Expand Down
21 changes: 12 additions & 9 deletions ruta-core/src/main/java/org/apache/uima/ruta/RutaStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,10 @@ private void updateIterators(CAS cas, Type basicType, FilterManager filter,
AnnotationFS additionalWindow) {
if (additionalWindow != null) {
// TODO UIMA-6281 replace select
this.basicIt = cas.getAnnotationIndex(basicType).select().coveredBy(additionalWindow)
.fsIterator();
basicIt = cas.getAnnotationIndex(basicType).select().coveredBy(additionalWindow).fsIterator();
// was: this.basicIt = cas.getAnnotationIndex(basicType).subiterator(additionalWindow);
} else {
this.basicIt = cas.getAnnotationIndex(basicType).iterator();
basicIt = cas.getAnnotationIndex(basicType).iterator();
}
currentIt = filter.createFilteredIterator(cas, basicType);
}
Expand Down Expand Up @@ -346,8 +345,9 @@ private void createBasicsForAnchors(List<Integer> anchors) {
createRutaBasic(0, 0);
} else if (anchors.size() == 1) {
Integer first = anchors.get(0);
if (first >= 0 && first <= cas.getDocumentText().length())
if (first >= 0 && first <= cas.getDocumentText().length()) {
createRutaBasic(first, first);
}
} else {
for (int i = 0; i < anchors.size() - 1; i++) {
Integer first = anchors.get(i);
Expand Down Expand Up @@ -1231,7 +1231,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
} else if (value instanceof IStringExpression) {
IStringExpression stringExpr = (IStringExpression) value;
String string = stringExpr.getStringValue(context, this);
StringArrayFS array = FSCollectionFactory.createStringArrayFS(cas, new String[] { string });
StringArrayFS array = FSCollectionFactory.createStringArrayFS(cas, string);
annotation.setFeatureValue(feature, array);
}
} else if (rangeName.equals(CAS.TYPE_NAME_INTEGER) || rangeName.equals(CAS.TYPE_NAME_LONG)
Expand All @@ -1253,7 +1253,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
int v = numberExpr.getIntegerValue(context, this);
IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas, new int[] { v });
IntArrayFS array = FSCollectionFactory.createIntArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
Expand All @@ -1272,7 +1272,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
double v = numberExpr.getDoubleValue(context, this);
DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas, new double[] { v });
DoubleArrayFS array = FSCollectionFactory.createDoubleArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
Expand All @@ -1291,7 +1291,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
if (value instanceof INumberExpression) {
INumberExpression numberExpr = (INumberExpression) value;
float v = numberExpr.getFloatValue(context, this);
FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas, new float[] { v });
FloatArrayFS array = FSCollectionFactory.createFloatArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
} else if (value instanceof INumberListExpression) {
INumberListExpression expr = (INumberListExpression) value;
Expand All @@ -1305,6 +1305,9 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
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) {
Expand All @@ -1315,7 +1318,7 @@ public void assignFeatureValue(FeatureStructure annotation, Feature feature,
} else if (value instanceof IBooleanExpression) {
IBooleanExpression expr = (IBooleanExpression) value;
Boolean v = expr.getBooleanValue(context, this);
BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, new boolean[] { v });
BooleanArrayFS array = FSCollectionFactory.createBooleanArrayFS(cas, v);
annotation.setFeatureValue(feature, array);
}
} else if (value instanceof AnnotationTypeExpression && !range.isPrimitive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down Expand Up @@ -66,7 +66,7 @@ public String toString() {

boolean getDictWSParamValue(MatchContext context) {
Object configParameterValue = context.getParent().getContext()
.getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS);
.getConfigParameterValue(RutaEngine.PARAM_DICT_REMOVE_WS);
if (configParameterValue instanceof Boolean) {
return (Boolean) configParameterValue;
}
Expand All @@ -76,7 +76,7 @@ boolean getDictWSParamValue(MatchContext context) {
protected List<Integer> getIndexList(MatchContext context, List<INumberExpression> list,
RutaStream stream) {
RuleElement element = context.getElement();
List<Integer> indexList = new ArrayList<Integer>();
List<Integer> indexList = new ArrayList<>();
if (list == null || list.isEmpty()) {
int self = element.getContainer().getRuleElements().indexOf(element) + 1;
indexList.add(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* 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
Expand Down
Loading

0 comments on commit 46eb095

Please sign in to comment.