Class VariationDiff<L extends Label>

java.lang.Object
org.variantsync.diffdetective.variation.diff.VariationDiff<L>
Type Parameters:
L - The type of label stored in this tree.

public class VariationDiff<L extends Label> extends Object
Implementation of variation tree diffs from our ESEC/FSE'22 paper. An instance of this class represents a variation tree diff. It stores the root of the graph as a DiffNode. It optionally holds a VariationDiffSource that describes how the variation tree diff was obtained. The graph structure is implemented by the DiffNode class.
Author:
Paul Bittner, Sören Viegener
  • Field Details

  • Constructor Details

    • VariationDiff

      public VariationDiff(DiffNode<L> root)
      Creates a VariationDiff that only consists of the single given root node.
      Parameters:
      root - The root of this tree.
    • VariationDiff

      public VariationDiff(DiffNode<L> root, VariationDiffSource source)
      Creates a VariationDiff that only consists of the single given root node. Remembers the given source as the tree's source of creation.
      Parameters:
      root - The root of this tree.
      source - The data from which the VariationDiff was created.
  • Method Details

    • fromFile

      public static VariationDiff<DiffLinesLabel> fromFile(Path p, VariationDiffParseOptions parseOptions) throws IOException, DiffParseException
      Parses a VariationDiff from the given file. The file should contain a text-based diff without any meta information. So just lines preceded by "+", "-", or " " are expected.
      Parameters:
      p - Path to a diff file.
      parseOptions - VariationDiffParseOptions for the parsing process.
      Returns:
      A result either containing the parsed VariationDiff or an error message in case of failure.
      Throws:
      IOException - when the given file could not be read for some reason.
      DiffParseException
    • fromDiff

      public static VariationDiff<DiffLinesLabel> fromDiff(String diff, VariationDiffParseOptions parseOptions) throws DiffParseException
      Parses a VariationDiff from the given unix diff. The file should contain a text-based diff without any meta information. So just lines preceded by "+", "-", or " " are expected.
      Parameters:
      diff - The diff as text. Lines should be separated by a newline character. Each line should be preceded by either "+", "-", or " ".
      parseOptions - VariationDiffParseOptions for the parsing process.
      Returns:
      A result either containing the parsed VariationDiff or an error message in case of failure.
      Throws:
      DiffParseException - if diff couldn't be parsed
    • fromPatch

      public static org.variantsync.functjonal.Result<VariationDiff<DiffLinesLabel>,List<DiffError>> fromPatch(PatchReference patchReference, Repository repository) throws IOException
      Parses a patch of a Git repository. Warning: The current implementation ignores patchReference.getParentCommitHash. It assumes that it's equal to the first parent of patchReference.getCommitHash, so it cannot parse patches across multiple commits.
      Parameters:
      patchReference - the patch to be parsed
      repository - the repository which contains the path patchReference
      Returns:
      a VariationDiff representing the referenced patch, or a list of errors encountered while trying to parse the VariationDiff
      Throws:
      IOException
    • fromFiles

      public static VariationDiff<DiffLinesLabel> fromFiles(Path beforeFile, Path afterFile, org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm algorithm, VariationDiffParseOptions options) throws IOException, DiffParseException
      Create a VariationDiff from two given text files.
      Throws:
      IOException
      DiffParseException
      See Also:
    • fromLines

      public static VariationDiff<DiffLinesLabel> fromLines(String before, String after, org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm algorithm, VariationDiffParseOptions options) throws IOException, DiffParseException
      Creates a variation diff from to line-based text inputs. This method just forwards to:
      Throws:
      IOException
      DiffParseException
      See Also:
    • fromTrees

      public static <L extends Label> VariationDiff<L> fromTrees(VariationTree<L> before, VariationTree<L> after)
      Create a VariationDiff by matching nodes between before and after with the default GumTree matcher.
      See Also:
    • project

      public VariationTree<L> project(Time t)
      Creates the projection of this variation diff at the given time. The returned value is a deep copy of the variation tree within this diff at the given time. If you instead wish to only have a view on the tree at the given diff have a look at DiffNode.projection(Time) for this trees root.
      Parameters:
      t - The time for which to project the variation tree.
    • forAll

      public VariationDiff<L> forAll(Consumer<DiffNode<L>> procedure)
      Invokes the given callback for each node in this VariationDiff.
      Parameters:
      procedure - callback
      Returns:
      this
    • traverse

      public VariationDiff<L> traverse(VariationDiffVisitor<L> visitor)
      Traverse this VariationDiff with the given visitor. When visiting a node, the visitor decides how to proceed.
      Parameters:
      visitor - visitor that is invoked on the root first and then decides how to proceed the traversal.
      Returns:
      this
    • allMatch

      public boolean allMatch(Predicate<DiffNode<L>> condition)
      Checks whether all nodes in this tree satisfy the given condition. The condition might not be invoked on every node in case it is not necessary.
      Parameters:
      condition - A condition to check on each node.
      Returns:
      True iff the given condition returns true for all nodes in this tree.
    • anyMatch

      public boolean anyMatch(Predicate<DiffNode<L>> condition)
      Checks whether any node in this tree satisfies the given condition. The condition might not be invoked on every node in case a node is found.
      Parameters:
      condition - A condition to check on each node.
      Returns:
      True iff the given condition returns true for at least one node in this tree.
    • noneMatch

      public boolean noneMatch(Predicate<DiffNode<L>> condition)
      Checks whether no node in this tree satisfies the given condition. The condition might not be invoked on every node.
      Parameters:
      condition - A condition to check on each node.
      Returns:
      True iff the given condition returns false for every node in this tree.
    • getRoot

      public DiffNode<L> getRoot()
      Returns the root node of this tree.
    • getNodeWithID

      public DiffNode<L> getNodeWithID(int id)
      Obtain the DiffNode with the given id in this VariationDiff.
      Parameters:
      id - The id of the node to search.
      Returns:
      The node with the given id if existing, null otherwise.
    • computeAllNodesThat

      public List<DiffNode<L>> computeAllNodesThat(Predicate<DiffNode<L>> property)
      Returns all nodes that satisfy the given predicate. Traverses the VariationDiff once.
      Parameters:
      property - Filter for nodes. Should return true if a node should be included.
      Returns:
      A List of all nodes satisfying the given predicate.
    • computeArtifactNodes

      public List<DiffNode<L>> computeArtifactNodes()
      Returns all artifact nodes of this VariationDiff.
      See Also:
    • computeAnnotationNodes

      public List<DiffNode<L>> computeAnnotationNodes()
      Returns all mapping nodes of this VariationDiff.
      See Also:
    • computeAllNodes

      public List<DiffNode<L>> computeAllNodes()
      Returns all nodes in this VariationDiff. Traverses the VariationDiff once.
    • count

      public int count(Predicate<DiffNode<L>> nodesToCount)
      Returns the number of nodes in this tree that satisfy the given condition.
      Parameters:
      nodesToCount - A condition that returns true for each node that should be counted.
      Returns:
      The number of nodes in this tree that satisfy the given condition.
    • setSource

      public void setSource(VariationDiffSource source)
      Sets the source of this VariationDiff.
      See Also:
    • getSource

      public VariationDiffSource getSource()
      Returns the source of this VariationDiff (i.e., the data this VariationDiff was created from).
      See Also:
    • computeSize

      public int computeSize()
      Returns the number of nodes in this VariationDiff.
    • isEmpty

      public boolean isEmpty()
      Returns true iff this tree is empty. The tree is considered empty if it only has a root or if it has no nodes at all.
    • removeNode

      public void removeNode(DiffNode<L> node)
      Removes the given node from the VariationDiff but keeps its children. The children are moved up, meaning that they are located below the parent of the given node afterwards.
      Parameters:
      node - The node to remove. Cannot be the root.
    • assertConsistency

      public void assertConsistency()
      Checks whether this VariationDiff is consistent. Throws an error when this VariationDiff is inconsistent (e.g., if it has cycles or an invalid internal state). Has no side-effects otherwise.
      See Also:
    • isConsistent

      public ConsistencyResult isConsistent()
      Checks whether this VariationDiff is consistent.
      Returns:
      A result that indicates either consistency or inconsistency.
    • isSameAs

      public boolean isSameAs(VariationDiff<L> b)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • deepCopy

      public VariationDiff<L> deepCopy()