1+ package de .vill .model ;
2+
3+ import de .vill .main .UVLModelFactory ;
4+ import de .vill .model .constraint .Constraint ;
5+ import de .vill .model .constraint .ExpressionConstraint ;
6+ import de .vill .model .expression .Expression ;
7+ import org .junit .jupiter .api .Test ;
8+
9+ import java .nio .file .Paths ;
10+ import java .util .Map ;
11+ import java .util .Set ;
12+
13+ import static org .junit .jupiter .api .Assertions .*;
14+
15+ class ExpressionTest {
16+
17+ @ Test
18+ void testMathExpressionsModel () {
19+ // get the feature model and parse it using the UVLModelFactory
20+ UVLModelFactory uvlModelFactory = new UVLModelFactory ();
21+ FeatureModel featureModel = uvlModelFactory .parse (Paths .get ("src/test/resources/parsing/expressions.uvl" ));
22+ // select features for evaluation
23+ Map <String , Feature > featureMap = featureModel .getFeatureMap ();
24+ Set <Feature > selectedFeatures = Set .of (
25+ featureMap .get ("B" ),
26+ featureMap .get ("C" )
27+ );
28+
29+ // test the constraints
30+ for (Constraint constraint : featureModel .getOwnConstraints ()) {
31+ if (constraint instanceof ExpressionConstraint ) {
32+ ExpressionConstraint exprConstraint = (ExpressionConstraint ) constraint ;
33+ Expression left = exprConstraint .getLeft ();
34+ Expression right = exprConstraint .getRight ();
35+ double expected = right .evaluate (selectedFeatures );
36+ double actual = left .evaluate (selectedFeatures );
37+ assertEquals (expected , actual , 0.0001 , "Constraint '" + exprConstraint );
38+ }
39+ }
40+ }
41+
42+ @ Test
43+ void testAggregateFunctions () {
44+ // get the feature model and parse it using the UVLModelFactory
45+ UVLModelFactory uvlModelFactory = new UVLModelFactory ();
46+ FeatureModel featureModel = uvlModelFactory .parse (Paths .get ("src/test/resources/parsing/aggregateFunctions.uvl" ));
47+ // select features for evaluation
48+ Map <String , Feature > featureMap = featureModel .getFeatureMap ();
49+ Set <Feature > selectedFeatures = Set .of (
50+ featureMap .get ("B" ),
51+ featureMap .get ("C" )
52+ );
53+
54+ // test the constraints
55+ for (Constraint constraint : featureModel .getOwnConstraints ()) {
56+ if (constraint instanceof ExpressionConstraint ) {
57+ ExpressionConstraint exprConstraint = (ExpressionConstraint ) constraint ;
58+ Expression left = exprConstraint .getLeft ();
59+ Expression right = exprConstraint .getRight ();
60+ double expected = right .evaluate (selectedFeatures );
61+ double actual = left .evaluate (selectedFeatures );
62+ assertEquals (expected , actual , 0.0001 , "Constraint '" + exprConstraint );
63+ }
64+ }
65+ }
66+ }
0 commit comments