use of won.shacl2java.annotation.ShapeNode in project webofneeds by researchstudio-sat.
the class Shacl2JavaInstanceFactory method scanPackages.
private void scanPackages(InstantiationContext ctx) {
// we pass this graph to individuals to hide their triples in the
// base graph when generating RDF using RdfOutput
Graph graphForIndividuals = GraphFactory.createGraphMem();
try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(packagesToScan).scan()) {
ClassInfoList shapeClassInfoList = scanResult.getClassesWithAnnotation(ShapeNode.class.getName());
for (ClassInfo shapeClassInfo : shapeClassInfoList) {
AnnotationInfo annotationInfo = shapeClassInfo.getAnnotationInfo(ShapeNode.class.getName());
AnnotationParameterValueList paramVals = annotationInfo.getParameterValues();
AnnotationParameterValue nodes = paramVals.get("value");
String[] shapeNodes = (String[]) nodes.getValue();
for (int i = 0; i < shapeNodes.length; i++) {
ctx.addClassForShape(shapeNodes[i], shapeClassInfo.loadClass());
}
}
ClassInfoList individualClassInfoList = scanResult.getClassesWithAnnotation(Individuals.class.getName());
for (ClassInfo individualClassInfo : individualClassInfoList) {
Class<?> individualType = individualClassInfo.loadClass();
for (FieldInfo field : individualClassInfo.getFieldInfo()) {
Object instance = null;
try {
Field rField = individualType.getField(field.getName());
if (rField != null) {
instance = rField.get(null);
} else {
continue;
}
// extract shape and focusNode, and add them to the data structures
AnnotationInfo annotationInfo = field.getAnnotationInfo(ShapeNode.class.getName());
AnnotationParameterValueList paramVals = annotationInfo.getParameterValues();
AnnotationParameterValue nodes = paramVals.get("value");
String shapeNodeStr = ((String[]) nodes.getValue())[0];
Node shapeNode = NodeFactory.createURI(shapeNodeStr);
annotationInfo = field.getAnnotationInfo(Individual.class.getName());
paramVals = annotationInfo.getParameterValues();
nodes = paramVals.get("value");
String focusNodeStr = ((String[]) nodes.getValue())[0];
Node focusNode = NodeFactory.createURI(focusNodeStr);
Method m = instance.getClass().getMethod("setNode", Node.class);
if (m != null) {
m.invoke(instance, focusNode);
} else {
continue;
}
m = instance.getClass().getMethod("setGraph", Graph.class);
if (m != null) {
m.invoke(instance, graphForIndividuals);
} else {
continue;
}
ctx.addInstanceForFocusNode(focusNode, instance);
ctx.setFocusNodeForInstance(instance, focusNode);
ctx.setClassForInstance(instance, instance.getClass());
ctx.addShapeForFocusNode(focusNode, this.shapes.getShape(shapeNode));
} catch (Exception e) {
throw new IllegalStateException("Cannot set node using setNode() on instance " + instance, e);
}
}
}
}
}
use of won.shacl2java.annotation.ShapeNode in project webofneeds by researchstudio-sat.
the class Shacl2JavaInstanceFactory method instantiateAll.
private void instantiateAll(InstantiationContext ctx) {
Set<DataNodeAndShapes> toInstantiate = null;
while (toInstantiate == null || toInstantiate.size() > 0) {
if (toInstantiate == null) {
Set<Shape> shapeSet = new HashSet();
Iterator<Shape> it = shapes.iteratorAll();
while (it.hasNext()) {
shapeSet.add(it.next());
}
toInstantiate = shapeSet.stream().map(shape -> {
return instantiate(null, shape, false, ctx);
}).reduce(CollectionUtils::union).orElseGet(() -> Collections.emptySet());
toInstantiate = deduplicate(toInstantiate);
} else {
toInstantiate = toInstantiate.parallelStream().map(dataNodeAndShapes -> {
if (dataNodeAndShapes.getShapeNodes().isEmpty()) {
return StreamSupport.stream(shapes.spliterator(), true).map(shape -> instantiate(dataNodeAndShapes.getDataNode(), shape, false, ctx)).reduce(CollectionUtils::union).orElseGet(() -> Collections.emptySet());
} else {
return dataNodeAndShapes.getShapeNodes().parallelStream().map(shapeNode -> instantiate(dataNodeAndShapes.getDataNode(), shapes.getShape(shapeNode), true, ctx)).reduce(CollectionUtils::union).orElseGet(() -> Collections.emptySet());
}
}).reduce(CollectionUtils::union).orElseGet(() -> Collections.emptySet());
toInstantiate = deduplicate(toInstantiate);
}
}
ctx.getInstancesByNode().stream().forEach(entry -> {
Node node = entry.getKey();
Set<Object> instances = entry.getValue();
for (Object instance : instances) {
if (logger.isDebugEnabled()) {
logger.debug("wiring dependencies of instance {} ", node);
}
wireDependencies(instance, ctx);
}
});
}
use of won.shacl2java.annotation.ShapeNode in project webofneeds by researchstudio-sat.
the class MainTypesGenerator method getJavadocGeneratedForShape.
private static String getJavadocGeneratedForShape(Shape shape) {
Node shapeNode = shape.getShapeNode();
if (shapeNode.isBlank()) {
return "Generated for an anonymous shape";
}
String uri = shapeNode.getURI();
return String.format("Generated for shape <a href=\"%s\">%s</a>", uri, uri);
}
Aggregations