diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilder.java b/src/main/java/org/cqfn/astranaut/core/algorithms/DiffTreeBuilder.java similarity index 90% rename from src/main/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilder.java rename to src/main/java/org/cqfn/astranaut/core/algorithms/DiffTreeBuilder.java index 940d213..e6df3e6 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilder.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/DiffTreeBuilder.java @@ -28,6 +28,7 @@ import org.cqfn.astranaut.core.algorithms.mapping.Mapper; import org.cqfn.astranaut.core.algorithms.mapping.Mapping; import org.cqfn.astranaut.core.base.DiffNode; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.base.Insertion; import org.cqfn.astranaut.core.base.Node; @@ -36,7 +37,7 @@ * * @since 1.1.5 */ -public final class DifferenceTreeBuilder { +public final class DiffTreeBuilder { /** * Default node info (to avoid null checks). */ @@ -58,9 +59,9 @@ public final class DifferenceTreeBuilder { * Constructor. * @param before Root node of an 'ordinary', non-difference original tree before the changes */ - public DifferenceTreeBuilder(final Node before) { + public DiffTreeBuilder(final Node before) { this.root = new DiffNode(before); - this.info = DifferenceTreeBuilder.buildNodeInfoMap(this.root); + this.info = DiffTreeBuilder.buildNodeInfoMap(this.root); } /** @@ -84,14 +85,6 @@ public boolean build(final Node after, final Mapper mapper) { return result; } - /** - * Returns root of resulting difference tree. - * @return Root node of difference tree - */ - public DiffNode getRoot() { - return this.root; - } - /** * Adds an action to the difference tree that inserts a node after another node. * If no other node is specified, inserts at the beginning of the children's list. @@ -102,12 +95,12 @@ public boolean insertNode(final Insertion insertion) { boolean result = false; DiffNode parent = this.info.getOrDefault( insertion.getInto(), - DifferenceTreeBuilder.DEFAULT_INFO + DiffTreeBuilder.DEFAULT_INFO ).getDiff(); if (parent == null) { parent = this.info.getOrDefault( insertion.getAfter(), - DifferenceTreeBuilder.DEFAULT_INFO + DiffTreeBuilder.DEFAULT_INFO ).getParent(); } if (parent != null) { @@ -125,7 +118,7 @@ public boolean insertNode(final Insertion insertion) { public boolean replaceNode(final Node node, final Node replacement) { boolean result = false; final DiffNode parent = - this.info.getOrDefault(node, DifferenceTreeBuilder.DEFAULT_INFO).getParent(); + this.info.getOrDefault(node, DiffTreeBuilder.DEFAULT_INFO).getParent(); if (parent != null) { result = parent.replaceNode(node, replacement); } @@ -140,13 +133,21 @@ public boolean replaceNode(final Node node, final Node replacement) { public boolean deleteNode(final Node node) { boolean result = false; final DiffNode parent = - this.info.getOrDefault(node, DifferenceTreeBuilder.DEFAULT_INFO).getParent(); + this.info.getOrDefault(node, DiffTreeBuilder.DEFAULT_INFO).getParent(); if (parent != null) { result = parent.deleteNode(node); } return result; } + /** + * Returns resulting difference tree. + * @return Difference tree + */ + public DiffTree getDiffTree() { + return new DiffTree(this.root); + } + /** * Builds the map containing relationship of the nodes to their parents. * @param root Root node @@ -155,7 +156,7 @@ public boolean deleteNode(final Node node) { private static Map buildNodeInfoMap(final DiffNode root) { final Map map = new HashMap<>(); map.put(root.getPrototype(), new NodeInfo(root, null)); - DifferenceTreeBuilder.buildNodeInfoMap(map, root); + DiffTreeBuilder.buildNodeInfoMap(map, root); return map; } @@ -172,7 +173,7 @@ private static void buildNodeInfoMap( if (child instanceof DiffNode) { final DiffNode node = (DiffNode) child; map.put(node.getPrototype(), new NodeInfo(node, parent)); - DifferenceTreeBuilder.buildNodeInfoMap(map, node); + DiffTreeBuilder.buildNodeInfoMap(map, node); } } ); diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/hash/Hash.java b/src/main/java/org/cqfn/astranaut/core/algorithms/hash/Hash.java index 962975f..1ff72a1 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/hash/Hash.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/hash/Hash.java @@ -24,6 +24,7 @@ package org.cqfn.astranaut.core.algorithms.hash; import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.Tree; /** * An interface that calculates the hash of a node. @@ -34,8 +35,21 @@ public interface Hash { /** * Calculates the hash of the node. - * @param node Node - * @return Hash of the node + * This method computes the hash value for the given node, which can be used + * to uniquely identify the node based on its content and structure. + * @param node Node to calculate the hash for. + * @return Hash value of the node */ int calculate(Node node); + + /** + * Calculates the hash of the tree. + * This default method computes the hash value for the entire tree by + * calculating the hash of its root node. + * @param tree Tree to calculate the hash for. + * @return Hash value of the tree, derived from its root node. + */ + default int calculate(Tree tree) { + return this.calculate(tree.getRoot()); + } } diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/patching/DefaultPatcher.java b/src/main/java/org/cqfn/astranaut/core/algorithms/patching/DefaultPatcher.java index d7e470c..f679048 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/patching/DefaultPatcher.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/patching/DefaultPatcher.java @@ -25,9 +25,10 @@ import java.util.Set; import org.cqfn.astranaut.core.base.ActionList; -import org.cqfn.astranaut.core.base.DiffNode; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.PatternNode; +import org.cqfn.astranaut.core.base.Tree; /** * Default algorithm that applies patches, i.e. makes some changes in the syntax tree @@ -37,15 +38,15 @@ */ public final class DefaultPatcher implements Patcher { @Override - public Node patch(final Node source, final PatternNode pattern) { - final PatternMatcher matcher = new PatternMatcher(source); + public Tree patch(final Tree source, final PatternNode pattern) { + final PatternMatcher matcher = new PatternMatcher(source.getRoot()); final Set nodes = matcher.match(pattern); - final Node result; + final Tree result; if (nodes.isEmpty()) { result = source; } else { final ActionList actions = matcher.getActionList(); - final DiffNode diff = actions.convertTreeToDifferenceTree(source); + final DiffTree diff = actions.convertTreeToDiffTree(source); result = diff.getAfter(); } return result; diff --git a/src/main/java/org/cqfn/astranaut/core/algorithms/patching/Patcher.java b/src/main/java/org/cqfn/astranaut/core/algorithms/patching/Patcher.java index dfce342..4afff0c 100644 --- a/src/main/java/org/cqfn/astranaut/core/algorithms/patching/Patcher.java +++ b/src/main/java/org/cqfn/astranaut/core/algorithms/patching/Patcher.java @@ -23,8 +23,8 @@ */ package org.cqfn.astranaut.core.algorithms.patching; -import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.PatternNode; +import org.cqfn.astranaut.core.base.Tree; /** * Interface to an algorithm that applies patches, i.e. makes some changes in the syntax tree @@ -35,9 +35,9 @@ public interface Patcher { /** * Applies a pattern to a syntax tree. - * @param source Root node of a syntax tree + * @param source Source syntax tree * @param pattern Root node af a pattern * @return Root node of updated syntax tree */ - Node patch(Node source, PatternNode pattern); + Tree patch(Tree source, PatternNode pattern); } diff --git a/src/main/java/org/cqfn/astranaut/core/base/ActionList.java b/src/main/java/org/cqfn/astranaut/core/base/ActionList.java index e09264d..ebe3532 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/ActionList.java +++ b/src/main/java/org/cqfn/astranaut/core/base/ActionList.java @@ -27,7 +27,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.cqfn.astranaut.core.algorithms.DifferenceTreeBuilder; +import org.cqfn.astranaut.core.algorithms.DiffTreeBuilder; /** * List of actions to be added to the tree after deserialization to produce a difference tree. @@ -96,11 +96,11 @@ public void deleteNode(final Node node) { /** * Converts the tree to a difference tree using the list of actions. - * @param root Root node of the tree - * @return Root node of a difference tree + * @param tree Source tree + * @return Difference tree */ - public DiffNode convertTreeToDifferenceTree(final Node root) { - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(root); + public DiffTree convertTreeToDiffTree(final Tree tree) { + final DiffTreeBuilder builder = new DiffTreeBuilder(tree.getRoot()); for (final Insertion insertion : this.insert) { builder.insertNode(insertion); } @@ -110,6 +110,6 @@ public DiffNode convertTreeToDifferenceTree(final Node root) { for (final Node node : this.delete) { builder.deleteNode(node); } - return builder.getRoot(); + return builder.getDiffTree(); } } diff --git a/src/main/java/org/cqfn/astranaut/core/base/DiffTree.java b/src/main/java/org/cqfn/astranaut/core/base/DiffTree.java index d4e63a7..e1cb199 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/DiffTree.java +++ b/src/main/java/org/cqfn/astranaut/core/base/DiffTree.java @@ -34,26 +34,18 @@ * * @since 2.0.0 */ -public class DiffTree { - /** - * Root node of the difference tree. - */ - private final DiffNode root; - +public final class DiffTree extends Tree { /** * Constructor. * @param root Root node the difference tree */ public DiffTree(final DiffNode root) { - this.root = root; + super(root); } - /** - * Returns the root node of the tree. - * @return Root node of the tree - */ + @Override public DiffNode getRoot() { - return this.root; + return (DiffNode) super.getRoot(); } /** @@ -61,7 +53,7 @@ public DiffNode getRoot() { * @return The syntax tree before the changes */ public Tree getBefore() { - return new Tree(this.root.getBefore()); + return new Tree(this.getRoot().getBefore()); } /** @@ -69,11 +61,6 @@ public Tree getBefore() { * @return The syntax tree after the changes */ public Tree getAfter() { - return new Tree(this.root.getAfter()); - } - - @Override - public final String toString() { - return this.root.toString(); + return new Tree(this.getRoot().getAfter()); } } diff --git a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java index 3e854c5..d69bd58 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java +++ b/src/main/java/org/cqfn/astranaut/core/base/DraftNode.java @@ -119,7 +119,7 @@ public String toString() { /** * Creates a tree from draft nodes based on description. - * Description format: A(B,C(...),...) where 'A' is the type name + * Description format: A(B<"data">,C(...),...) where 'A' is the type name * (it consists only of letters) followed by child nodes (in the same format) in parentheses * separated by commas. * @param description Description diff --git a/src/main/java/org/cqfn/astranaut/core/base/Node.java b/src/main/java/org/cqfn/astranaut/core/base/Node.java index f23e011..550660a 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/Node.java +++ b/src/main/java/org/cqfn/astranaut/core/base/Node.java @@ -134,7 +134,7 @@ default void forEachChild(Consumer action) { /** * Performs a deep comparison of a node with another node, - * i.e., compares nodes, as well as recursively all children of nodes one-to-one. + * i.e., compares nodes, as well as recursively all children of nodes one-to-one. * @param other Other node * @return Comparison result, {@code true} if the nodes are equal */ diff --git a/src/main/java/org/cqfn/astranaut/core/base/Tree.java b/src/main/java/org/cqfn/astranaut/core/base/Tree.java index 4ebde0a..cfd1512 100644 --- a/src/main/java/org/cqfn/astranaut/core/base/Tree.java +++ b/src/main/java/org/cqfn/astranaut/core/base/Tree.java @@ -23,6 +23,9 @@ */ package org.cqfn.astranaut.core.base; +import java.util.Map; +import java.util.Set; + /** * A syntax tree, which represents the hierarchical structure of source code, * depicting the syntactic structure and relationships between code elements. @@ -66,6 +69,43 @@ public final String toString() { return this.root.toString(); } + /** + * Performs a deep comparison of a tree with another tree, + * i.e., compares root nodes, as well as recursively all children of nodes one-to-one. + * @param other Other node + * @return Comparison result, {@code true} if the nodes are equal + */ + public boolean deepCompare(final Tree other) { + return this.root.deepCompare(other.root); + } + + /** + * Creates a tree from draft nodes based on description. + * Description format: A(B,C(...),...) where 'A' is the type name + * (it consists only of letters) followed by child nodes (in the same format) in parentheses + * separated by commas. + * @param description Description + * @return Tree created by description + */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") + public static Tree createDraft(final String description) { + return new Tree(DraftNode.create(description)); + } + + /** + * Creates a tree from draft nodes based on description. + * Description format: A(B<"data">,C(...),...) where 'A' is the type name + * (it consists only of letters) followed by child nodes (in the same format) in parentheses + * separated by commas. + * @param description Description + * @param nodes Collection in which to place the nodes to be created, sorted by type name + * @return Tree created by description + */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") + public static Tree createDraft(final String description, final Map> nodes) { + return new Tree(DraftNode.create(description, nodes)); + } + /** * Analyzes the node and its children (recursively) to determine if a programming language * name is specified. diff --git a/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java b/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java index 978bf4b..9358f5e 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java @@ -27,7 +27,7 @@ import com.kniazkov.json.JsonException; import org.cqfn.astranaut.core.base.EmptyTree; import org.cqfn.astranaut.core.base.FactorySelector; -import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.Tree; import org.cqfn.astranaut.core.utils.deserializer.TreeDescriptor; /** @@ -60,8 +60,8 @@ public JsonDeserializer(final String source, final FactorySelector selector) { * Converts the source string contains JSON object to a syntax tree. * @return Root node */ - public Node convert() { - Node result = EmptyTree.INSTANCE; + public Tree convert() { + Tree result = new Tree(EmptyTree.INSTANCE); try { final TreeDescriptor tree = Json.parse(this.source, TreeDescriptor.class); if (tree != null) { diff --git a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java index 84d76ee..151bbac 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java @@ -27,7 +27,7 @@ import org.cqfn.astranaut.core.base.EmptyTree; import org.cqfn.astranaut.core.base.Factory; import org.cqfn.astranaut.core.base.FactorySelector; -import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.Tree; /** * Tree descriptor represented as it is stored in the JSON file. @@ -57,14 +57,14 @@ public TreeDescriptor() { * @param selector The node factory selector * @return A root node */ - public Node convert(final FactorySelector selector) { - Node result = EmptyTree.INSTANCE; + public Tree convert(final FactorySelector selector) { + Tree result = new Tree(EmptyTree.INSTANCE); final Factory factory = selector.select(this.language); if (factory != null) { final ActionList actions = new ActionList(); - result = this.root.convert(factory, actions); + result = new Tree(this.root.convert(factory, actions)); if (actions.hasActions()) { - result = actions.convertTreeToDifferenceTree(result); + result = actions.convertTreeToDiffTree(result); } } return result; diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java index 15effe8..2d6a92c 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/DepthTest.java @@ -23,7 +23,7 @@ */ package org.cqfn.astranaut.core.algorithms; -import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.example.LittleTrees; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -36,11 +36,11 @@ class DepthTest { @Test void test() { - final Node tree = LittleTrees.createTreeWithDeleteAction(); + final DiffTree tree = LittleTrees.createTreeWithDeleteAction(); final Depth depth = new Depth(); - int value = depth.calculate(tree); + int value = depth.calculate(tree.getRoot()); Assertions.assertEquals(5, value); - value = depth.calculate(tree.getChild(0)); + value = depth.calculate(tree.getRoot().getChild(0)); Assertions.assertEquals(3, value); } } diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilderTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilderTest.java index 4ac73c8..2a5abf1 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilderTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/DifferenceTreeBuilderTest.java @@ -26,7 +26,7 @@ import org.cqfn.astranaut.core.algorithms.hash.AbsoluteHash; import org.cqfn.astranaut.core.algorithms.hash.Hash; import org.cqfn.astranaut.core.algorithms.mapping.TopDownMapper; -import org.cqfn.astranaut.core.base.DiffNode; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.base.DraftNode; import org.cqfn.astranaut.core.base.Insertion; import org.cqfn.astranaut.core.base.Node; @@ -35,7 +35,7 @@ import org.junit.jupiter.api.Test; /** - * Testing {@link DifferenceTreeBuilder} class. + * Testing {@link DiffTreeBuilder} class. * * @since 1.1.0 */ @@ -49,14 +49,14 @@ void testTreeWithInsertedNode() { final Node after = LittleTrees.createStatementListWithThreeChildren( LittleTrees.createIntegerLiteral(3) ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before); + final DiffTreeBuilder builder = new DiffTreeBuilder(before); final boolean result = builder.build(after, TopDownMapper.INSTANCE); Assertions.assertTrue(result); - final DiffNode diff = builder.getRoot(); - final Node expected = LittleTrees.createTreeWithInsertAction(); + final DiffTree diff = builder.getDiffTree(); + final DiffTree expected = LittleTrees.createTreeWithInsertAction(); Assertions.assertTrue(expected.deepCompare(diff)); - Assertions.assertTrue(before.deepCompare(diff.getBefore())); - Assertions.assertTrue(after.deepCompare(diff.getAfter())); + Assertions.assertTrue(before.deepCompare(diff.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(diff.getAfter().getRoot())); } /** @@ -70,18 +70,18 @@ void testTreeWithReplacedNode() { final Node after = LittleTrees.createStatementListWithThreeChildren( LittleTrees.createVariable("x") ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before); + final DiffTreeBuilder builder = new DiffTreeBuilder(before); final boolean result = builder.build(after, TopDownMapper.INSTANCE); Assertions.assertTrue(result); - final DiffNode diff = builder.getRoot(); - final Node expected = LittleTrees.createTreeWithReplaceAction(); + final DiffTree diff = builder.getDiffTree(); + final DiffTree expected = LittleTrees.createTreeWithReplaceAction(); final Hash hash = new AbsoluteHash(); final int diffhash = hash.calculate(diff); final int expectedhash = hash.calculate(expected); Assertions.assertEquals(expectedhash, diffhash); Assertions.assertTrue(expected.deepCompare(diff)); - Assertions.assertTrue(before.deepCompare(diff.getBefore())); - Assertions.assertTrue(after.deepCompare(diff.getAfter())); + Assertions.assertTrue(before.deepCompare(diff.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(diff.getAfter().getRoot())); } /** @@ -93,14 +93,14 @@ void testTreeWithDeletedNode() { LittleTrees.createIntegerLiteral(2) ); final Node after = LittleTrees.createStatementListWithTwoChildren(); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before); + final DiffTreeBuilder builder = new DiffTreeBuilder(before); final boolean result = builder.build(after, TopDownMapper.INSTANCE); Assertions.assertTrue(result); - final DiffNode diff = builder.getRoot(); - final Node expected = LittleTrees.createTreeWithDeleteAction(); + final DiffTree diff = builder.getDiffTree(); + final DiffTree expected = LittleTrees.createTreeWithDeleteAction(); Assertions.assertTrue(expected.deepCompare(diff)); - Assertions.assertTrue(before.deepCompare(diff.getBefore())); - Assertions.assertTrue(after.deepCompare(diff.getAfter())); + Assertions.assertTrue(before.deepCompare(diff.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(diff.getAfter().getRoot())); } /** @@ -118,14 +118,14 @@ void testTreeWithDeletedNodeInDepth() { final Node after = LittleTrees.createStatementBlock( LittleTrees.createStatementListWithTwoChildren() ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before); + final DiffTreeBuilder builder = new DiffTreeBuilder(before); final boolean result = builder.build(after, TopDownMapper.INSTANCE); Assertions.assertTrue(result); - final DiffNode diff = builder.getRoot(); - final Node expected = LittleTrees.createTreeWithDeleteActionInDepth(); + final DiffTree diff = builder.getDiffTree(); + final DiffTree expected = LittleTrees.createTreeWithDeleteActionInDepth(); Assertions.assertTrue(expected.deepCompare(diff)); - Assertions.assertTrue(before.deepCompare(diff.getBefore())); - Assertions.assertTrue(after.deepCompare(diff.getAfter())); + Assertions.assertTrue(before.deepCompare(diff.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(diff.getAfter().getRoot())); } @Test @@ -172,16 +172,16 @@ void testTreeWithReplacedNotUniqueNode() { ) ) ); - final DifferenceTreeBuilder first = new DifferenceTreeBuilder(before); + final DiffTreeBuilder first = new DiffTreeBuilder(before); first.replaceNode(removed, added); - final DiffNode expected = first.getRoot(); - Assertions.assertTrue(before.deepCompare(expected.getBefore())); - Assertions.assertTrue(after.deepCompare(expected.getAfter())); - final DifferenceTreeBuilder second = new DifferenceTreeBuilder(before); + final DiffTree expected = first.getDiffTree(); + Assertions.assertTrue(before.deepCompare(expected.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(expected.getAfter().getRoot())); + final DiffTreeBuilder second = new DiffTreeBuilder(before); second.build(after, TopDownMapper.INSTANCE); - final DiffNode actual = second.getRoot(); - Assertions.assertTrue(before.deepCompare(actual.getBefore())); - Assertions.assertTrue(after.deepCompare(actual.getAfter())); + final DiffTree actual = second.getDiffTree(); + Assertions.assertTrue(before.deepCompare(actual.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(actual.getAfter().getRoot())); Assertions.assertTrue(actual.deepCompare(expected)); } @@ -189,17 +189,17 @@ void testTreeWithReplacedNotUniqueNode() { void testComplexCase() { final Node before = DraftNode.create("X(A,B,Y(C,D,E,F,J,K))"); final Node after = DraftNode.create("X(A,G,Y(H,C,I,E,J,K))"); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(before); + final DiffTreeBuilder builder = new DiffTreeBuilder(before); final boolean result = builder.build(after, TopDownMapper.INSTANCE); Assertions.assertTrue(result); - final DiffNode diff = builder.getRoot(); - Assertions.assertTrue(before.deepCompare(diff.getBefore())); - Assertions.assertTrue(after.deepCompare(diff.getAfter())); + final DiffTree diff = builder.getDiffTree(); + Assertions.assertTrue(before.deepCompare(diff.getBefore().getRoot())); + Assertions.assertTrue(after.deepCompare(diff.getAfter().getRoot())); } @Test void testWrongInsertion() { - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( DraftNode.create("X") ); final boolean result = builder.insertNode( @@ -213,7 +213,7 @@ void testWrongInsertion() { @Test void testWrongReplacement() { - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( DraftNode.create("X") ); final boolean result = builder.replaceNode( @@ -225,7 +225,7 @@ void testWrongReplacement() { @Test void testWrongDeletion() { - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( DraftNode.create("X") ); final boolean result = builder.deleteNode(DraftNode.create("A")); diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/hash/AbsoluteHashTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/hash/AbsoluteHashTest.java index ba9939d..96a28d9 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/hash/AbsoluteHashTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/hash/AbsoluteHashTest.java @@ -23,7 +23,7 @@ */ package org.cqfn.astranaut.core.algorithms.hash; -import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.example.LittleTrees; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -40,13 +40,13 @@ class AbsoluteHashTest { @Test void testingEqualityFeature() { final Hash hash = new AbsoluteHash(); - final Node first = LittleTrees.createTreeWithDeleteAction(); - final Node second = LittleTrees.createTreeWithDeleteAction(); + final DiffTree first = LittleTrees.createTreeWithDeleteAction(); + final DiffTree second = LittleTrees.createTreeWithDeleteAction(); int expected = hash.calculate(first); int actual = hash.calculate(second); Assertions.assertEquals(expected, actual); - expected = hash.calculate(first.getChild(0)); - actual = hash.calculate(second.getChild(0)); + expected = hash.calculate(first.getRoot().getChild(0)); + actual = hash.calculate(second.getRoot().getChild(0)); Assertions.assertEquals(expected, actual); } } diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatcherTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatcherTest.java index 8df2eec..1c1fd08 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatcherTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatcherTest.java @@ -28,7 +28,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; -import org.cqfn.astranaut.core.algorithms.DifferenceTreeBuilder; +import org.cqfn.astranaut.core.algorithms.DiffTreeBuilder; import org.cqfn.astranaut.core.algorithms.PatternBuilder; import org.cqfn.astranaut.core.base.Builder; import org.cqfn.astranaut.core.base.DiffNode; @@ -36,6 +36,7 @@ import org.cqfn.astranaut.core.base.Insertion; import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.PatternNode; +import org.cqfn.astranaut.core.base.Tree; import org.cqfn.astranaut.core.example.green.Addition; import org.cqfn.astranaut.core.example.green.ExpressionStatement; import org.cqfn.astranaut.core.example.green.IntegerLiteral; @@ -52,14 +53,14 @@ class PatcherTest { @Test void patchingByPatternThatDoesNotMatch() { - final Node source = DraftNode.create("A(B,C,D)"); + final Tree source = Tree.createDraft("A(B,C,D)"); final PatternNode pattern = new PatternNode( new DiffNode( DraftNode.create("D") ) ); final Patcher patcher = new DefaultPatcher(); - final Node result = patcher.patch(source, pattern); + final Tree result = patcher.patch(source, pattern); Assertions.assertTrue(source.deepCompare(result)); } @@ -67,7 +68,7 @@ void patchingByPatternThatDoesNotMatch() { void patchPatternWithInsertion() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.insertNode( new Insertion( DraftNode.create("C"), @@ -75,11 +76,11 @@ void patchPatternWithInsertion() { nodes.get("B").iterator().next() ) ); - final PatternNode pattern = new PatternNode(builder.getRoot()); - final Node tree = DraftNode.create("X(Y,A(B),Z)"); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); + final Tree tree = Tree.createDraft("X(Y,A(B),Z)"); final Patcher patcher = new DefaultPatcher(); - final Node result = patcher.patch(tree, pattern); - final Node expected = DraftNode.create("X(Y,A(B,C),Z)"); + final Tree result = patcher.patch(tree, pattern); + final Tree expected = Tree.createDraft("X(Y,A(B,C),Z)"); Assertions.assertTrue(expected.deepCompare(result)); } @@ -87,13 +88,13 @@ void patchPatternWithInsertion() { void patchPatternWithReplacement() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B, D)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.replaceNode(nodes.get("B").iterator().next(), DraftNode.create("C")); - final PatternNode pattern = new PatternNode(builder.getRoot()); - final Node tree = DraftNode.create("X(Y,A(B,D),Z)"); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); + final Tree tree = Tree.createDraft("X(Y,A(B,D),Z)"); final Patcher patcher = new DefaultPatcher(); - final Node result = patcher.patch(tree, pattern); - final Node expected = DraftNode.create("X(Y,A(C,D),Z)"); + final Tree result = patcher.patch(tree, pattern); + final Tree expected = Tree.createDraft("X(Y,A(C,D),Z)"); Assertions.assertTrue(expected.deepCompare(result)); } @@ -101,13 +102,13 @@ void patchPatternWithReplacement() { void patchPatternWithDeletion() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B, D)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.deleteNode(nodes.get("B").iterator().next()); - final PatternNode pattern = new PatternNode(builder.getRoot()); - final Node tree = DraftNode.create("X(Y,A(B,D),Z)"); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); + final Tree tree = Tree.createDraft("X(Y,A(B,D),Z)"); final Patcher patcher = new DefaultPatcher(); - final Node result = patcher.patch(tree, pattern); - final Node expected = DraftNode.create("X(Y,A(D),Z)"); + final Tree result = patcher.patch(tree, pattern); + final Tree expected = Tree.createDraft("X(Y,A(D),Z)"); Assertions.assertTrue(expected.deepCompare(result)); } @@ -133,7 +134,7 @@ void patchPatternWithHole() { final Node stmt = ctor.createNode(); final Patcher patcher = new DefaultPatcher(); final PatternNode pattern = PatcherTest.createPatternWithHole(); - final Node patched = patcher.patch(stmt, pattern); + final Tree patched = patcher.patch(new Tree(stmt), pattern); ctor = new Variable.Constructor(); ctor.setData("a"); first = ctor.createNode(); @@ -152,19 +153,19 @@ void patchPatternWithHole() { ctor = new ExpressionStatement.Constructor(); ctor.setChildrenList(Collections.singletonList(assignment)); final Node expected = ctor.createNode(); - Assertions.assertTrue(expected.deepCompare(patched)); + Assertions.assertTrue(expected.deepCompare(patched.getRoot())); } @Test void patchWithPatternThatDoesNotMatch() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("E(B,D)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.replaceNode(nodes.get("B").iterator().next(), DraftNode.create("C")); - final PatternNode pattern = new PatternNode(builder.getRoot()); - final Node tree = DraftNode.create("X(Y,K(B,D),Z)"); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); + final Tree tree = Tree.createDraft("X(Y,K(B,D),Z)"); final Patcher patcher = new DefaultPatcher(); - final Node result = patcher.patch(tree, pattern); + final Tree result = patcher.patch(tree, pattern); Assertions.assertTrue(tree.deepCompare(result)); } @@ -185,9 +186,9 @@ private static PatternNode createPatternWithHole() { ctor = new IntegerLiteral.Constructor(); ctor.setData("2"); final Node replacement = ctor.createNode(); - final DifferenceTreeBuilder dtbld = new DifferenceTreeBuilder(addition); + final DiffTreeBuilder dtbld = new DiffTreeBuilder(addition); dtbld.replaceNode(second, replacement); - final PatternBuilder pbld = new PatternBuilder(dtbld.getRoot()); + final PatternBuilder pbld = new PatternBuilder(dtbld.getDiffTree().getRoot()); pbld.makeHole(first, 0); final PatternNode pattern = pbld.getRoot(); Assertions.assertNotNull(pattern); diff --git a/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatternMatcherTest.java b/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatternMatcherTest.java index 05772a8..590e28c 100644 --- a/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatternMatcherTest.java +++ b/src/test/java/org/cqfn/astranaut/core/algorithms/patching/PatternMatcherTest.java @@ -26,7 +26,7 @@ import java.util.Map; import java.util.Set; import java.util.TreeMap; -import org.cqfn.astranaut.core.algorithms.DifferenceTreeBuilder; +import org.cqfn.astranaut.core.algorithms.DiffTreeBuilder; import org.cqfn.astranaut.core.algorithms.PatternBuilder; import org.cqfn.astranaut.core.base.DiffNode; import org.cqfn.astranaut.core.base.DraftNode; @@ -74,7 +74,7 @@ void findReducedSubtreeInATree() { void findPatternWithInsertionInATree() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.insertNode( new Insertion( DraftNode.create("C"), @@ -82,7 +82,7 @@ void findPatternWithInsertionInATree() { nodes.get("B").iterator().next() ) ); - final PatternNode pattern = new PatternNode(builder.getRoot()); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); final Node tree = DraftNode.create("X(Y,A(B),Z)"); final PatternMatcher matcher = new PatternMatcher(tree); final Set found = matcher.match(pattern); @@ -95,9 +95,9 @@ void findPatternWithInsertionInATree() { void findPatternWithReplacementInATree() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B, D)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.replaceNode(nodes.get("B").iterator().next(), DraftNode.create("C")); - final PatternNode pattern = new PatternNode(builder.getRoot()); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); final Node tree = DraftNode.create("X(Y,A(B,D),Z)"); final PatternMatcher matcher = new PatternMatcher(tree); final Set found = matcher.match(pattern); @@ -110,9 +110,9 @@ void findPatternWithReplacementInATree() { void findPatternWithDeletionInATree() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B, D)", nodes); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder builder = new DiffTreeBuilder(prepattern); builder.deleteNode(nodes.get("B").iterator().next()); - final PatternNode pattern = new PatternNode(builder.getRoot()); + final PatternNode pattern = new PatternNode(builder.getDiffTree().getRoot()); final Node tree = DraftNode.create("X(Y,A(B,D),Z)"); final PatternMatcher matcher = new PatternMatcher(tree); final Set found = matcher.match(pattern); @@ -125,9 +125,9 @@ void findPatternWithDeletionInATree() { void findPatternWithHoleInATree() { final Map> nodes = new TreeMap<>(); final Node prepattern = DraftNode.create("A(B, D<\"7\">)", nodes); - final DifferenceTreeBuilder dtbuilder = new DifferenceTreeBuilder(prepattern); + final DiffTreeBuilder dtbuilder = new DiffTreeBuilder(prepattern); dtbuilder.deleteNode(nodes.get("B").iterator().next()); - final PatternBuilder pbuilder = new PatternBuilder(dtbuilder.getRoot()); + final PatternBuilder pbuilder = new PatternBuilder(dtbuilder.getDiffTree().getRoot()); final boolean flag = pbuilder.makeHole(nodes.get("D").iterator().next(), 0); Assertions.assertTrue(flag); final PatternNode pattern = pbuilder.getRoot(); diff --git a/src/test/java/org/cqfn/astranaut/core/base/DifferenceNodeTest.java b/src/test/java/org/cqfn/astranaut/core/base/DifferenceNodeTest.java index 91ceca7..519657f 100644 --- a/src/test/java/org/cqfn/astranaut/core/base/DifferenceNodeTest.java +++ b/src/test/java/org/cqfn/astranaut/core/base/DifferenceNodeTest.java @@ -259,7 +259,8 @@ private Node loadTree(final String name) { source, language -> GreenFactory.INSTANCE ); - final Node root = deserializer.convert(); + final Tree tree = deserializer.convert(); + final Node root = tree.getRoot(); Assertions.assertNotEquals(EmptyTree.INSTANCE, root); return root; } diff --git a/src/test/java/org/cqfn/astranaut/core/example/LittleTrees.java b/src/test/java/org/cqfn/astranaut/core/example/LittleTrees.java index 41fa3fe..eb83909 100644 --- a/src/test/java/org/cqfn/astranaut/core/example/LittleTrees.java +++ b/src/test/java/org/cqfn/astranaut/core/example/LittleTrees.java @@ -25,8 +25,8 @@ import java.util.Arrays; import java.util.Collections; -import org.cqfn.astranaut.core.algorithms.DifferenceTreeBuilder; -import org.cqfn.astranaut.core.base.DiffNode; +import org.cqfn.astranaut.core.algorithms.DiffTreeBuilder; +import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.base.EmptyTree; import org.cqfn.astranaut.core.base.Insertion; import org.cqfn.astranaut.core.base.Node; @@ -207,7 +207,7 @@ public static Node createStatementListWithThreeChildren(final Node assignable) { * Creates a tree that has a "insert" action in it. * @return Root node */ - public static DiffNode createTreeWithInsertAction() { + public static DiffTree createTreeWithInsertAction() { final Node after = wrapExpressionWithStatement( createAssignment( @@ -221,7 +221,7 @@ public static DiffNode createTreeWithInsertAction() { createIntegerLiteral(3) ) ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( createStatementBlock( after, createReturnStatement( @@ -230,17 +230,17 @@ public static DiffNode createTreeWithInsertAction() { ) ); builder.insertNode(new Insertion(inserted, after)); - return builder.getRoot(); + return builder.getDiffTree(); } /** * Creates a tree that has a "replace" action in it. * @return Root node */ - public static DiffNode createTreeWithReplaceAction() { + public static DiffTree createTreeWithReplaceAction() { final Node before = createIntegerLiteral(2); final Node after = createVariable("x"); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( createStatementBlock( wrapExpressionWithStatement( createAssignment( @@ -260,21 +260,21 @@ public static DiffNode createTreeWithReplaceAction() { ) ); builder.replaceNode(before, after); - return builder.getRoot(); + return builder.getDiffTree(); } /** * Creates a tree that has a "delete" action in it. * @return Root node */ - public static DiffNode createTreeWithDeleteAction() { + public static DiffTree createTreeWithDeleteAction() { final Node victim = wrapExpressionWithStatement( createAssignment( createVariable("y"), createIntegerLiteral(2) ) ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( createStatementBlock( wrapExpressionWithStatement( createAssignment( @@ -289,7 +289,7 @@ public static DiffNode createTreeWithDeleteAction() { ) ); builder.deleteNode(victim); - return builder.getRoot(); + return builder.getDiffTree(); } /** @@ -298,14 +298,14 @@ public static DiffNode createTreeWithDeleteAction() { * do not change. * @return Root node */ - public static DiffNode createTreeWithDeleteActionInDepth() { + public static DiffTree createTreeWithDeleteActionInDepth() { final Node victim = wrapExpressionWithStatement( createAssignment( createVariable("y"), createIntegerLiteral(2) ) ); - final DifferenceTreeBuilder builder = new DifferenceTreeBuilder( + final DiffTreeBuilder builder = new DiffTreeBuilder( createStatementBlock( createStatementBlock( wrapExpressionWithStatement( @@ -322,6 +322,6 @@ public static DiffNode createTreeWithDeleteActionInDepth() { ) ); builder.deleteNode(victim); - return builder.getRoot(); + return builder.getDiffTree(); } } diff --git a/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java b/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java index 2ada442..8df9b52 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/JsonDeserializerTest.java @@ -28,6 +28,7 @@ import java.util.TreeMap; import org.cqfn.astranaut.core.base.Factory; import org.cqfn.astranaut.core.base.Node; +import org.cqfn.astranaut.core.base.Tree; import org.cqfn.astranaut.core.base.Type; import org.cqfn.astranaut.core.example.LittleTrees; import org.cqfn.astranaut.core.example.green.Addition; @@ -71,15 +72,16 @@ void testDeserialization() { source, language -> new Factory(types) ); - final Node result = deserializer.convert(); - Assertions.assertEquals(JsonDeserializerTest.ADD, result.getTypeName()); - Assertions.assertEquals("", result.getData()); - Assertions.assertEquals(2, result.getChildCount()); - final Node first = result.getChild(0); + final Tree result = deserializer.convert(); + final Node root = result.getRoot(); + Assertions.assertEquals(JsonDeserializerTest.ADD, root.getTypeName()); + Assertions.assertEquals("", root.getData()); + Assertions.assertEquals(2, root.getChildCount()); + final Node first = root.getChild(0); Assertions.assertEquals(JsonDeserializerTest.INT_LITERAL, first.getTypeName()); Assertions.assertEquals("2", first.getData()); Assertions.assertEquals(0, first.getChildCount()); - final Node second = result.getChild(1); + final Node second = root.getChild(1); Assertions.assertEquals(JsonDeserializerTest.INT_LITERAL, second.getTypeName()); Assertions.assertEquals("3", second.getData()); Assertions.assertEquals(0, second.getChildCount()); @@ -96,9 +98,9 @@ void loadIntoDraftNode() { source, language -> new Factory(Collections.emptyMap()) ); - final Node node = deserializer.convert(); - Assertions.assertNotNull(node); - Assertions.assertEquals("Example", node.getTypeName()); + final Tree tree = deserializer.convert(); + Assertions.assertNotNull(tree); + Assertions.assertEquals("Example", tree.getRoot().getTypeName()); } /** @@ -111,8 +113,8 @@ void loadTreeWithActions() { source, language -> GreenFactory.INSTANCE ); - final Node actual = deserializer.convert(); - final Node expected = LittleTrees.createTreeWithDeleteAction(); + final Tree actual = deserializer.convert(); + final Tree expected = LittleTrees.createTreeWithDeleteAction(); Assertions.assertTrue(expected.deepCompare(actual)); } diff --git a/src/test/java/org/cqfn/astranaut/core/utils/JsonSerializerTest.java b/src/test/java/org/cqfn/astranaut/core/utils/JsonSerializerTest.java index f287c41..56ea012 100644 --- a/src/test/java/org/cqfn/astranaut/core/utils/JsonSerializerTest.java +++ b/src/test/java/org/cqfn/astranaut/core/utils/JsonSerializerTest.java @@ -92,7 +92,7 @@ void testSerializationWithLanguageSpecified() { @Test void testSerializationTreeWithAction() { final boolean result = this.serializeAndCompare( - LittleTrees.createTreeWithDeleteAction(), + LittleTrees.createTreeWithDeleteAction().getRoot(), "tree_containing_delete_action.json" ); Assertions.assertTrue(result);