Beispiel #1
0
    def __init__(self, shape, rule_node):
        """

        :param shape:
        :type shape: Shape
        :param rule_node:
        :type rule_node: rdflib.Identifier
        """
        super(SPARQLRule, self).__init__(shape, rule_node)
        construct_nodes = set(self.shape.sg.objects(self.node, SH_construct))
        if len(construct_nodes) < 1:
            raise RuleLoadError("No sh:construct on SPARQLRule",
                                "https://www.w3.org/TR/shacl-af/#SPARQLRule")
        self._constructs = []
        for c in construct_nodes:
            if not isinstance(c, Literal) or not (c.datatype == XSD_string
                                                  or c.language is not None
                                                  or isinstance(c.value, str)):
                raise RuleLoadError(
                    "SPARQLRule sh:construct must be an xsd:string",
                    "https://www.w3.org/TR/shacl-af/#SPARQLRule")
            self._constructs.append(str(c.value))
        query_helper = SPARQLQueryHelper(self.shape,
                                         self.node,
                                         None,
                                         deactivated=self._deactivated)
        query_helper.collect_prefixes()
        self._qh = query_helper
Beispiel #2
0
 def __init__(self, shape):
     super(SPARQLBasedConstraint, self).__init__(shape)
     sg = self.shape.sg.graph
     sparql_node_list = set(self.shape.objects(SH_sparql))
     if len(sparql_node_list) < 1:
         raise ConstraintLoadError(
             "SPARQLConstraintComponent must have at least one sh:sparql predicate.",
             "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
         )
     sparql_constraints = set()
     for s in iter(sparql_node_list):
         select_node_list = set(sg.objects(s, SH_select))
         if len(select_node_list) < 1:
             raise ConstraintLoadError(
                 "SPARQLConstraintComponent value for sh:select must have at least one sh:select predicate.",
                 "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
             )
         elif len(select_node_list) > 1:
             raise ConstraintLoadError(
                 "SPARQLConstraintComponent value for sh:select must have at most one sh:select predicate.",
                 "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
             )
         select_node = next(iter(select_node_list))
         if not (isinstance(select_node, rdflib.Literal)
                 and isinstance(select_node.value, str)):
             raise ConstraintLoadError(
                 "SPARQLConstraintComponent value for sh:select must be a Literal with type xsd:string.",
                 "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
             )
         message_node_list = set(sg.objects(s, SH_message))
         msgs = None
         if len(message_node_list) > 0:
             message = next(iter(message_node_list))
             if not (isinstance(message, rdflib.Literal)
                     and isinstance(message.value, str)):
                 raise ConstraintLoadError(
                     "SPARQLConstraintComponent value for sh:message must be a Literal with type xsd:string.",
                     "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
                 )
             msgs = message_node_list
         deactivated_node_list = set(sg.objects(s, SH_deactivated))
         deact = False
         if len(deactivated_node_list) > 0:
             deactivated = next(iter(deactivated_node_list))
             if not (isinstance(deactivated, rdflib.Literal)
                     and isinstance(deactivated.value, bool)):
                 raise ConstraintLoadError(
                     "SPARQLConstraintComponent value for sh:deactivated must be "
                     "a Literal with type xsd:boolean.",
                     "https://www.w3.org/TR/shacl/#SPARQLConstraintComponent",
                 )
             deact = bool(deactivated.value)
         query_helper = SPARQLQueryHelper(self.shape,
                                          s,
                                          select_node.value,
                                          messages=msgs,
                                          deactivated=deact)
         query_helper.collect_prefixes()
         sparql_constraints.add(query_helper)
     self.sparql_constraints = sparql_constraints
Beispiel #3
0
 def advanced_target(self):
     custom_targets = set(self.sg.graph.objects(self.node, SH_target))
     result_set = dict()
     for c in custom_targets:
         ct = dict()
         is_types = set(self.sg.graph.objects(c, RDF_type))
         is_target_type = False
         parameters = set(self.sg.graph.objects(c, SH_parameter))
         if SH_SPARQLTargetType in is_types or len(parameters) > 0:
             is_target_type = True
         ct['type'] = SH_SPARQLTargetType if is_target_type else SH_SPARQLTarget
         selects = set(self.sg.graph.objects(c, SH_select))
         if len(selects) < 1:
             continue
         ct['select'] = next(iter(selects))
         qh = SPARQLQueryHelper(self, c, ct['select'], deactivated=self._deactivated)
         ct['qh'] = qh
         qh.collect_prefixes()
         result_set[c] = ct
     return result_set