Skip to content

Commit e556df0

Browse files
ibbempiameier
authored andcommitted
feat: VariationTree.assertConsistency
1 parent 7c4366a commit e556df0

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

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

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

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

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

501501
/**
502-
* Checks that this node satisfies some easy to check invariants.
503-
* In particular, this method checks that
504-
* <ul>
505-
* <li>if-chains are nested correctly,
506-
* <li>the root is an {@link NodeType#IF} with the feature mapping {@code "true"},
507-
* <li>the feature mapping is {@code null} iff {@code isConditionalAnnotation} is {@code false}
508-
* and
509-
* <li>all edges are well-formed (e.g., edges can be inconsistent because edges are
510-
* double-linked).
511-
* </ul>
512-
*
513-
* <p>Some invariants are not checked. These include
514-
* <ul>
515-
* <li>There should be no cycles and
516-
* <li>{@link getID} should be unique in the whole variation tree.
517-
* </ul>
518-
*
519-
* @see Assert#assertTrue
520-
* @throws AssertionError when an inconsistency is detected.
521-
*/
502+
* Checks that this node satisfies some easy to check invariants.
503+
* In particular, this method checks that
504+
* <ul>
505+
* <li>if-chains are nested correctly,
506+
* <li>the root is an {@link NodeType#IF} with the feature mapping {@code "true"},
507+
* <li>the feature mapping is {@code null} iff {@code isConditionalAnnotation} is {@code false}
508+
* and
509+
* <li>all edges are well-formed (e.g., edges can be inconsistent because edges are
510+
* double-linked).
511+
* </ul>
512+
*
513+
* <p>Some invariants are not checked. These include
514+
* <ul>
515+
* <li>There should be no cycles,
516+
* <li>{@link getID} should be unique in the whole variation tree, and
517+
* <li>children are not checked recursively.
518+
* </ul>
519+
* Use {@link VariationTree#assertConsistency} to check all children recursively.
520+
*
521+
* @see Assert#assertTrue
522+
* @throws AssertionError when an inconsistency is detected.
523+
*/
522524
public void assertConsistency() {
523525
// ELSE and ELIF nodes have an IF or ELIF as parent.
524526
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;
@@ -223,6 +224,17 @@ public String unparse() {
223224
return result.toString();
224225
}
225226

227+
/**
228+
* Checks whether this {@link VariationTree} is consistent.
229+
* Throws an error if this {@link VariationTree} is inconsistent (e.g., if there are multiple
230+
* {@link NodeType#ELSE} nodes). Has no side-effects otherwise.
231+
*
232+
* @see VariationNode#assertConsistency
233+
*/
234+
public void assertConsistency() {
235+
forAllPreorder(VariationTreeNode::assertConsistency);
236+
}
237+
226238
@Override
227239
public String toString() {
228240
return "variation tree from " + source;

0 commit comments

Comments
 (0)