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