Class SHACLUtil


  • public class SHACLUtil
    extends Object
    Various SHACL-related utility methods that didn't fit elsewhere.
    Author:
    Holger Knublauch
    • Constructor Summary

      Constructors 
      Constructor Description
      SHACLUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addDirectPropertiesOfClass​(org.apache.jena.rdf.model.Resource cls, Collection<org.apache.jena.rdf.model.Property> results)  
      static void addNodesInTarget​(org.apache.jena.rdf.model.Resource target, org.apache.jena.query.Dataset dataset, Set<org.apache.jena.graph.Node> results)
      Adds all resources from a given sh:target to a given results Set of Nodes.
      static org.apache.jena.rdf.model.Model createIncludesModel​(org.apache.jena.rdf.model.Model model, String graphURI)
      Creates an includes Model for a given input Model.
      static URI createRandomShapesGraphURI()  
      static boolean exists​(org.apache.jena.graph.Graph graph)  
      static boolean exists​(org.apache.jena.rdf.model.Model model)
      Checks whether the SHACL vocabulary is present in a given Model.
      static Set<org.apache.jena.graph.Node> getAllFocusNodes​(org.apache.jena.query.Dataset dataset, boolean validateShapes)
      Gets all focus nodes from the default Model of a given dataset.
      static List<org.apache.jena.rdf.model.Property> getAllPropertiesOfClass​(org.apache.jena.rdf.model.Resource cls)
      Gets all the predicates of all declared sh:properties and sh:parameters of a given class, including inherited ones.
      static List<SHNodeShape> getAllShapesAtClassOrShape​(org.apache.jena.rdf.model.Resource clsOrShape)
      Gets all sh:Shapes that have a given class in their target, including ConstraintComponents and the class or shape itself if it is marked as sh:Shape.
      static List<SHNodeShape> getAllShapesAtNode​(org.apache.jena.rdf.model.RDFNode node)
      Gets all shapes associated with a given focus node.
      static List<SHNodeShape> getAllShapesAtNode​(org.apache.jena.rdf.model.RDFNode node, Iterable<org.apache.jena.rdf.model.Resource> types)  
      static Set<org.apache.jena.rdf.model.Resource> getAllSuperClassesAndShapesStar​(org.apache.jena.rdf.model.Resource cls)
      Gets all (transitive) superclasses including shapes that reference a class via sh:targetClass.
      static List<SHResult> getAllTopLevelResults​(org.apache.jena.rdf.model.Model model)  
      static SHConstraintComponent getConstraintComponentOfValidator​(org.apache.jena.rdf.model.Resource validator)  
      static org.apache.jena.rdf.model.Resource getDefaultTypeForConstraintPredicate​(org.apache.jena.rdf.model.Property predicate)  
      static Collection<SHNodeShape> getDirectShapesAtClassOrShape​(org.apache.jena.rdf.model.Resource clsOrShape)
      Gets the directly associated sh:Shapes that have a given class in their target, including ConstraintComponents and the class or shape itself if it is marked as sh:Shape.
      static Set<org.apache.jena.rdf.model.Resource> getDirectShapesAtResource​(org.apache.jena.rdf.model.Resource resource)  
      static String getLocalPropertyLabel​(org.apache.jena.rdf.model.Resource property, org.apache.jena.rdf.model.Resource context)
      Gets any locally-defined label for a given property.
      static SHParameter getParameterAtClass​(org.apache.jena.rdf.model.Resource cls, org.apache.jena.rdf.model.Property predicate)  
      static SHParameter getParameterAtInstance​(org.apache.jena.rdf.model.Resource instance, org.apache.jena.rdf.model.Property predicate)  
      static SHPropertyShape getPropertyConstraintAtClass​(org.apache.jena.rdf.model.Resource cls, org.apache.jena.rdf.model.Property predicate)  
      static SHPropertyShape getPropertyConstraintAtInstance​(org.apache.jena.rdf.model.Resource instance, org.apache.jena.rdf.model.Property predicate)  
      static org.apache.jena.rdf.model.Resource getResourceDefaultType​(org.apache.jena.rdf.model.Resource resource)  
      static Iterable<org.apache.jena.rdf.model.RDFNode> getResourcesInTarget​(org.apache.jena.rdf.model.Resource target, org.apache.jena.query.Dataset dataset)
      Gets all nodes from a given sh:target.
      static List<org.apache.jena.rdf.model.RDFNode> getTargetNodes​(org.apache.jena.rdf.model.Resource shape, org.apache.jena.query.Dataset dataset)  
      static List<org.apache.jena.rdf.model.RDFNode> getTargetNodes​(org.apache.jena.rdf.model.Resource shape, org.apache.jena.query.Dataset dataset, boolean includeApplicableToClass)  
      static List<org.apache.jena.rdf.model.Resource> getTypes​(org.apache.jena.rdf.model.Resource subject)  
      static boolean hasMinSeverity​(org.apache.jena.rdf.model.Resource severity, org.apache.jena.rdf.model.Resource minSeverity)  
      static boolean isDeactivated​(org.apache.jena.rdf.model.Resource resource)  
      static boolean isInTarget​(org.apache.jena.rdf.model.RDFNode focusNode, org.apache.jena.query.Dataset dataset, org.apache.jena.rdf.model.Resource target)  
      static boolean isParameterAtInstance​(org.apache.jena.rdf.model.Resource subject, org.apache.jena.rdf.model.Property predicate)  
      static boolean isSPARQLProperty​(org.apache.jena.rdf.model.Property property)  
      static org.apache.jena.graph.Node walkPropertyShapesHelper​(org.apache.jena.graph.Node propertyShape, org.apache.jena.graph.Graph graph)  
      static URI withShapesGraph​(org.apache.jena.query.Dataset dataset)  
    • Constructor Detail

      • SHACLUtil

        public SHACLUtil()
    • Method Detail

      • addDirectPropertiesOfClass

        public static void addDirectPropertiesOfClass​(org.apache.jena.rdf.model.Resource cls,
                                                      Collection<org.apache.jena.rdf.model.Property> results)
      • addNodesInTarget

        public static void addNodesInTarget​(org.apache.jena.rdf.model.Resource target,
                                            org.apache.jena.query.Dataset dataset,
                                            Set<org.apache.jena.graph.Node> results)
        Adds all resources from a given sh:target to a given results Set of Nodes.
        Parameters:
        target - the value of sh:target (parameterized or SPARQL target)
        dataset - the dataset to operate on
        results - the Set to add the resulting Nodes to
      • createIncludesModel

        public static org.apache.jena.rdf.model.Model createIncludesModel​(org.apache.jena.rdf.model.Model model,
                                                                          String graphURI)
        Creates an includes Model for a given input Model. The includes Model is the union of the input Model will all graphs linked via sh:include (or owl:imports), transitively.
        Parameters:
        model - the Model to create the includes Model for
        graphURI - the URI of the named graph represented by Model
        Returns:
        a Model including the semantics
      • createRandomShapesGraphURI

        public static URI createRandomShapesGraphURI()
      • getAllFocusNodes

        public static Set<org.apache.jena.graph.Node> getAllFocusNodes​(org.apache.jena.query.Dataset dataset,
                                                                       boolean validateShapes)
        Gets all focus nodes from the default Model of a given dataset. This includes all targets of all defined targets as well as all instances of classes that are also shapes.
        Parameters:
        dataset - the Dataset
        validateShapes - true to include the validation of constraint components
        Returns:
        a Set of focus Nodes
      • getAllTopLevelResults

        public static List<SHResult> getAllTopLevelResults​(org.apache.jena.rdf.model.Model model)
      • getAllSuperClassesAndShapesStar

        public static Set<org.apache.jena.rdf.model.Resource> getAllSuperClassesAndShapesStar​(org.apache.jena.rdf.model.Resource cls)
        Gets all (transitive) superclasses including shapes that reference a class via sh:targetClass.
        Parameters:
        cls - the class to start at
        Returns:
        a Set of classes and shapes
      • getConstraintComponentOfValidator

        public static SHConstraintComponent getConstraintComponentOfValidator​(org.apache.jena.rdf.model.Resource validator)
      • getDefaultTypeForConstraintPredicate

        public static org.apache.jena.rdf.model.Resource getDefaultTypeForConstraintPredicate​(org.apache.jena.rdf.model.Property predicate)
      • getParameterAtClass

        public static SHParameter getParameterAtClass​(org.apache.jena.rdf.model.Resource cls,
                                                      org.apache.jena.rdf.model.Property predicate)
      • getParameterAtInstance

        public static SHParameter getParameterAtInstance​(org.apache.jena.rdf.model.Resource instance,
                                                         org.apache.jena.rdf.model.Property predicate)
      • getResourceDefaultType

        public static org.apache.jena.rdf.model.Resource getResourceDefaultType​(org.apache.jena.rdf.model.Resource resource)
      • getLocalPropertyLabel

        public static String getLocalPropertyLabel​(org.apache.jena.rdf.model.Resource property,
                                                   org.apache.jena.rdf.model.Resource context)
        Gets any locally-defined label for a given property. The labels are expected to be attached to shapes associated with a given context resource (instance). That context resource may for example be the subject of the current UI form.
        Parameters:
        property - the property to get the label of
        context - the context instance
        Returns:
        the local label or null if it should fall back to a global label
      • getPropertyConstraintAtClass

        public static SHPropertyShape getPropertyConstraintAtClass​(org.apache.jena.rdf.model.Resource cls,
                                                                   org.apache.jena.rdf.model.Property predicate)
      • getPropertyConstraintAtInstance

        public static SHPropertyShape getPropertyConstraintAtInstance​(org.apache.jena.rdf.model.Resource instance,
                                                                      org.apache.jena.rdf.model.Property predicate)
      • getAllPropertiesOfClass

        public static List<org.apache.jena.rdf.model.Property> getAllPropertiesOfClass​(org.apache.jena.rdf.model.Resource cls)
        Gets all the predicates of all declared sh:properties and sh:parameters of a given class, including inherited ones.
        Parameters:
        cls - the class to get the predicates of
        Returns:
        the declared predicates
      • getResourcesInTarget

        public static Iterable<org.apache.jena.rdf.model.RDFNode> getResourcesInTarget​(org.apache.jena.rdf.model.Resource target,
                                                                                       org.apache.jena.query.Dataset dataset)
        Gets all nodes from a given sh:target.
        Parameters:
        target - the value of sh:target (parameterizable or SPARQL target)
        dataset - the dataset to operate on
        Returns:
        an Iterable over the resources
      • getAllShapesAtNode

        public static List<SHNodeShape> getAllShapesAtNode​(org.apache.jena.rdf.model.RDFNode node)
        Gets all shapes associated with a given focus node. This looks for all shapes based on class-based targets. Future versions will also look for property-based targets.
        Parameters:
        node - the (focus) node
        Returns:
        a List of shapes
      • getAllShapesAtNode

        public static List<SHNodeShape> getAllShapesAtNode​(org.apache.jena.rdf.model.RDFNode node,
                                                           Iterable<org.apache.jena.rdf.model.Resource> types)
      • getAllShapesAtClassOrShape

        public static List<SHNodeShape> getAllShapesAtClassOrShape​(org.apache.jena.rdf.model.Resource clsOrShape)
        Gets all sh:Shapes that have a given class in their target, including ConstraintComponents and the class or shape itself if it is marked as sh:Shape. Also walks up the class hierarchy.
        Parameters:
        clsOrShape - the class or Shape to get the shapes of
        Returns:
        the shapes, ordered by the most specialized (subclass) first
      • getDirectShapesAtClassOrShape

        public static Collection<SHNodeShape> getDirectShapesAtClassOrShape​(org.apache.jena.rdf.model.Resource clsOrShape)
        Gets the directly associated sh:Shapes that have a given class in their target, including ConstraintComponents and the class or shape itself if it is marked as sh:Shape. Does not walk up the class hierarchy.
        Parameters:
        clsOrShape - the class or Shape to get the shapes of
        Returns:
        the shapes
      • getDirectShapesAtResource

        public static Set<org.apache.jena.rdf.model.Resource> getDirectShapesAtResource​(org.apache.jena.rdf.model.Resource resource)
      • getTargetNodes

        public static List<org.apache.jena.rdf.model.RDFNode> getTargetNodes​(org.apache.jena.rdf.model.Resource shape,
                                                                             org.apache.jena.query.Dataset dataset)
      • getTargetNodes

        public static List<org.apache.jena.rdf.model.RDFNode> getTargetNodes​(org.apache.jena.rdf.model.Resource shape,
                                                                             org.apache.jena.query.Dataset dataset,
                                                                             boolean includeApplicableToClass)
      • getTypes

        public static List<org.apache.jena.rdf.model.Resource> getTypes​(org.apache.jena.rdf.model.Resource subject)
      • hasMinSeverity

        public static boolean hasMinSeverity​(org.apache.jena.rdf.model.Resource severity,
                                             org.apache.jena.rdf.model.Resource minSeverity)
      • isDeactivated

        public static boolean isDeactivated​(org.apache.jena.rdf.model.Resource resource)
      • isParameterAtInstance

        public static boolean isParameterAtInstance​(org.apache.jena.rdf.model.Resource subject,
                                                    org.apache.jena.rdf.model.Property predicate)
      • isSPARQLProperty

        public static boolean isSPARQLProperty​(org.apache.jena.rdf.model.Property property)
      • exists

        public static boolean exists​(org.apache.jena.rdf.model.Model model)
        Checks whether the SHACL vocabulary is present in a given Model. The condition is that the SHACL namespace must be declared and sh:Constraint must have an rdf:type.
        Parameters:
        model - the Model to check
        Returns:
        true if SHACL is present
      • exists

        public static boolean exists​(org.apache.jena.graph.Graph graph)
      • withShapesGraph

        public static URI withShapesGraph​(org.apache.jena.query.Dataset dataset)
      • isInTarget

        public static boolean isInTarget​(org.apache.jena.rdf.model.RDFNode focusNode,
                                         org.apache.jena.query.Dataset dataset,
                                         org.apache.jena.rdf.model.Resource target)
      • walkPropertyShapesHelper

        public static org.apache.jena.graph.Node walkPropertyShapesHelper​(org.apache.jena.graph.Node propertyShape,
                                                                          org.apache.jena.graph.Graph graph)