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