Search in sources :

Example 1 with ShaclSailValidationException

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;
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ServletException(jakarta.servlet.ServletException)

Example 2 with ShaclSailValidationException

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 !!!");
        }
    }
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) StringReader(java.io.StringReader) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) IOException(java.io.IOException)

Example 3 with ShaclSailValidationException

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");
    }
}
Also used : MemoryStore(org.eclipse.rdf4j.sail.memory.MemoryStore) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException) ValidationException(nl.dtls.fairdatapoint.entity.exception.ValidationException) RdfValidationException(nl.dtls.fairdatapoint.entity.exception.RdfValidationException) RdfValidationException(nl.dtls.fairdatapoint.entity.exception.RdfValidationException) SailRepository(org.eclipse.rdf4j.repository.sail.SailRepository) Model(org.eclipse.rdf4j.model.Model) ShaclSail(org.eclipse.rdf4j.sail.shacl.ShaclSail) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) SailRepositoryConnection(org.eclipse.rdf4j.repository.sail.SailRepositoryConnection) ShaclSailValidationException(org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException)

Aggregations

Model (org.eclipse.rdf4j.model.Model)3 SailRepository (org.eclipse.rdf4j.repository.sail.SailRepository)3 SailRepositoryConnection (org.eclipse.rdf4j.repository.sail.SailRepositoryConnection)3 MemoryStore (org.eclipse.rdf4j.sail.memory.MemoryStore)3 ShaclSail (org.eclipse.rdf4j.sail.shacl.ShaclSail)3 ShaclSailValidationException (org.eclipse.rdf4j.sail.shacl.ShaclSailValidationException)3 ServletException (jakarta.servlet.ServletException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 RdfValidationException (nl.dtls.fairdatapoint.entity.exception.RdfValidationException)1 ValidationException (nl.dtls.fairdatapoint.entity.exception.ValidationException)1 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)1