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,77 @@ 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+ /*
93+ additional constraint with all cloned versions ored
94+ Set<String> allFeatureNamesInSubTree = new HashSet<>();
95+ getAllSubFeatureNamesRecursively(feature, allFeatureNamesInSubTree);
96+ for (Constraint constraint : constraintsToClone) {
97+ Constraint newConstraint = constraint.clone();
98+ orAdaptedConstraint(newConstraint, allFeatureNamesInSubTree, min, max, featureModel);
99+ featureModel.getOwnConstraints().add(newConstraint);
100+ }
101+
102+ */
103+
104+
79105 feature .getChildren ().removeAll (feature .getChildren ());
80106 feature .getChildren ().add (newChildren );
81107 newChildren .setParentFeature (feature );
82108 }
83109
84- private void addPrefixToNamesRecursively (Feature feature , String prefix ) {
110+ private void orAdaptedConstraint (Constraint constraint , Set <String > featuresToReplace , int min , int max , FeatureModel featureModel ) {
111+ for (Constraint subPart : constraint .getConstraintSubParts ()) {
112+ if (subPart instanceof LiteralConstraint ) {
113+ String toReplace = ((LiteralConstraint ) subPart ).getReference ().getIdentifier ();
114+ if (featuresToReplace .contains (toReplace )) {
115+ Feature f = featureModel .getFeatureMap ().get (toReplace + "-" + (min == 0 ? 1 : min ) + "-1" );
116+ Constraint newOr = new LiteralConstraint (f );
117+ for (int i = min + 1 ; i <= max ; i ++) {
118+ for (int j = 1 ; j <= i ; j ++) {
119+ newOr = new OrConstraint (newOr , new LiteralConstraint (featureModel .getFeatureMap ().get (toReplace + "-" + i + "-" + j )));
120+ }
121+ }
122+ constraint .replaceConstraintSubPart (subPart , new ParenthesisConstraint (newOr ));
123+ }
124+ } else {
125+ orAdaptedConstraint (subPart , featuresToReplace , min , max , featureModel );
126+ }
127+ }
128+ }
129+
130+ private void getAllSubFeatureNamesRecursively (Feature feature , Set <String > names ) {
131+ names .add (feature .getFeatureName ());
132+ for (Group child : feature .getChildren ()) {
133+ for (Feature childFeature : child .getFeatures ()) {
134+ getAllSubFeatureNamesRecursively (childFeature , names );
135+ }
136+ }
137+ }
138+
139+ private void addPrefixToNamesRecursively (Feature feature , String prefix , FeatureModel featureModel ) {
85140 feature .setFeatureName (feature .getFeatureName () + prefix );
141+ var attributes = feature .getAttributes ();
142+ featureModel .getFeatureMap ().put (feature .getFeatureName (), feature );
86143 if (!feature .isSubmodelRoot ()) {
87144 for (Group group : feature .getChildren ()) {
88145 for (Feature subFeature : group .getFeatures ()) {
89- addPrefixToNamesRecursively (subFeature , prefix );
146+ addPrefixToNamesRecursively (subFeature , prefix , featureModel );
90147 }
91148 }
92149 }
@@ -118,14 +175,46 @@ private List<Feature> getFeatureFromSubTree(Group group) {
118175
119176 private boolean constraintContains (Constraint constraint , List <Feature > subTreeFeatures ) {
120177 List <Constraint > subParts = constraint .getConstraintSubParts ();
178+ if (constraint instanceof LiteralConstraint && ((LiteralConstraint ) constraint ).getReference () instanceof Feature ) {
179+ Feature feature = (Feature ) ((LiteralConstraint ) constraint ).getReference ();
180+ if (subTreeFeatures .contains (feature )) {
181+ return true ;
182+ }
183+ } else if (constraint instanceof ExpressionConstraint ) {
184+ Expression left = ((ExpressionConstraint ) constraint ).getLeft ();
185+ Expression right = ((ExpressionConstraint ) constraint ).getRight ();
186+ return expressionContains (left , subTreeFeatures ) || expressionContains (right , subTreeFeatures );
187+ }
188+
121189 for (Constraint subPart : subParts ) {
122190 if (subPart instanceof LiteralConstraint && ((LiteralConstraint ) subPart ).getReference () instanceof Feature ) {
123191 Feature feature = (Feature ) ((LiteralConstraint ) subPart ).getReference ();
124192 if (subTreeFeatures .contains (feature )) {
125193 return true ;
126194 }
127- } else {
128- constraintContains (subPart , subTreeFeatures );
195+ } else if (constraintContains (subPart , subTreeFeatures )) {
196+ return true ;
197+ }
198+ }
199+ return false ;
200+ }
201+
202+ private boolean expressionContains (Expression expression , List <Feature > subTreeFeatures ) {
203+ if (expression instanceof LiteralExpression ) {
204+ Feature feature = (Feature ) ((Attribute <?>) ((LiteralExpression ) expression ).getContent ()).getFeature ();
205+ if (subTreeFeatures .contains (feature )) {
206+ return true ;
207+ }
208+ }
209+
210+ for (Expression subExpression : expression .getExpressionSubParts ()) {
211+ if (expression instanceof LiteralExpression ) {
212+ Feature feature = (Feature ) ((LiteralExpression ) expression ).getContent ();
213+ if (subTreeFeatures .contains (feature )) {
214+ return true ;
215+ }
216+ } else if (expressionContains (subExpression , subTreeFeatures )) {
217+ return true ;
129218 }
130219 }
131220 return false ;
@@ -144,17 +233,38 @@ private void createFeatureReplacementMap(Feature oldSubTree, Feature newSubTree,
144233 }
145234
146235 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 )));
236+ if (constraint instanceof ExpressionConstraint ) {
237+ adaptExpression (((ExpressionConstraint ) constraint ).getLeft (), featureReplacementMap );
238+ adaptExpression (((ExpressionConstraint ) constraint ).getRight (), featureReplacementMap );
239+ } else {
240+ List <Constraint > subParts = constraint .getConstraintSubParts ();
241+ for (Constraint subPart : subParts ) {
242+ if (subPart instanceof LiteralConstraint ) {
243+ String toReplace = ((LiteralConstraint ) subPart ).getReference ().getIdentifier ();
244+ if (featureReplacementMap .containsKey (toReplace )) {
245+ LiteralConstraint newLiteral = new LiteralConstraint (featureReplacementMap .get (toReplace ));
246+ constraint .replaceConstraintSubPart (subPart , newLiteral );
247+ }
248+ } else {
249+ adaptConstraint (subTreeRoot , subPart , featureReplacementMap );
155250 }
156- } else {
157- adaptConstraint (subTreeRoot , subPart , featureReplacementMap );
251+ }
252+ }
253+ }
254+
255+ private void adaptExpression (Expression expression , Map <String , Feature > featureReplacementMap ) {
256+ if (expression instanceof LiteralExpression ) {
257+ LiteralExpression literalExpression = (LiteralExpression ) expression ;
258+ Attribute <?> attribute = (Attribute <?>) literalExpression .getContent ();
259+ if (featureReplacementMap .containsKey (attribute .getFeature ().getFeatureName ())) {
260+ var newAttribute = attribute .clone ();
261+ newAttribute .setFeature (featureReplacementMap .get (attribute .getFeature ().getFeatureName ()));
262+ literalExpression .setContent (newAttribute );
263+ }
264+
265+ } else {
266+ for (Expression subExpression : expression .getExpressionSubParts ()) {
267+ adaptExpression (subExpression , featureReplacementMap );
158268 }
159269 }
160270 }
0 commit comments