Newer
Older
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
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
/**
* Main class for the car configurator.
* Is refined by modules, which add selected components to component list.
* This class has to save a file containing the configuration
* and launch the test seletion application afterwards.
*
*
* Naming conventions:
* m_: Member variable
* s: String
* n: Integer number (int, long,...)
* f: Floating point number (float, double,...)
* o: Object
* a: Collection (List, Array,...)
*
*/
public class Configurator {
//module list
private List m_aModuleList;
//output path
private String m_sOutputPath = "resources/output/featuremodel.xml";
//TODO: set this
//path for test case selector, which has to be launched from this application
private String m_sTestCaseSelectorPath;
//path to the feature model xml
private String m_sFeatureModelPath = "model.xml";
//document representation of the feature model
private Document m_oFeatureModelDoc;
//representation of a single module with all of its types
class Module {
String sModuleName;
ArrayList aModuleTypes;
}
public static void main(String[] args) {
new Configurator().save();
}
public void save() {
//create empty xml
Element oStruct = new Element("specification");
Document oDoc = new Document(oStruct);
//if the module list was initialized
if(m_aModuleList!=null) {
for(int i=0; i<m_aModuleList.size(); i++) {
//Add module and tag to Document
Element oModuleElement = new Element("module");
//Add name
Module oModule = (Module)m_aModuleList.get(i);
oModuleElement.setAttribute("name", oModule.sModuleName);
//Create type String
ArrayList<String> aTypes = oModule.aModuleTypes;
String sTypesString = "";
//concat String
for(int j=0; j<aTypes.size(); j++) {
sTypesString += aTypes.get(j);
//add comma if it's not the last Type
if(j<aTypes.size()-1)
sTypesString += ", ";
}
oModuleElement.setAttribute("types", sTypesString);
oStruct.addContent(oModuleElement);
}
}
//generate xml String and save it to a file
try {
Adapter.saveAsXml(oDoc, m_sOutputPath);
}
catch (Exception oEx) {
System.out.println(oEx);
return;
}
//create and launch Adapter
new Adapter();
}
//get all parents of a given module from the Feature Model file
private Module GetModuleParents(String sModuleName) throws IOException, JDOMException {
Module oModule = new Module();
//set name
oModule.sModuleName = sModuleName;
//init Doc if not done
if(m_oFeatureModelDoc==null) {
m_oFeatureModelDoc = Adapter.readXml(m_sFeatureModelPath);
System.out.println(m_oFeatureModelDoc.toString());
}
//get element
String sQuery = "//*[@name= '"+sModuleName+"']";
List<Element> aModules = Adapter.xQueryElements(m_oFeatureModelDoc, sQuery);
Element oModuleElement;
try {
oModuleElement = aModules.get(0);
}
catch (Exception oEx) {
System.out.println(oEx.toString() + "\nList empty for query "+ sQuery);
return null;
}
//get all anchestors
Element oParent = oModuleElement.getParentElement();
ArrayList<String> aTypes = new ArrayList();
//itterate through parents of module
while(true) {
String sElementName = oParent.getAttribute("name").getValue();
//break if we reach the Main class, as there are no types further up
if(sElementName.equals("ConfiguratorMain")) {
break;
}
aTypes.add(sElementName);
//iterate further trough List
oModuleElement = oParent;
oParent = oModuleElement.getParentElement();
}
oModule.aModuleTypes = aTypes;
return oModule;
}
//add a module name and module type to the feature list. Init list, if not already done
public void addToModuleList(String sModuleName) {
//init List, if not already done
if(m_aModuleList==null) {
m_aModuleList = new ArrayList();
}
//get the module types
Module oModule;
try {
oModule = GetModuleParents(sModuleName);
}
catch (Exception oEx) {
System.out.print(oEx);
return;
}
m_aModuleList.add(oModule);
}
}