-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConvertSMTLevel.java
More file actions
179 lines (160 loc) · 8.06 KB
/
ConvertSMTLevel.java
File metadata and controls
179 lines (160 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package de.vill.conversion;
import com.google.common.collect.Sets;
import de.vill.model.*;
import de.vill.model.constraint.*;
import de.vill.model.expression.Expression;
import de.vill.model.expression.LiteralExpression;
import java.util.*;
public class ConvertSMTLevel implements IConversionStrategy {
@Override
public Set<LanguageLevel> getLevelsToBeRemoved() {
return new HashSet<>(Arrays.asList(LanguageLevel.ARITHMETIC_LEVEL));
}
@Override
public Set<LanguageLevel> getTargetLevelsOfConversion() {
return new HashSet<>(Arrays.asList(LanguageLevel.BOOLEAN_LEVEL));
}
private Constraint getFalseConstraint(FeatureModel featureModel){
return new NotConstraint(new LiteralConstraint(featureModel.getRootFeature()));
}
@Override
public void convertFeatureModel(FeatureModel rootFeatureModel, FeatureModel featureModel) {
List<Constraint> constraints = featureModel.getFeatureConstraints();
constraints.addAll(featureModel.getOwnConstraints());
for(Constraint constraint : constraints) {
replaceEquationInConstraint(constraint, featureModel);
}
List<Constraint> replacements = new LinkedList<>();
for (Constraint constraint : featureModel.getOwnConstraints()) {
if (constraint instanceof ExpressionConstraint) {
Constraint equationReplacement = convertEquationToConstraint((ExpressionConstraint) constraint, getFalseConstraint(featureModel));
replacements.add(equationReplacement);
}
}
featureModel.getOwnConstraints().removeIf(x -> x instanceof ExpressionConstraint);
featureModel.getOwnConstraints().addAll(replacements);
for (Constraint constraint : featureModel.getOwnConstraints()) {
convertConstraint(constraint, featureModel);
}
traverseFeatures(featureModel.getRootFeature(), featureModel);
}
private void convertConstraint(Constraint constraint, FeatureModel featureModel) {
for (Constraint subConstraint : constraint.getConstraintSubParts()) {
if (subConstraint instanceof ExpressionConstraint) {
Constraint equationReplacement = convertEquationToConstraint((ExpressionConstraint) subConstraint, getFalseConstraint(featureModel));
constraint.replaceConstraintSubPart(subConstraint, equationReplacement);
}else{
convertConstraint(subConstraint, featureModel);
}
}
}
private void replaceEquationInConstraint(Constraint constraint, FeatureModel featureModel) {
for (Constraint subConstraint : constraint.getConstraintSubParts()) {
if (subConstraint instanceof ExpressionConstraint) {
Constraint equationReplacement = convertEquationToConstraint((ExpressionConstraint) subConstraint, getFalseConstraint(featureModel));
constraint.replaceConstraintSubPart(subConstraint, equationReplacement);
}
}
}
private Constraint convertEquationToConstraint(ExpressionConstraint equation, Constraint falseConstraint) {
Set<Feature> featuresInEquation = getFeaturesInEquation(equation);
Set<Set<Feature>> featureCombinations = getFeatureCombinations(featuresInEquation);
Set<Constraint> disjunction = new HashSet<>();
for (Set<Feature> configuration : featureCombinations) {
boolean result = equation.evaluate(configuration);
if (result) {
disjunction.add(createConjunction(configuration, new HashSet<>(featuresInEquation)));
}
}
if (disjunction.isEmpty()) {
return falseConstraint;
}else{
return new ParenthesisConstraint(createDisjunction(disjunction));
}
}
private Set<Feature> getFeaturesInEquation(ExpressionConstraint equation) {
Set<Feature> featuresInEquation = new HashSet<>();
for (Expression expression : equation.getExpressionSubParts()) {
featuresInEquation.addAll(getFeaturesInExpression(expression));
}
return featuresInEquation;
}
private Set<Feature> getFeaturesInExpression(Expression expression) {
Set<Feature> featuresInEquation = new HashSet<>();
if (expression instanceof LiteralExpression) {
LiteralExpression literalExpression = (LiteralExpression) expression;
if (literalExpression.getContent() instanceof Feature) {
featuresInEquation.add((Feature) ((LiteralExpression) expression).getContent());
} else if (literalExpression.getContent() instanceof Attribute<?>) {
featuresInEquation.add(((Attribute<?>) literalExpression.getContent()).getFeature());
}
} else {
for (Expression subExpression : expression.getExpressionSubParts()) {
featuresInEquation.addAll(getFeaturesInExpression(subExpression));
}
}
return featuresInEquation;
}
private Set<Set<Feature>> getFeatureCombinations(Set<Feature> features) {
Set<Set<Feature>> featureCombinations = new HashSet<>();
for (int i = 0; i <= features.size(); i++) {
featureCombinations.addAll(Sets.combinations(features, i));
}
return featureCombinations;
}
private Constraint createConjunction(Set<Feature> selectedFeatures, Set<Feature> allFeatures) {
Constraint constraint;
Feature feature = null;
if (allFeatures.size() >= 1) {
feature = allFeatures.iterator().next();
allFeatures.remove(feature);
}
Constraint literalConstraint = new LiteralConstraint(feature);
if (!selectedFeatures.contains(feature)) {
literalConstraint = new NotConstraint(literalConstraint);
}
if (allFeatures.size() == 0) {
constraint = literalConstraint;
} else {
constraint = new AndConstraint(literalConstraint, createConjunction(selectedFeatures, allFeatures));
}
return constraint;
}
private Constraint createDisjunction(Set<Constraint> constraints) {
OrConstraint orConstraint = new OrConstraint();
for (Constraint constraint : constraints) {
orConstraint.addChild(constraint);
}
return orConstraint;
}
private void removeEquationFromAttributes(Feature feature, FeatureModel featureModel) {
Attribute<?> attributeConstraint = feature.getAttributes().get("constraint");
Attribute<?> attributeConstraintList = feature.getAttributes().get("constraints");
if (attributeConstraint != null) {
if (attributeConstraint.getValue() instanceof ExpressionConstraint) {
Constraint equationReplacement = convertEquationToConstraint((ExpressionConstraint) attributeConstraint.getValue(), getFalseConstraint(featureModel));
feature.getAttributes().put("constraint", new Attribute<>("constraint", equationReplacement, feature));
}
}
if (attributeConstraintList != null && attributeConstraintList.getValue() instanceof List<?>) {
List<Object> newConstraintList = new LinkedList<>();
for (Object constraint : (List<?>) attributeConstraintList.getValue()) {
if (constraint instanceof ExpressionConstraint) {
Constraint equationReplacement = convertEquationToConstraint((ExpressionConstraint) constraint, getFalseConstraint(featureModel));
newConstraintList.add(equationReplacement);
} else {
newConstraintList.add(constraint);
}
}
feature.getAttributes().put("constraints", new Attribute<>("constraints", newConstraintList, feature));
}
}
private void traverseFeatures(Feature feature, FeatureModel featureModel) {
removeEquationFromAttributes(feature, featureModel);
for (Group group : feature.getChildren()) {
for (Feature subFeature : group.getFeatures()) {
traverseFeatures(subFeature, featureModel);
}
}
}
}