Skip to content

Commit b550bde

Browse files
committed
refactoring, renaming set and removing unused product loading functionality
1 parent 5bc0929 commit b550bde

12 files changed

Lines changed: 128 additions & 174 deletions

File tree

src/main/java/org/variantsync/boosting/TraceBoosting.java

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.util.Collection;
1616
import java.util.Collections;
1717
import java.util.HashMap;
18-
import java.util.LinkedList;
1918
import java.util.List;
2019
import java.util.Map;
2120
import java.util.Objects;
@@ -35,11 +34,8 @@
3534
import org.logicng.transformations.dnf.DNFSubsumption;
3635
import org.tinylog.Logger;
3736

38-
import org.variantsync.boosting.datastructure.ASTNode;
39-
import org.variantsync.boosting.datastructure.Association;
40-
import org.variantsync.boosting.datastructure.EccoSet;
41-
import org.variantsync.boosting.datastructure.Feature;
42-
import org.variantsync.boosting.datastructure.MainTree;
37+
import org.variantsync.boosting.datastructure.*;
38+
import org.variantsync.boosting.datastructure.CustomHashSet;
4339
import org.variantsync.boosting.datastructure.Module;
4440
import org.variantsync.boosting.parsing.AbstractAST;
4541
import org.variantsync.boosting.parsing.CAST;
@@ -48,8 +44,6 @@
4844
import org.variantsync.boosting.parsing.LineAST;
4945
import org.variantsync.boosting.product.Product;
5046
import org.variantsync.boosting.product.ProductInitializationTask;
51-
import org.variantsync.boosting.product.ProductLoadTask;
52-
import org.variantsync.boosting.product.ProductLoader;
5347
import org.variantsync.boosting.product.ProductPassport;
5448
import org.variantsync.boosting.product.ProductSaveTask;
5549

@@ -289,19 +283,19 @@ private static String getName(String suffix, final ASTNode astNode, boolean insi
289283
}
290284
}
291285

292-
private static EccoSet<Module> featuresToModules(final EccoSet<Feature> positiveFeatures,
293-
final EccoSet<Feature> negativeFeatures) {
294-
final EccoSet<Module> result = new EccoSet<>();
295-
final EccoSet<EccoSet<Feature>> positivePowerSet = positiveFeatures.powerSet();
296-
final EccoSet<EccoSet<Feature>> negativePowerSet = negativeFeatures.powerSet();
286+
private static CustomHashSet<Module> featuresToModules(final CustomHashSet<Feature> positiveFeatures,
287+
final CustomHashSet<Feature> negativeFeatures) {
288+
final CustomHashSet<Module> result = new CustomHashSet<>();
289+
final CustomHashSet<CustomHashSet<Feature>> positivePowerSet = positiveFeatures.powerSet();
290+
final CustomHashSet<CustomHashSet<Feature>> negativePowerSet = negativeFeatures.powerSet();
297291

298292
// Create all possible modules
299-
for (final EccoSet<Feature> posSet : positivePowerSet) {
293+
for (final CustomHashSet<Feature> posSet : positivePowerSet) {
300294
if (posSet.isEmpty()) {
301295
continue;
302296
}
303-
for (final EccoSet<Feature> negSet : negativePowerSet) {
304-
final EccoSet<Literal> literals = new EccoSet<>();
297+
for (final CustomHashSet<Feature> negSet : negativePowerSet) {
298+
final CustomHashSet<Literal> literals = new CustomHashSet<>();
305299
posSet.stream().map(feature -> f.literal(feature.getName(), true))
306300
.forEach(literals::add);
307301
negSet.stream().map(feature -> f.literal(feature.getName(), false))
@@ -313,13 +307,13 @@ private static EccoSet<Module> featuresToModules(final EccoSet<Feature> positive
313307
return result;
314308
}
315309

316-
private static EccoSet<Module> updateModules(final EccoSet<Module> moduleSet,
317-
final EccoSet<Feature> negativeFeatures) {
318-
final EccoSet<Module> result = new EccoSet<>();
319-
final EccoSet<EccoSet<Feature>> negativePowerSet = negativeFeatures.powerSet();
310+
private static CustomHashSet<Module> updateModules(final CustomHashSet<Module> moduleSet,
311+
final CustomHashSet<Feature> negativeFeatures) {
312+
final CustomHashSet<Module> result = new CustomHashSet<>();
313+
final CustomHashSet<CustomHashSet<Feature>> negativePowerSet = negativeFeatures.powerSet();
320314
for (final Module module : moduleSet) {
321-
for (final EccoSet<Feature> negSet : negativePowerSet) {
322-
final EccoSet<Literal> negLiterals = new EccoSet<>();
315+
for (final CustomHashSet<Feature> negSet : negativePowerSet) {
316+
final CustomHashSet<Literal> negLiterals = new CustomHashSet<>();
323317
negSet.stream().map(feature -> f.literal(feature.getName(), false))
324318
.forEach(negLiterals::add);
325319
result.add(new Module(module.getLiterals().unite(negLiterals)));
@@ -328,7 +322,7 @@ private static EccoSet<Module> updateModules(final EccoSet<Module> moduleSet,
328322
return result;
329323
}
330324

331-
private EccoSet<Feature> allFeatures;
325+
private CustomHashSet<Feature> allFeatures;
332326

333327
private int nThreads = Runtime.getRuntime().availableProcessors();
334328

@@ -362,7 +356,7 @@ public TraceBoosting(final List<ProductPassport> sourceLocations, final Path wor
362356
this.productInitTasks = initialize();
363357
}
364358

365-
public EccoSet<Feature> getAllFeatures() {
359+
public CustomHashSet<Feature> getAllFeatures() {
366360
return allFeatures;
367361
}
368362

@@ -417,46 +411,6 @@ public List<Product> getProducts() {
417411
return this.products;
418412
}
419413

420-
public Product[] loadProducts(final String inputFolder) {
421-
final ProductLoader loader = prepareProductLoader(inputFolder);
422-
final List<Product> products = new LinkedList<>();
423-
loader.forEachRemaining(products::add);
424-
return products.toArray(new Product[0]);
425-
}
426-
427-
public Product[] loadProducts(final Collection<Path> productLocations) {
428-
final List<ProductLoadTask> tasks = new ArrayList<>();
429-
for (final Path productPath : productLocations) {
430-
final ProductLoadTask task = new ProductLoadTask(productPath);
431-
tasks.add(task);
432-
}
433-
434-
final ProductLoader loader = new ProductLoader(tasks, this.nThreads);
435-
final List<Product> products = new LinkedList<>();
436-
loader.forEachRemaining(products::add);
437-
return products.toArray(new Product[0]);
438-
}
439-
440-
public ProductLoader prepareProductLoader(final String folderName) {
441-
Logger.info("Loading all products from " + folderName);
442-
final Path pathToInput = Path.of(folderName);
443-
final List<Path> productPaths;
444-
try {
445-
productPaths = Files.list(pathToInput).filter(p -> p.toString().endsWith(".product"))
446-
.collect(Collectors.toList());
447-
productPaths.sort(Path::compareTo);
448-
} catch (final IOException e) {
449-
Logger.error("Was not able to read input directory.", e);
450-
throw new RuntimeException(e);
451-
}
452-
final List<ProductLoadTask> tasks = new ArrayList<>();
453-
for (final Path productPath : productPaths) {
454-
final ProductLoadTask task = new ProductLoadTask(productPath);
455-
tasks.add(task);
456-
}
457-
458-
return new ProductLoader(tasks, this.nThreads);
459-
}
460414

461415
/**
462416
* Initializes the products by creating them from variants and configuration
@@ -471,7 +425,7 @@ public ProductLoader prepareProductLoader(final String folderName) {
471425
public List<ProductInitializationTask> initialize() {
472426
// creates products from variants and config files
473427
Logger.info("Collecting variant dirs...");
474-
allFeatures = new EccoSet<>();
428+
allFeatures = new CustomHashSet<>();
475429
final List<ProductInitializationTask> products = startProductCreation();
476430
// saveProducts(products, inputFolder);
477431
// Logger.info("Parsing and saving of products complete.");
@@ -488,7 +442,7 @@ public List<ProductInitializationTask> initialize() {
488442
*/
489443
public MainTree computeMappings() {
490444
// Initialize variables
491-
allFeatures = new EccoSet<>();
445+
allFeatures = new CustomHashSet<>();
492446
final AbstractAST mainAST;
493447

494448
// Determine mainAST based on targetLanguage
@@ -510,7 +464,7 @@ public MainTree computeMappings() {
510464
final MainTree mainTree = new MainTree(mainAST);
511465

512466
// Extract associations using traceExtractionAlgorithm
513-
final EccoSet<Association> associations = traceExtractionAlgorithm(mainTree);
467+
final CustomHashSet<Association> associations = extractAssociationsComparisonBased(mainTree);
514468

515469
// Assign proactive traces to associations
516470
assignProactiveTraces(associations);
@@ -657,9 +611,9 @@ public void setPaths(final String inputFolder, final String inputFile,
657611
* @return An EccoSet of Association objects representing the extracted
658612
* associations
659613
*/
660-
public EccoSet<Association> traceExtractionAlgorithm(final MainTree mainTree) {
614+
public CustomHashSet<Association> extractAssociationsComparisonBased(final MainTree mainTree) {
661615
int productCount = 0;
662-
EccoSet<Association> associations = new EccoSet<>();
616+
CustomHashSet<Association> associations = new CustomHashSet<>();
663617
for (Product product : this.getProducts()) {
664618
// merge each product AST into the main tree and collect corresponding main tree
665619
// nodes
@@ -671,14 +625,14 @@ public EccoSet<Association> traceExtractionAlgorithm(final MainTree mainTree) {
671625
product.forgetAST();
672626

673627
Logger.info("Considering product " + productCount + "...");
674-
final EccoSet<Feature> productFeatures = product.getFeatures();
675-
final EccoSet<Feature> negFeatures = productFeatures.without(allFeatures);
676-
final EccoSet<Module> modules = featuresToModules(productFeatures, allFeatures.without(productFeatures));
628+
final CustomHashSet<Feature> productFeatures = product.getFeatures();
629+
final CustomHashSet<Feature> negFeatures = productFeatures.without(allFeatures);
630+
final CustomHashSet<Module> modules = featuresToModules(productFeatures, allFeatures.without(productFeatures));
677631
allFeatures.addAll(productFeatures);
678-
Association aNew = new Association(modules, modules, modules, new EccoSet<>(),
632+
Association aNew = new Association(modules, modules, modules, new CustomHashSet<>(),
679633
product.getAstNodesMainTree());
680634

681-
final EccoSet<Association> newAssociations = new EccoSet<>();
635+
final CustomHashSet<Association> newAssociations = new CustomHashSet<>();
682636
for (final Association association : associations) {
683637
// Update modules in association
684638
Association updatedAssociation = new Association(
@@ -689,7 +643,7 @@ public EccoSet<Association> traceExtractionAlgorithm(final MainTree mainTree) {
689643
association.getAstNodes());
690644

691645
// Intersect ASTs
692-
final EccoSet<ASTNode> intNodes = updatedAssociation.getAstNodes().intersect(aNew.getAstNodes());
646+
final CustomHashSet<ASTNode> intNodes = updatedAssociation.getAstNodes().intersect(aNew.getAstNodes());
693647

694648
// compute intersection
695649
final Association aInt = new Association(
@@ -706,15 +660,15 @@ public EccoSet<Association> traceExtractionAlgorithm(final MainTree mainTree) {
706660
updatedAssociation.removeNodes(intNodes);
707661
updatedAssociation = new Association(
708662
updatedAssociation.getMin().without(aInt.getMin()),
709-
updatedAssociation.getAll(), new EccoSet<>(),
663+
updatedAssociation.getAll(), new CustomHashSet<>(),
710664
updatedAssociation.getNot().unite(aNew.getAll()),
711665
updatedAssociation.getAstNodes());
712666
updatedAssociation
713667
.setMax(updatedAssociation.getAll().without(updatedAssociation.getNot()));
714668
// set mapping for code appearing in aNew but in not association
715669
aNew.removeNodes(intNodes);
716670
aNew = new Association(aNew.getMin().without(aInt.getMin()), aNew.getAll(),
717-
new EccoSet<>(), aNew.getNot().unite(updatedAssociation.getAll()),
671+
new CustomHashSet<>(), aNew.getNot().unite(updatedAssociation.getAll()),
718672
aNew.getAstNodes());
719673
aNew.setMax(aNew.getAll().without(aNew.getNot()));
720674

@@ -748,7 +702,7 @@ public EccoSet<Association> traceExtractionAlgorithm(final MainTree mainTree) {
748702
* @throws RuntimeException if there is an error while assigning proactive
749703
* traces
750704
*/
751-
private void assignProactiveTraces(EccoSet<Association> associations) {
705+
private void assignProactiveTraces(CustomHashSet<Association> associations) {
752706
ExecutorService threadPool = Executors.newFixedThreadPool(nThreads);
753707
List<Future<Integer>> futures = new ArrayList<>();
754708
for (Association assoc : associations) {
@@ -819,14 +773,14 @@ private void determineAssociationMapping(Association association) {
819773
formula = f.verum();
820774
} else {
821775
formula = f.or(modules.stream().map(m -> {
822-
final EccoSet<Literal> literals = m.getLiterals();
776+
final CustomHashSet<Literal> literals = m.getLiterals();
823777
return f.cnf(literals);
824778
}).collect(Collectors.toList()));
825779
}
826780
} else {
827781
// Continue with the min modules
828782
formula = f.and(modules.stream().map(m -> {
829-
final EccoSet<Literal> literals = m.getLiterals();
783+
final CustomHashSet<Literal> literals = m.getLiterals();
830784
return f.cnf(literals);
831785
}).collect(Collectors.toList()));
832786
}

src/main/java/org/variantsync/boosting/datastructure/ASTNode.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public enum NODE_TYPE {
3030

3131
private final String code;
3232
private ASTNode parent;
33-
private final EccoSet<ASTNode> children;
33+
private final CustomHashSet<ASTNode> children;
3434
private final NODE_TYPE type;
3535
private transient Position startPosition;
3636
private transient Formula mapping;
@@ -48,7 +48,7 @@ public ASTNode(final ASTNode parent, final String code, final Position position,
4848
this.startPosition = Objects.requireNonNull(position);
4949
this.type = type;
5050
this.mapping = mapping;
51-
children = new EccoSet<>();
51+
children = new CustomHashSet<>();
5252
productEquivalent = null;
5353
}
5454

@@ -145,7 +145,7 @@ public void setParent(final ASTNode parent) {
145145
/**
146146
* Returns a set of this node's child nodes.
147147
*/
148-
public EccoSet<ASTNode> getChildren() {
148+
public CustomHashSet<ASTNode> getChildren() {
149149
return children;
150150
}
151151

@@ -164,8 +164,8 @@ public Position getStartPosition() {
164164
* Returns set set of all possible mappings (if the mapping is a DNF formula,
165165
* each clause is a possible mapping on its own) for this node.
166166
*/
167-
public EccoSet<Formula> getMappings() {
168-
final EccoSet<Formula> mappings = new EccoSet<>();
167+
public CustomHashSet<Formula> getMappings() {
168+
final CustomHashSet<Formula> mappings = new CustomHashSet<>();
169169
if (mapping.type() == FType.OR) {
170170
for (final Formula clause : mapping) {
171171
mappings.add(clause);

0 commit comments

Comments
 (0)