use of won.shacl2java.annotation.Individuals 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);
}
}
}
}
}
Aggregations