Skip to content

Commit

Permalink
Rename algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Jun 29, 2024
1 parent 6e6ab95 commit 994ae17
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @since 1.1.0
*/
public final class Depth {
public final class DepthCalculator {
/**
* A table with the calculated values.
* Since nodes are immutable, depth calculated once for a node will never change.
Expand All @@ -42,7 +42,7 @@ public final class Depth {
/**
* Constructor.
*/
public Depth() {
public DepthCalculator() {
this.calculated = new HashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import org.cqfn.astranaut.core.base.Node;

/**
* Performs a deep traversal of the syntax tree.
* Performs a depth-first traversal of the syntax tree.
*
* @since 1.1.5
*/
public class DeepTraversal {
public class DepthFirstWalker {
/**
* The root node of the tree being traversed.
*/
Expand All @@ -43,7 +43,7 @@ public class DeepTraversal {
* Constructor.
* @param root The root node of the tree being traversed
*/
public DeepTraversal(final Node root) {
public DepthFirstWalker(final Node root) {
this.root = root;
}

Expand All @@ -58,7 +58,7 @@ public DeepTraversal(final Node root) {
* @return Found node (optional)
*/
public Optional<Node> findFirst(final Visitor visitor) {
return Optional.ofNullable(DeepTraversal.findFirst(this.root, visitor));
return Optional.ofNullable(DepthFirstWalker.findFirst(this.root, visitor));
}

/**
Expand All @@ -70,7 +70,7 @@ public Optional<Node> findFirst(final Visitor visitor) {
*/
public List<Node> findAll(final Visitor visitor) {
final List<Node> list = new ArrayList<>(0);
DeepTraversal.findAll(this.root, visitor, list);
DepthFirstWalker.findAll(this.root, visitor, list);
return list;
}

Expand All @@ -88,7 +88,7 @@ private static Node findFirst(final Node node, final Visitor visitor) {
} else {
final int count = node.getChildCount();
for (int index = 0; index < count && result == null; index = index + 1) {
result = DeepTraversal.findFirst(node.getChild(index), visitor);
result = DepthFirstWalker.findFirst(node.getChild(index), visitor);
}
}
return result;
Expand All @@ -107,7 +107,7 @@ private static void findAll(final Node node, final Visitor visitor, final List<N
} else {
final int count = node.getChildCount();
for (int index = 0; index < count; index = index + 1) {
DeepTraversal.findAll(node.getChild(index), visitor, list);
DepthFirstWalker.findAll(node.getChild(index), visitor, list);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*
* @since 1.1.5
*/
public class Identical {
public class IdenticalNodeFinder {
/**
* The root of the tree to search in.
*/
Expand All @@ -53,7 +53,7 @@ public class Identical {
*
* @param root The root of the tree to search in
*/
public Identical(final Node root) {
public IdenticalNodeFinder(final Node root) {
this.root = root;
this.hashes = new SimpleHash();
}
Expand All @@ -63,7 +63,7 @@ public Identical(final Node root) {
*
* @return The set of sets with identical nodes.
*/
public Set<Set<Node>> get() {
public Set<Set<Node>> find() {
final Map<Integer, Set<Node>> result = new HashMap<>();
this.search(this.root, result);
return result.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @since 1.0
*/
public class ReplaceNode {
public class NodeReplacer {
/**
* Replaces a subtree of the initial tree with the newly created subtree.
* @param tree The initial tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @since 1.1.4
*/
public class Subtree {
public class SubtreeBuilder {
/**
* Algorithm that composes a subtree only from the nodes that are specified in the set.
*/
Expand Down Expand Up @@ -66,7 +66,7 @@ public class Subtree {
* @param root The root node of the original tree
* @param algorithm Algorithm that selects nodes based on some criteria
*/
public Subtree(final Node root, final Algorithm algorithm) {
public SubtreeBuilder(final Node root, final Algorithm algorithm) {
this.root = root;
this.algorithm = algorithm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.cqfn.astranaut.core.algorithms.DeepTraversal;
import org.cqfn.astranaut.core.algorithms.DepthFirstWalker;
import org.cqfn.astranaut.core.base.Action;
import org.cqfn.astranaut.core.base.ActionList;
import org.cqfn.astranaut.core.base.Delete;
Expand Down Expand Up @@ -78,7 +78,7 @@ public ActionList getActionList() {
* @return Nodes that match the root node of the pattern
*/
Set<Node> match(final Pattern pattern) {
final DeepTraversal deep = new DeepTraversal(this.root);
final DepthFirstWalker deep = new DepthFirstWalker(this.root);
final PatternNode head = pattern.getRoot();
final List<Node> preset = deep.findAll(
node -> node.getTypeName().equals(head.getTypeName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
import org.junit.jupiter.api.Test;

/**
* Testing {@link Depth} class.
* Testing {@link DepthCalculator} class.
*
* @since 1.1.0
*/
class DepthTest {
class DepthCalculatorTest {
@Test
void test() {
final DiffTree tree = LittleTrees.createTreeWithDeleteAction();
final Depth depth = new Depth();
final DepthCalculator depth = new DepthCalculator();
int value = depth.calculate(tree.getRoot());
Assertions.assertEquals(5, value);
value = depth.calculate(tree.getRoot().getChild(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
import org.junit.jupiter.api.Test;

/**
* Testing {@link DeepTraversal} class.
* Testing {@link DepthFirstWalker} class.
*
* @since 1.1.5
*/
class DeepTraversalTest {
class DepthFirstWalkerTest {
@Test
void testFindFirst() {
final Node root = DraftNode.create("A(B,C,D(E<\"eee\">,F<\"fff\">)))");
final DeepTraversal traversal = new DeepTraversal(root);
final DepthFirstWalker traversal = new DepthFirstWalker(root);
final Optional<Node> node = traversal.findFirst(node1 -> !node1.getData().isEmpty());
Assertions.assertTrue(node.isPresent());
Assertions.assertEquals("E", node.get().getTypeName());
Expand All @@ -48,15 +48,15 @@ void testFindFirst() {
@Test
void testNotFoundFirst() {
final Node root = DraftNode.create("A(B,C,D)");
final DeepTraversal traversal = new DeepTraversal(root);
final DepthFirstWalker traversal = new DepthFirstWalker(root);
final Optional<Node> node = traversal.findFirst(node1 -> !node1.getData().isEmpty());
Assertions.assertFalse(node.isPresent());
}

@Test
void testFindAll() {
final Node root = DraftNode.create("A(B,C<\"ccc\">,D(E<\"eee\">)))");
final DeepTraversal traversal = new DeepTraversal(root);
final DepthFirstWalker traversal = new DepthFirstWalker(root);
final List<Node> list = traversal.findAll(node1 -> !node1.getData().isEmpty());
Assertions.assertNotNull(list);
Assertions.assertEquals(2, list.size());
Expand All @@ -65,7 +65,7 @@ void testFindAll() {
@Test
void testFindNothing() {
final Node root = DraftNode.create("A(X,Y,Z)");
final DeepTraversal traversal = new DeepTraversal(root);
final DepthFirstWalker traversal = new DepthFirstWalker(root);
final List<Node> list = traversal.findAll(node1 -> !node1.getData().isEmpty());
Assertions.assertNotNull(list);
Assertions.assertTrue(list.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@
import org.junit.jupiter.api.Test;

/**
* Tests for {@link Identical} class.
* Tests for {@link IdenticalNodeFinder} class.
*
* @since 1.1.5
*/
class IdenticalTest {
class IdenticalNodeFinderTest {
@Test
void testIdenticalSets() {
final Node original = DraftNode.create(
"T<\"a\">(T<\"b\">,T<\"c\">(F<\"a\">,T<\"b\">,T<\"a\">,F<\"a\">))"
);
final Identical identical = new Identical(original);
final Set<Set<Node>> identicals = identical.get();
final IdenticalNodeFinder identical = new IdenticalNodeFinder(original);
final Set<Set<Node>> identicals = identical.find();
Assertions.assertEquals(3, identicals.size());
}

Expand All @@ -50,8 +50,8 @@ void testNoIdenticals() {
final Node original = DraftNode.create(
"T<\"a\">(T<\"b\">,T<\"c\">(F<\"a\">,F<\"b\">,F<\"c\">,K<\"a\">))"
);
final Identical identical = new Identical(original);
final Set<Set<Node>> identicals = identical.get();
final IdenticalNodeFinder identical = new IdenticalNodeFinder(original);
final Set<Set<Node>> identicals = identical.find();
Assertions.assertEquals(0, identicals.size());
}

Expand All @@ -60,8 +60,8 @@ void testIdenticalsAndEmptyData() {
final Node original = DraftNode.create(
"T<\"a\">(T<\"b\">,T<\"c\">(F<\"a\">,T<\"b\">,T<\"a\">,F,F,T))"
);
final Identical identical = new Identical(original);
final Set<Set<Node>> identicals = identical.get();
final IdenticalNodeFinder identical = new IdenticalNodeFinder(original);
final Set<Set<Node>> identicals = identical.find();
Assertions.assertEquals(2, identicals.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.junit.jupiter.api.Test;

/**
* Test for {@link ReplaceNode} class.
* Test for {@link NodeReplacer} class.
*
* @since 1.0
*/
Expand Down Expand Up @@ -73,7 +73,7 @@ void testReplacementOfRoot() {
)
);
final Node target = this.createTargetTree();
final Pair<Node, Integer> result = new ReplaceNode().replace(root, root, target);
final Pair<Node, Integer> result = new NodeReplacer().replace(root, root, target);
Assertions.assertEquals(target, result.getKey());
Assertions.assertEquals(-1, result.getValue());
}
Expand Down Expand Up @@ -116,7 +116,7 @@ void testReplacementOfRootChild() {
source
);
final Node target = this.createTargetTree();
final Pair<Node, Integer> result = new ReplaceNode().replace(root, source, target);
final Pair<Node, Integer> result = new NodeReplacer().replace(root, source, target);
Assertions.assertEquals(target, result.getKey().getChild(2));
Assertions.assertEquals(2, result.getValue());
}
Expand Down Expand Up @@ -157,7 +157,7 @@ void testReplacementOfRootGrandChild() {
)
);
final Node target = this.createTargetTree();
final Pair<Node, Integer> result = new ReplaceNode().replace(root, source, target);
final Pair<Node, Integer> result = new NodeReplacer().replace(root, source, target);
Assertions.assertEquals(target, result.getKey().getChild(2).getChild(0));
Assertions.assertEquals(left, result.getKey().getChild(0));
Assertions.assertEquals(mid, result.getKey().getChild(1));
Expand All @@ -178,7 +178,7 @@ void testReplacementWithoutMatch() {
);
final Node source = this.createNode("444", "");
final Node target = this.createTargetTree();
final Pair<Node, Integer> result = new ReplaceNode().replace(root, source, target);
final Pair<Node, Integer> result = new NodeReplacer().replace(root, source, target);
Assertions.assertEquals(DummyNode.INSTANCE, result.getKey());
Assertions.assertEquals(-1, result.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void creatingPatternWithHole() {
builder.makeHole(first, 1);
final Pattern pattern = builder.getPattern();
Assertions.assertNotNull(pattern);
final DeepTraversal traversal = new DeepTraversal(pattern.getRoot());
final DepthFirstWalker traversal = new DepthFirstWalker(pattern.getRoot());
final Optional<Node> hole = traversal.findFirst(node -> node instanceof Hole);
Assertions.assertTrue(hole.isPresent());
Assertions.assertEquals("#1", hole.get().getData());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@
import org.junit.jupiter.api.Test;

/**
* Test for {@link Subtree}.
* Test for {@link SubtreeBuilder}.
*
* @since 1.1.4
*/
class SubtreeTest {
class SubtreeBuilderTest {
@Test
void testSubtreeCreation() {
final Node original = DraftNode.create("A(B,C(D,E,F))");
final List<String> list = Arrays.asList("A", "C", "E", "F");
final Set<Node> selected =
new NodeSelector(original).select((node, parents) -> list.contains(node.getTypeName()));
final Subtree algorithm = new Subtree(original, Subtree.INCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.INCLUDE);
final Node subtree = algorithm.create(selected);
final Node expected = DraftNode.create("A(C(E,F))");
Assertions.assertTrue(expected.deepCompare(subtree));
Expand All @@ -54,7 +54,7 @@ void testSubtreeCreation() {
@Test
void testUsingOneSubtreeInstanceForCreatingTwoSubtrees() {
final Node original = DraftNode.create("X(A,B,C,D,E)");
final Subtree algorithm = new Subtree(original, Subtree.INCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.INCLUDE);
final Node first = algorithm.create(
new HashSet<>(
Arrays.asList(
Expand Down Expand Up @@ -86,7 +86,7 @@ void testingParametersThatProduceEmptyTree() {
DraftNode.create("E")
)
);
final Subtree algorithm = new Subtree(original, Subtree.INCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.INCLUDE);
final Node subtree = algorithm.create(set);
Assertions.assertSame(subtree, DummyNode.INSTANCE);
}
Expand All @@ -96,7 +96,7 @@ void testingParametersThatProduceTreeIdenticalToOriginal() {
final Node original = DraftNode.create("X(A,B,C,D(E,F))");
final Set<Node> selected =
new NodeSelector(original).select((node, parents) -> true);
final Subtree algorithm = new Subtree(original, Subtree.INCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.INCLUDE);
final Node subtree = algorithm.create(selected);
Assertions.assertTrue(original.deepCompare(subtree));
}
Expand All @@ -111,7 +111,7 @@ void testSubtreeCreationWithExcludedNodes() {
return name.equals("B") || name.equals("E");
}
);
final Subtree algorithm = new Subtree(original, Subtree.EXCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.EXCLUDE);
final Node subtree = algorithm.create(selected);
final Node expected = DraftNode.create("A(C(D,F))");
Assertions.assertTrue(expected.deepCompare(subtree));
Expand All @@ -122,7 +122,7 @@ void testSubtreeCreationWhenAllNodesAreExcluded() {
final Node original = DraftNode.create("X(A,B,C(D,E))");
final Set<Node> selected =
new NodeSelector(original).select((node, parents) -> true);
final Subtree algorithm = new Subtree(original, Subtree.EXCLUDE);
final SubtreeBuilder algorithm = new SubtreeBuilder(original, SubtreeBuilder.EXCLUDE);
final Node subtree = algorithm.create(selected);
Assertions.assertSame(subtree, DummyNode.INSTANCE);
}
Expand Down

0 comments on commit 994ae17

Please sign in to comment.