Skip to content

Commit

Permalink
Demonstration of how to use the Trace-Boosting library with graph exa…
Browse files Browse the repository at this point in the history
…mple
  • Loading branch information
sandragreiner committed Jun 17, 2024
1 parent 9975a92 commit 76abd24
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/main/java/Demo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import org.logicng.formulas.FormulaFactory;
import org.variantsync.boosting.TraceBoosting;
import org.variantsync.boosting.datastructure.ASTNode;
import org.variantsync.boosting.datastructure.Feature;
import org.variantsync.boosting.datastructure.MainTree;
import org.variantsync.boosting.parsing.ESupportedLanguages;
import org.variantsync.boosting.product.Variant;
import org.variantsync.boosting.product.VariantPassport;

import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;

public class Demo {

public static void main(String[] args) {

List<VariantPassport> variantPassports = createVariantPassports();

TraceBoosting traceBoosting = new TraceBoosting(
variantPassports,
Path.of(System.getProperty("user.dir") + "/data/graph/workdir"),
ESupportedLanguages.LINES);

// optionally the number of threads can be adjusted, per default it is set to the number of available system threads
traceBoosting.setNumThreads(1);

List<Variant> variants = traceBoosting.getVariants();
System.out.println("initiated with " + variants.size() + " products");
applyDistribution(variants);

// runs the boosted comparison-based feature tracoing algorithm
MainTree tree = traceBoosting.computeMappings();

for (ASTNode n : tree.getTree().getAstNodes()) {
if (!n.getMapping().toString().equals("$true")) {
System.out.println(n.getMapping()+ " mapped onto " + n.getCode());
}
}

Set<String> relevantFeatures = variants.stream().flatMap(p -> p.getFeatures().stream().map(Feature::getName))
.collect(Collectors.toSet());

System.out.println(relevantFeatures);

// for (Variant p : traceBoosting.getVariants())
// {
// System.out.println("Variant: " + p.getName());
// for (ASTNode as : p.getAstNodesMainTree()) {
// if (!as.getMapping().toString().equals("$true")) {
// System.out.println( as.getMapping()+ " mapped onto " + as.getCode());
// }
// }
// System.out.println();
// }
}

private static List<VariantPassport> createVariantPassports() {
var variantPassports = new ArrayList<VariantPassport>(3);
variantPassports.add(
new VariantPassport("weighted",
Path.of(System.getProperty("user.dir") + "/data/graph/variant1/src"),
//configFileMap.get(variantName)
Path.of(System.getProperty("user.dir") + "/data/graph/variant1/config1.config")
)
);

variantPassports.add(
new VariantPassport("directed",
Path.of(System.getProperty("user.dir") + "/data/graph/variant2/src"),
//configFileMap.get(variantName)
Path.of(System.getProperty("user.dir") + "/data/graph/variant2/config2.config")
)
);

variantPassports.add(
new VariantPassport("colored",
Path.of(System.getProperty("user.dir") + "/data/graph/variant3/src"),
Path.of(System.getProperty("user.dir") + "/data/graph/variant3/config3.config"))
);
return variantPassports;
}

// simulates setting manually annotations of some AST nodes
private static void applyDistribution(List<Variant> variants) {
var factory = new FormulaFactory();
for (Variant p : variants) {
// Nodes that can be mapped
if (p.getName().equals("weighted")) {
for (ASTNode n : p.getProductAst().getAstNodes()) {
if (n.getCode().contains("List<Edge> edges();"))
n.setMapping(factory.variable("E"));
}
}
if (p.getName().equals("weighted")) {
for (ASTNode n : p.getProductAst().getAstNodes()) {
if (n.getCode().contains("public interface Graph {"))
n.setMapping(factory.variable("G"));
}
}
if (p.getName().equals("directed")) {
for (ASTNode n : p.getProductAst().getAstNodes()) {
if (n.getCode().contains("Graph subGraph(Color c);"))
n.setMapping(factory.variable("C"));
}
}
if (p.getName().equals("colored")) {
for (ASTNode n : p.getProductAst().getAstNodes()) {
if (n.getCode().contains("Graph subGraph(Color c);"))
n.setMapping(factory.variable("C"));
}
}
}
}
}

0 comments on commit 76abd24

Please sign in to comment.