Search in sources :

Example 1 with ValidationReport

use of org.apache.jena.shacl.ValidationReport in project webofneeds by researchstudio-sat.

the class WonAclEvaluatorTest method validateTestData.

public void validateTestData(Shapes shapes, Graph graph, boolean failTest, String testCaseIdentifier) {
    ValidationReport shaclReport = ShaclSystem.get().validate(shapes, new Union(shapes.getGraph(), graph));
    if (failTest) {
        if (!shaclReport.conforms()) {
            ShLib.printReport(shaclReport);
            System.out.println();
            RDFDataMgr.write(System.out, shaclReport.getModel(), Lang.TTL);
        }
        Assert.assertTrue(String.format("%s: test data invalid", testCaseIdentifier), shaclReport.conforms());
    } else {
        if (!shaclReport.conforms()) {
            logger.info("failed SHACL validation: {}", testCaseIdentifier);
        }
    }
}
Also used : ValidationReport(org.apache.jena.shacl.ValidationReport) Union(org.apache.jena.graph.compose.Union)

Example 2 with ValidationReport

use of org.apache.jena.shacl.ValidationReport in project webofneeds by researchstudio-sat.

the class AuthShapeTest method assertConformsTo.

private void assertConformsTo(Node focusNode, Node shapeNode, Shapes shapes, Graph data, boolean expected) {
    data = new Union(data, shapes.getGraph());
    ResettableErrorHandler handler = new ResettableErrorHandler();
    Shape shape = shapes.getShape(shapeNode);
    if (shape == null) {
        throw new IllegalArgumentException("no such shape: " + shapeNode);
    }
    boolean isFocusNode = VLib.isFocusNode(shape, focusNode, data);
    if (expected && !isFocusNode) {
        Assert.fail(String.format("%s should be focus node of %s", focusNode, shape));
    }
    if (!expected && isFocusNode) {
        Assert.fail(String.format("%s should not be focus node of %s", focusNode, shape));
    }
    if (isFocusNode) {
        ValidationContext vCtx = ValidationContext.create(shapes, shapes.getGraph(), handler);
        VLib.validateShape(vCtx, data, shapes.getShape(shapeNode), focusNode);
        if (vCtx.hasViolation()) {
            ValidationReport report = vCtx.generateReport();
            printNonconformingReport(report);
            Assert.fail(String.format("Data does not conform to shapes"));
        }
        if (handler.isError() || handler.isFatal()) {
            Assert.fail(String.format("Node %s %s to shape %s", focusNode, expected ? "does not conform" : "unexpectedly conforms", shapeNode));
        }
    }
}
Also used : Shape(org.apache.jena.shacl.parser.Shape) ValidationReport(org.apache.jena.shacl.ValidationReport) ResettableErrorHandler(won.shacl2java.validation.ResettableErrorHandler) Union(org.apache.jena.graph.compose.Union) ValidationContext(org.apache.jena.shacl.engine.ValidationContext)

Example 3 with ValidationReport

use of org.apache.jena.shacl.ValidationReport in project jena by apache.

the class shacl_validate method exec.

@Override
protected void exec() {
    Graph shapesGraph = load(shapesfile, "shapes file");
    Graph dataGraph;
    if (datafile.equals(shapesfile))
        dataGraph = shapesGraph;
    else
        dataGraph = load(datafile, "data file");
    Node node = null;
    if (targetNode != null) {
        String x = dataGraph.getPrefixMapping().expandPrefix(targetNode);
        node = NodeFactory.createURI(x);
    }
    if (isVerbose())
        ValidationContext.VERBOSE = true;
    ValidationReport report = (node != null) ? ShaclValidator.get().validate(shapesGraph, dataGraph, node) : ShaclValidator.get().validate(shapesGraph, dataGraph);
    if (textOutput)
        ShLib.printReport(report);
    else
        RDFDataMgr.write(System.out, report.getGraph(), Lang.TTL);
}
Also used : Graph(org.apache.jena.graph.Graph) ValidationReport(org.apache.jena.shacl.ValidationReport) Node(org.apache.jena.graph.Node)

Example 4 with ValidationReport

use of org.apache.jena.shacl.ValidationReport in project jena by apache.

the class SHACL_Validation method doPost.

@Override
protected void doPost(HttpAction action) {
    // Response syntax
    MediaType mediaType = ActionLib.contentNegotation(action, DEF.rdfOffer, DEF.acceptTurtle);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentTypeStr());
    if (lang == null)
        lang = RDFLanguages.TTL;
    String targetNodeStr = action.getRequestParameter(HttpNames.paramTarget);
    action.beginRead();
    try {
        GraphTarget graphTarget = determineTarget(action.getActiveDSG(), action);
        if (!graphTarget.exists())
            ServletOps.errorNotFound("No data graph: " + graphTarget.label());
        Graph data = graphTarget.graph();
        Graph shapesGraph = ActionLib.readFromRequest(action, Lang.TTL);
        Node targetNode = null;
        if (targetNodeStr != null) {
            String x = data.getPrefixMapping().expandPrefix(targetNodeStr);
            targetNode = NodeFactory.createURI(x);
        }
        Shapes shapes = Shapes.parse(shapesGraph);
        ValidationReport report = (targetNode == null) ? ShaclValidator.get().validate(shapesGraph, data) : ShaclValidator.get().validate(shapesGraph, data, targetNode);
        if (report.conforms())
            action.log.info(format("[%d] shacl: conforms", action.id));
        else
            action.log.info(format("[%d] shacl: %d validation errors", action.id, report.getEntries().size()));
        report.getEntries().size();
        action.setResponseStatus(HttpSC.OK_200);
        ActionLib.graphResponse(action, report.getGraph(), lang);
    } finally {
        action.endRead();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) ValidationReport(org.apache.jena.shacl.ValidationReport) Node(org.apache.jena.graph.Node) MediaType(org.apache.jena.atlas.web.MediaType) Lang(org.apache.jena.riot.Lang) Shapes(org.apache.jena.shacl.Shapes)

Example 5 with ValidationReport

use of org.apache.jena.shacl.ValidationReport in project jena by apache.

the class TestValidationReport method testRoundTripGraph.

private void testRoundTripGraph(String message, Graph graph1) {
    ValidationReport report = ValidationReport.fromGraph(graph1);
    Resource r = report.getResource();
    Graph graph2 = r.getModel().getGraph();
    boolean b = graph1.isIsomorphicWith(graph2);
    if (!b) {
        System.out.println("++++");
        RDFDataMgr.write(System.out, graph1, Lang.TTL);
        System.out.println("----");
        RDFDataMgr.write(System.out, graph2, Lang.TTL);
        System.out.flush();
    }
    assertTrue("Does not match: " + message, b);
}
Also used : Graph(org.apache.jena.graph.Graph) ValidationReport(org.apache.jena.shacl.ValidationReport) Resource(org.apache.jena.rdf.model.Resource)

Aggregations

ValidationReport (org.apache.jena.shacl.ValidationReport)24 RDFConnection (org.apache.jena.rdfconnection.RDFConnection)9 Test (org.junit.Test)9 Graph (org.apache.jena.graph.Graph)7 Union (org.apache.jena.graph.compose.Union)5 Model (org.apache.jena.rdf.model.Model)3 Shapes (org.apache.jena.shacl.Shapes)3 IOException (java.io.IOException)2 Node (org.apache.jena.graph.Node)2 Resource (org.apache.jena.rdf.model.Resource)2 ValidationContext (org.apache.jena.shacl.engine.ValidationContext)2 MediaType (org.apache.jena.atlas.web.MediaType)1 FusekiServer (org.apache.jena.fuseki.main.FusekiServer)1 Operation (org.apache.jena.fuseki.server.Operation)1 Lang (org.apache.jena.riot.Lang)1 Shape (org.apache.jena.shacl.parser.Shape)1 ResettableErrorHandler (won.shacl2java.validation.ResettableErrorHandler)1