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);
}
}
}
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));
}
}
}
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);
}
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();
}
}
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);
}
Aggregations