Skip to content

Commit

Permalink
new diff utility methods and added doc on most important API classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfaller committed Apr 5, 2020
1 parent f7e5ed2 commit 3fc11ca
Show file tree
Hide file tree
Showing 26 changed files with 106 additions and 91 deletions.
Expand Up @@ -20,7 +20,7 @@
package com.github.gumtree.dist;

import com.github.gumtreediff.client.Run;
import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.tree.TreeContext;

Expand Down Expand Up @@ -68,7 +68,7 @@ public static void collectTrees(String dir) throws Exception {

private static TreeContext getTreeContext(String file) {
try {
TreeContext t = Generators.getInstance().getTree(file);
TreeContext t = TreeGenerators.getInstance().getTree(file);
return t;
} catch (IOException e) {
e.printStackTrace();
Expand Down
Expand Up @@ -20,16 +20,10 @@

package com.github.gumtreediff.client.diff;

import com.github.gumtreediff.actions.ChawatheScriptGenerator;
import com.github.gumtreediff.actions.Diff;
import com.github.gumtreediff.actions.EditScript;
import com.github.gumtreediff.client.Option;
import com.github.gumtreediff.client.Client;
import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.matchers.Matcher;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.TreeContext;
import com.github.gumtreediff.gen.TreeGenerators;

import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -94,7 +88,7 @@ public AbstractDiffClient(String[] args) {
opts.dump(System.out);
}

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

if (!Files.exists(Paths.get(opts.src)))
Expand All @@ -109,26 +103,6 @@ protected Diff getDiff() throws IOException {
}

protected Diff getDiff(String src, String dst) throws IOException {
TreeContext srcCtx = getTreeContext(src);
TreeContext dstCtx = getTreeContext(dst);
MappingStore mappings = getMatcher().match(srcCtx.getRoot(), dstCtx.getRoot());
EditScript editScript = new ChawatheScriptGenerator().computeActions(mappings);
return new Diff(srcCtx, dstCtx, mappings, editScript);
}

protected Matcher getMatcher() {
return Matchers.getInstance().getMatcherWithFallback(opts.matcher);
}

protected TreeContext getSrcTreeContext() throws IOException {
return getTreeContext(opts.src);
}

protected TreeContext getDstTreeContext() throws IOException {
return getTreeContext(opts.dst);
}

protected TreeContext getTreeContext(String file) throws IOException {
return Generators.getInstance().getTree(file, opts.treeGenerator);
return Diff.compute(src, dst, opts.treeGenerator, opts.matcher);
}
}
Expand Up @@ -20,13 +20,9 @@
package com.github.gumtreediff.client.diff;

import com.github.gumtreediff.actions.ActionClusterFinder;
import com.github.gumtreediff.actions.ChawatheScriptGenerator;
import com.github.gumtreediff.actions.Diff;
import com.github.gumtreediff.actions.EditScript;
import com.github.gumtreediff.actions.model.Action;
import com.github.gumtreediff.client.Register;
import com.github.gumtreediff.matchers.MappingStore;
import org.eclipse.jetty.util.IO;

import java.io.IOException;
import java.util.Set;
Expand All @@ -42,7 +38,7 @@ public ClusterDiff(String[] args) {
@Override
public void run() throws IOException {
Diff diff = getDiff();
ActionClusterFinder f = new ActionClusterFinder(diff.src, diff.dst, diff.editScript);
ActionClusterFinder f = new ActionClusterFinder(diff.editScript);
for (Set<Action> cluster: f.getClusters()) {
System.out.println("New cluster:");
System.out.println(f.getClusterLabel(cluster));
Expand Down
Expand Up @@ -52,7 +52,7 @@ public void run() throws Exception {
writer.write("}\n");
for (Mapping m: diff.mappings) {
writer.write(String.format("%s -> %s [style=dashed]\n;",
getDotId(getSrcTreeContext(), m.first), getDotId(getDstTreeContext(), m.second)));
getDotId(diff.src, m.first), getDotId(diff.dst, m.second)));
}
writer.write("}\n");
System.out.println(writer.toString());
Expand Down
Expand Up @@ -45,7 +45,6 @@
import com.github.gumtreediff.actions.Diff;
import com.github.gumtreediff.actions.ITreeClassifier;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.actions.AllNodesClassifier;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;

Expand All @@ -66,7 +65,6 @@ public class MappingsPanel extends JPanel implements TreeSelectionListener {
private static final Color DEL_COLOR = new Color(190, 0, 0);
private static final Color ADD_COLOR = new Color(0, 158, 0);
private static final Color UPD_COLOR = new Color(189, 162, 0);
//private static final Color MIS_COLOR = new Color(0, 0, 128);
private static final Color MV_COLOR = new Color(128, 0, 128);

public MappingsPanel(String srcPath, String dstPath, Diff diff) {
Expand Down Expand Up @@ -143,11 +141,11 @@ public void valueChanged(TreeSelectionEvent e) {
JTree jtree = (JTree) e.getSource();
if (jtree.getSelectionPath() == null) return;
ITree sel = (ITree) ((DefaultMutableTreeNode) jtree.getLastSelectedPathComponent()).getUserObject();
JTextArea selJTextArea = null;
JTextArea selJTextArea;
boolean isMapped = false;
ITree match = null;
TreePanel matchTreePanel = null;
JTextArea matchJTextArea = null;
TreePanel matchTreePanel;
JTextArea matchJTextArea;

if (jtree == panSrc.getJTree()) {
selJTextArea = txtSrc;
Expand Down
Expand Up @@ -20,7 +20,7 @@

package com.github.gumtreediff.client.diff.swing;

import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.tree.TreeContext;

import javax.swing.*;
Expand All @@ -29,7 +29,7 @@
public final class SwingTree {

public static void main(String[] args) throws IOException {
final TreeContext t = Generators.getInstance().getTree(args[0]);
final TreeContext t = TreeGenerators.getInstance().getTree(args[0]);
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Expand Down
Expand Up @@ -20,7 +20,7 @@

package com.github.gumtreediff.client.diff.web;

import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.utils.Pair;
import com.github.gumtreediff.io.DirectoryComparator;
import org.rendersnake.DocType;
Expand Down Expand Up @@ -119,7 +119,7 @@ public void renderOn(HtmlCanvas html) throws IOException {
.div(class_("btn-toolbar justify-content-end"))
.div(class_("btn-group"))
//TODO: integrate this with the -g option
.if_(Generators.getInstance().hasGeneratorForFile(file.first.getAbsolutePath()))
.if_(TreeGenerators.getInstance().hasGeneratorForFile(file.first.getAbsolutePath()))
.a(class_("btn btn-primary btn-sm").href("/monaco-diff/" + id)).content("monaco")
.a(class_("btn btn-primary btn-sm").href("/vanilla-diff/" + id)).content("classic")
._if()
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/com/github/gumtreediff/client/List.java
Expand Up @@ -20,7 +20,7 @@

package com.github.gumtreediff.client;

import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.matchers.Matchers;

import java.io.IOException;
Expand Down Expand Up @@ -63,7 +63,7 @@ Collection<?> list() {
GENERATORS {
@Override
Collection<?> list() {
return Generators.getInstance().getEntries();
return TreeGenerators.getInstance().getEntries();
}
},
PROPERTIES {
Expand Down
4 changes: 2 additions & 2 deletions client/src/main/java/com/github/gumtreediff/client/Run.java
Expand Up @@ -20,7 +20,7 @@

package com.github.gumtreediff.client;

import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.gen.Registry;
import com.github.gumtreediff.gen.TreeGenerator;
import org.atteo.classindex.ClassIndex;
Expand Down Expand Up @@ -55,7 +55,7 @@ public static void initGenerators() {
com.github.gumtreediff.gen.Register a =
gen.getAnnotation(com.github.gumtreediff.gen.Register.class);
if (a != null)
Generators.getInstance().install(gen, a);
TreeGenerators.getInstance().install(gen, a);
});
}

Expand Down
Expand Up @@ -21,18 +21,14 @@
package com.github.gumtreediff.client;

import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.gen.Generators;
import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.io.TreeIoUtils.TreeSerializer;
import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.tree.TreeContext;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Register(name = "parse", description = "Parse file and dump result")
public class Serializer extends Client {
Expand Down Expand Up @@ -141,6 +137,6 @@ public void run() throws Exception {
}

private TreeContext getTreeContext(String file) throws IOException {
return Generators.getInstance().getTree(file, opts.generator);
return TreeGenerators.getInstance().getTree(file, opts.generator);
}
}
Expand Up @@ -20,33 +20,20 @@
package com.github.gumtreediff.actions;

import com.github.gumtreediff.actions.model.*;
import com.github.gumtreediff.tree.TreeContext;
import org.jgrapht.DirectedGraph;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

import java.util.List;
import java.util.Set;

public class ActionClusterFinder {

private TreeContext src;

private TreeContext dst;

private EditScript actions;

private DirectedGraph<Action, DefaultEdge> graph;

private List<Set<Action>> clusters;

public ActionClusterFinder(TreeContext src, TreeContext dst, EditScript actions) {
this.src = src;
this.dst = dst;
this.actions = actions;
public ActionClusterFinder(EditScript actions) {
graph = new DefaultDirectedGraph<>(DefaultEdge.class);

for (Action a: actions)
Expand Down
Expand Up @@ -26,6 +26,9 @@
import com.github.gumtreediff.actions.model.Update;
import com.github.gumtreediff.matchers.MappingStore;

/**
* Partition all moved, inserted, updated or deleted nodes.
*/
public class AllNodesClassifier extends AbstractITreeClassifier {
public AllNodesClassifier(Diff diff) {
super(diff);
Expand Down
Expand Up @@ -28,6 +28,9 @@

import java.util.*;

/**
* An edit script generator based upon Chawathe algorithm.
*/
public class ChawatheScriptGenerator implements EditScriptGenerator {
private ITree origSrc;

Expand Down Expand Up @@ -90,7 +93,7 @@ public EditScript generate() {

List<ITree> bfsDst = TreeUtils.breadthFirst(origDst);
for (ITree x: bfsDst) {
ITree w = null;
ITree w;
ITree y = x.getParent();
ITree z = cpyMappings.getSrcForDst(y);

Expand Down
24 changes: 20 additions & 4 deletions core/src/main/java/com/github/gumtreediff/actions/Diff.java
Expand Up @@ -19,25 +19,41 @@

package com.github.gumtreediff.actions;

import com.github.gumtreediff.gen.TreeGenerators;
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.matchers.Matchers;
import com.github.gumtreediff.tree.TreeContext;

import java.io.IOException;

public class Diff {
public final TreeContext src;

public final TreeContext dst;

public final MappingStore mappings;

public final EditScript editScript;

public Diff(TreeContext src, TreeContext dst, MappingStore mappings, EditScript editScript) {
public Diff(TreeContext src, TreeContext dst,
MappingStore mappings, EditScript editScript) {
this.src = src;
this.dst = dst;
this.mappings = mappings;
this.editScript = editScript;
}

public static Diff compute(String srcFile, String dstFile,
String treeGenerator, String matcher) throws IOException {
TreeContext src = TreeGenerators.getInstance().getTree(srcFile, treeGenerator);
TreeContext dst = TreeGenerators.getInstance().getTree(dstFile, treeGenerator);
MappingStore mappings = Matchers.getInstance()
.getMatcherWithFallback(matcher).match(src.getRoot(), dst.getRoot());
EditScript editScript = new SimplifiedChawatheScriptGenerator().computeActions(mappings);
return new Diff(src, dst, mappings, editScript);
}

public static Diff compute(String srcFile, String dstFile) throws IOException {
return compute(srcFile, dstFile, null, null);
}

public ITreeClassifier createAllNodeClassifier() {
return new AllNodesClassifier(this);
}
Expand Down
Expand Up @@ -25,6 +25,11 @@
import java.util.Iterator;
import java.util.List;

/**
* Class representing edit scripts: sequence of edit actions.
*
* @see Action
*/
public class EditScript implements Iterable<Action> {
private List<Action> actions;

Expand Down
Expand Up @@ -21,6 +21,12 @@

import com.github.gumtreediff.matchers.MappingStore;

/**
* Interface for script generators that compute edit scripts from mappings.
*
* @see MappingStore
* @see EditScript
*/
public interface EditScriptGenerator {
EditScript computeActions(MappingStore ms);
EditScript computeActions(MappingStore mappings);
}
Expand Up @@ -23,6 +23,11 @@

import java.util.Set;

/**
* An interface to partition the nodes of a tree into sets of updated, deleted, moved,
* and updated nodes.
* @see ITree
*/
public interface ITreeClassifier {
Set<ITree> getUpdatedSrcs();

Expand Down
Expand Up @@ -23,6 +23,12 @@
import com.github.gumtreediff.matchers.MappingStore;
import com.github.gumtreediff.tree.ITree;

/**
* A script generator, based upon the simplified Chawathe script generator,
* that replaces moved and updated nodes by inserted and deleted nodes.
*
* @see SimplifiedChawatheScriptGenerator
*/
public class InsertDeleteChawatheScriptGenerator implements EditScriptGenerator {
private EditScript actions;
private MappingStore origMappings;
Expand Down

0 comments on commit 3fc11ca

Please sign in to comment.