Skip to content

Commit

Permalink
Add test for diff with labels
Browse files Browse the repository at this point in the history
  • Loading branch information
rctauber committed Sep 19, 2018
1 parent 49586b8 commit 53726c1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/diff.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ If `--output` is provided then a report will be written with any differences bet

See <a href="/examples/release-diff.txt" target="_blank">`release-diff.txt`</a> for an example. In the output, 'Ontology 1' corresponds to your `--left` input and 'Ontology 2' corresponds to your `--right` input.

The output is in OWL Manchester syntax, but you can include entity labels with `--labels true`.

You can also compare ontologies by IRI with `--left-iri` and `--right-iri`. You may want to compare a local file to a release, in which case:
<!-- DO NOT TEST -->
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public DiffCommand() {
o.addOption("r", "right", true, "load right ontology from file");
o.addOption("R", "right-iri", true, "load right ontology from IRI");
o.addOption("o", "output", true, "save results to file");
o.addOption("u", "use-labels", true, "if true, use labels in place of entity IRIs");
o.addOption(null, "labels", true, "if true, use labels in place of entity IRIs");
options = o;
}

Expand Down Expand Up @@ -149,7 +149,7 @@ public CommandState execute(CommandState state, String[] args) throws Exception
}

Map<String, String> options = new HashMap<>();
options.put("use-labels", CommandLineHelper.getDefaultValue(line, "use-labels", "false"));
options.put("labels", CommandLineHelper.getDefaultValue(line, "labels", "false"));

DiffOperation.compare(leftOntology, rightOntology, ioHelper, writer, options);
writer.flush();
Expand Down
13 changes: 8 additions & 5 deletions robot-core/src/main/java/org/obolibrary/robot/DiffOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class DiffOperation {
*/
public static Map<String, String> getDefaultOptions() {
Map<String, String> options = new HashMap<>();
options.put("use-labels", "false");
options.put("labels", "false");
return options;
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public static boolean compare(
Map<String, String> options)
throws IOException {

boolean useLabels = OptionsHelper.optionIsTrue(options, "use-labels");
boolean useLabels = OptionsHelper.optionIsTrue(options, "labels");

Map<IRI, String> labels = OntologyHelper.getLabels(ontology1);
labels.putAll(OntologyHelper.getLabels(ontology2));
Expand Down Expand Up @@ -160,7 +160,7 @@ public static String addLabels(Map<IRI, String> labels, String axiom) {
if (id.startsWith(oboBase)) {
id = id.substring(oboBase.length()).replace("_", ":");
}
String replacement = "<" + iri + ">";
String replacement = "<" + id + ">";
if (labels.containsKey(iri)) {
replacement = "<" + id + ">[" + labels.get(iri) + "]";
}
Expand Down Expand Up @@ -189,9 +189,12 @@ public static String addLabels(IOHelper ioHelper, Map<IRI, String> labels, Strin
if (id.startsWith("obo:")) {
id = id.substring(4).replace("_", ":");
}
String replacement = "<" + iri + ">";
if (!id.startsWith("<") && !id.endsWith(">")) {
id = "<" + id + ">";
}
String replacement = id;
if (labels.containsKey(iri)) {
replacement = "<" + id + ">[" + labels.get(iri) + "]";
replacement = id + "[" + labels.get(iri) + "]";
}
matcher.appendReplacement(sb, replacement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
Expand Down Expand Up @@ -54,4 +56,25 @@ public void testCompareModified() throws IOException, OWLOntologyCreationExcepti
String expected = IOUtils.toString(this.getClass().getResourceAsStream("/simple1.diff"));
assertEquals(expected, writer.toString());
}

/**
* Compare one ontology to a modified copy with labels in output.
*
* @throws IOException on file problem
* @throws OWLOntologyCreationException on ontology problem
*/
@Test
public void testCompareModifiedWithLabels() throws IOException, OWLOntologyCreationException {
OWLOntology simple = loadOntology("/simple.owl");
OWLOntology elk = loadOntology("/simple_elk.owl");

StringWriter writer = new StringWriter();
Map<String, String> options = new HashMap<>();
options.put("labels", "true");
boolean actual = DiffOperation.compare(simple, elk, new IOHelper(), writer, options);
System.out.println(writer.toString());
assertFalse(actual);
String expected = IOUtils.toString(this.getClass().getResourceAsStream("/simple.diff"));
assertEquals(expected, writer.toString());
}
}
4 changes: 4 additions & 0 deletions robot-core/src/test/resources/simple.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0 axioms in Ontology 1 but not in Ontology 2:

1 axioms in Ontology 2 but not in Ontology 1:
+ SubClassOf(<https://github.com/ontodev/robot/robot-core/src/test/resources/simple.owl#test1>[Test 1] owl:Thing)

0 comments on commit 53726c1

Please sign in to comment.