Exemple #1
0
    def __init__(self,
                 data_graph,
                 *args,
                 shacl_graph=None,
                 ont_graph=None,
                 options=None,
                 **kwargs):
        options = options or {}
        self._load_default_options(options)
        self.options = options
        self.logger = options['logger']
        self.pre_inferenced = kwargs.pop('pre_inferenced', False)
        assert isinstance(data_graph, rdflib.Graph),\
            "data_graph must be a rdflib Graph object"
        self.data_graph = data_graph
        self._target_graph = None
        self.ont_graph = ont_graph
        self.data_graph_is_multigraph = isinstance(
            self.data_graph, (rdflib.Dataset, rdflib.ConjunctiveGraph))
        if self.ont_graph is not None and \
            isinstance(self.ont_graph, (rdflib.Dataset, rdflib.ConjunctiveGraph)):
            self.ont_graph.default_union = True

        if shacl_graph is None:
            shacl_graph = clone_graph(data_graph, identifier='shacl')
        assert isinstance(shacl_graph, rdflib.Graph),\
            "shacl_graph must be a rdflib Graph object"
        self.shacl_graph = ShapesGraph(shacl_graph, self.logger)
Exemple #2
0
    def __init__(
        self,
        data_graph: GraphLike,
        *args,
        shacl_graph: Optional[GraphLike] = None,
        ont_graph: Optional[GraphLike] = None,
        options: Optional[dict] = None,
        **kwargs,
    ):
        options = options or {}
        self._load_default_options(options)
        self.options = options  # type: dict
        self.logger = options['logger']  # type: logging.Logger
        self.pre_inferenced = kwargs.pop('pre_inferenced', False)
        if not isinstance(data_graph, rdflib.Graph):
            raise RuntimeError("data_graph must be a rdflib Graph object")
        self.data_graph = data_graph
        self._target_graph = None
        self.ont_graph = ont_graph
        self.data_graph_is_multigraph = isinstance(
            self.data_graph, (rdflib.Dataset, rdflib.ConjunctiveGraph))
        if self.ont_graph is not None and isinstance(
                self.ont_graph, (rdflib.Dataset, rdflib.ConjunctiveGraph)):
            self.ont_graph.default_union = True

        if shacl_graph is None:
            shacl_graph = clone_graph(data_graph, identifier='shacl')
        assert isinstance(
            shacl_graph,
            rdflib.Graph), "shacl_graph must be a rdflib Graph object"
        self.shacl_graph = ShapesGraph(shacl_graph, self.logger)
Exemple #3
0
 def __init__(self, data_graph, *args, shacl_graph=None,
              ont_graph=None, options=None, **kwargs):
     options = options or {}
     self._load_default_options(options)
     self.options = options
     self.logger = options['logger']
     self.pre_inferenced = kwargs.pop('pre_inferenced', False)
     assert isinstance(data_graph, rdflib.Graph),\
         "data_graph must be a rdflib Graph object"
     self.data_graph = data_graph
     self._target_graph = self.data_graph
     self.ont_graph = ont_graph
     if shacl_graph is None:
         shacl_graph = clone_graph(data_graph, identifier='shacl')
     assert isinstance(shacl_graph, rdflib.Graph),\
         "shacl_graph must be a rdflib Graph object"
     self.shacl_graph = SHACLGraph(shacl_graph, self.logger)
Exemple #4
0
 def apply(self, data_graph):
     focus_nodes = self.shape.focus_nodes(
         data_graph)  # uses target nodes to find focus nodes
     applicable_nodes = self.filter_conditions(focus_nodes, data_graph)
     construct_graphs = set()
     for a in applicable_nodes:
         for c in self._constructs:
             init_bindings = {}
             found_this = SPARQLQueryHelper.bind_this_regex.search(c)
             if found_this:
                 init_bindings['this'] = a
             c = self._qh.apply_prefixes(c)
             results = data_graph.query(c, initBindings=init_bindings)
             if results.type != "CONSTRUCT":
                 raise ReportableRuntimeError(
                     "Query executed by a SHACL SPARQLRule must be CONSTRUCT query."
                 )
             construct_graphs.add(results.graph)
     for g in construct_graphs:
         data_graph = clone_graph(g, target_graph=data_graph)
Exemple #5
0
 def apply(self, data_graph: 'GraphLike') -> int:
     focus_nodes = self.shape.focus_nodes(data_graph)  # uses target nodes to find focus nodes
     all_added = 0
     SPARQLQueryHelper = get_query_helper_cls()
     iterate_limit = 100
     while True:
         if iterate_limit < 1:
             raise ReportableRuntimeError("Local rule iteration exceeded iteration limit of 100.")
         iterate_limit -= 1
         added = 0
         applicable_nodes = self.filter_conditions(focus_nodes, data_graph)
         construct_graphs = set()
         for a in applicable_nodes:
             for c in self._constructs:
                 init_bindings = {}
                 found_this = SPARQLQueryHelper.bind_this_regex.search(c)
                 if found_this:
                     init_bindings['this'] = a
                 c = self._qh.apply_prefixes(c)
                 results = data_graph.query(c, initBindings=init_bindings)
                 if results.type != "CONSTRUCT":
                     raise ReportableRuntimeError("Query executed by a SHACL SPARQLRule must be CONSTRUCT query.")
                 this_added = False
                 for i in results.graph:
                     if not this_added and i not in data_graph:
                         this_added = True
                         # We only need to know at least one triple was added, then break!
                         break
                 if this_added:
                     added += 1
                     construct_graphs.add(results.graph)
         if added > 0:
             for g in construct_graphs:
                 data_graph = clone_graph(g, target_graph=data_graph)
             all_added += added
             if self.iterate:
                 continue  # Jump up to iterate
             else:
                 break  # Don't iterate
         break  # We've reached a local steady state
     return all_added