-
Notifications
You must be signed in to change notification settings - Fork 2
/
FACT.java
492 lines (439 loc) · 17 KB
/
FACT.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* (c) https://github.com/MontiCore/monticore */
package mcfdtool;
import de.monticore.featureconfiguration.FeatureConfigurationTool;
import de.monticore.featureconfiguration._ast.ASTFeatureConfiguration;
import de.monticore.featureconfiguration._parser.FeatureConfigurationParser;
import de.monticore.featureconfigurationpartial.FeatureConfigurationPartialMill;
import de.monticore.featurediagram.FeatureDiagramTool;
import de.monticore.featurediagram.FeatureDiagramMill;
import de.monticore.featurediagram.ModelPaths;
import de.monticore.featurediagram._ast.ASTFeatureDiagram;
import de.monticore.featurediagram._parser.FeatureDiagramParser;
import de.monticore.featurediagram._symboltable.FeatureDiagramSymbols2Json;
import de.monticore.io.paths.MCPath;
import de.se_rwth.commons.logging.Log;
import fddiff.FDSemDiff;
import mcfdtool.analyses.*;
import mcfdtool.transform.flatzinc.Constraint;
import org.apache.commons.cli.*;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* FACT is the main class of the Feature Diagram Language
* <p>
* It provides a tool processing inputs and commands to
* perform analyses on FDs, provide symtabs and pretty printing
*/
public class FACT {
protected FeatureDiagramTool fdTool = new FeatureDiagramTool();
protected FeatureDiagramParser fdParser = new FeatureDiagramParser();
protected FeatureDiagramSymbols2Json s2j = new FeatureDiagramSymbols2Json();
protected FeatureConfigurationTool fcTool = new FeatureConfigurationTool();
protected FeatureConfigurationParser fcParser = new FeatureConfigurationParser();
/**
* Main function: Delegates to the FACT instance it creates
*
* @param args command line arguments (e.g. --help)
*/
public static void main(String[] args) {
FACT tool = new FACT();
Log.initWARN();
FeatureConfigurationPartialMill.init();
FeatureDiagramMill.init();
tool.run(args);
}
/**
* Uses Apache CLI Parser to access args
*
* @param args command line arguments (e.g. --help)
*/
public void run(String[] args) {
//init tool options and log
Options options = initOptions();
try {
// Basic processing
CommandLineParser parser = new BasicParser();
CommandLine cmd = parser.parse(options, args);
// Language tool specific handling of commands
// help:
if (null == cmd || 0 == cmd.getArgList().size() || cmd.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter
.printHelp("java -jar MCFACT.jar <test1.fd> <test2.fd>? [analysis options] ", options);
return;
}
if (cmd.getArgList().size() > 2) {
// received a greater number of arguments as inputs as expected (max 2 for semdiff)
Log.error("0xFC999 Too many arguments. At most two FDs are expected.");
return;
}
// First argument: First FD that is being processed
ASTFeatureDiagram firstFD = readFeatureDiagram(cmd, 0);
// check all implemented analysis one after another
if (checkInput("isValid", cmd, 1)) {
String fcString = cmd.getOptionValue("isValid"); //read FC file path from command line
ASTFeatureConfiguration fc = readFeatureConfiguration(fcString, cmd);
execIsValid(firstFD, fc);
}
if (checkInput("allProducts", cmd, 1)) {
execAllProducts(firstFD);
}
if (checkInput("deadFeatures", cmd, 1)) {
execDeadFeature(firstFD);
}
if (checkInput("falseOptional", cmd, 1)) {
execFalseOptional(firstFD);
}
if (checkInput("completeToValid", cmd, 1)) {
String fcString = cmd.getOptionValue("completeToValid"); //read FC file path from cmd
ASTFeatureConfiguration fc = readFeatureConfiguration(fcString, cmd);
execCompleteToValid(firstFD, fc);
}
if (checkInput("findValid", cmd, 1)) {
execFindValid(firstFD);
}
if (checkInput("isVoidFeatureModel", cmd, 1)) {
execIsVoidFeatureModel(firstFD);
}
if (checkInput("numberOfProducts", cmd, 1)) {
execNumberOfProducts(firstFD);
}
if (checkInput("semdiff", cmd, 2)) {
ASTFeatureDiagram secondFD = readFeatureDiagram(cmd, 1);
if (secondFD != null) {
String optionVal = cmd.getOptionValue("semdiff");
execSemDiff(firstFD, secondFD, optionVal);
}
}
}
catch (ParseException e) {
Log.error("0xFC901 Error while parsing the command line options!", e);
}
}
/**
* Checks whether the current command line arguments contain the passed analysis option.
* If not, return false. Otherwise, check whether the number of passed arguments
* (i.e., FeatureDiagrams) equals the expected number of feature diagrams for
* the analysis. If the numbers match, return true. Otherwise, log an error and return false.
*
* @param optionName
* @param cmd
* @param expectedArgNum
* @return
*/
private boolean checkInput(String optionName, CommandLine cmd, int expectedArgNum) {
if (!cmd.hasOption(optionName)) {
return false;
}
if (cmd.getArgList().size() != expectedArgNum) {
Log.error(
String.format("0xFC910 Number of specified input FDs is '%s'. "
+ "Option '%s' expects that number of input FDs is exactly '%s'",
cmd.getArgList().size(), optionName, expectedArgNum));
return false;
}
return true;
}
private void execSemDiff(ASTFeatureDiagram from, ASTFeatureDiagram to, String optionVal) {
FDSemDiff fdSemDiff = new FDSemDiff();
Optional<ASTFeatureConfiguration> witness = Optional.empty();
if (optionVal == null || optionVal.equals("open") || optionVal.equals("closed")) {
if (optionVal == null || optionVal.equals("open")) {
witness = fdSemDiff.semDiffOpenWorld(from, to);
}
else if (optionVal.equals("closed")) {
witness = fdSemDiff.semDiffClosedWorld(from, to);
}
if (witness.isPresent()) {
System.out.println(
"Diff witness: " + FeatureConfigurationPartialMill.prettyPrint(witness.get(), true));
}
else {
System.out.println("The first input FD is a refinement of the second input FD.");
}
}
else {
Log.error(String.format(
"0xFC902 Unknown value '%s' for the argument 'semantics' of the option '-semdiff'. "
+ "Possible values for the argument are 'open' and 'closed'.", optionVal));
}
}
/**
* handle -allProducts
*/
public List<ASTFeatureConfiguration> execAllProducts(ASTFeatureDiagram fd) {
AllProducts analysis = new AllProducts();
List<ASTFeatureConfiguration> result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC782 AllProducts was not successful");
}
else {
String pp = result.stream()
.map(astFeatureConfiguration -> FeatureConfigurationPartialMill.prettyPrint(astFeatureConfiguration, true))
.collect(Collectors.joining(System.lineSeparator()));
System.out.println("Result of AllProducts: " + pp);
}
return result;
}
/**
* handle -completeToValid
*/
public ASTFeatureConfiguration execCompleteToValid(ASTFeatureDiagram fd,
ASTFeatureConfiguration fc) {
CompleteToValid analysis = new CompleteToValid();
ASTFeatureConfiguration result = analysis.perform(fd, fc);
if (null == result) {
Log.error("0xFC775 CompleteToValid was not successful");
}
else {
System.out.println("Result of CompleteToValid: "
+ FeatureConfigurationPartialMill.prettyPrint(result, true));
}
return result;
}
/**
* handle -deadFeature
*/
public List<String> execDeadFeature(ASTFeatureDiagram fd) {
DeadFeature analysis = new DeadFeature();
List<String> result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC776 DeadFeature was not successful");
}
else {
System.out.println("Result of DeadFeature: "
+ result.stream().collect(Collectors.joining(", ")));
}
return result;
}
/**
* handle -falseOptional
*/
public List<String> execFalseOptional(ASTFeatureDiagram fd) {
FalseOptional analysis = new FalseOptional();
List<String> result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC777 FalseOptional was not successful");
}
else {
System.out.println("Result of FalseOptional: "
+ result.stream().collect(Collectors.joining(", ")));
}
return result;
}
/**
* handle -findValid
*/
public ASTFeatureConfiguration execFindValid(ASTFeatureDiagram fd) {
FindValid analysis = new FindValid();
ASTFeatureConfiguration result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC774 FindValid was not successful");
}
else {
System.out.println("Result of FindValid: "
+ FeatureConfigurationPartialMill.prettyPrint(result, true));
}
return result;
}
/**
* handle -generalFilter
*/
public List<ASTFeatureConfiguration> execGeneralFilter(ASTFeatureDiagram fd,
List<Constraint> constraints) {
GeneralFilter analysis = new GeneralFilter();
List<ASTFeatureConfiguration> result = analysis.perform(fd, constraints);
if (null == result) {
Log.error("0xFC778 GeneralFilter was not successful");
}
else {
String pp = result.stream()
.map(ast -> FeatureConfigurationPartialMill.prettyPrint(ast, true))
.collect(Collectors.joining(System.lineSeparator()));
System.out.println("Result of GeneralFilter: " + pp);
}
return result;
}
/**
* handle -isValid
*/
public boolean execIsValid(ASTFeatureDiagram fd, ASTFeatureConfiguration fc) {
IsValid analysis = new IsValid();
Boolean result = analysis.perform(fd, fc);
if (null == result) {
Log.error("0xFC779 IsValid was not successful");
}
else {
System.out.println("Result of IsValid: " + result);
}
return result;
}
/**
* handle -isVoidFeatureModel
*/
public boolean execIsVoidFeatureModel(ASTFeatureDiagram fd) {
IsVoidFeatureModel analysis = new IsVoidFeatureModel();
Boolean result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC780 IsVoidFeatureModel was not successful");
}
else {
System.out.println("Result of IsVoidFeatureModel: " + result);
}
return result;
}
/**
* handle -numberOfProducts
*/
public int execNumberOfProducts(ASTFeatureDiagram fd) {
NumberOfProducts analysis = new NumberOfProducts();
Integer result = analysis.perform(fd);
if (null == result) {
Log.error("0xFC781 NumberOfProducts was not successful");
}
else {
System.out.println("Result of NumberOfProducts: " + result);
}
return result;
}
/**
* Read in a Feature Diagram at the passed location, with the passed location for storing FD symbol
* tables and the passed MCPath for searching stored symbol tables (e.g., of imported FDs)
*
* @param modelFile
* @param symbolOutPath
* @param symbolInputPath
* @return
*/
public ASTFeatureDiagram readFeatureDiagram(String modelFile, String symbolOutPath,
MCPath symbolInputPath) {
ModelPaths.merge(FeatureDiagramMill.globalScope().getSymbolPath(), symbolInputPath);
return fdTool.run(modelFile, Paths.get(symbolOutPath));
}
/**
* Read in a Feature Configuration at the passed location with the passed MCPath for searching
* stored symbol tables (of FDs). No symbol table is stored for FCs themselves.
*
* @param modelFile
* @param symbolInputPath
* @return
*/
public ASTFeatureConfiguration readFeatureConfiguration(String modelFile,
MCPath symbolInputPath) {
return fcTool.run(modelFile, symbolInputPath);
}
/**
* Read in a Feature Configuration at the passed location. Use the base location of the passed FC
* (= path without FC file name and FC package) for searching for stored symbol tables (of FDs).
* No symbol table is stored for FCs themselves.
*
* @param modelFile
* @return
*/
public ASTFeatureConfiguration readFeatureConfiguration(String modelFile) {
return fcTool.run(modelFile);
}
/**
* reads the feature diagram passed as the num-th argument (starting at 0) to the command line.
* The feature diagrams are the only "arguments" in the command line, as all other
* arguments should be parsed as "options" and are treated elsewhere. Stores the symbols
* at a specified location if the option "symbolPath" is set
*
* @param cmd
* @param num
* @return
*/
protected ASTFeatureDiagram readFeatureDiagram(CommandLine cmd, int num) {
if (0 == cmd.getArgList().size()) {
Log.error("0xFC900 No feature diagram given as first argument!");
return null;
}
if (cmd.getArgList().size() < num + 1) {
// received a smaller number of FDs as inputs as expected
Log.error(String.format("0xFC998 Received %s feature diagrams as inputs. "
+ "Expecting at least %s feature diagrams as inputs!", cmd.getArgList().size(), num + 1));
return null;
}
else {
String fdModelFile = cmd.getArgList().get(num).toString();
//by default, use this for the symbol output
String symbolOutPath = FeatureDiagramTool.SYMBOL_OUT.toString();
//except if the option "symbolPath" is set, then use the passed location to store (and load) symbols
if (cmd.hasOption("symbolPath")) {
symbolOutPath = cmd.getOptionValue("symbolPath");
}
MCPath mp = new MCPath(Paths.get(symbolOutPath));
return readFeatureDiagram(fdModelFile, symbolOutPath, mp);
}
}
/**
* Read a feature configuration from the passed command line.
*
* @param fcString
* @param cmd
* @return
*/
protected ASTFeatureConfiguration readFeatureConfiguration(String fcString, CommandLine cmd) {
//Without setting a modelpath, the FC tool would never be able to search
// for the FD model referenced in the FC. Therefore, identifying this is mandatory here.
//if the option "modelPath" is set, use the passed location to load symbols
if (cmd.hasOption("modelPath")) {
MCPath mp = new MCPath(Paths.get(cmd.getOptionValue("modelPath")));
return readFeatureConfiguration(fcString, mp);
}
//if "modelPath" is not set, but "symbolPath" is set to store FD symbols, use this as modelPath
else if (cmd.hasOption("symbolPath")) {
MCPath mp = new MCPath(Paths.get(cmd.getOptionValue("symbolPath")));
return readFeatureConfiguration(fcString, mp);
}
// else use the folder, in which the FC model is located, as modelpath. This is done by the
// invoked method below
else {
return readFeatureConfiguration(fcString);
}
}
/**
* This creates the options for the FACT tool. These are both used for the CommandLineParser
* and printing the "help" option.
*
* @return
*/
protected Options initOptions() {
Options options = new Options();
createAnalysisOption(options, "isValid", "test.fc", false,
"check if <test.fc> is a valid configuration in <test1.fd>");
createAnalysisOption(options, "allProducts", "find all valid configurations for <test1.fd>");
createAnalysisOption(options, "deadFeatures", "find all dead features for <test1.fd>");
createAnalysisOption(options, "falseOptional",
"find all false optional features for <test1.fd>");
createAnalysisOption(options, "completeToValid", "test.fc", false, "find a valid "
+ "configurations of <test1.fd> that fulfils the partial configuration <test.fc>");
createAnalysisOption(options, "findValid", "find a valid configuration for <test1.fd>");
createAnalysisOption(options, "isVoidFeatureModel",
"check if <test1.fd> has any valid configuration");
createAnalysisOption(options, "numberOfProducts",
"calculate the number of valid configurations for <test1.fd>");
createAnalysisOption(options, "semdiff", "semantics", true, "calculate "
+ "a diff witness contained in the semantic difference from <test1.fd> to <test2.fd> using the semantics "
+ "as specified by the argument <semantics>. Possible values for the argument are 'closed' and 'open' "
+ "for choosing between the closed- and open-world semantics. If no argument is specified, then 'open' "
+ "is chosen by default. The differences between the semantics are described here: "
+ "'https://se-rwth.de/publications/Semantic-Evolution-Analysis-of-Feature-Models.pdf'");
// ... place for further analyses
options.addOption("h", "help", false, "show this explanation");
return options;
}
protected static void createAnalysisOption(Options options, String key, String argName,
boolean optionalArg, String description) {
Option opt = new Option(key, argName != null, description);
opt.setArgName(argName);
opt.setOptionalArg(optionalArg);
options.addOption(opt);
}
protected static void createAnalysisOption(Options options, String key, String description) {
Option opt = new Option(key, false, description);
options.addOption(opt);
}
}