Skip to content

Commit

Permalink
Iterate through all the patterns in an Ssurgeon, even if one does (or…
Browse files Browse the repository at this point in the history
… doesn't) change the graph

Adds a test of the multi-pattern iterate
  • Loading branch information
AngledLuffa committed Jan 20, 2023
1 parent 60d728a commit b8dfde8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,15 @@ public SemanticGraph iterate(SemanticGraph sg) {
// We reset the named node map with each edit set, since these edits
// should exist in a separate graph for each unique Semgrex match.
nodeMap = Generics.newHashMap();
boolean edited = false;
for (SsurgeonEdit edit : editScript) {
if (edit.evaluate(copied, matcher)) {
matcher = semgrexPattern.matcher(copied);
break;
edited = true;
}
}
if (edited) {
matcher = semgrexPattern.matcher(copied);
}
}
return copied;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public void readXMLRemoveNamedEdgeIterate() {
/**
* Check that cutting a graph with two nodes into two pieces, then
* pruning any disjoint pieces, results in a graph with just the root
* when done as a single operation
*/
@Test
public void readXMLPruneNodesIterate() {
Expand Down Expand Up @@ -242,6 +243,55 @@ public void readXMLPruneNodesIterate() {
assertEquals(pruneSG, expected);
}


/**
* Check that cutting a graph with two nodes into two pieces, then
* pruning any disjoint pieces, results in a graph with just the root,
* this time done as one combined operation
*/
@Test
public void readXMLTwoStepPruneIterate() {
Ssurgeon inst = Ssurgeon.inst();

String xml = String.join(newline,
"<ssurgeon-pattern-list>",
" <ssurgeon-pattern>",
" <uid>38</uid>",
" <notes>Remove dep edges and prune</notes>",
" <semgrex>" + XMLUtils.escapeXML("{}=a1 >dep {}=a2") + "</semgrex>",
" <edit-list>removeEdge -gov a1 -dep a2 -reln dep</edit-list>",
" <edit-list>delete -node a2</edit-list>",
" </ssurgeon-pattern>",
"</ssurgeon-pattern-list>");
List<SsurgeonPattern> patterns = inst.readFromString(xml);
assertEquals(1, patterns.size());
SsurgeonPattern ssurgeon = patterns.get(0);
assertEquals(2, ssurgeon.editScript.size());

// Test a two node only version
SemanticGraph sg = SemanticGraph.valueOf("[A dep> B]");
SemanticGraph cutSG = ssurgeon.iterate(sg);
SemanticGraph expected = SemanticGraph.valueOf("[A]");
assertEquals(1, cutSG.vertexSet().size());
assertEquals(expected, cutSG);

// Test a chain cut at the start
sg = SemanticGraph.valueOf("[A dep> [B obj> C]]");
cutSG = ssurgeon.iterate(sg);
assertEquals(expected, cutSG);

// Test the chain cut at the bottom
sg = SemanticGraph.valueOf("[A obj> [B dep> C]]");
cutSG = ssurgeon.iterate(sg);
assertEquals(SemanticGraph.valueOf("[A obj> B]"), cutSG);

// Test a chain cut at the start
// Only the root will be left at the end
sg = SemanticGraph.valueOf("[A dep> B dep> C]");
cutSG = ssurgeon.iterate(sg);
assertEquals(expected, cutSG);
}

/**
* Test that if the root is removed by a prune operation,
* the roots on the graph are reset
Expand Down

0 comments on commit b8dfde8

Please sign in to comment.