Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix releasing dataset after exception when running [`report`] with `--tdb true` [#659]
- Reduced time spent loading datasets for [`query`] in [#666]
- Fix writing JSON format to use `OutputStream` with ['convert'] in [#671]
- Fix IRI resolution for `template` in [#689]


## [1.6.0] - 2020-03-04
Expand Down Expand Up @@ -186,6 +187,7 @@ First official release of ROBOT!
[`report`]: http://robot.obolibrary.org/report
[`template`]: http://robot.obolibrary.org/template

[#689]: https://github.com/ontodev/robot/pull/689
[#685]: https://github.com/ontodev/robot/pull/685
[#671]: https://github.com/ontodev/robot/pull/671
[#666]: https://github.com/ontodev/robot/pull/666
Expand Down
3 changes: 3 additions & 0 deletions docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ Annotation properties should not have any value in the `CHARACTERISTIC` column,
### Annotation Property Error

The annotation property provided could not be resolved. Check your template to ensure the provided annotation property is in a correct IRI or CURIE format. For legibility, using CURIEs is recommended, but you must ensure that the prefix is defined.

If you are using a label, make sure that the label is defined either in the template or input ontology.

```
A rdfs:label
A http://www.w3.org/2000/01/rdf-schema#label
Expand Down
28 changes: 25 additions & 3 deletions robot-core/src/main/java/org/obolibrary/robot/IOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.*;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.rdf.rdfxml.renderer.IllegalElementNameException;
import org.semanticweb.owlapi.rdf.rdfxml.renderer.XMLWriterPreferences;
import org.semanticweb.owlapi.util.DefaultPrefixManager;
import org.slf4j.Logger;
Expand Down Expand Up @@ -748,9 +749,22 @@ public Set<String> extractTerms(String input) {
*/
@SuppressWarnings("unchecked")
public IRI createIRI(String term) {
return createIRI(term, false);
}

/**
* Given a term string, use the current prefixes to create an IRI.
*
* @param term the term to convert to an IRI
* @param qName if true, check that the expanded IRI is a valid QName (if not, return null)
* @return the new IRI or null
*/
@SuppressWarnings("unchecked")
public IRI createIRI(String term, boolean qName) {
if (term == null) {
return null;
}
IRI iri;

try {
// This is stupid, because better methods aren't public.
Expand All @@ -764,15 +778,21 @@ public IRI createIRI(String term) {
Object expanded = new JsonLdApi().expand(context, jsonMap);
String result = ((Map<String, Object>) expanded).keySet().iterator().next();
if (result != null) {
return IRI.create(result);
iri = IRI.create(result);
} else {
return IRI.create(term);
iri = IRI.create(term);
}
} catch (Exception e) {
logger.warn("Could not create IRI for {}", term);
logger.warn(e.getMessage());
return null;
}

// Check that this is a valid QName
if (qName && !iri.getRemainder().isPresent()) {
return null;
}
return null;
return iri;
}

/**
Expand Down Expand Up @@ -1434,6 +1454,8 @@ private void saveOntologyFile(
// use native save functionality
try {
ontology.getOWLOntologyManager().saveOntology(ontology, format, ontologyIRI);
} catch (IllegalElementNameException e) {
throw new IOException("ELEMENT NAME EXCEPTION " + e.getCause().getMessage());
} catch (OWLOntologyStorageException e) {
// Determine if its caused by an OBO Format error
if (format instanceof OBODocumentFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private IRI getIRI(Map<String, IRI> map, String name) {
public IRI getIRI(String name, boolean create) {
IRI iri = iris.getOrDefault(name, null);
if (iri == null && ioHelper != null && create) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
}
return iri;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public OWLAnnotationProperty getOWLAnnotationProperty(@Nonnull String name, bool
return dataFactory.getOWLAnnotationProperty(iri);
}
if (create && ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLAnnotationProperty(iri);
}
Expand All @@ -358,7 +358,7 @@ public OWLClass getOWLClass(@Nonnull String name) {
return dataFactory.getOWLClass(iri);
}
if (ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLClass(iri);
}
Expand All @@ -379,7 +379,7 @@ public OWLDataProperty getOWLDataProperty(@Nonnull String name) {
return dataFactory.getOWLDataProperty(iri);
}
if (ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLDataProperty(iri);
}
Expand Down Expand Up @@ -413,7 +413,7 @@ public OWLDatatype getOWLDatatype(@Nonnull String name, boolean create) {
return dataFactory.getOWLDatatype(iri);
}
if (create && ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLDatatype(iri);
}
Expand All @@ -434,7 +434,7 @@ public OWLNamedIndividual getOWLIndividual(@Nonnull String name) {
return dataFactory.getOWLNamedIndividual(iri);
}
if (ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLNamedIndividual(iri);
}
Expand All @@ -455,7 +455,7 @@ public OWLObjectProperty getOWLObjectProperty(@Nonnull String name) {
return dataFactory.getOWLObjectProperty(iri);
}
if (ioHelper != null) {
iri = ioHelper.createIRI(name);
iri = ioHelper.createIRI(name, true);
if (iri != null) {
return dataFactory.getOWLObjectProperty(iri);
}
Expand Down
42 changes: 35 additions & 7 deletions robot-core/src/main/java/org/obolibrary/robot/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ public class Template {
NS
+ "UNKNOWN TEMPLATE ERROR could not interpret template string \"%4$s\" for column %2$d (\"%3$s\") in table \"%1$s\".";

private static final String unknownEntityError =
NS
+ "UNKNOWN ENTITY ERROR could not interpret '%1$s' in row %2$d, column %3$d (\"%4$s\") in table \"%5$s\".";

private static final List<String> validClassTypes =
new ArrayList<>(Arrays.asList("subclass", "disjoint", "equivalent"));

Expand Down Expand Up @@ -549,13 +553,13 @@ private void addLabels() {
type = "class";
}

IRI iri = ioHelper.createIRI(id);
IRI iri = ioHelper.createIRI(id, true);
if (iri == null) {
iri = IRI.create(id);
}

// Try to resolve a CURIE
IRI typeIRI = ioHelper.createIRI(type);
IRI typeIRI = ioHelper.createIRI(type, true);

// Set to IRI string or to type string
String typeOrIRI = type;
Expand Down Expand Up @@ -626,18 +630,27 @@ private void processRow(List<String> row) throws Exception {
String id = null;
try {
id = row.get(idColumn);
if (id.trim().isEmpty()) {
id = null;
}
} catch (IndexOutOfBoundsException e) {
// ignore
}
String label = null;
try {
label = row.get(labelColumn);
if (label.trim().isEmpty()) {
label = null;
}
} catch (IndexOutOfBoundsException e) {
// ignore
}
String type = null;
try {
type = row.get(typeColumn);
if (type.trim().isEmpty()) {
type = null;
}
} catch (IndexOutOfBoundsException e) {
// ignore
}
Expand All @@ -647,7 +660,7 @@ private void processRow(List<String> row) throws Exception {
return;
}

if (type == null || type.trim().isEmpty()) {
if (type == null) {
// Try to guess the type from already existing entities
if (label != null) {
OWLEntity e = checker.getOWLEntity(label);
Expand All @@ -668,13 +681,28 @@ private void processRow(List<String> row) throws Exception {
}
}

// Create an IRI for the subject of this row
IRI iri = getIRI(id, label);
if (iri == null) {
return;
// Unable to create an IRI from the entity
if (idColumn != -1) {
if (id != null) {
// Has an ID column with contents that could not be resolved
throw new RowParseException(
String.format(unknownEntityError, id, rowNum, idColumn, "ID", name));
} else {
// Has an ID column, but it's empty
return;
}
} else {
// No ID column and label could not be resolved
throw new RowParseException(
String.format(unknownEntityError, label, rowNum, labelColumn, "LABEL", name));
}
}

// Try to resolve a CURIE
IRI typeIRI = ioHelper.createIRI(type);
IRI typeIRI = ioHelper.createIRI(type, true);

// Set to IRI string or to type string
String typeOrIRI = type;
Expand Down Expand Up @@ -1734,7 +1762,7 @@ private void addIndividualAxioms(IRI iri, List<String> row) throws Exception {
type = type.trim();

// Try to resolve a CURIE
IRI typeIRI = ioHelper.createIRI(type);
IRI typeIRI = ioHelper.createIRI(type, true);

// Set to IRI string or to type string
String typeOrIRI = type;
Expand Down Expand Up @@ -2122,7 +2150,7 @@ private IRI getIRI(String id, String label) throws Exception {
throw new Exception("You must specify either an ID or a label");
}
if (id != null) {
return ioHelper.createIRI(id);
return ioHelper.createIRI(id, true);
}
return checker.getIRI(label, true);
}
Expand Down
24 changes: 7 additions & 17 deletions robot-core/src/main/java/org/obolibrary/robot/TemplateHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ public static Set<OWLAnnotation> getAnnotations(
if (split != null) {
String[] values = value.split(Pattern.quote(split));
for (String v : values) {
annotations.add(maybeGetIRIAnnotation(tableName, checker, template, v, rowNum, column));
annotations.add(maybeGetIRIAnnotation(checker, template, v));
}
} else {
annotations.add(maybeGetIRIAnnotation(tableName, checker, template, value, rowNum, column));
annotations.add(maybeGetIRIAnnotation(checker, template, value));
}
return annotations;
} else if (template.equals("LABEL")) {
Expand Down Expand Up @@ -640,7 +640,7 @@ public static List<IRI> getIRIs(String tableName, List<List<String>> rows, IOHel
if (id == null || id.trim().isEmpty()) {
continue;
}
IRI iri = ioHelper.createIRI(id);
IRI iri = ioHelper.createIRI(id, true);
if (iri == null) {
continue;
}
Expand Down Expand Up @@ -1225,29 +1225,19 @@ private static String getTemplate(String template) {
/**
* Given a checker, a template string, and a value for the template, return an IRI annotation.
*
* @param tableName name of table
* @param checker QuotedEntityChecker to resolve entities
* @param template template string
* @param value value to use with the template string
* @param rowNum the row number for logging
* @param column the column number for logging
* @return OWLAnnotation created from template and value
* @throws RowParseException if entities cannot be resolved
*/
private static OWLAnnotation maybeGetIRIAnnotation(
String tableName,
QuotedEntityChecker checker,
String template,
String value,
int rowNum,
int column)
throws Exception {
QuotedEntityChecker checker, String template, String value) throws Exception {
IRI iri = checker.getIRI(value, true);
if (iri != null) {
return getIRIAnnotation(checker, template, iri);
} else {
throw new RowParseException(String.format(iriError, rowNum, column + 1, tableName, value));
if (iri == null) {
iri = IRI.create(value);
}
return getIRIAnnotation(checker, template, iri);
}

/**
Expand Down
Loading