Skip to content

Commit

Permalink
'Pattern' class
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Jun 26, 2024
1 parent 8603d7a commit a1bf88c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import org.cqfn.astranaut.core.base.DiffNode;
import org.cqfn.astranaut.core.base.Node;
import org.cqfn.astranaut.core.base.Pattern;
import org.cqfn.astranaut.core.base.PatternNode;

/**
Expand Down Expand Up @@ -65,8 +66,8 @@ public PatternBuilder(final DiffNode diff) {
* Returns root of resulting pattern.
* @return Root node of pattern
*/
public PatternNode getRoot() {
return this.root;
public Pattern getPattern() {
return new Pattern(this.root);
}

/**
Expand Down Expand Up @@ -118,7 +119,7 @@ private static void buildNodeInfoMap(final Map<Node, NodeInfo> map, final Patter
* Some additional information about each node needed to make holes.
* So far there's only a parent node here, but we may need something else.
*
* @since 1.1.0
* @since 1.1.5
*/
private static final class NodeInfo {
/**
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/base/Pattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core.base;

/**
* Represents a pattern which is a syntax tree containing actions, similar to a difference tree,
* but with some nodes that can be replaced by "holes".
* A pattern is a somewhat generalized difference tree. The presence of holes allows the
* pattern's tree to be overlaid on subtrees of another syntax tree (a process called matching)
* and then replace (add, delete) nodes in the processed syntax tree according to the actions
* specified in the pattern (a process called patching or applying the pattern).
* Using the pattern mechanism enables transferring changes made in one syntax tree
* (i.e., the source code from which this syntax tree was derived) to another, unrelated
* syntax tree. One possible application of such a mechanism is the detection and automatic
* correction of errors in source code.
*
* @since 2.0.0
*/
public final class Pattern extends Tree {
/**
* Constructor.
* @param root Root node the difference tree
*/
public Pattern(final PatternNode root) {
super(root);
}

@Override
public PatternNode getRoot() {
return (PatternNode) super.getRoot();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
public final class PatternNode implements PatternItem {
/**
* The prototype node, i.e. 'ordinary', non-difference original node.
* The prototype node, i.e. 'ordinary', non-pattern original node.
*/
private final Node prototype;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;
import org.cqfn.astranaut.core.base.Builder;
import org.cqfn.astranaut.core.base.DiffNode;
import org.cqfn.astranaut.core.base.DraftNode;
import org.cqfn.astranaut.core.base.Hole;
import org.cqfn.astranaut.core.base.Node;
import org.cqfn.astranaut.core.base.PatternNode;

import org.cqfn.astranaut.core.base.*;
import org.cqfn.astranaut.core.example.green.Addition;
import org.cqfn.astranaut.core.example.green.ExpressionStatement;
import org.cqfn.astranaut.core.example.green.IntegerLiteral;
Expand Down Expand Up @@ -68,9 +64,9 @@ void creatingPatternWithHole() {
final Node stmt = ctor.createNode();
final PatternBuilder builder = new PatternBuilder(new DiffNode(stmt));
builder.makeHole(first, 1);
final PatternNode pattern = builder.getRoot();
final Pattern pattern = builder.getPattern();
Assertions.assertNotNull(pattern);
final DeepTraversal traversal = new DeepTraversal(pattern);
final DeepTraversal traversal = new DeepTraversal(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 @@ -30,13 +30,7 @@
import java.util.TreeMap;
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;
import org.cqfn.astranaut.core.base.DraftNode;
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.base.*;
import org.cqfn.astranaut.core.example.green.Addition;
import org.cqfn.astranaut.core.example.green.ExpressionStatement;
import org.cqfn.astranaut.core.example.green.IntegerLiteral;
Expand Down Expand Up @@ -133,8 +127,8 @@ void patchPatternWithHole() {
ctor.setChildrenList(Collections.singletonList(assignment));
final Node stmt = ctor.createNode();
final Patcher patcher = new DefaultPatcher();
final PatternNode pattern = PatcherTest.createPatternWithHole();
final Tree patched = patcher.patch(new Tree(stmt), pattern);
final Pattern pattern = PatcherTest.createPatternWithHole();
final Tree patched = patcher.patch(new Tree(stmt), pattern.getRoot());
ctor = new Variable.Constructor();
ctor.setData("a");
first = ctor.createNode();
Expand Down Expand Up @@ -173,7 +167,7 @@ void patchWithPatternThatDoesNotMatch() {
* Creates pattern with a hole.
* @return A pattern
*/
private static PatternNode createPatternWithHole() {
private static Pattern createPatternWithHole() {
Builder ctor = new Variable.Constructor();
ctor.setData("w");
final Node first = ctor.createNode();
Expand All @@ -190,7 +184,7 @@ private static PatternNode createPatternWithHole() {
dtbld.replaceNode(second, replacement);
final PatternBuilder pbld = new PatternBuilder(dtbld.getDiffTree().getRoot());
pbld.makeHole(first, 0);
final PatternNode pattern = pbld.getRoot();
final Pattern pattern = pbld.getPattern();
Assertions.assertNotNull(pattern);
return pattern;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
import java.util.TreeMap;
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;
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.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -130,10 +126,10 @@ void findPatternWithHoleInATree() {
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();
final Pattern pattern = pbuilder.getPattern();
final Node tree = DraftNode.create("X(Y,A(B,D<\"11\">),Z)");
final PatternMatcher matcher = new PatternMatcher(tree);
final Set<Node> found = matcher.match(pattern);
final Set<Node> found = matcher.match(pattern.getRoot());
Assertions.assertEquals(1, found.size());
final Node node = found.iterator().next();
Assertions.assertEquals("A", node.getTypeName());
Expand Down

0 comments on commit a1bf88c

Please sign in to comment.