Skip to content

Commit

Permalink
Merge pull request #160 from apache/bugfix/159-Assignment-of-composed…
Browse files Browse the repository at this point in the history
…-number-expression-is-broken

Issue #159: Assignment of composed number expression is broken
  • Loading branch information
pkluegl authored Jan 31, 2024
2 parents cd8ee4b + 2343d2e commit 302688c
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 59 deletions.
14 changes: 11 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;}
| (genericComposedExpression) => gce = genericComposedExpression {expr = gce;}
| (featureExpression)=> fe = featureExpression {expr = expressionFactory.createGenericFeatureExpression(fe);}
| a2 = booleanExpression {expr = a2;}
| a3 = numberExpression {expr = a3;}
Expand Down Expand Up @@ -2720,6 +2720,14 @@ numberVariable returns [Token ref = null]
;


complexNumberExpression returns [INumberExpression expr = null]
@init{List<INumberExpression> exprs = new ArrayList<INumberExpression>();
List<Token> ops = new ArrayList<Token>();}
:
e = multiplicativeExpression{exprs.add(e);} ((PLUS | MINUS)=> op = (PLUS | MINUS){ops.add(op);} e = multiplicativeExpression{exprs.add(e);} )+
{expr = expressionFactory.createComposedNumberExpression(exprs,ops);}
;

additiveExpression returns [INumberExpression expr = null]
@init{List<INumberExpression> exprs = new ArrayList<INumberExpression>();
List<Token> ops = new ArrayList<Token>();}
Expand Down Expand Up @@ -2778,15 +2786,15 @@ List<IStringExpression> exprs = new ArrayList<IStringExpression>();
|(e = stringFunction)=> e = stringFunction{expr = e;}
;

complexStringExpression returns [IStringExpression expr = null]
genericComposedExpression returns [IRutaExpression 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);}
{expr = expressionFactory.createGenericComposedExpression(list);}
;


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
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ public IStringExpression createGenericComposedStringExpression(
for (IRutaExpression each : expressions) {
if (each instanceof IStringExpression) {
stringExpression.add((IStringExpression) each);
} else {
System.out.println();
}
}
return new ComposedStringExpression(stringExpression);
}

public IRutaExpression createGenericComposedExpression(List<IRutaExpression> list) {
return new GenericComposedExpression(list);
}

public AbstractStringExpression createReferenceStringExpression(Token var) {
return new StringVariableExpression(var.getText());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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;

import java.util.List;

import org.apache.uima.ruta.RutaStream;
import org.apache.uima.ruta.expression.number.INumberExpression;
import org.apache.uima.ruta.expression.string.IStringExpression;
import org.apache.uima.ruta.rule.MatchContext;

public class GenericComposedExpression extends RutaExpression implements INumberExpression {

private final List<IRutaExpression> expressions;

public GenericComposedExpression(List<IRutaExpression> expressions) {
super();
this.expressions = expressions;
}

@Override
public String getStringValue(MatchContext context, RutaStream stream) {
if (expressions == null) {
return null;
}
if (expressions.size() == 1) {
IRutaExpression first = expressions.get(0);
if (first instanceof IStringExpression) {
return ((IStringExpression) first).getStringValue(context, stream);
}
return null;
}
StringBuilder result = new StringBuilder();
for (IRutaExpression each : expressions) {
if (each instanceof IStringExpression) {
result.append(((IStringExpression) each).getStringValue(context, stream));
}
}
return result.toString();
}

@Override
public int getIntegerValue(MatchContext context, RutaStream stream) {
return (int) getDoubleValue(context, stream);
}

@Override
public double getDoubleValue(MatchContext context, RutaStream stream) {
double result = 0;
for (IRutaExpression each : expressions) {
if (each instanceof INumberExpression) {
result += ((INumberExpression) each).getDoubleValue(context, stream);
}
}
return result;
}

@Override
public float getFloatValue(MatchContext context, RutaStream stream) {
return (float) getDoubleValue(context, stream);
}

public List<IRutaExpression> getExpressions() {
return expressions;
}
}
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 All @@ -33,7 +33,7 @@ public class UIMAUtils {

public static FSArray<? extends FeatureStructure> toFSArray(JCas jCas,
List<? extends FeatureStructure> fsList) {
FSArray<FeatureStructure> fsArray = new FSArray<FeatureStructure>(jCas, fsList.size());
FSArray<FeatureStructure> fsArray = new FSArray<>(jCas, fsList.size());
fsArray.copyFromArray(fsList.toArray(new FeatureStructure[fsList.size()]), 0, 0, fsList.size());
return fsArray;
}
Expand All @@ -58,7 +58,7 @@ public static IntegerArray toIntegerArray(JCas jCas, int[] sArray) {

public static <T extends FeatureStructure> List<T> toList(FSArray<FeatureStructure> fsArray,
Class<T> cls) {
List<T> list = new ArrayList<T>();
List<T> list = new ArrayList<>();
if (fsArray == null) {
return list;
}
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 All @@ -20,10 +20,13 @@
package org.apache.uima.ruta.verbalize;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.cas.CAS;
import org.apache.uima.ruta.expression.AnnotationTypeExpression;
import org.apache.uima.ruta.expression.GenericComposedExpression;
import org.apache.uima.ruta.expression.IRutaExpression;
import org.apache.uima.ruta.expression.MatchReference;
import org.apache.uima.ruta.expression.NullExpression;
Expand Down Expand Up @@ -83,6 +86,8 @@ public ExpressionVerbalizer(RutaVerbalizer verbalizer) {
public String verbalize(IRutaExpression expression) {
if (expression instanceof NullExpression) {
return "null";
} else if (expression instanceof GenericComposedExpression) {
return verbalize((GenericComposedExpression) expression);
} else if (expression instanceof GenericFeatureExpression) {
return verbalize(((GenericFeatureExpression) expression).getFeatureExpression());
} else if (expression instanceof AnnotationTypeExpression) {
Expand Down Expand Up @@ -336,4 +341,9 @@ public String verbalize(FeatureMatchExpression expression) {
+ verbalize(expression.getArg());
}

public String verbalize(GenericComposedExpression expression) {
GenericComposedExpression gce = expression;
List<IRutaExpression> expressions = gce.getExpressions();
return expressions.stream().map(e -> verbalize(e)).collect(Collectors.joining("+"));
}
}
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 All @@ -31,7 +31,7 @@ public class ComposedNumberExpressionTest {
public void testGetStringValueWithInteger() {
List<INumberExpression> list = new ArrayList<>();
list.add(new SimpleNumberExpression(Integer.valueOf(1)));
ComposedNumberExpression expr = new ComposedNumberExpression(list, new ArrayList<String>());
ComposedNumberExpression expr = new ComposedNumberExpression(list, new ArrayList<>());
String string = expr.getStringValue(null, null);
assertThat(string).isEqualTo("1");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.number;

import org.apache.uima.cas.CAS;
import org.apache.uima.ruta.engine.Ruta;
import org.apache.uima.ruta.engine.RutaTestUtils;
import org.junit.jupiter.api.Test;

public class NumberExpressionTest {

@Test
public void testComposedAssignment() throws Exception {
String script = "INT i = 1;";
script += "Document{i==2 -> T1};";
script += "Document{-> i = i + 1};";
script += "Document{i==2 -> T2};";
script += "Document{-> i = 1 + 1 + 1};";
script += "Document{i==3 -> T3};";
script += "Document{i== 1+1+1 -> T4};";
script += "Document{i== 1+i-1 -> T5};";
script += "Document{-> i = (1 + 1 * 3) / 2};";
script += "Document{i== 8 / 4 -> T6};";
CAS cas = RutaTestUtils.getCAS("This is a test.");
Ruta.apply(cas, script);

RutaTestUtils.assertAnnotationsEquals(cas, 1, 0);
RutaTestUtils.assertAnnotationsEquals(cas, 2, 1, "This is a test.");
RutaTestUtils.assertAnnotationsEquals(cas, 3, 1, "This is a test.");
RutaTestUtils.assertAnnotationsEquals(cas, 4, 1, "This is a test.");
RutaTestUtils.assertAnnotationsEquals(cas, 5, 1, "This is a test.");
RutaTestUtils.assertAnnotationsEquals(cas, 6, 1, "This is a test.");
}

}
Loading

0 comments on commit 302688c

Please sign in to comment.