|
20 | 20 | import de.vill.model.constraint.Constraint; |
21 | 21 | import de.vill.model.constraint.ExpressionConstraint; |
22 | 22 | import de.vill.model.constraint.NotConstraint; |
| 23 | +import edu.kit.dopler.model.AND; |
23 | 24 | import edu.kit.dopler.model.BooleanDecision; |
24 | 25 | import edu.kit.dopler.model.BooleanEnforce; |
25 | 26 | import edu.kit.dopler.model.BooleanLiteralExpression; |
26 | 27 | import edu.kit.dopler.model.BooleanValue; |
27 | 28 | import edu.kit.dopler.model.Decision; |
28 | 29 | import edu.kit.dopler.model.Dopler; |
29 | 30 | import edu.kit.dopler.model.DoubleValue; |
| 31 | +import edu.kit.dopler.model.Equals; |
| 32 | +import edu.kit.dopler.model.GreatherThan; |
30 | 33 | import edu.kit.dopler.model.IAction; |
31 | 34 | import edu.kit.dopler.model.IDecision; |
32 | 35 | import edu.kit.dopler.model.IExpression; |
| 36 | +import edu.kit.dopler.model.LessThan; |
| 37 | +import edu.kit.dopler.model.NOT; |
33 | 38 | import edu.kit.dopler.model.NumberDecision; |
34 | 39 | import edu.kit.dopler.model.NumberEnforce; |
| 40 | +import edu.kit.dopler.model.OR; |
35 | 41 | import edu.kit.dopler.model.Rule; |
36 | 42 | import edu.kit.dopler.model.StringDecision; |
37 | 43 | import edu.kit.dopler.model.StringEnforce; |
| 44 | +import edu.kit.dopler.model.StringLiteralExpression; |
38 | 45 | import edu.kit.dopler.model.StringValue; |
39 | 46 | import edu.kit.dopler.model.ValueRestrictionAction; |
40 | 47 | import edu.kit.travart.dopler.transformation.feature.to.decision.constraint.dnf.DnfAlwaysTrueAndFalseRemover; |
41 | 48 | import edu.kit.travart.dopler.transformation.feature.to.decision.constraint.dnf.DnfToTreeConverter; |
42 | 49 | import edu.kit.travart.dopler.transformation.feature.to.decision.constraint.dnf.TreeToDnfConverter; |
| 50 | +import edu.kit.travart.dopler.transformation.util.DecisionFinder; |
| 51 | +import edu.kit.travart.dopler.transformation.util.DecisionFinderImpl; |
43 | 52 | import edu.kit.travart.dopler.transformation.exceptions.CanNotBeTranslatedException; |
44 | 53 | import edu.kit.travart.dopler.transformation.exceptions.DecisionNotPresentException; |
45 | 54 | import edu.kit.travart.dopler.transformation.exceptions.DnfAlwaysFalseException; |
|
55 | 64 | import java.util.Optional; |
56 | 65 | import java.util.Set; |
57 | 66 | import java.util.Stack; |
| 67 | +import java.util.stream.Collectors; |
58 | 68 |
|
59 | 69 | /** |
60 | 70 | * Implementation of {@link ConstraintHandler}. |
@@ -114,7 +124,7 @@ public void handleConstraints(FeatureModel featureModel, Dopler decisionModel) { |
114 | 124 | for (Constraint constraint : getSanitizedConstraints(featureModel)) { |
115 | 125 | List<List<Constraint>> dnf = convertConstraintIntoDnf(featureModel, constraint); |
116 | 126 | Optional<Rule> rule = createRuleFromDnf(featureModel, decisionModel, dnf); |
117 | | - rule.ifPresent(this::distributeRule); |
| 127 | + rule.ifPresent(otherRule -> distributeRule(otherRule, decisionModel)); |
118 | 128 | } |
119 | 129 | } |
120 | 130 |
|
@@ -197,13 +207,59 @@ private Constraint createLeft(List<List<Constraint>> leftSide) { |
197 | 207 | } |
198 | 208 |
|
199 | 209 | /** Distribute the generated rule to a decision */ |
200 | | - private void distributeRule(Rule rule) { |
201 | | - //Just take decision of first action. |
202 | | - //The action must be a ValueRestrictionAction. Only those have decisions. |
203 | | - rule.getActions().stream().sorted(Comparator.comparing(Object::toString)) |
| 210 | + private void distributeRule(Rule rule, Dopler decisionModel) { |
| 211 | + // Add rule to the decisions of the condition |
| 212 | + DecisionFinder decisionFinder = new DecisionFinderImpl(); |
| 213 | + // To keep rules like if(true) {decision=true}, they are need to restore to fm |
| 214 | + if (rule.getCondition() instanceof BooleanLiteralExpression) { |
| 215 | + rule.getActions().stream() |
| 216 | + .sorted(Comparator.comparing(Object::toString)) |
204 | 217 | .filter(action -> action instanceof ValueRestrictionAction) |
205 | | - .map(action -> (ValueRestrictionAction) action).findFirst().orElseThrow().getDecision().addRule(rule); |
| 218 | + .map(action -> (ValueRestrictionAction) action).forEach(action -> action.getDecision().addRule(rule)); |
| 219 | + } else { |
| 220 | + Set<String> decisionIds = getDecisionIds(rule.getCondition()); |
| 221 | + for(String decisionId : decisionIds) { |
| 222 | + Optional<IDecision<?>> decisionOptional = decisionFinder.findDecisionById(decisionModel, decisionId); |
| 223 | + decisionOptional.ifPresent(decision -> decision.addRule(rule)); |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | +private Set<String> getDecisionIds(IExpression condition) { |
| 229 | + Set<String> decisionIds = new LinkedHashSet<>(); |
| 230 | + // Recursive lookUp for decisionIds |
| 231 | + if (condition instanceof StringLiteralExpression stringLiteralExpression) { |
| 232 | + decisionIds.add(stringLiteralExpression.getLiteral()); |
| 233 | + } else if (condition instanceof Equals equalsExpression) { |
| 234 | + decisionIds.addAll(getDecisionIds(equalsExpression.getLeftExpression())); |
| 235 | + decisionIds.addAll(getDecisionIds(equalsExpression.getRightExpression())); |
| 236 | + } else if (condition instanceof NOT notExpression) { |
| 237 | + decisionIds.addAll(getDecisionIds(notExpression.getOperand())); |
| 238 | + } else if (condition instanceof AND andExpression) { |
| 239 | + decisionIds.addAll(getDecisionIds(andExpression.getLeftExpression())); |
| 240 | + decisionIds.addAll(getDecisionIds(andExpression.getRightExpression())); |
| 241 | + } else if (condition instanceof OR orExpression) { |
| 242 | + decisionIds.addAll(getDecisionIds(orExpression.getLeftExpression())); |
| 243 | + decisionIds.addAll(getDecisionIds(orExpression.getRightExpression())); |
| 244 | + } else if (condition instanceof GreatherThan greaterThanExpression) { |
| 245 | + decisionIds.addAll(getDecisionIds(greaterThanExpression.getLeftExpression())); |
| 246 | + decisionIds.addAll(getDecisionIds(greaterThanExpression.getRightExpression())); |
| 247 | + } else if (condition instanceof LessThan lessThanExpression) { |
| 248 | + decisionIds.addAll(getDecisionIds(lessThanExpression.getLeftExpression())); |
| 249 | + decisionIds.addAll(getDecisionIds(lessThanExpression.getRightExpression())); |
206 | 250 | } |
| 251 | + // Other Expression types are ignored (e.g. Boolean, Double) and these do not contain decision IDs. |
| 252 | + |
| 253 | + // Strip part after last dot to get the correct ID |
| 254 | + decisionIds = decisionIds.stream() |
| 255 | + .map(id -> { |
| 256 | + int lastIndex = id.lastIndexOf('.'); |
| 257 | + return (lastIndex != -1) ? id.substring(0, lastIndex) : id; |
| 258 | + }) |
| 259 | + .collect(Collectors.toSet()); |
| 260 | + |
| 261 | + return decisionIds; |
| 262 | +} |
207 | 263 |
|
208 | 264 | /** Creates a set of {@link IAction}s that contradict each other. */ |
209 | 265 | private List<IAction> createContradiction(Dopler decisionModel) { |
|
0 commit comments