Skip to content

Commit

Permalink
Using 'DiffTree' class instead of 'DiffNode' where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Jun 26, 2024
1 parent d14ca1d commit 8603d7a
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -36,7 +37,7 @@
*
* @since 1.1.5
*/
public final class DifferenceTreeBuilder {
public final class DiffTreeBuilder {
/**
* Default node info (to avoid null checks).
*/
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -155,7 +156,7 @@ public boolean deleteNode(final Node node) {
private static Map<Node, NodeInfo> buildNodeInfoMap(final DiffNode root) {
final Map<Node, NodeInfo> map = new HashMap<>();
map.put(root.getPrototype(), new NodeInfo(root, null));
DifferenceTreeBuilder.buildNodeInfoMap(map, root);
DiffTreeBuilder.buildNodeInfoMap(map, root);
return map;
}

Expand All @@ -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);
}
}
);
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/cqfn/astranaut/core/algorithms/hash/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Node> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
12 changes: 6 additions & 6 deletions src/main/java/org/cqfn/astranaut/core/base/ActionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
25 changes: 6 additions & 19 deletions src/main/java/org/cqfn/astranaut/core/base/DiffTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,46 +34,33 @@
*
* @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();
}

/**
* Returns the syntax tree before the changes were applied.
* @return The syntax tree before the changes
*/
public Tree getBefore() {
return new Tree(this.root.getBefore());
return new Tree(this.getRoot().getBefore());
}

/**
* Returns the syntax tree after the changes were applied.
* @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());
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/base/DraftNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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&lt;"data"&gt;,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
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/base/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ default void forEachChild(Consumer<Node> 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
*/
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/base/Tree.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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&lt;"data"&gt;,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<String, Set<Node>> nodes) {
return new Tree(DraftNode.create(description, nodes));
}

/**
* Analyzes the node and its children (recursively) to determine if a programming language
* name is specified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 8603d7a

Please sign in to comment.