Skip to content

Commit 7b6b47d

Browse files
Implemented loading of variables file as feature model. Each variable identified by KernelHaven is represented by an optional feature in the model. There are no constraints between the features.
1 parent 51caa62 commit 7b6b47d

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

src/main/java/org/variantsync/vevos/simulation/io/data/VariabilityDatasetLoader.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class VariabilityDatasetLoader implements ResourceLoader<VariabilityDatas
2525
private final static String PRESENCE_CONDITIONS_FILE = "code-variability.spl.csv";
2626
private final static String PARENTS_FILE = "PARENTS.txt";
2727
private final static String MESSAGE_FILE = "MESSAGE.txt";
28+
private final static String VARIABLES_FILE = "VARIABLES.txt";
2829
private static final String DATA_DIR_NAME = "data";
2930
private static final String LOG_DIR_NAME = "log";
3031
private static final String FILTER_COUNTS_FILE = "FILTERED.txt";
@@ -135,7 +136,12 @@ private Path resolvePathToCommitOutputDir(final Path rootDir, final String commi
135136
}
136137

137138
private SPLCommit.FeatureModelPath resolvePathToFeatureModel(final Path rootDir, final String commitId) {
138-
final Path p = resolvePathToCommitOutputDir(rootDir, commitId).resolve(FEATURE_MODEL_FILE);
139+
Path p = resolvePathToCommitOutputDir(rootDir, commitId).resolve(FEATURE_MODEL_FILE);
140+
if (!Files.exists(p)) {
141+
// If no feature model is found, we instead set the variables file, as feature model
142+
// TODO: Move this logic to VEVOS_extraction, if we convert a feature model a FeatureIDE format?
143+
p = resolvePathToCommitOutputDir(rootDir, commitId).resolve(VARIABLES_FILE);
144+
}
139145
return new SPLCommit.FeatureModelPath(p);
140146
}
141147

src/main/java/org/variantsync/vevos/simulation/io/kernelhaven/VariabilityModelLoader.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
package org.variantsync.vevos.simulation.io.kernelhaven;
22

3+
import de.ovgu.featureide.fm.core.base.FeatureUtils;
4+
import de.ovgu.featureide.fm.core.base.IFeature;
35
import de.ovgu.featureide.fm.core.base.IFeatureModel;
6+
import de.ovgu.featureide.fm.core.base.impl.DefaultFeatureModelFactory;
47
import net.ssehub.kernel_haven.variability_model.JsonVariabilityModelCache;
58
import org.variantsync.functjonal.Result;
69
import org.variantsync.vevos.simulation.io.ResourceLoader;
710
import org.variantsync.vevos.simulation.util.fide.FeatureModelUtils;
811
import org.variantsync.vevos.simulation.util.io.PathUtils;
912

13+
import java.nio.file.Files;
1014
import java.nio.file.Path;
15+
import java.util.List;
16+
import java.util.stream.Collectors;
1117

1218
public class VariabilityModelLoader implements ResourceLoader<IFeatureModel> {
1319

1420
@Override
1521
public boolean canLoad(Path p) {
16-
return PathUtils.hasExtension(p,".json");
22+
return PathUtils.hasExtension(p,".json", ".txt");
1723
}
1824

1925
@Override
2026
public Result<IFeatureModel, ? extends Exception> load(Path p) {
2127
return Result.Try(() -> {
22-
JsonVariabilityModelCache cache = new JsonVariabilityModelCache(p.getParent().toFile());
23-
return FeatureModelUtils.FromVariabilityModel(cache.readFixed(p.toFile()));
28+
if (p.endsWith(".json")) {
29+
JsonVariabilityModelCache cache = new JsonVariabilityModelCache(p.getParent().toFile());
30+
return FeatureModelUtils.FromVariabilityModel(cache.readFixed(p.toFile()));
31+
} else {
32+
List<String> variables = Files.readAllLines(p).stream().map(String::trim).collect(Collectors.toList());
33+
final IFeatureModel featureModel = DefaultFeatureModelFactory.getInstance().create();
34+
IFeature root = DefaultFeatureModelFactory.getInstance().createFeature(featureModel, "ROOT");
35+
FeatureUtils.setRoot(featureModel, root);
36+
return FeatureModelUtils.FillFeatureModel(featureModel, variables);
37+
}
2438
});
2539
}
2640

src/main/java/org/variantsync/vevos/simulation/util/fide/FeatureModelUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,20 @@ public static IFeatureModel FromVariabilityModel(VariabilityModel vm) throws Res
150150
final Set<String> featureNames = new HashSet<>(vm.getVariableMap().keySet());
151151
// We only want to add features that are not part of the model yet
152152
fm.getFeatures().stream().map(IFeatureModelElement::getName).forEach(featureNames::remove);
153+
154+
return FillFeatureModel(fm, featureNames);
155+
}
156+
157+
/// Create optional features from a collection of feature names and add them to the feature model
158+
public static IFeatureModel FillFeatureModel(IFeatureModel fm, Collection<String> featureNames) {
153159
final IFeatureModelFactory factory = FMFactoryManager.getInstance().getFactory(fm);
154160
final IFeature root = FeatureUtils.getRoot(fm);
155161
// Add all remaining feature from the variability model to the feature model
156162
featureNames.stream().map(name -> factory.createFeature(fm, name)).forEach(feature -> {
157163
FeatureUtils.addFeature(fm, feature);
158164
FeatureUtils.addChild(root, feature);
159165
});
160-
Logger.debug("Added all feature from VariabilityModel to FeatureModel.");
166+
Logger.debug("Added all optional features to the FeatureModel.");
161167
return fm;
162168
}
163169

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.variantsync.vevos.simulation;
2+
3+
import de.ovgu.featureide.fm.core.base.IFeature;
4+
import de.ovgu.featureide.fm.core.base.IFeatureModel;
5+
import de.ovgu.featureide.fm.core.base.IFeatureModelElement;
6+
import org.junit.Test;
7+
import org.variantsync.vevos.simulation.variability.SPLCommit;
8+
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.util.Collection;
13+
import java.util.stream.Collectors;
14+
15+
public class FeatureModelLoading {
16+
17+
static {
18+
VEVOS.Initialize();
19+
}
20+
21+
@Test
22+
public void fromVariables() throws IOException {
23+
Path variablesPath = Path.of("src/test/java/org/variantsync/vevos/simulation/variability/TEST_VARIABLES.txt");
24+
SPLCommit commit = new SPLCommit("aaaaa", null, null, new SPLCommit.FeatureModelPath(variablesPath), null, null, null);
25+
IFeatureModel featureModel = commit.featureModel().run().orElseThrow();
26+
Collection<String> features = featureModel.getFeatures().stream().map(IFeatureModelElement::getName).collect(Collectors.toList());
27+
assert features.size() == 5;
28+
for (String feature : Files.readAllLines(variablesPath)) {
29+
assert features.contains(feature);
30+
}
31+
}
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
First Feature
2+
Feature A
3+
Feature B
4+
Feature C
5+
Last Feature

0 commit comments

Comments
 (0)