Search in sources :

Example 1 with Individuals

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);
                }
            }
        }
    }
}
Also used : ShapeNode(won.shacl2java.annotation.ShapeNode) VLib.isFocusNode(org.apache.jena.shacl.validation.VLib.isFocusNode) Node(org.apache.jena.graph.Node) NameUtils.setterNameForField(won.shacl2java.util.NameUtils.setterNameForField) Graph(org.apache.jena.graph.Graph) ShapeNode(won.shacl2java.annotation.ShapeNode) Individuals(won.shacl2java.annotation.Individuals)

Aggregations

Graph (org.apache.jena.graph.Graph)1 Node (org.apache.jena.graph.Node)1 VLib.isFocusNode (org.apache.jena.shacl.validation.VLib.isFocusNode)1 Individuals (won.shacl2java.annotation.Individuals)1 ShapeNode (won.shacl2java.annotation.ShapeNode)1 NameUtils.setterNameForField (won.shacl2java.util.NameUtils.setterNameForField)1