Skip to content

Commit

Permalink
Merge pull request #9 from VariantSync/next-release
Browse files Browse the repository at this point in the history
VEVOS Simulation V1.1.1
  • Loading branch information
AlexanderSchultheiss authored Aug 22, 2022
2 parents 9050939 + d0d4469 commit d244446
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<!-- Adjust group id for anonymous projects -->
<groupId>org.variantsync.vevos</groupId>
<!-- Adjust the generateVariant name -->
<artifactId>Simulation</artifactId>
<version>1.0.0</version>
<artifactId>simulation</artifactId>
<version>1.1.1</version>

<properties>
<!-- Adjust your java version here -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public boolean canLoad(Path p) {
return FeatureModelUtils.FromVariabilityModel(cache.readFixed(p.toFile()));
} else {
List<String> variables = Files.readAllLines(p).stream().map(String::trim).collect(Collectors.toList());
final IFeatureModel featureModel = DefaultFeatureModelFactory.getInstance().create();
IFeature root = DefaultFeatureModelFactory.getInstance().createFeature(featureModel, "ROOT");
FeatureUtils.setRoot(featureModel, root);
return FeatureModelUtils.FillFeatureModel(featureModel, variables);
return FeatureModelUtils.FromOptionalFeatures(variables);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

public class FormulaUtils {
public static Node negate(final Node node) {
Expand Down Expand Up @@ -60,7 +62,51 @@ public static void flatten(final And and) {
} while (!redundantChildren.isEmpty());
}

public static String toString(final Node formula, final String[] symbols) {
/**
* Replaces all nodes within the given formula's tree that match the given predicate.
* Matching nodes (i.e., nodes for which the predicate who returns true) will be replaced by the value returned
* by the replacement function, invoked on the matching node.
* @param root The root of the formula in which occurences of formulas should be replaced. The object remains unaltered.
* @param who A replacement is made whenever this predicate evaluates to true on a given node.
* @param replacement Whenever a node should be replaced, this function is invoked with that node as argument.
* The node will be replaced with the node returned.
* @return A new formula in which all nodes matching the given predicate are replaced.
*/
public static Node replaceAll(final Node root, final Predicate<Node> who, final Function<Node, Node> replacement) {
if (root == null) {
return null;
}

return replaceAllInplace(root.clone(), who, replacement);
}

/**
* Inplace variant of the {@link #replaceAll(Node, Predicate, Function)} function.
* This means the given formula (root parameter) will be altered.
*/
public static Node replaceAllInplace(final Node root, final Predicate<Node> who, final Function<Node, Node> replacement) {
if (root == null) {
return null;
}

if (who.test(root)) {
return replacement.apply(root);
} else {
final Node[] children = root.getChildren();
if (children != null) {
for (int i = 0; i < children.length; ++i) {
children[i] = replaceAllInplace(children[i], who, replacement);
}
root.setChildren(children);
}
return root;
}
}

public static String toString(Node formula, final String[] symbols) {
formula = replaceAll(formula, FixTrueFalse::isTrue, n -> FixTrueFalse.TrueAs1);
formula = replaceAllInplace(formula, FixTrueFalse::isFalse, n -> FixTrueFalse.FalseAs0);

final NodeWriter writer = new NodeWriter(formula);
writer.setNotation(NodeWriter.Notation.INFIX);
writer.setEnquoteWhitespace(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@
* as well as a conversion method for parsing certain feature names to true and false, respectively.
*/
public class FixTrueFalse {
/// Names of variables that we want to interpret as atomic values true or false, respectively.
public final static List<String> TrueNames = Arrays.asList("true", "1");
public final static List<String> FalseNames = Arrays.asList("false", "0");

/*
Constant literals representing the true and false value
*/
public final static Literal True = new org.prop4j.True();
public final static Literal False = new org.prop4j.False();

/*
Constant literals representing the true and false values only for serialization.
True and False are represented by the numeric values 0 and 1.
*/
public final static Literal TrueAs1 = new Literal("1");
public final static Literal FalseAs0 = new Literal("0");

/// Names of variables that we want to interpret as atomic values true or false, respectively.
public final static List<String> TrueNames = Arrays.asList("true", (String) TrueAs1.var);
public final static List<String> FalseNames = Arrays.asList("false", (String) FalseAs0.var);

/**
* @return True iff the given formula is a true literal.
* @see FixTrueFalse::isTrueLiteral
Expand All @@ -47,14 +54,14 @@ public static boolean isFalse(final Node n) {
* @return True iff the given name represents the atomic value true w.r.t. the constant TrueNames.
*/
public static boolean isTrueLiteral(final Literal l) {
return TrueNames.stream().anyMatch(t -> t.equals(l.var.toString().toLowerCase()));
return TrueNames.stream().anyMatch(t -> t.equalsIgnoreCase(l.var.toString()));
}

/**
* @return True iff the given name represents the atomic value false w.r.t. the constant FalseNames.
*/
public static boolean isFalseLiteral(final Literal l) {
return FalseNames.stream().anyMatch(f -> f.equals(l.var.toString().toLowerCase()));
return FalseNames.stream().anyMatch(f -> f.equalsIgnoreCase(l.var.toString()));
}

private static Node[] filterMatches(final Node[] nodes, Predicate<Node> filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.prop4j.Node;
import org.prop4j.NodeWriter;
import org.variantsync.vevos.simulation.util.fide.FormulaUtils;
import org.variantsync.vevos.simulation.variability.pc.options.VariantGenerationOptions;

import java.util.ArrayList;
Expand All @@ -16,7 +17,7 @@ public List<String> project(final VariantGenerationOptions projectionOptions, fi
final List<String> result = new ArrayList<>();

if (projectionOptions.withMacros()) {
result.add("#if " + condition.toString(NodeWriter.javaSymbols));
result.add("#if " + FormulaUtils.toString(condition, NodeWriter.javaSymbols));
}

for (final VariantLineChunk child : lines) {
Expand Down

0 comments on commit d244446

Please sign in to comment.