Skip to content

Commit c7af55b

Browse files
author
Petr Křemen
committed
[ontodev#1017] Added check for empty labels into missing_label.rq.
1 parent f99c8e8 commit c7af55b

File tree

5 files changed

+117
-17
lines changed

5 files changed

+117
-17
lines changed

docs/report_queries/missing_label.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Missing Label
2-
3-
**Problem:** An entity does not have a label. This may cause confusion or misuse. Excludes deprecated entities.
4-
5-
**OBO Foundry Principle:** [12 - Naming Conventions](http://www.obofoundry.org/principles/fp-012-naming-conventions.html)
6-
7-
**Solution:** Add a label.
1+
# # Missing Label
2+
#
3+
# **Problem:** An entity does not have a label or the label is empty. This may cause confusion or misuse. Excludes deprecated entities.
4+
#
5+
# **OBO Foundry Principle:** [12 - Naming Conventions](http://www.obofoundry.org/principles/fp-012-naming-conventions.html)
6+
#
7+
# **Solution:** Add a label, or make it non-empty.
88

99
```sparql
1010
PREFIX owl: <http://www.w3.org/2002/07/owl#>
@@ -15,7 +15,10 @@ PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
1515
SELECT DISTINCT ?entity ?property ?value WHERE {
1616
VALUES ?property { rdfs:label }
1717
?entity ?any ?o .
18-
FILTER NOT EXISTS { ?entity ?property ?value }
18+
FILTER NOT EXISTS {
19+
?entity ?property ?value .
20+
FILTER(str(?value) != "")
21+
}
1922
FILTER NOT EXISTS { ?entity a owl:Ontology }
2023
FILTER NOT EXISTS { ?entity owl:deprecated true }
2124
FILTER NOT EXISTS {

robot-core/src/main/java/org/obolibrary/robot/ReportOperation.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.net.URL;
66
import java.net.URLDecoder;
77
import java.nio.charset.Charset;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
811
import java.util.*;
912
import java.util.jar.JarEntry;
1013
import java.util.jar.JarFile;
14+
import java.util.stream.Collectors;
1115
import org.apache.commons.io.FileUtils;
1216
import org.apache.jena.query.*;
1317
import org.apache.jena.tdb.TDBFactory;
@@ -695,20 +699,21 @@ private static Map<String, String> getDefaultQueryStrings(Set<String> rules)
695699
Map<String, String> queries = new HashMap<>();
696700
// Handle simple file path, probably accessed during testing
697701
if (dirURL != null && dirURL.getProtocol().equals("file")) {
698-
String[] queryFilePaths = new File(dirURL.toURI()).list();
699-
if (queryFilePaths == null || queryFilePaths.length == 0) {
702+
final Set<Path> queryFilePaths =
703+
Files.list(Paths.get(dirURL.toURI())).collect(Collectors.toSet());
704+
if (queryFilePaths.size() == 0) {
700705
throw new IOException(
701706
"Cannot access report query files. There are no files in the directory.");
702707
}
703708
Set<String> defaultRuleNames = new HashSet<>();
704-
for (String qPath : queryFilePaths) {
705-
String ruleName = qPath.substring(qPath.lastIndexOf("/")).split(".")[0];
709+
for (final Path qPath : queryFilePaths) {
710+
final String ruleName = qPath.getFileName().toString().split("[.]")[0];
706711
defaultRuleNames.add(ruleName);
707712
// Only add it to the queries if the rule set contains that rule
708713
// If rules == null, include all rules
709714
if (rules == null || rules.contains(ruleName)) {
710715
queries.put(
711-
ruleName, FileUtils.readFileToString(new File(qPath), Charset.defaultCharset()));
716+
ruleName, FileUtils.readFileToString(qPath.toFile(), Charset.defaultCharset()));
712717
}
713718
}
714719
// Check that all the provided rule names are valid defaults
@@ -841,7 +846,7 @@ private static String getQueryResultOrNull(QuerySolution qs, String var) {
841846
* @return List of Violations
842847
* @throws IOException on issue parsing query
843848
*/
844-
private static List<Violation> getViolations(
849+
public static List<Violation> getViolations(
845850
IOHelper ioHelper,
846851
Dataset dataset,
847852
String queryName,

robot-core/src/main/resources/report_queries/missing_label.rq

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# # Missing Label
22
#
3-
# **Problem:** An entity does not have a label. This may cause confusion or misuse. Excludes deprecated entities.
3+
# **Problem:** An entity does not have a label or the label is empty. This may cause confusion or misuse. Excludes deprecated entities.
44
#
55
# **OBO Foundry Principle:** [12 - Naming Conventions](http://www.obofoundry.org/principles/fp-012-naming-conventions.html)
66
#
7-
# **Solution:** Add a label.
7+
# **Solution:** Add a label, or make it non-empty.
88

99
PREFIX owl: <http://www.w3.org/2002/07/owl#>
1010
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@@ -14,7 +14,10 @@ PREFIX oboInOwl: <http://www.geneontology.org/formats/oboInOwl#>
1414
SELECT DISTINCT ?entity ?property ?value WHERE {
1515
VALUES ?property { rdfs:label }
1616
?entity ?any ?o .
17-
FILTER NOT EXISTS { ?entity ?property ?value }
17+
FILTER NOT EXISTS {
18+
?entity ?property ?value .
19+
FILTER(str(?value) != "")
20+
}
1821
FILTER NOT EXISTS { ?entity a owl:Ontology }
1922
FILTER NOT EXISTS { ?entity owl:deprecated true }
2023
FILTER NOT EXISTS {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.obolibrary.robot;
2+
3+
import java.io.InputStream;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
6+
import org.apache.jena.query.Dataset;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.Parameterized;
11+
import org.obolibrary.robot.checks.Violation;
12+
import org.semanticweb.owlapi.model.OWLOntology;
13+
14+
@RunWith(Parameterized.class)
15+
public class ReportQueriesTest extends CoreTest {
16+
17+
@Parameterized.Parameters
18+
public static Collection<Object[]> data() {
19+
return Arrays.asList(
20+
new Object[][] {
21+
{
22+
"missing_label",
23+
"/1017-empty-label-input.owl",
24+
new String[] {
25+
"http://test.org/test#B", "http://test.org/test#C", "http://test.org/test#D"
26+
}
27+
}
28+
});
29+
}
30+
31+
private final String inputFile;
32+
private final String queryName;
33+
private final String[] failingEntities;
34+
35+
public ReportQueriesTest(
36+
final String queryFile, final String inputFile, final String[] failingEntities) {
37+
this.queryName = queryFile;
38+
this.inputFile = inputFile;
39+
this.failingEntities = failingEntities;
40+
}
41+
42+
/** Test report queries */
43+
@Test
44+
public void testReportQuery() throws Exception {
45+
final OWLOntology ontology = loadOntology(inputFile);
46+
final Dataset dataset = QueryOperation.loadOntologyAsDataset(ontology, false);
47+
48+
final InputStream is = getClass().getResourceAsStream("/report_queries/" + queryName + ".rq");
49+
assert is != null;
50+
final String query =
51+
new Scanner(is, "UTF-8").useDelimiter("" + Character.LINE_SEPARATOR).next();
52+
final List<Violation> violations =
53+
ReportOperation.getViolations(
54+
new IOHelper(), dataset, queryName, query, Collections.emptyMap());
55+
56+
final Set<String> expected = new HashSet<>(Arrays.asList(failingEntities));
57+
assert violations != null;
58+
final Set<String> actual =
59+
violations.stream().map(v -> v.entity.getIRI().toString()).collect(Collectors.toSet());
60+
Assert.assertEquals(expected, actual);
61+
}
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<rdf:RDF
2+
xmlns:obo="http://purl.obolibrary.org/obo/"
3+
xmlns:owl="http://www.w3.org/2002/07/owl#"
4+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
5+
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
6+
xmlns:xml="http://www.w3.org/XML/1998/namespace">
7+
<owl:Ontology rdf:about="http://test.org/test"/>
8+
9+
<owl:Class rdf:about="http://test.org/test#A"/>
10+
11+
<owl:Class rdf:about="http://test.org/test#B">
12+
<obo:IAO_0000115>Sample definition</obo:IAO_0000115>
13+
</owl:Class>
14+
15+
<owl:Class rdf:about="http://test.org/test#C">
16+
<rdfs:label></rdfs:label>
17+
</owl:Class>
18+
19+
<owl:Class rdf:about="http://test.org/test#D">
20+
<rdfs:label xml:lang="en"></rdfs:label>
21+
</owl:Class>
22+
23+
<owl:Class rdf:about="http://test.org/test#E">
24+
<rdfs:label>E</rdfs:label>
25+
</owl:Class>
26+
27+
</rdf:RDF>

0 commit comments

Comments
 (0)