11package de .vill .main ;
22
3- import de .vill .config .Configuration ;
43import de .vill .model .Attribute ;
54import de .vill .model .Feature ;
65import de .vill .model .FeatureModel ;
1615
1716public class Example {
1817 public static void main (String [] args ) throws IOException {
19- FeatureModel featureModel = loadUVLFeatureModelFromFile ("test.uvl" );
18+ // Load a UVL feature model from a file
19+ FeatureModel featureModel = loadUVLFeatureModelFromFile ("src/test/resources/test_resources/parsing/complex/bike.uvl" );
20+ System .out .println ("Loaded feature model from file:" + featureModel .toString ());
2021
21- //traverse all features depth first search
22+ // Traverse all features in the model using depth-first search
23+ System .out .println ("Traversing all features:" );
2224 traverseAllFeatures (featureModel .getRootFeature ());
2325
24- //get constraints of feature model from constraints section (no constraints from submodels or attribtues)
25- List <Constraint > ownConstraint = featureModel .getOwnConstraints ();
26+ // Retrieve constraints defined directly in the feature model (excluding submodels and attributes)
27+ List <Constraint > ownConstraints = featureModel .getOwnConstraints ();
28+ System .out .println ("Number of own constraints: " + ownConstraints .size ());
2629
27- //traverse a constraint depth first search
28- if (ownConstraint .size () > 0 ) {
29- traverseConstraint (ownConstraint .get (0 ));
30+ // If there are constraints, traverse the first one
31+ if (!ownConstraints .isEmpty ()) {
32+ System .out .println ("Traversing first own constraint:" );
33+ traverseConstraint (ownConstraints .get (0 ));
3034 }
3135
32- //get all constraints of feature model and submodels and attributes
33- List <Constraint > allConstraint = featureModel .getConstraints ();
36+ // Retrieve all constraints, including those from submodels and attributes
37+ List <Constraint > allConstraints = featureModel .getConstraints ();
38+ System .out .println ("Total constraints (including submodels/attributes): " + allConstraints .size ());
3439
35- //get attribute from feature
36- String featureName = "featureName " ;
37- String attributeName = "attributeName " ;
40+ // Access a specific attribute of a feature
41+ String featureName = "Brake " ;
42+ String attributeName = "Weight " ;
3843 Feature feature = featureModel .getFeatureMap ().get (featureName );
3944 if (feature != null ) {
4045 Attribute <?> attribute = feature .getAttributes ().get (attributeName );
@@ -48,83 +53,80 @@ public static void main(String[] args) throws IOException {
4853 System .err .println ("Feature " + featureName + " not found!" );
4954 }
5055
51- //make feature abstract
52- featureName = "featureName " ;
56+ // Make a feature abstract by adding an "abstract" attribute
57+ featureName = "Inch " ;
5358 feature = featureModel .getFeatureMap ().get (featureName );
5459 if (feature != null ) {
5560 feature .getAttributes ().put ("abstract" , new Attribute <>("abstract" , true , feature ));
61+ System .out .println ("Feature " + featureName + " set to abstract." );
5662 } else {
5763 System .err .println ("Feature " + featureName + " not found!" );
5864 }
5965
60- //change newline and tabulator symbol for printing
61- Configuration .setTabulatorSymbol (" " );
62- Configuration .setNewlineSymbol ("\n " );
63-
64- //safe a single uvl model (this ignores any submodels)
66+ // Save the feature model as a single UVL file (ignoring submodels)
6567 String uvlModel = featureModel .toString ();
66- Path filePath = Paths .get ("test_singleModel.uvl" );
67- Files .write (filePath , uvlModel .getBytes ());
68+ Path filePath = Paths .get ("src/main/java/de/vill/main/modified_files/test_singleModel.uvl" );
69+ try {
70+ Files .write (filePath , uvlModel .getBytes ());
71+ } catch (IOException e ) {
72+ System .err .println ("Error saving single UVL model: " + e .getMessage ());
73+ return ;
74+ }
75+ System .out .println ("Saved single UVL model to test_singleModel.uvl" );
6876
69- //safe a decomposed uvl model with all its submodels to individual files
77+ // Save a decomposed UVL model with all submodels to individual files
7078 Map <String , String > modelList = featureModel .decomposedModelToString ();
7179 for (Map .Entry <String , String > uvlSubModel : modelList .entrySet ()) {
72- //safe submodel in sub directory directory with namespace as name
73- Files .createDirectories (Paths .get ("./subModels/" ));
74- filePath = Paths .get ("./subModels/" + uvlSubModel .getKey () + ".uvl" );
80+ Files .createDirectories (Paths .get ("./src/main/java/de/vill/main/modified_files/subModels/" ));
81+ filePath = Paths .get ("./src/main/java/de/vill/main/modified_files/subModels/" + uvlSubModel .getKey () + ".uvl" );
7582 Files .write (filePath , uvlSubModel .getValue ().getBytes ());
83+ System .out .println ("Saved submodel: " + uvlSubModel .getKey ());
7684 }
7785
78- //create a single uvl representation from a decomposed model and safe it to a single file
86+ // Compose a single UVL representation from a decomposed model and save it
7987 uvlModel = featureModel .composedModelToString ();
80- filePath = Paths .get ("test_composedModel.uvl" );
88+ filePath = Paths .get ("src/main/java/de/vill/main/modified_files/ test_composedModel.uvl" );
8189 Files .write (filePath , uvlModel .getBytes ());
90+ System .out .println ("Saved composed UVL model to test_composedModel.uvl" );
8291 }
8392
93+ // TODO: Add conversion example methods for converting between different formats (e.g., JSON, XML, etc.)
94+
95+ // ----------------------------------- Utility Methods -----------------------------------
96+
8497 /**
85- * Parse uvl model from file (if decomposed all submodels must be in the current working directory)
86- *
87- * @param path path to the file with uvl model
88- * @return the uvl model described in the file
89- * @throws IOException for io exceptions while loading the file content
98+ * Loads a UVL feature model from a single file.
99+ * If the model is decomposed, all submodels must be present in the current working directory.
100+ * @param path The path to the UVL model file.
101+ * @return The loaded feature model.
102+ * @throws IOException If an I/O error occurs while reading the file.
90103 */
91104 private static FeatureModel loadUVLFeatureModelFromFile (String path ) throws IOException {
92- Path filePath = Paths .get (path );
93- String content = new String (Files .readAllBytes (filePath ));
94105 UVLModelFactory uvlModelFactory = new UVLModelFactory ();
95- FeatureModel featureModel = uvlModelFactory .parse (content );
106+ FeatureModel featureModel = uvlModelFactory .parse (Paths . get ( path ) );
96107 return featureModel ;
97108 }
98109
99110 /**
100- * Parse a decomposed uvl model where all submodels are in a directory and named according to their namespaces.
101- *
102- * @param rootModelPath Path to the uvl root model file
103- * @param subModelDir Path to the directory with all submodels
104- * @return the uvl model described in the file
105- * @throws IOException for io exceptions while loading the file content
111+ * Traverses a constraint and all its sub-parts using depth-first search.
112+ * @param constraint The constraint to traverse.
106113 */
107- private static FeatureModel loadUVLFeatureModelFromDirectory (String rootModelPath , String subModelDir ) throws IOException {
108- Path filePath = Paths .get (rootModelPath );
109- String content = new String (Files .readAllBytes (filePath ));
110- UVLModelFactory uvlModelFactory = new UVLModelFactory ();
111- FeatureModel featureModel = uvlModelFactory .parse (content , subModelDir );
112- return featureModel ;
113- }
114-
115-
116114 private static void traverseConstraint (Constraint constraint ) {
115+ System .out .println ("\t Visiting constraint: " + constraint );
117116 for (Constraint subConstraint : constraint .getConstraintSubParts ()) {
118- //... do something with constraint
119117 traverseConstraint (subConstraint );
120118 }
121119 }
122120
121+ /**
122+ * Traverses all features in the feature tree using depth-first search.
123+ * @param feature The root feature to start traversal from.
124+ */
123125 public static void traverseAllFeatures (Feature feature ) {
126+ System .out .println ("\t Visiting feature: " + feature .getFeatureName ());
124127 for (Group group : feature .getChildren ()) {
125128 for (Feature childFeature : group .getFeatures ()) {
126- //... do something with feature
127- //or stop at submodel with if(!childfeature.isSubmodelroot())
129+ // Optionally, skip submodel roots: if (!childFeature.isSubmodelroot()) { ... }
128130 traverseAllFeatures (childFeature );
129131 }
130132 }
0 commit comments