11package de .vill .conversion ;
22
33import de .vill .model .*;
4- import de .vill .model .constraint .Constraint ;
5- import de .vill .model .constraint .ImplicationConstraint ;
6- import de .vill .model .constraint .LiteralConstraint ;
7- import de .vill .model .constraint .ParenthesisConstraint ;
4+ import de .vill .model .constraint .*;
5+ import de .vill .model .expression .Expression ;
6+ import de .vill .model .expression .LiteralExpression ;
87
98import java .util .*;
109
@@ -52,6 +51,7 @@ private void removeFeatureCardinality(Feature feature, FeatureModel featureModel
5251
5352 for (int i = min ; i <= max ; i ++) {
5453 Feature newChild = new Feature (feature .getFeatureName () + "-" + i );
54+ featureModel .getFeatureMap ().put (newChild .getFeatureName (), newChild );
5555 newChild .getAttributes ().put ("abstract" , new Attribute <Boolean >("abstract" , true , feature ));
5656 newChildren .getFeatures ().add (newChild );
5757 newChild .setParentGroup (newChildren );
@@ -62,7 +62,9 @@ private void removeFeatureCardinality(Feature feature, FeatureModel featureModel
6262 }
6363 for (int j = 1 ; j <= i ; j ++) {
6464 Feature subTreeClone = feature .clone ();
65- addPrefixToNamesRecursively (subTreeClone , "-" + i + "-" + j );
65+ subTreeClone .getAttributes ().clear ();
66+ subTreeClone .getAttributes ().put ("abstract" , new Attribute <Boolean >("abstract" , true , subTreeClone ));
67+ addPrefixToNamesRecursively (subTreeClone , "-" + i + "-" + j , featureModel );
6668 mandatoryGroup .getFeatures ().add (subTreeClone );
6769 subTreeClone .setParentGroup (mandatoryGroup );
6870
@@ -71,22 +73,35 @@ private void removeFeatureCardinality(Feature feature, FeatureModel featureModel
7173 constraintReplacementMap .remove (feature .getFeatureName ());
7274 for (Constraint constraint : constraintsToClone ) {
7375 Constraint newConstraint = constraint .clone ();
76+ if (newConstraint instanceof LiteralConstraint ) {
77+ String toReplace = ((LiteralConstraint ) newConstraint ).getReference ().getIdentifier ();
78+ if (constraintReplacementMap .containsKey (toReplace )) {
79+ LiteralConstraint newLiteral = new LiteralConstraint (constraintReplacementMap .get (toReplace ));
80+ LiteralConstraint subTreeRootConstraint = new LiteralConstraint (newChild );
81+ newConstraint = new ImplicationConstraint (subTreeRootConstraint , new ParenthesisConstraint (newLiteral ));
82+ }
83+ } else {
84+ adaptConstraint (subTreeClone , newConstraint , constraintReplacementMap );
85+ LiteralConstraint subTreeRootConstraint = new LiteralConstraint (newChild );
86+ newConstraint = new ImplicationConstraint (subTreeRootConstraint , new ParenthesisConstraint (newConstraint ));
87+ }
7488 featureModel .getOwnConstraints ().add (newConstraint );
75- adaptConstraint (subTreeClone , newConstraint , constraintReplacementMap );
7689 }
7790 }
7891 }
92+
7993 feature .getChildren ().removeAll (feature .getChildren ());
8094 feature .getChildren ().add (newChildren );
8195 newChildren .setParentFeature (feature );
8296 }
8397
84- private void addPrefixToNamesRecursively (Feature feature , String prefix ) {
98+ private void addPrefixToNamesRecursively (Feature feature , String prefix , FeatureModel featureModel ) {
8599 feature .setFeatureName (feature .getFeatureName () + prefix );
100+ featureModel .getFeatureMap ().put (feature .getFeatureName (), feature );
86101 if (!feature .isSubmodelRoot ()) {
87102 for (Group group : feature .getChildren ()) {
88103 for (Feature subFeature : group .getFeatures ()) {
89- addPrefixToNamesRecursively (subFeature , prefix );
104+ addPrefixToNamesRecursively (subFeature , prefix , featureModel );
90105 }
91106 }
92107 }
@@ -118,14 +133,46 @@ private List<Feature> getFeatureFromSubTree(Group group) {
118133
119134 private boolean constraintContains (Constraint constraint , List <Feature > subTreeFeatures ) {
120135 List <Constraint > subParts = constraint .getConstraintSubParts ();
136+ if (constraint instanceof LiteralConstraint && ((LiteralConstraint ) constraint ).getReference () instanceof Feature ) {
137+ Feature feature = (Feature ) ((LiteralConstraint ) constraint ).getReference ();
138+ if (subTreeFeatures .contains (feature )) {
139+ return true ;
140+ }
141+ } else if (constraint instanceof ExpressionConstraint ) {
142+ Expression left = ((ExpressionConstraint ) constraint ).getLeft ();
143+ Expression right = ((ExpressionConstraint ) constraint ).getRight ();
144+ return expressionContains (left , subTreeFeatures ) || expressionContains (right , subTreeFeatures );
145+ }
146+
121147 for (Constraint subPart : subParts ) {
122148 if (subPart instanceof LiteralConstraint && ((LiteralConstraint ) subPart ).getReference () instanceof Feature ) {
123149 Feature feature = (Feature ) ((LiteralConstraint ) subPart ).getReference ();
124150 if (subTreeFeatures .contains (feature )) {
125151 return true ;
126152 }
127- } else {
128- constraintContains (subPart , subTreeFeatures );
153+ } else if (constraintContains (subPart , subTreeFeatures )) {
154+ return true ;
155+ }
156+ }
157+ return false ;
158+ }
159+
160+ private boolean expressionContains (Expression expression , List <Feature > subTreeFeatures ) {
161+ if (expression instanceof LiteralExpression ) {
162+ Feature feature = (Feature ) ((Attribute <?>) ((LiteralExpression ) expression ).getContent ()).getFeature ();
163+ if (subTreeFeatures .contains (feature )) {
164+ return true ;
165+ }
166+ }
167+
168+ for (Expression subExpression : expression .getExpressionSubParts ()) {
169+ if (expression instanceof LiteralExpression ) {
170+ Feature feature = (Feature ) ((LiteralExpression ) expression ).getContent ();
171+ if (subTreeFeatures .contains (feature )) {
172+ return true ;
173+ }
174+ } else if (expressionContains (subExpression , subTreeFeatures )) {
175+ return true ;
129176 }
130177 }
131178 return false ;
@@ -144,17 +191,38 @@ private void createFeatureReplacementMap(Feature oldSubTree, Feature newSubTree,
144191 }
145192
146193 private void adaptConstraint (Feature subTreeRoot , Constraint constraint , Map <String , Feature > featureReplacementMap ) {
147- List <Constraint > subParts = constraint .getConstraintSubParts ();
148- for (Constraint subPart : subParts ) {
149- if (subPart instanceof LiteralConstraint ) {
150- String toReplace = ((LiteralConstraint ) subPart ).getReference ().getIdentifier ();
151- if (featureReplacementMap .containsKey (toReplace )) {
152- LiteralConstraint subTreeRootConstraint = new LiteralConstraint (subTreeRoot );
153- LiteralConstraint newLiteral = new LiteralConstraint (featureReplacementMap .get (toReplace ));
154- constraint .replaceConstraintSubPart (subPart , new ParenthesisConstraint (new ImplicationConstraint (subTreeRootConstraint , newLiteral )));
194+ if (constraint instanceof ExpressionConstraint ) {
195+ adaptExpression (((ExpressionConstraint ) constraint ).getLeft (), featureReplacementMap );
196+ adaptExpression (((ExpressionConstraint ) constraint ).getRight (), featureReplacementMap );
197+ } else {
198+ List <Constraint > subParts = constraint .getConstraintSubParts ();
199+ for (Constraint subPart : subParts ) {
200+ if (subPart instanceof LiteralConstraint ) {
201+ String toReplace = ((LiteralConstraint ) subPart ).getReference ().getIdentifier ();
202+ if (featureReplacementMap .containsKey (toReplace )) {
203+ LiteralConstraint newLiteral = new LiteralConstraint (featureReplacementMap .get (toReplace ));
204+ constraint .replaceConstraintSubPart (subPart , newLiteral );
205+ }
206+ } else {
207+ adaptConstraint (subTreeRoot , subPart , featureReplacementMap );
155208 }
156- } else {
157- adaptConstraint (subTreeRoot , subPart , featureReplacementMap );
209+ }
210+ }
211+ }
212+
213+ private void adaptExpression (Expression expression , Map <String , Feature > featureReplacementMap ) {
214+ if (expression instanceof LiteralExpression ) {
215+ LiteralExpression literalExpression = (LiteralExpression ) expression ;
216+ Attribute <?> attribute = (Attribute <?>) literalExpression .getContent ();
217+ if (featureReplacementMap .containsKey (attribute .getFeature ().getFeatureName ())) {
218+ var newAttribute = attribute .clone ();
219+ newAttribute .setFeature (featureReplacementMap .get (attribute .getFeature ().getFeatureName ()));
220+ literalExpression .setContent (newAttribute );
221+ }
222+
223+ } else {
224+ for (Expression subExpression : expression .getExpressionSubParts ()) {
225+ adaptExpression (subExpression , featureReplacementMap );
158226 }
159227 }
160228 }
0 commit comments