Search in sources :

Example 1 with Individual

use of won.shacl2java.annotation.Individual in project webofneeds by researchstudio-sat.

the class Shacl2JavaInstanceFactory method wireIndividuals.

private void wireIndividuals(Object instance, Field field, InstantiationContext ctx) {
    Individual individual = field.getAnnotation(Individual.class);
    if (individual == null) {
        return;
    }
    String[] individualUris = individual.value();
    if (individualUris == null || individualUris.length == 0) {
        return;
    }
    Stream.of(individualUris).flatMap(individualUri -> ctx.getInstancesForFocusNode(NodeFactory.createURI(individualUri)).stream()).parallel().forEach(individualInstance -> setDependency(instance, field.getName(), individualInstance));
}
Also used : Individual(won.shacl2java.annotation.Individual)

Example 2 with Individual

use of won.shacl2java.annotation.Individual 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

Individual (won.shacl2java.annotation.Individual)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 ValidationContext (org.apache.jena.shacl.engine.ValidationContext)1 ShapeNode (won.shacl2java.annotation.ShapeNode)1 ResettableErrorHandler (won.shacl2java.validation.ResettableErrorHandler)1