Skip to content

Commit 8f441d9

Browse files
RDFquality stage first try
1 parent 7fa28c7 commit 8f441d9

File tree

5 files changed

+69
-17
lines changed

5 files changed

+69
-17
lines changed

Jenkinsfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
#!/usr/bin/env groovy
22

33
def SPARQLendpoint = "http://172.16.0.81:58080/blazegraph/sparql"
4-
def NamedGraph = "http://lod.eurohelp.es/dataset/parkings"
54
def URLParkingsDonosti = "http://www.donostia.eus/info/ciudadano/camaras_trafico.nsf/dameParkings?OpenAgent&idioma=cas"
65
def CSVParkingsClean = "donostiaparkingsclean.csv"
7-
def RDFParkingsClean = "donostiaparkingsclean.nquads"
6+
def RDFParkingsClean = "donostiaparkingsclean.nq"
7+
def NamedGraph = "http://lod.eurohelp.es/dataset/parkings"
8+
def SHACLfile = "shacl/parkings.ttl"
9+
def SHACLReportCheckingQuery = "shacl/report.sparql"
10+
def SHACLReportFile = "shacl/report.ttl"
811

912
node {
10-
stage('Clean blazegraph named graph'){
13+
stage('Remove data from blazegraph'){
1114
sh 'curl --get -X DELETE -H "Accept: application/xml" ' + SPARQLendpoint + ' --data-urlencode "?c=<' + NamedGraph + '>"'
1215
}
1316
stage('Checkout pipeline') {
14-
git branch: 'develop', url: 'https://github.com/Eurohelp/Replicate-LinkedOpenData-Datasets.git'
17+
git branch: 'RDFTests', url: 'https://github.com/Eurohelp/Replicate-LinkedOpenData-Datasets.git'
1518
}
1619
stage('Obtain and clean data') {
1720
sh 'java -jar loadparkingdata/cleanparkingdata.jar "' + URLParkingsDonosti + '" ' + CSVParkingsClean + ' temp_parkingsclean temp_parkingsclean.json'
1821
}
1922
stage('Convert CSV to RDF') {
2023
sh 'java -jar csv2rdf/csvparkingsclean2rdf.jar ' + CSVParkingsClean + ' ' + RDFParkingsClean + ' ' + NamedGraph
2124
}
25+
stage('RDF quality') {
26+
sh 'java -jar rdfquality/SHACLValidator.jar ' + RDFParkingsClean + ' ' + NamedGraph + ' ' + SHACLfile + ' ' + SHACLReportCheckingQuery + ' ' + SHACLfile
27+
}
2228
stage('Upload RDF to blazegraph') {
2329
sh 'curl -X POST -H Content-Type:text/x-nquads --data-binary @' + RDFParkingsClean + ' ' + SPARQLendpoint
2430
}

rdfquality/SHACLValidator.jar

189 Bytes
Binary file not shown.

rdfquality/src/main/java/donosti/lod/replicate/eurohelp/es/SHACLValidator.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44
import java.io.FileWriter;
5-
import java.io.IOException;
65

76
import org.apache.commons.io.FileUtils;
87
import org.apache.jena.query.Query;
@@ -12,39 +11,44 @@
1211
import org.apache.jena.rdf.model.Model;
1312
import org.apache.jena.rdf.model.ModelFactory;
1413
import org.apache.jena.rdf.model.Resource;
15-
import org.topbraid.shacl.util.ModelPrinter;
14+
import org.apache.jena.riot.RDFDataMgr;
1615
import org.topbraid.shacl.validation.ValidationUtil;
1716

1817
public class SHACLValidator {
1918

2019
public static void main(String[] args) throws Exception {
2120
String targetFile = args[0];
22-
String SHACLFile = args[1];
23-
String reportCheckingQueryFile = args[2];
24-
String reportFile = args[3];
25-
26-
Model model = ModelFactory.createDefaultModel();
27-
model.read(targetFile);
28-
21+
String namedGraphURI = args [1];
22+
String SHACLFile = args[2];
23+
String reportCheckingQueryFile = args[3];
24+
String reportFile = args[4];
25+
26+
// Load the data to validate
27+
Model model = RDFDataMgr.loadDataset(targetFile).getNamedModel(namedGraphURI);
28+
29+
// Load the quality tests
2930
Model shacl = ModelFactory.createDefaultModel();
3031
shacl.read(SHACLFile);
3132

33+
// Create a validation report (execute the tests)
3234
Resource report = ValidationUtil.validateModel(model, shacl, true);
3335

34-
// System.out.println(ModelPrinter.get().print(report.getModel()));
35-
36+
// Write report to disk
3637
FileWriter out = new FileWriter(reportFile);
37-
3838
report.getModel().write(out, "TURTLE");
3939

40+
// Query report to check if data is conformant
4041
String reportCheckingQuery = FileUtils.readFileToString(new File(reportCheckingQueryFile));
4142
Query query = QueryFactory.create(reportCheckingQuery);
4243
QueryExecution qexec = QueryExecutionFactory.create(query, report.getModel());
4344
boolean result = qexec.execAsk();
4445
qexec.close();
46+
47+
// Data is not conformant
4548
if (result) {
46-
throw new Exception("SHACL violation: non-conformant RDF");
49+
throw new Exception("SHACL violation: non-conformant RDF, see report at " + reportFile);
4750
}
51+
// Conformant data
4852
else{
4953
System.out.println("Valid RDF");
5054
}

shacl/parkings.ttl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
2+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
3+
@prefix sh:<http://www.w3.org/ns/shacl#> .
4+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
5+
@prefix schema: <http://schema.org/> .
6+
@prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> .
7+
@prefix ex:<http://lod.eurohelp.es/replicate/shacl/parkings> .
8+
9+
# A parking should have a latitude with xsd:double value between 43.2 and 43.4
10+
11+
ex:ParkingShape
12+
a sh:NodeShape ;
13+
sh:targetClass schema:ParkingFacility ;
14+
sh:property [
15+
sh:path geo:lat ;
16+
sh:datatype xsd:double ;
17+
sh:minInclusive 43.2 ;
18+
sh:maxInclusive 43.4 ;
19+
] .
20+
21+
# Since all the data is conformant, this shape can be used to make sure the thing works,
22+
# since it will flag valid data as invalid (the sh:minInclusive value is 43.3 instead of 43.2)
23+
# To activate it comment out the line sh:deactivated true ;
24+
25+
ex:ParkingShapeDebug
26+
a sh:NodeShape ;
27+
sh:targetClass schema:ParkingFacility ;
28+
#sh:deactivated true ;
29+
sh:property [
30+
sh:path geo:lat ;
31+
sh:datatype xsd:double ;
32+
sh:minInclusive 43.3 ;
33+
sh:maxInclusive 43.4 ;
34+
] .

shacl/report.sparql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
2+
PREFIX sh: <http://www.w3.org/ns/shacl#>
3+
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
4+
5+
ASK WHERE {
6+
?ValidationReport rdf:type sh:ValidationReport .
7+
?ValidationReport sh:conforms "false"^^xsd:boolean .
8+
}

0 commit comments

Comments
 (0)