Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix entity rendering in report when value is an entity #874

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -944,13 +944,14 @@ private static List<Violation> getViolationsFromResults(
OWLEntity e = dataFactory.getOWLClass(ioHelper.createIRI(property));
if (value != null) {
IRI valIRI = ioHelper.createIRI(value);
if (valIRI != null) {
violation.addStatement(e, valIRI);
if (valIRI != null && !valIRI.toString().contains(" ")) {
beckyjackson marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class Violation {
public String subject;

// Statements about OWLEntity subject
public Map<OWLEntity, List<OWLObject>> entityStatements = new HashMap<>();
public Map<OWLEntity, List<OWLEntity>> entityStatements = new HashMap<>();
// Statements about String subject
public Map<OWLEntity, List<String>> literalStatements = new HashMap<>();

// Deprecated statements with string properties
public Map<String, List<String>> statements = new HashMap<>();

/**
Expand All @@ -27,13 +30,22 @@ public Violation(OWLEntity subject) {
}

/**
* Add a statement to the Violation about the subject.
* Create a new Violation object about a String. This is used for blank nodes.
*
* @param subject String subject
*/
public Violation(String subject) {
this.subject = subject;
}

/**
* Add a statement with an OWLEntity value to the Violation about the subject.
*
* @param property OWLEntity property
* @param value OWLObject value
* @param value OWLEntity value
*/
public void addStatement(OWLEntity property, OWLObject value) {
List<OWLObject> values;
public void addStatement(OWLEntity property, OWLEntity value) {
List<OWLEntity> values;
if (entityStatements.get(property) != null) {
// This property already has one value in the map
// Add this value to the existing list
Expand All @@ -51,19 +63,38 @@ public void addStatement(OWLEntity property, OWLObject value) {
entityStatements.put(property, values);
}

/** @param subject String subject */
public Violation(String subject) {
this.subject = subject;
/**
* Add a literal statement to the Violation about the subject.
*
* @param property OWLEntity property
* @param value String literal value
*/
public void addStatement(OWLEntity property, String value) {
List<String> values;
if (literalStatements.get(property) != null) {
values = new ArrayList<>(literalStatements.get(property));
values.add(value);
} else {
if (value != null) {
values = Collections.singletonList(value);
} else {
values = Collections.emptyList();
}
}
literalStatements.put(property, values);
}

/**
* @param property String property
* @param value String value
* @deprecated String properties are no longer recommended; create an OWLEntity for the property
* instead
*/
@Deprecated
public void addStatement(String property, String value) {
List<String> values;
if (statements.get(property) != null) {
values = new ArrayList<String>(statements.get(property));
values = new ArrayList<>(statements.get(property));
values.add(value);
} else {
if (value != null) {
Expand All @@ -74,4 +105,18 @@ public void addStatement(String property, String value) {
}
statements.put(property, values);
}

/**
* @param property String property
* @param object OWLObject value
* @deprecated OWLObjects are no longer supported; use OWLEntity or String literal instead
*/
@Deprecated
public void addStatement(OWLEntity property, OWLObject object) {
// Do nothing
logger.error(
String.format(
"Cannot add OWLObject '%s'; use an OWLEntity or String literal instead.",
object.toString()));
}
}