use of org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException in project RDFConverter by Mat-O-Lab.
the class RDFValidator method validateRDF.
protected static String validateRDF(String rdf, String shaclShapes) throws UnsupportedEncodingException {
String ret = null;
boolean bValid = true;
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try {
SailRepositoryConnection connection = sailRepository.getConnection();
connection.begin();
StringReader shaclRules = new StringReader(shaclShapes);
connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
connection.begin();
StringReader rdfData = new StringReader(rdf);
connection.add(rdfData, "", RDFFormat.TURTLE);
connection.commit();
} catch (Exception exception) {
Throwable cause = exception.getCause();
bValid = false;
if (cause instanceof ShaclSailValidationException) {
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
ret = baos.toString(String.valueOf(StandardCharsets.UTF_8));
}
if (cause == null) {
ret = "fail: " + exception.getMessage();
} else {
ret = "fail: " + ret;
}
}
if (bValid) {
ret = "VALID";
}
return ret;
}
use of org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException in project RDFConverter by Mat-O-Lab.
the class ShaclSampleCode method main.
public static void main(String[] args) throws IOException {
boolean bValid = true;
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
// Logger root = (Logger) LoggerFactory.getLogger(ShaclSail.class.getName());
// root.setLevel(Level.INFO);
// shaclSail.setLogValidationPlans(true);
// shaclSail.setGlobalLogValidationExecution(true);
// shaclSail.setLogValidationViolations(true);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
connection.begin();
StringReader shaclRules = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix sh: <http://www.w3.org/ns/shacl#> .", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "ex:PersonShape", " a sh:NodeShape ;", " sh:targetClass foaf:Person ;", " sh:property ex:PersonShapeProperty .", "ex:PersonShapeProperty ", " sh:path foaf:age ;", " sh:datatype xsd:int ;", " sh:maxCount 1 ;", " sh:minCount 1 ."));
connection.add(shaclRules, "", RDFFormat.TURTLE, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
connection.begin();
StringReader invalidSampleData = new StringReader(String.join("\n", "", "@prefix ex: <http://example.com/ns#> .", "@prefix foaf: <http://xmlns.com/foaf/0.1/>.", "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .", "ex:peter a foaf:Person ;", " foaf:age 20, \"30\"^^xsd:int ."));
connection.add(invalidSampleData, "", RDFFormat.TURTLE);
try {
connection.commit();
} catch (Exception exception) {
Throwable cause = exception.getCause();
if (cause instanceof ShaclSailValidationException) {
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
// Rio.write(validationReportModel, System.out, RDFFormat.TURTLE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Rio.write(validationReportModel, baos, RDFFormat.TURTLE);
System.err.println(baos.toString(StandardCharsets.UTF_8.toString()));
bValid = false;
}
if (!bValid) {
System.err.println("RDF NOT VALID !!! Throw Exception !!!");
}
throw exception;
}
if (bValid) {
System.out.println("RDF VALID !!!");
}
}
}
use of org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException in project FAIRDataPoint by FAIRDataTeam.
the class ShaclValidator method validate.
public void validate(Model shacl, Model data, String baseUri) {
// 1. Prepare repository
ShaclSail shaclSail = new ShaclSail(new MemoryStore());
shaclSail.setRdfsSubClassReasoning(true);
shaclSail.setUndefinedTargetValidatesAllSubjects(true);
SailRepository sailRepository = new SailRepository(shaclSail);
sailRepository.init();
try (SailRepositoryConnection connection = sailRepository.getConnection()) {
// 2. Save Shacl
connection.begin();
connection.add(shacl, RDF4J.SHACL_SHAPE_GRAPH);
connection.commit();
// 3. Validate data
connection.begin();
connection.add(new ArrayList<>(data), i(baseUri));
connection.commit();
} catch (RepositoryException exception) {
Throwable cause = exception.getCause();
if (cause instanceof ShaclSailValidationException) {
Model validationReportModel = ((ShaclSailValidationException) cause).validationReportAsModel();
throw new RdfValidationException(validationReportModel);
}
throw new ValidationException("Validation failed (unsupported exception");
}
}
Aggregations