Skip to content

Commit

Permalink
Fix entity rendering in report when value is an entity (#874)
Browse files Browse the repository at this point in the history
* Fix entity rendering in report when value is an entity

* Return null on IRIs with spaces
  • Loading branch information
beckyjackson authored May 18, 2021
1 parent 4e50f39 commit 7d9018f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 87 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix missing annotations from [`export`] [#850]
- Fail on unknown rule names in [`report`] [#858]
- Fix behaviour of `--preserve-structure` when using internal or external axiom selectors for [`remove`] or [`filter`] [#816]
- Fix value rendering for entities in [`report`] [#874]

## [1.8.1] - 2021-01-27

Expand Down Expand Up @@ -252,6 +253,8 @@ First official release of ROBOT!
[`template`]: http://robot.obolibrary.org/template
[`validate`]: http://robot.obolibrary.org/validate

[#874]: https://github.com/ontodev/robot/pull/874
[#858]: https://github.com/ontodev/robot/pull/858
[#850]: https://github.com/ontodev/robot/pull/850
[#834]: https://github.com/ontodev/robot/pull/834
[#823]: https://github.com/ontodev/robot/pull/823
Expand Down
5 changes: 5 additions & 0 deletions robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,11 @@ public IRI createIRI(String term) {
logger.warn(e.getMessage());
return null;
}

if (iri.toString().contains(" ")) {
// Invalid IRI
return null;
}
return iri;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ private static Map<String, String> getProfile(String path) throws IOException {
if (path == null) {
is = ReportOperation.class.getResourceAsStream("/report_profile.txt");
} else {
is = new FileInputStream(new File(path));
is = new FileInputStream(path);
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
Expand Down Expand Up @@ -945,12 +945,13 @@ private static List<Violation> getViolationsFromResults(
if (value != null) {
IRI valIRI = ioHelper.createIRI(value);
if (valIRI != null) {
violation.addStatement(e, valIRI);
OWLEntity v = dataFactory.getOWLClass(valIRI);
violation.addStatement(e, v);
} else {
violation.addStatement(e, dataFactory.getOWLLiteral(value));
violation.addStatement(e, value);
}
} else {
violation.addStatement(e, null);
violation.addStatement(e, "");
}
}
violations.add(violation);
Expand Down
163 changes: 89 additions & 74 deletions robot-core/src/main/java/org/obolibrary/robot/checks/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Report {
private Integer errorCount = 0;

/** IOHelper to use. */
private IOHelper ioHelper;
private final IOHelper ioHelper;

/** Manager to use. */
private OWLOntologyManager manager;
Expand Down Expand Up @@ -270,20 +270,7 @@ public Integer getViolationCount(String ruleName) throws Exception {
* @return export Table object
*/
public Table toTable(String format) {
CURIEShortFormProvider curieProvider = new CURIEShortFormProvider(ioHelper.getPrefixes());
QuotedAnnotationValueShortFormProvider nameProvider =
new QuotedAnnotationValueShortFormProvider(
manager,
curieProvider,
ioHelper.getPrefixManager(),
Collections.singletonList(OWLManager.getOWLDataFactory().getRDFSLabel()),
Collections.emptyMap());
ShortFormProvider provider;
if (useLabels) {
provider = nameProvider;
} else {
provider = curieProvider;
}
ShortFormProvider provider = getProvider();

Table table = new Table(format);
for (String h : header) {
Expand Down Expand Up @@ -316,20 +303,7 @@ public String toJSON() throws IOException {
* @return YAML string
*/
public String toYAML() {
CURIEShortFormProvider curieProvider = new CURIEShortFormProvider(ioHelper.getPrefixes());
QuotedAnnotationValueShortFormProvider nameProvider =
new QuotedAnnotationValueShortFormProvider(
manager,
curieProvider,
ioHelper.getPrefixManager(),
Collections.singletonList(OWLManager.getOWLDataFactory().getRDFSLabel()),
Collections.emptyMap());
ShortFormProvider provider;
if (useLabels) {
provider = nameProvider;
} else {
provider = curieProvider;
}
ShortFormProvider provider = getProvider();
return yamlHelper(provider, ERROR, error)
+ yamlHelper(provider, WARN, warn)
+ yamlHelper(provider, INFO, info);
Expand Down Expand Up @@ -372,7 +346,7 @@ private void addToTable(
}
}
Cell subjectCell = new Cell(columns.get(2), subject);
for (Entry<OWLEntity, List<OWLObject>> statement : v.entityStatements.entrySet()) {
for (Entry<OWLEntity, List<OWLEntity>> statement : v.entityStatements.entrySet()) {
// Property of the violation for the following rows
String property = "";
if (statement.getKey() != null) {
Expand All @@ -382,33 +356,43 @@ private void addToTable(
Cell propertyCell = new Cell(columns.get(3), property);

if (statement.getValue().isEmpty()) {
Row row = new Row(level);
Cell valueCell = new Cell(columns.get(4), "");
row.add(levelCell);
row.add(ruleCell);
row.add(subjectCell);
row.add(propertyCell);
row.add(valueCell);
table.addRow(row);
addRowToTable(table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
} else {
for (OWLObject o : statement.getValue()) {
Row row = new Row(level);
for (OWLEntity e : statement.getValue()) {
String value = OntologyHelper.renderManchester(e, provider, displayRenderer);
Cell valueCell = new Cell(columns.get(4), value);
addRowToTable(
table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
}
}
}

String value = "";
if (o != null) {
value = OntologyHelper.renderManchester(o, provider, displayRenderer);
}
for (Entry<OWLEntity, List<String>> statement : v.literalStatements.entrySet()) {
// Property of the violation for the following rows
String property = "";
if (statement.getKey() != null) {
property =
OntologyHelper.renderManchester(statement.getKey(), provider, displayRenderer);
}
Cell propertyCell = new Cell(columns.get(3), property);

if (statement.getValue().isEmpty()) {
Cell valueCell = new Cell(columns.get(4), "");
addRowToTable(table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
} else {
for (String value : statement.getValue()) {
if (value == null) {
value = "";
}
Cell valueCell = new Cell(columns.get(4), value);
row.add(levelCell);
row.add(ruleCell);
row.add(subjectCell);
row.add(propertyCell);
row.add(valueCell);
table.addRow(row);
addRowToTable(
table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
}
}
}

// Support for old statements method
for (Entry<String, List<String>> statement : v.statements.entrySet()) {
String property = statement.getKey();
if (property == null) {
Expand All @@ -417,34 +401,71 @@ private void addToTable(
Cell propertyCell = new Cell(columns.get(3), property);

if (statement.getValue().isEmpty()) {
Row row = new Row(level);
Cell valueCell = new Cell(columns.get(4), "");
row.add(levelCell);
row.add(ruleCell);
row.add(subjectCell);
row.add(propertyCell);
row.add(valueCell);
table.addRow(row);
addRowToTable(table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
} else {
for (String value : statement.getValue()) {
if (value == null) {
continue;
}
Row row = new Row(level);
Cell valueCell = new Cell(columns.get(4), value);
row.add(levelCell);
row.add(ruleCell);
row.add(subjectCell);
row.add(propertyCell);
row.add(valueCell);
table.addRow(row);
addRowToTable(
table, level, levelCell, ruleCell, subjectCell, propertyCell, valueCell);
}
}
}
}
}
}

/**
* Add a row to a table.
*
* @param table Table to add row to
* @param level String violation level
* @param levelCell Cell for level
* @param ruleCell Cell for rule
* @param subjectCell Cell for subject
* @param propertyCell Cell for property
* @param valueCell Cell for value
*/
private void addRowToTable(
Table table,
String level,
Cell levelCell,
Cell ruleCell,
Cell subjectCell,
Cell propertyCell,
Cell valueCell) {
Row row = new Row(level);
row.add(levelCell);
row.add(ruleCell);
row.add(subjectCell);
row.add(propertyCell);
row.add(valueCell);
table.addRow(row);
}

/**
* Get a ShortFormProvider to render entities based on if we are rendering labels or not.
*
* @return ShortFormProvider that either uses names (labels or CURIEs) or CURIEs
*/
private ShortFormProvider getProvider() {
CURIEShortFormProvider curieProvider = new CURIEShortFormProvider(ioHelper.getPrefixes());
QuotedAnnotationValueShortFormProvider nameProvider =
new QuotedAnnotationValueShortFormProvider(
manager,
curieProvider,
ioHelper.getPrefixManager(),
Collections.singletonList(OWLManager.getOWLDataFactory().getRDFSLabel()),
Collections.emptyMap());
if (useLabels) {
return nameProvider;
}
return curieProvider;
}

/**
* Given a rule name, return a rule name. If the string starts with "file", the name is the path
* and should be stripped to just the name. Otherwise, the input is returned.
Expand Down Expand Up @@ -493,7 +514,7 @@ private String yamlHelper(
OntologyHelper.renderManchester(v.entity, provider, RendererType.OBJECT_RENDERER);
sb.append(" - subject: \"").append(subject).append("\"");
sb.append("\n");
for (Entry<OWLEntity, List<OWLObject>> statement : v.entityStatements.entrySet()) {
for (Entry<OWLEntity, List<OWLEntity>> statement : v.entityStatements.entrySet()) {
String property =
OntologyHelper.renderManchester(
statement.getKey(), provider, RendererType.OBJECT_RENDERER);
Expand All @@ -504,7 +525,7 @@ private String yamlHelper(
}
sb.append(" values:");
sb.append("\n");
for (OWLObject value : statement.getValue()) {
for (OWLEntity value : statement.getValue()) {
String display = "";
if (value != null) {
display =
Expand Down Expand Up @@ -566,16 +587,10 @@ public Set<String> getIRIs(Map<String, List<Violation>> violationSets) {
for (Entry<String, List<Violation>> vs : violationSets.entrySet()) {
for (Violation v : vs.getValue()) {
iris.add(v.entity.getIRI().toString());
for (Entry<OWLEntity, List<OWLObject>> statement : v.entityStatements.entrySet()) {
for (Entry<OWLEntity, List<OWLEntity>> statement : v.entityStatements.entrySet()) {
iris.add(statement.getKey().getIRI().toString());
for (OWLObject value : statement.getValue()) {
if (value instanceof OWLEntity) {
OWLEntity e = (OWLEntity) value;
iris.add(e.getIRI().toString());
} else if (value instanceof IRI) {
IRI iri = (IRI) value;
iris.add(iri.toString());
}
for (OWLEntity value : statement.getValue()) {
iris.add(value.getIRI().toString());
}
}
}
Expand Down
Loading

0 comments on commit 7d9018f

Please sign in to comment.