Commit 3e67cdc6 authored by Daniel Ratiu's avatar Daniel Ratiu
Browse files

resolving this issue: now the serialization of expressions as strings is...

resolving this issue: now the serialization of expressions as strings is consistent with the AF3 compiler
refs 1443
parent ce103398
......@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: AF3 Expression
Bundle-SymbolicName: org.fortiss.af3.expression;singleton:=true
Bundle-Version: 2.4.0.qualifier
Bundle-Version: 2.3.0.qualifier
Bundle-ClassPath: .,
lib/antlr-3.5-complete.jar
Bundle-Vendor: fortiss GmbH
......@@ -39,7 +39,5 @@ Export-Package: org.fortiss.af3.expression,
org.fortiss.af3.expression.utils,
test.org.fortiss.af3.expression,
test.org.fortiss.af3.expression.base,
test.org.fortiss.af3.expression.constraint,
test.org.fortiss.af3.expression.evaluator,
test.org.fortiss.af3.expression.evaluator.parameterized,
test.org.fortiss.af3.expression.parser
test.org.fortiss.af3.expression.evaluator
......@@ -57,31 +57,31 @@ public final class OperatorStrengthUtils {
private static int getOperatorStrength(EOperator op) {
switch(op) {
case OR:
return 60;
return 10;
case AND:
return 55;
return 20;
case EQUAL:
case NOT_EQUAL:
return 50;
return 30;
case LOWER_THAN:
case LOWER_EQUAL:
case GREATER_THAN:
case GREATER_EQUAL:
return 45;
return 40;
case ADD:
case SUBTRACT:
return 40;
return 50;
case MULTIPLY:
return 35;
return 60;
case DIVIDE:
case MODULO:
return 30;
return 60;
case NEGATE:
case NOT:
return 25;
return 70;
case MEMBER:
case INDEX:
return 20;
return 80;
default:
return DEFAULT_OPERATOR_STRENGTH;
}
......@@ -108,7 +108,7 @@ public final class OperatorStrengthUtils {
// Due to Issue #950 (i.e. correct rendering of the parsed expression "a - (b + c + d)")
// we must have in the if below a greater-equal
if(outer instanceof FunctionCall) {
if(((FunctionCall)outer).getArguments(0) != inner && innerStr >= outerStr) {
if(innerStr <= outerStr) {
return '(' + string + ')';
}
}
......
......@@ -22,13 +22,12 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import test.org.fortiss.af3.expression.compiler.CompilerTest;
import test.org.fortiss.af3.expression.compiler.TypeSystemParserTest;
import test.org.fortiss.af3.expression.constraint.ConstraintCheckerTest;
import test.org.fortiss.af3.expression.evaluator.EvaluatorTest;
import test.org.fortiss.af3.expression.evaluator.parameterized.EvaluatorTest3_Arith_NoVal_Vars;
import test.org.fortiss.af3.expression.evaluator.parameterized.EvaluatorTest5_Strict;
import test.org.fortiss.af3.expression.evaluator.struct.StructEvaluatorTest;
import test.org.fortiss.af3.expression.parser.TermCompilerTest;
import test.org.fortiss.af3.expression.parser.TypeSystemParserTest;
/**
* This suite is an index to all expressions tests defined in this plugin.
......@@ -45,7 +44,6 @@ import test.org.fortiss.af3.expression.parser.TypeSystemParserTest;
EvaluatorTest.class,
EvaluatorTest3_Arith_NoVal_Vars.class,
EvaluatorTest5_Strict.class,
TermCompilerTest.class,
TypeSystemParserTest.class,
StructEvaluatorTest.class,
CompilerTest.class
......
......@@ -19,6 +19,8 @@ package test.org.fortiss.af3.expression.base;
import static org.fortiss.af3.expression.utils.DataDictionaryUtils.findDataDictionary;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createMicroTypesystem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.fortiss.af3.expression.language.Compiler;
import org.fortiss.af3.expression.language.evaluation.EnumerationValue;
......@@ -26,6 +28,7 @@ import org.fortiss.af3.expression.model.DataDictionary;
import org.fortiss.af3.expression.model.definitions.Enumeration;
import org.fortiss.af3.expression.model.definitions.EnumerationMember;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.model.typesystem.ITypeSystem;
import test.org.fortiss.af3.project.ProjectTestModelBase;
......@@ -58,4 +61,18 @@ public abstract class ExpressionTestModelBase extends ProjectTestModelBase {
FunctionCall functionCall = (FunctionCall)compiler.compileTerm(enumMember.getName() + "()");
return new EnumerationValue(functionCall, enumMember);
}
/** Compile expression, make initial checks, and return the compiled term. */
protected ITerm parseAndInitialAsserts(String exprString) {
ITerm expr = compiler.compileTerm(exprString);
assertTrue(expr instanceof FunctionCall);
assertTrue(((FunctionCall)expr).getArgumentsLength() == 2);
return expr;
}
/** Compile expression and check the serialization to string. */
protected void checkParserAndToString(String initial, String expected) {
ITerm expr = compiler.compileTerm(initial);
assertEquals(expected, expr.toString());
}
}
......@@ -17,14 +17,24 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+--------------------------------------------------------------------------*/
package test.org.fortiss.af3.expression.compiler;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.var;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.fortiss.af3.expression.language.Compiler;
import org.fortiss.af3.expression.language.ParseException;
import org.fortiss.af3.expression.model.terms.IExpressionTerm;
import org.fortiss.af3.expression.language.evaluation.NoVal;
import org.fortiss.af3.expression.model.terms.DefinedConst;
import org.fortiss.af3.expression.model.terms.EOperator;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.expression.model.terms.PredefinedFunction;
import org.fortiss.af3.expression.model.terms.UserdefinedFunction;
import org.fortiss.af3.expression.utils.OperatorStrengthUtils;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.junit.Test;
import test.org.fortiss.af3.expression.base.ExpressionTestModelBase;
/**
* Tests for the AF3 compiler.
*
......@@ -33,18 +43,29 @@ import org.junit.Test;
* @version $Rev: 18709 $
* @ConQAT.Rating GREEN Hash: DD07420EDF0574BA02C65AA18A23F4A3
*/
public class CompilerTest {
/** The compiler. */
private Compiler compiler = new Compiler();
public class CompilerTest extends ExpressionTestModelBase {
/** Tests the compilation of different expressions. */
/**
* Tests the compilation of different expressions and the
* {@link OperatorStrengthUtils#insertBracketIfNeeded(ITerm, ITerm, String)}.
*/
@Test
public void testCompileExpressionTerms() {
IExpressionTerm res;
checkParserAndToString("aVar == aFunCall() && anotherVar == anotherFunCall()",
"aVar == aFunCall( ) && anotherVar == anotherFunCall( )");
res = compiler.compileTerm("aVar == aFunCall() && anotherVar == anotherFunCall()");
assertEquals("aVar == aFunCall( ) && anotherVar == anotherFunCall( )", res.toString());
checkParserAndToString("b1 && i1 == 5", "b1 && i1 == 5");
checkParserAndToString("b1 && (i1 == 5)", "b1 && i1 == 5");
checkParserAndToString("(b1 && b2) == true", "(b1 && b2) == true");
checkParserAndToString("i1 + i2 == i3", "i1 + i2 == i3");
checkParserAndToString("(i1 + i2) == i3", "i1 + i2 == i3");
checkParserAndToString("i1 == i2 && b1", "i1 == i2 && b1");
checkParserAndToString("b1 && (b2 == true)", "b1 && b2 == true");
checkParserAndToString("(b1 && b2) == true", "(b1 && b2) == true");
checkParserAndToString("i1 + i2 < i3 != false", "i1 + i2 < i3 != false");
checkParserAndToString("i1 + i2 * i3 != 4", "i1 + i2 * i3 != 4");
checkParserAndToString("i1 + (i2 * i3) != 4", "i1 + i2 * i3 != 4");
checkParserAndToString("(i1 + i2) * i3 != 4", "(i1 + i2) * i3 != 4");
}
/** Tests with invalid expressions. */
......@@ -55,6 +76,36 @@ public class CompilerTest {
assertExpressionInvalid("aVar == aFunCall() & && anotherVar == anotherFunCall()");
}
/** Test parser. */
@Test
public void testParseTreeBuilding() {
ITerm expr;
expr = parseAndInitialAsserts("Accelerate + Speed");
FunctionCall fc = (FunctionCall)expr;
assertEquals(var("Accelerate"), fc.getArguments(0));
assertEquals(var("Speed"), fc.getArguments(1));
assertEquals(EOperator.ADD, ((PredefinedFunction)fc.getFunction()).getOperator());
expr = parseAndInitialAsserts("f(Accelerate, Speed) == Accelerate + Speed");
fc = (FunctionCall)expr;
assertEquals(var("Accelerate"), ((FunctionCall)fc.getArguments(0)).getArguments(0));
assertEquals(var("Speed"), ((FunctionCall)fc.getArguments(0)).getArguments(1));
assertTrue(((FunctionCall)fc.getArguments(0)).getFunction() instanceof UserdefinedFunction);
assertEquals("f",
((UserdefinedFunction)((FunctionCall)fc.getArguments(0)).getFunction()).getName());
expr = parseAndInitialAsserts("NoVal > 0");
fc = (FunctionCall)expr;
assertTrue(fc.getArguments(0) instanceof DefinedConst);
assertEquals(NoVal.NOVAL.toITerm(), fc.getArguments(0));
assertEquals(intConst(0), fc.getArguments(1));
assertEquals(EOperator.GREATER_THAN, ((PredefinedFunction)fc.getFunction()).getOperator());
}
/** Checks whether an expression is indeed invalid. */
private void assertExpressionInvalid(String cond) {
try {
......
......@@ -15,7 +15,7 @@ $Id$
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package test.org.fortiss.af3.expression.parser;
package test.org.fortiss.af3.expression.compiler;
import static org.junit.Assert.assertTrue;
......
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 ForTISS GmbH |
| |
| Licensed 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 test.org.fortiss.af3.expression.parser;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intConst;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.var;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.fortiss.af3.expression.language.Compiler;
import org.fortiss.af3.expression.language.evaluation.NoVal;
import org.fortiss.af3.expression.model.terms.DefinedConst;
import org.fortiss.af3.expression.model.terms.EOperator;
import org.fortiss.af3.expression.model.terms.FunctionCall;
import org.fortiss.af3.expression.model.terms.PredefinedFunction;
import org.fortiss.af3.expression.model.terms.UserdefinedFunction;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.junit.Test;
import test.org.fortiss.af3.expression.base.ExpressionTestModelBase;
/**
* Tests for the {@link Compiler}.
*
* @author ratiu, trachten
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: D681BC9E75A072F8DDA1B68F88368399
*/
public class TermCompilerTest extends ExpressionTestModelBase {
/**
* Compile expression, make initial checks, and return the compiled term.
*
* @see #testParser()
*/
protected ITerm parseAndInitialAsserts(String exprString) {
ITerm expr = compiler.compileTerm(exprString);
assertTrue(expr instanceof FunctionCall);
assertTrue(((FunctionCall)expr).getArgumentsLength() == 2);
return expr;
}
/** Test parser. */
@Test
public void testParser() {
ITerm expr;
expr = parseAndInitialAsserts("Accelerate + Speed");
assertEquals(var("Accelerate"), ((FunctionCall)expr).getArguments(0));
assertEquals(var("Speed"), ((FunctionCall)expr).getArguments(1));
assertEquals(EOperator.ADD,
((PredefinedFunction)((FunctionCall)expr).getFunction()).getOperator());
expr = parseAndInitialAsserts("f(Accelerate, Speed) == Accelerate + Speed");
assertEquals(var("Accelerate"),
((FunctionCall)((FunctionCall)expr).getArguments(0)).getArguments(0));
assertEquals(var("Speed"),
((FunctionCall)((FunctionCall)expr).getArguments(0)).getArguments(1));
assertTrue(((FunctionCall)((FunctionCall)expr).getArguments(0)).getFunction() instanceof UserdefinedFunction);
assertEquals("f",
((UserdefinedFunction)((FunctionCall)((FunctionCall)expr).getArguments(0))
.getFunction()).getName());
expr = parseAndInitialAsserts("NoVal > 0");
assertTrue(((FunctionCall)expr).getArguments(0) instanceof DefinedConst);
assertEquals(NoVal.NOVAL.toITerm(), ((FunctionCall)expr).getArguments(0));
assertEquals(intConst(0), ((FunctionCall)expr).getArguments(1));
assertEquals(EOperator.GREATER_THAN,
((PredefinedFunction)((FunctionCall)expr).getFunction()).getOperator());
}
}
<!--
$Id: package.html 894 2011-07-07 07:36:03Z ratiu $
@version $Rev: 894 $
@ConQAT.Rating GREEN Hash: 18DCD09A14AF76E3EA8EB1826171579C
-->
<body>
This package contains tests for the compiler.
</body>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment