Search in sources :

Example 1 with ResettableErrorHandler

use of won.shacl2java.validation.ResettableErrorHandler 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 2 with ResettableErrorHandler

use of won.shacl2java.validation.ResettableErrorHandler in project webofneeds by researchstudio-sat.

the class IndividualsGenerator method generateIndividuals.

private TypeSpec generateIndividuals(Shapes shapes, Shacl2JavaConfig config) {
    Set<String> individualNames = new HashSet<>();
    ResettableErrorHandler err = new ResettableErrorHandler();
    ValidationContext vCtx = ValidationContext.create(shapes, shapes.getGraph(), err);
    TypeSpec.Builder individualsTypeBuilder = TypeSpec.classBuilder("Individuals").addModifiers(PUBLIC).addAnnotation(AnnotationSpec.builder(Individuals.class).build());
    StreamSupport.stream(Spliterators.spliteratorUnknownSize(shapes.iteratorAll(), Spliterator.ORDERED), false).distinct().forEach(shape -> {
        // find focus nodes in the shapes graph itself
        Collection<Node> focusNodes = focusNodes(shapes.getGraph(), shape);
        for (Node focusNode : focusNodes) {
            if (focusNode.isBlank() || focusNode.isLiteral()) {
                continue;
            }
            err.reset();
            VLib.validateShape(vCtx, shapes.getGraph(), shape, focusNode);
            if (err.isError() || err.isFatal()) {
                continue;
            }
            String name = NameUtils.enumConstantName(focusNode.getURI());
            if (individualNames.contains(name)) {
                name = NameUtils.enumConstantName(focusNode.getURI()) + "_" + NameUtils.enumConstantName(NameUtils.classNameForShape(shape, config));
                if (individualNames.contains(name)) {
                    throw new IllegalStateException("Name clash while creating individual: " + name);
                }
            }
            individualNames.add(name);
            String typeName = NameUtils.classNameForShape(shape, config);
            ClassName type = ClassName.get(config.getPackageName(), typeName);
            // remember so we can inject individuals into shape-classes
            individualClassNames.put(focusNode, type);
            FieldSpec fieldSpec = FieldSpec.builder(type, name).addModifiers(PUBLIC, STATIC, FINAL).addAnnotation(AnnotationSpec.builder(ClassName.get(Individual.class)).addMember("value", "$S", focusNode.getURI()).build()).addAnnotation(AnnotationSpec.builder(ShapeNode.class).addMember("value", "{ $S }", shape.getShapeNode().getURI()).build()).initializer("new $T($S)", type, focusNode.getURI()).build();
            individualsTypeBuilder.addField(fieldSpec);
        }
    });
    return individualsTypeBuilder.build();
}
Also used : ShapeNode(won.shacl2java.annotation.ShapeNode) Node(org.apache.jena.graph.Node) FieldSpec(com.squareup.javapoet.FieldSpec) ValidationContext(org.apache.jena.shacl.engine.ValidationContext) ResettableErrorHandler(won.shacl2java.validation.ResettableErrorHandler) Individual(won.shacl2java.annotation.Individual) ClassName(com.squareup.javapoet.ClassName) ShapeNode(won.shacl2java.annotation.ShapeNode) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ValidationContext (org.apache.jena.shacl.engine.ValidationContext)2 ResettableErrorHandler (won.shacl2java.validation.ResettableErrorHandler)2 ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Node (org.apache.jena.graph.Node)1 Union (org.apache.jena.graph.compose.Union)1 ValidationReport (org.apache.jena.shacl.ValidationReport)1 Shape (org.apache.jena.shacl.parser.Shape)1 Individual (won.shacl2java.annotation.Individual)1 ShapeNode (won.shacl2java.annotation.ShapeNode)1