Skip to content

Commit

Permalink
fix: refactored priority queue. changed and fixed matcher options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaller committed Nov 19, 2020
1 parent a2f9b23 commit d8c5d91
Show file tree
Hide file tree
Showing 134 changed files with 2,276 additions and 2,143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.matchers.CompositeMatchers;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;

import org.openjdk.jmh.annotations.*;

Expand All @@ -44,9 +42,9 @@ public void load() {
@Param({})
public String refPath;

public ITree src;
public Tree src;

public ITree dst;
public Tree dst;
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
import com.github.gumtreediff.matchers.CompositeMatchers;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeContext;
import com.github.gumtreediff.tree.TreeValidator;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -71,8 +70,8 @@ public static void processFiles() throws Exception {

public static void processFilePair(String src, String dst) {
processedFilePairs++;
ITree tsrc = getTreeContext(src).getRoot();
ITree tdst = getTreeContext(dst).getRoot();
Tree tsrc = getTreeContext(src).getRoot();
Tree tdst = getTreeContext(dst).getRoot();
Matcher m = new CompositeMatchers.SimpleGumtree();
MappingStore ms = m.match(tsrc, tdst);
EditScriptGenerator g = new SimplifiedChawatheScriptGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,45 @@
import com.github.gumtreediff.client.Option;
import com.github.gumtreediff.client.Client;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.matchers.ConfigurationOptions;
import com.github.gumtreediff.matchers.GumTreeProperties;
import com.github.gumtreediff.matchers.Matchers;

import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public abstract class AbstractDiffClient<O extends AbstractDiffClient.Options> extends Client {
public abstract class AbstractDiffClient<O extends AbstractDiffClient.DiffOptions> extends Client {
protected final O opts;
public static final String SYNTAX = "Syntax: [options] srcFile dstFile";

public static class Options implements Option.Context {
public String matcher;
public String treeGenerator;
public String src;
public String dst;
public static class DiffOptions implements Option.Context {
public String matcherId;
public String treeGeneratorId;
public String srcPath;
public String dstPath;
public GumTreeProperties properties = new GumTreeProperties();

@Override
public Option[] values() {
return new Option[] {
new Option("-m", "Matcher to use.", 1) {
new Option("-m", "Id of the matcher to use.", 1) {
@Override
protected void process(String name, String[] args) {
matcher = args[0];
matcherId = args[0];
}
},
new Option("-g", "Tree generator to use.", 1) {
new Option("-g", "Id of the tree generator to use.", 1) {
@Override
protected void process(String name, String[] args) {
treeGenerator = args[0];
treeGeneratorId = args[0];
}
},
new Option("-M", "Add a matcher property (-M property value). Available: ", 2) {
@Override
protected void process(String name, String[] args) {
properties.put(ConfigurationOptions.valueOf(args[0]), args[1]);
}
},
new Option.Help(this) {
Expand All @@ -67,7 +77,7 @@ public void process(String name, String[] args) {

void dump(PrintStream out) {
out.printf("Active path: %s\n", System.getProperty("user.dir"));
out.printf("Diffed paths: %s %s\n", src, dst);
out.printf("Diffed paths: %s %s\n", srcPath, dstPath);
}
}

Expand All @@ -81,28 +91,31 @@ public AbstractDiffClient(String[] args) {
if (args.length < 2)
throw new Option.OptionException("Two arguments are required. " + SYNTAX, opts);

opts.src = args[0];
opts.dst = args[1];
opts.srcPath = args[0];
opts.dstPath = args[1];

if (Option.Verbose.verbose) {
opts.dump(System.out);
}

if (opts.treeGenerator != null && TreeGenerators.getInstance().find(opts.treeGenerator) == null)
throw new Option.OptionException("Error loading tree generator: " + opts.treeGenerator);
if (opts.matcherId != null && Matchers.getInstance().findById(opts.matcherId) == null)
throw new Option.OptionException("Error loading matcher: " + opts.matcherId);

if (opts.treeGeneratorId != null && TreeGenerators.getInstance().findById(opts.treeGeneratorId) == null)
throw new Option.OptionException("Error loading tree generator: " + opts.treeGeneratorId);

if (!Files.exists(Paths.get(opts.src)))
throw new Option.OptionException("Error loading file or folder: " + opts.src);
if (!Files.exists(Paths.get(opts.srcPath)))
throw new Option.OptionException("Error loading file or folder: " + opts.srcPath);

if (!Files.exists(Paths.get(opts.dst)))
throw new Option.OptionException("Error loading file or folder: " + opts.dst);
if (!Files.exists(Paths.get(opts.dstPath)))
throw new Option.OptionException("Error loading file or folder: " + opts.dstPath);
}

protected Diff getDiff() throws IOException {
return getDiff(opts.src, opts.dst);
return getDiff(opts.srcPath, opts.dstPath);
}

protected Diff getDiff(String src, String dst) throws IOException {
return Diff.compute(src, dst, opts.treeGenerator, opts.matcher);
return Diff.compute(src, dst, opts.treeGeneratorId, opts.matcherId, opts.properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@
import com.github.gumtreediff.gen.Registry;
import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.client.Register;
import com.github.gumtreediff.matchers.MappingStore;

import java.io.IOException;

@Register(name = "axmldiff", description = "Dump annotated xml tree",
priority = Registry.Priority.LOW, options = AbstractDiffClient.Options.class)
public class AnnotatedXmlDiff extends AbstractDiffClient<AnnotatedXmlDiff.Options> {
priority = Registry.Priority.LOW, options = AbstractDiffClient.DiffOptions.class)
public class AnnotatedXmlDiff extends AbstractDiffClient<AnnotatedXmlDiff.AnnotatedXmlDiffOptions> {

public AnnotatedXmlDiff(String[] args) {
super(args);
}

static class Options extends AbstractDiffClient.Options {
static class AnnotatedXmlDiffOptions extends AbstractDiffClient.DiffOptions {
protected boolean isSrc = true;

@Override
Expand All @@ -61,8 +58,8 @@ protected void process(String name, String[] args) {
}

@Override
protected Options newOptions() {
return new Options();
protected AnnotatedXmlDiffOptions newOptions() {
return new AnnotatedXmlDiffOptions();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import java.util.Set;

@Register(name = "cluster", description = "Extract action clusters",
options = AbstractDiffClient.Options.class)
public class ClusterDiff extends AbstractDiffClient<AbstractDiffClient.Options> {
options = AbstractDiffClient.DiffOptions.class)
public class ClusterDiff extends AbstractDiffClient<AbstractDiffClient.DiffOptions> {

public ClusterDiff(String[] args) {
super(args);
Expand All @@ -50,7 +50,7 @@ public void run() throws IOException {
}

@Override
protected Options newOptions() {
return new Options();
protected DiffOptions newOptions() {
return new DiffOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
import java.nio.file.Paths;
import java.util.Arrays;

@Register(name = "textdiff", description = "Dump actions in a textual format",
options = AbstractDiffClient.Options.class)
public class TextDiff extends AbstractDiffClient<TextDiff.Options> {
@Register(name = "textdiff", description = "Dump actions in a textual format.",
options = TextDiff.TextDiffOptions.class)
public class TextDiff extends AbstractDiffClient<TextDiff.TextDiffOptions> {
public TextDiff(String[] args) {
super(args);
if (!Files.isRegularFile(Paths.get(opts.src)))
throw new Option.OptionException("Source must be a file: " + opts.src, opts);
if (!Files.isRegularFile(Paths.get(opts.dst)))
throw new Option.OptionException("Destination must be a file: " + opts.dst, opts);
if (!Files.isRegularFile(Paths.get(opts.srcPath)))
throw new Option.OptionException("Source must be a file: " + opts.srcPath, opts);
if (!Files.isRegularFile(Paths.get(opts.dstPath)))
throw new Option.OptionException("Destination must be a file: " + opts.dstPath, opts);

if (opts.format == null) {
opts.format = OutputFormat.TEXT;
Expand All @@ -55,7 +55,7 @@ else if (opts.output.endsWith(".xml"))
}
}

public static class Options extends AbstractDiffClient.Options {
public static class TextDiffOptions extends AbstractDiffClient.DiffOptions {
protected OutputFormat format;
protected String output;

Expand Down Expand Up @@ -92,8 +92,8 @@ void dump(PrintStream out) {
}

@Override
protected Options newOptions() {
return new Options();
protected TextDiffOptions newOptions() {
return new TextDiffOptions();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
import com.github.gumtreediff.client.diff.AbstractDiffClient;
import com.github.gumtreediff.matchers.Mapping;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeContext;

import java.io.StringWriter;
import java.io.Writer;

@Register(description = "A dot diff client", options = AbstractDiffClient.Options.class)
public final class DotDiff extends AbstractDiffClient<AbstractDiffClient.Options> {
@Register(description = "A dot diff client", options = AbstractDiffClient.DiffOptions.class)
public final class DotDiff extends AbstractDiffClient<AbstractDiffClient.DiffOptions> {

public DotDiff(String[] args) {
super(args);
Expand All @@ -59,7 +59,7 @@ public void run() throws Exception {
}

private void writeTree(TreeContext context, Writer writer, MappingStore mappings) throws Exception {
for (ITree tree : context.getRoot().preOrder()) {
for (Tree tree : context.getRoot().preOrder()) {
String fillColor = "red";
if (mappings.isSrcMapped(tree) || mappings.isDstMapped(tree))
fillColor = "blue";
Expand All @@ -72,11 +72,11 @@ private void writeTree(TreeContext context, Writer writer, MappingStore mappings

}

private String getDotId(TreeContext context, ITree tree) {
private String getDotId(TreeContext context, Tree tree) {
return "n_" + context.hashCode() + "_" + tree.hashCode();
}

private String getDotLabel(ITree tree) {
private String getDotLabel(Tree tree) {
String label = tree.toString();
if (label.contains("\"") || label.contains("\\s"))
label = label
Expand All @@ -89,7 +89,7 @@ private String getDotLabel(ITree tree) {
}

@Override
protected Options newOptions() {
return new Options();
protected DiffOptions newOptions() {
return new DiffOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
import javax.swing.tree.TreePath;

import com.github.gumtreediff.actions.Diff;
import com.github.gumtreediff.actions.ITreeClassifier;
import com.github.gumtreediff.actions.TreeClassifier;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.Tree;
import com.github.gumtreediff.tree.TreeContext;

public class MappingsPanel extends JPanel implements TreeSelectionListener {
Expand All @@ -54,7 +54,7 @@ public class MappingsPanel extends JPanel implements TreeSelectionListener {

private TreeContext src;
private TreeContext dst;
private ITreeClassifier classifyTrees;
private TreeClassifier classifyTrees;
private MappingStore mappings;

private TreePanel panSrc;
Expand Down Expand Up @@ -122,17 +122,17 @@ private static String readFileAsString(String filePath) {
}

private void openNodes() {
for (ITree t: classifyTrees.getDeletedSrcs()) openNode(panSrc, t);
for (ITree t: classifyTrees.getInsertedDsts()) openNode(panDst, t);
for (ITree t: classifyTrees.getUpdatedSrcs()) openNode(panSrc, t);
for (ITree t: classifyTrees.getUpdatedDsts()) openNode(panDst, t);
for (ITree t: classifyTrees.getMovedSrcs()) openNode(panSrc, t);
for (ITree t: classifyTrees.getMovedDsts()) openNode(panDst, t);
for (Tree t: classifyTrees.getDeletedSrcs()) openNode(panSrc, t);
for (Tree t: classifyTrees.getInsertedDsts()) openNode(panDst, t);
for (Tree t: classifyTrees.getUpdatedSrcs()) openNode(panSrc, t);
for (Tree t: classifyTrees.getUpdatedDsts()) openNode(panDst, t);
for (Tree t: classifyTrees.getMovedSrcs()) openNode(panSrc, t);
for (Tree t: classifyTrees.getMovedDsts()) openNode(panDst, t);
panSrc.getJTree().scrollPathToVisible(new TreePath(panSrc.getTrees().get(src.getRoot()).getPath()));
panDst.getJTree().scrollPathToVisible(new TreePath(panDst.getTrees().get(dst.getRoot()).getPath()));
}

private void openNode(TreePanel p, ITree t) {
private void openNode(TreePanel p, Tree t) {
DefaultMutableTreeNode n = p.getTrees().get(t);
p.getJTree().scrollPathToVisible(new TreePath(n.getPath()));
}
Expand All @@ -141,10 +141,10 @@ private void openNode(TreePanel p, ITree t) {
public void valueChanged(TreeSelectionEvent e) {
JTree jtree = (JTree) e.getSource();
if (jtree.getSelectionPath() == null) return;
ITree sel = (ITree) ((DefaultMutableTreeNode) jtree.getLastSelectedPathComponent()).getUserObject();
Tree sel = (Tree) ((DefaultMutableTreeNode) jtree.getLastSelectedPathComponent()).getUserObject();
JTextArea selJTextArea;
boolean isMapped = false;
ITree match = null;
Tree match = null;
TreePanel matchTreePanel;
JTextArea matchJTextArea;

Expand Down Expand Up @@ -172,8 +172,8 @@ public void valueChanged(TreeSelectionEvent e) {
}
}

private void updateJTreeAndJTextArea(ITree sel, JTextArea selJTextArea, boolean isMapped,
ITree match, TreePanel matchTreePanel,
private void updateJTreeAndJTextArea(Tree sel, JTextArea selJTextArea, boolean isMapped,
Tree match, TreePanel matchTreePanel,
JTextArea matchJTextArea) throws BadLocationException {
selJTextArea.getHighlighter().removeAllHighlights();
selJTextArea.getHighlighter().addHighlight(sel.getPos(), sel.getEndPos(), DefaultHighlighter.DefaultPainter);
Expand Down Expand Up @@ -207,7 +207,7 @@ public Component getTreeCellRendererComponent(JTree jtree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(jtree, value, selected, expanded, leaf, row, hasFocus);
ITree tree = (ITree) ((DefaultMutableTreeNode) value).getUserObject();
Tree tree = (Tree) ((DefaultMutableTreeNode) value).getUserObject();
if (isSrc && classifyTrees.getDeletedSrcs().contains(tree)) setForeground(DEL_COLOR);
else if (!isSrc && classifyTrees.getInsertedDsts().contains(tree)) setForeground(ADD_COLOR);
else if (isSrc && classifyTrees.getUpdatedSrcs().contains(tree)) setForeground(UPD_COLOR);
Expand Down
Loading

0 comments on commit d8c5d91

Please sign in to comment.