Skip to content

Commit 9f43cc9

Browse files
committed
feat: VariationTree.assertConsistency
1 parent 282258d commit 9f43cc9

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

src/main/java/org/variantsync/diffdetective/variation/diff/DiffNode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ public static DiffNode<DiffLinesLabel> fromID(int id, String label) {
733733
* Checks that the VariationDiff is in a valid state.
734734
* In particular, this method checks that all edges are well-formed (e.g., edges can be inconsistent because edges are double-linked).
735735
* This method also checks that a node with exactly one parent was edited, and that a node with exactly two parents was not edited.
736+
* To check all children recursively, use {@link VariationDiff#assertConsistency}.
736737
* @see Assert#assertTrue
737738
* @throws AssertionError when an inconsistency is detected.
738739
*/

src/main/java/org/variantsync/diffdetective/variation/tree/VariationNode.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -487,26 +487,28 @@ public VariationTreeNode<L> toVariationTree(final Map<? super T, VariationTreeNo
487487
}
488488

489489
/**
490-
* Checks that this node satisfies some easy to check invariants.
491-
* In particular, this method checks that
492-
* <ul>
493-
* <li>if-chains are nested correctly,
494-
* <li>the root is an {@link NodeType#IF} with the feature mapping {@code "true"},
495-
* <li>the feature mapping is {@code null} iff {@code isConditionalAnnotation} is {@code false}
496-
* and
497-
* <li>all edges are well-formed (e.g., edges can be inconsistent because edges are
498-
* double-linked).
499-
* </ul>
500-
*
501-
* <p>Some invariants are not checked. These include
502-
* <ul>
503-
* <li>There should be no cycles and
504-
* <li>{@link getID} should be unique in the whole variation tree.
505-
* </ul>
506-
*
507-
* @see Assert#assertTrue
508-
* @throws AssertionError when an inconsistency is detected.
509-
*/
490+
* Checks that this node satisfies some easy to check invariants.
491+
* In particular, this method checks that
492+
* <ul>
493+
* <li>if-chains are nested correctly,
494+
* <li>the root is an {@link NodeType#IF} with the feature mapping {@code "true"},
495+
* <li>the feature mapping is {@code null} iff {@code isConditionalAnnotation} is {@code false}
496+
* and
497+
* <li>all edges are well-formed (e.g., edges can be inconsistent because edges are
498+
* double-linked).
499+
* </ul>
500+
*
501+
* <p>Some invariants are not checked. These include
502+
* <ul>
503+
* <li>There should be no cycles,
504+
* <li>{@link getID} should be unique in the whole variation tree, and
505+
* <li>children are not checked recursively.
506+
* </ul>
507+
* Use {@link VariationTree#assertConsistency} to check all children recursively.
508+
*
509+
* @see Assert#assertTrue
510+
* @throws AssertionError when an inconsistency is detected.
511+
*/
510512
public void assertConsistency() {
511513
// ELSE and ELIF nodes have an IF or ELIF as parent.
512514
if (isElse() || isElif()) {

src/main/java/org/variantsync/diffdetective/variation/tree/VariationTree.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.variantsync.diffdetective.util.Assert;
66
import org.variantsync.diffdetective.variation.DiffLinesLabel;
77
import org.variantsync.diffdetective.variation.Label;
8+
import org.variantsync.diffdetective.variation.NodeType; // For Javadoc
89
import org.variantsync.diffdetective.variation.diff.DiffNode;
910
import org.variantsync.diffdetective.variation.diff.VariationDiff;
1011
import org.variantsync.diffdetective.variation.diff.Projection;
@@ -213,6 +214,17 @@ public String unparse() {
213214
return result.toString();
214215
}
215216

217+
/**
218+
* Checks whether this {@link VariationTree} is consistent.
219+
* Throws an error if this {@link VariationTree} is inconsistent (e.g., if there are multiple
220+
* {@link NodeType#ELSE} nodes). Has no side-effects otherwise.
221+
*
222+
* @see VariationNode#assertConsistency
223+
*/
224+
public void assertConsistency() {
225+
forAllPreorder(VariationTreeNode::assertConsistency);
226+
}
227+
216228
@Override
217229
public String toString() {
218230
return "variation tree from " + source;

0 commit comments

Comments
 (0)