Search in sources :

Example 1 with ShapeNode

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

Example 2 with ShapeNode

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);
        }
    });
}
Also used : PropertyPath(won.shacl2java.annotation.PropertyPath) java.util(java.util) NodeFactory(org.apache.jena.graph.NodeFactory) PathParser(org.apache.jena.sparql.path.PathParser) InstantiationContext(won.shacl2java.instantiation.InstantiationContext) ValidationContext(org.apache.jena.shacl.engine.ValidationContext) LoggerFactory(org.slf4j.LoggerFactory) Graph(org.apache.jena.graph.Graph) NameUtils.setterNameForField(won.shacl2java.util.NameUtils.setterNameForField) PropertyShape(org.apache.jena.shacl.parser.PropertyShape) Path(org.apache.jena.sparql.path.Path) NameUtils.adderNameForFieldNameInPlural(won.shacl2java.util.NameUtils.adderNameForFieldNameInPlural) ShapeUtils(won.shacl2java.util.ShapeUtils) Shape(org.apache.jena.shacl.parser.Shape) StreamSupport(java.util.stream.StreamSupport) NameUtils(won.shacl2java.util.NameUtils) URI(java.net.URI) NodeShape(org.apache.jena.shacl.parser.NodeShape) java.lang.reflect(java.lang.reflect) Logger(org.slf4j.Logger) VLib.focusNodes(org.apache.jena.shacl.validation.VLib.focusNodes) MethodHandles(java.lang.invoke.MethodHandles) PrefixMapping(org.apache.jena.shared.PrefixMapping) Individuals(won.shacl2java.annotation.Individuals) CollectionUtils(won.shacl2java.util.CollectionUtils) Collectors(java.util.stream.Collectors) ShaclPaths(org.apache.jena.shacl.engine.ShaclPaths) VLib(org.apache.jena.shacl.validation.VLib) DerivedInstantiationContext(won.shacl2java.instantiation.DerivedInstantiationContext) ShapeNode(won.shacl2java.annotation.ShapeNode) GraphEntity(won.shacl2java.runtime.model.GraphEntity) Stream(java.util.stream.Stream) VLib.isFocusNode(org.apache.jena.shacl.validation.VLib.isFocusNode) Individual(won.shacl2java.annotation.Individual) DataNodeAndShapes(won.shacl2java.instantiation.DataNodeAndShapes) Node(org.apache.jena.graph.Node) io.github.classgraph(io.github.classgraph) Shapes(org.apache.jena.shacl.Shapes) GraphFactory(org.apache.jena.sparql.graph.GraphFactory) PropertyShape(org.apache.jena.shacl.parser.PropertyShape) Shape(org.apache.jena.shacl.parser.Shape) NodeShape(org.apache.jena.shacl.parser.NodeShape) ShapeNode(won.shacl2java.annotation.ShapeNode) VLib.isFocusNode(org.apache.jena.shacl.validation.VLib.isFocusNode) Node(org.apache.jena.graph.Node) DataNodeAndShapes(won.shacl2java.instantiation.DataNodeAndShapes) CollectionUtils(won.shacl2java.util.CollectionUtils)

Example 3 with ShapeNode

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);
}
Also used : ShapeNode(won.shacl2java.annotation.ShapeNode) Node(org.apache.jena.graph.Node)

Aggregations

Node (org.apache.jena.graph.Node)3 ShapeNode (won.shacl2java.annotation.ShapeNode)3 Graph (org.apache.jena.graph.Graph)2 VLib.isFocusNode (org.apache.jena.shacl.validation.VLib.isFocusNode)2 Individuals (won.shacl2java.annotation.Individuals)2 NameUtils.setterNameForField (won.shacl2java.util.NameUtils.setterNameForField)2 io.github.classgraph (io.github.classgraph)1 MethodHandles (java.lang.invoke.MethodHandles)1 java.lang.reflect (java.lang.reflect)1 URI (java.net.URI)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 NodeFactory (org.apache.jena.graph.NodeFactory)1 Shapes (org.apache.jena.shacl.Shapes)1 ShaclPaths (org.apache.jena.shacl.engine.ShaclPaths)1 ValidationContext (org.apache.jena.shacl.engine.ValidationContext)1 NodeShape (org.apache.jena.shacl.parser.NodeShape)1 PropertyShape (org.apache.jena.shacl.parser.PropertyShape)1