Skip to content

Commit aa2465c

Browse files
authored
Merge pull request #12 from MiriamGaus/main
change mul evaluate operation
2 parents b89e8b4 + 5211136 commit aa2465c

10 files changed

Lines changed: 188 additions & 13 deletions

src/main/java/de/vill/model/expression/AggregateFunctionExpression.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static de.vill.util.Util.addNecessaryQuotes;
44

5-
import de.vill.model.Attribute;
65
import de.vill.model.Feature;
76
import de.vill.model.GlobalAttribute;
87
import de.vill.model.building.VariableReference;
@@ -12,7 +11,7 @@
1211
import java.util.Objects;
1312
import java.util.Set;
1413

15-
public class AggregateFunctionExpression extends Expression {
14+
public abstract class AggregateFunctionExpression extends Expression {
1615

1716
protected GlobalAttribute attribute;
1817
protected Feature rootFeature;
@@ -58,10 +57,7 @@ public GlobalAttribute getAttribute() {
5857

5958

6059
@Override
61-
public double evaluate(Set<Feature> selectedFeatures) {
62-
// TODO not necessary for now
63-
return 0;
64-
}
60+
abstract public double evaluate(Set<Feature> selectedFeatures);
6561

6662
protected String toString(boolean withSubmodels, String functionName, String currentAlias) {
6763
final StringBuilder result = new StringBuilder();

src/main/java/de/vill/model/expression/AvgAggregateFunctionExpression.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
import de.vill.model.Attribute;
44
import de.vill.model.Feature;
55
import de.vill.model.GlobalAttribute;
6-
import de.vill.model.building.VariableReference;
76
import de.vill.util.Constants;
87
import java.util.Collections;
98
import java.util.List;
9+
import java.util.Set;
1010

1111
public class AvgAggregateFunctionExpression extends AggregateFunctionExpression {
12+
/**
13+
* Evaluates the average of the values of the selected features.
14+
*
15+
* @param selectedFeatures The set of selected features.
16+
* @return The average of the values of the selected features.
17+
*/
18+
@Override
19+
public double evaluate(Set<Feature> selectedFeatures) {
20+
double sum = 0;
21+
double count = 0;
22+
for (Feature feature : selectedFeatures) {
23+
Attribute<?> attribute = feature.getAttributes().get(getAttribute().getIdentifier());
24+
if (attribute != null && attribute.getValue() instanceof Number) {
25+
sum += ((Number) attribute.getValue()).doubleValue();
26+
count++;
27+
}
28+
}
29+
return count == 0 ? 0 : sum / count;
30+
}
31+
1232
public AvgAggregateFunctionExpression(GlobalAttribute reference) {
1333
super(reference);
1434
}

src/main/java/de/vill/model/expression/LengthAggregateFunctionExpression.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package de.vill.model.expression;
22

3-
import com.google.errorprone.annotations.Var;
4-
import de.vill.model.Attribute;
53
import de.vill.model.Feature;
64
import de.vill.model.building.VariableReference;
75
import de.vill.util.Constants;

src/main/java/de/vill/model/expression/MaxAggregateFunctionExpression.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,31 @@
33
import de.vill.model.Attribute;
44
import de.vill.model.Feature;
55
import de.vill.model.GlobalAttribute;
6-
import de.vill.model.building.VariableReference;
6+
77

88
import java.util.Arrays;
99
import java.util.List;
10+
import java.util.Set;
1011

1112
public class MaxAggregateFunctionExpression extends AggregateFunctionExpression {
13+
/**
14+
* Evaluates the maximum of the values of the selected features.
15+
*
16+
* @param selectedFeatures The set of selected features.
17+
* @return The maximum of the values of the selected features.
18+
*/
19+
@Override
20+
public double evaluate(Set<Feature> selectedFeatures) {
21+
double max = Double.NEGATIVE_INFINITY;
22+
for (Feature feature : selectedFeatures) {
23+
Attribute<?> attribute = feature.getAttributes().get(getAttribute().getIdentifier());
24+
if (attribute != null && attribute.getValue() instanceof Number) {
25+
max = Math.max(max, ((Number) attribute.getValue()).doubleValue());
26+
}
27+
}
28+
return max == Double.NEGATIVE_INFINITY ? 0 : max;
29+
}
30+
1231
public MaxAggregateFunctionExpression(GlobalAttribute attribute) {
1332
super(attribute);
1433
}

src/main/java/de/vill/model/expression/MinAggregateFunctionExpression.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,32 @@
33
import de.vill.model.Attribute;
44
import de.vill.model.Feature;
55
import de.vill.model.GlobalAttribute;
6-
import de.vill.model.building.VariableReference;
7-
import org.w3c.dom.Attr;
6+
87

98
import java.util.Arrays;
109
import java.util.List;
10+
import java.util.Set;
1111

1212
public class MinAggregateFunctionExpression extends AggregateFunctionExpression {
1313

14+
/**
15+
* Evaluates the minimum of the values of the selected features.
16+
*
17+
* @param selectedFeatures The set of selected features.
18+
* @return The minimum of the values of the selected features.
19+
*/
20+
@Override
21+
public double evaluate(Set<Feature> selectedFeatures) {
22+
double min = Double.POSITIVE_INFINITY;
23+
for (Feature feature : selectedFeatures) {
24+
Attribute<?> attribute = feature.getAttributes().get(getAttribute().getIdentifier());
25+
if (attribute != null && attribute.getValue() instanceof Number) {
26+
min = Math.min(min, ((Number) attribute.getValue()).doubleValue());
27+
}
28+
}
29+
return min == Double.POSITIVE_INFINITY ? 0 : min;
30+
}
31+
1432
public MinAggregateFunctionExpression(GlobalAttribute attribute) {
1533
super(attribute);
1634
}

src/main/java/de/vill/model/expression/MulExpression.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public double evaluate(Set<Feature> selectedFeatures) {
6262
} else {
6363
rightResult = right.evaluate(selectedFeatures);
6464
}
65-
return leftResult + rightResult;
65+
return leftResult * rightResult;
6666
}
6767

6868
@Override

src/main/java/de/vill/model/expression/SumAggregateFunctionExpression.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@
66
import de.vill.util.Constants;
77
import java.util.Collections;
88
import java.util.List;
9+
import java.util.Set;
910

1011
public class SumAggregateFunctionExpression extends AggregateFunctionExpression {
12+
13+
/**
14+
* Evaluates the sum of the values of the selected features.
15+
*
16+
* @param selectedFeatures The set of selected features.
17+
* @return The sum of the values of the selected features.
18+
*/
19+
@Override
20+
public double evaluate(Set<Feature> selectedFeatures) {
21+
double sum = 0;
22+
for (Feature feature : selectedFeatures) {
23+
Attribute<?> attribute = feature.getAttributes().get(getAttribute().getIdentifier());
24+
if (attribute != null && attribute.getValue() instanceof Number) {
25+
sum += ((Number) attribute.getValue()).doubleValue();
26+
}
27+
}
28+
return sum;
29+
}
1130
public SumAggregateFunctionExpression(GlobalAttribute attribute) {
1231
super(attribute);
1332
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
features
2+
A {Price 2}
3+
optional
4+
B {Price 2}
5+
C {Price 5}
6+
7+
constraints
8+
sum(Price) == 7
9+
avg(Price) == 3.5
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
features
2+
A
3+
or
4+
B {Price 2, Fun 12}
5+
C {Price 7, Fun 6}
6+
constraints
7+
B.Fun + C.Fun - B.Price == 16
8+
B.Fun + (C.Fun - B.Price) == 16
9+
B.Fun + C.Fun * B.Price == 24
10+
B.Fun + (C.Fun * B.Price) == 24
11+
B.Fun + C.Fun / B.Price == 15
12+
B.Fun + (C.Fun / B.Price) == 15
13+
B.Fun - C.Fun + B.Price == 8
14+
B.Fun - (C.Fun + B.Price) == 4
15+
B.Fun - C.Fun * B.Price == 0
16+
B.Fun - (C.Fun * B.Price) == 0
17+
B.Fun - C.Fun / B.Price == 9
18+
B.Fun - (C.Fun / B.Price) == 9
19+
B.Fun * C.Fun / B.Price == 36
20+
B.Fun * (C.Fun / B.Price) == 36
21+
B.Fun * C.Fun - B.Price == 70
22+
B.Fun * (C.Fun - B.Price) == 48
23+
B.Fun * C.Fun + B.Price == 74
24+
B.Fun * (C.Fun + B.Price) == 96
25+
B.Fun / C.Fun * B.Price == 4
26+
B.Fun / (C.Fun * B.Price) == 1
27+
B.Fun / C.Fun + B.Price == 4
28+
B.Fun / (C.Fun + B.Price) == 1.5
29+
B.Fun / C.Fun - B.Price == 0
30+
B.Fun / (C.Fun - B.Price) == 3

0 commit comments

Comments
 (0)