Coverage for target/native-coverage/profiles/11fb979bfcf049d7ab666c331dc8db2a/project/.venv/lib/python3.13/site-packages/pyshacl_turbo/_runner.py: 100%
80 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-30 14:51 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-30 14:51 +0000
1"""Private shared construction and native preparation for public runner types."""
3import logging
5from rdflib import URIRef
6from rdflib.graph import Dataset, Graph
8from ._inference import inference_mode
9from .graph_abstraction import DataGraph
10from .shapes_graph import ShapesGraph
13class Runner:
14 @classmethod
15 def _load_default_options(cls, options_dict):
16 for name, value in cls._defaults.items():
17 options_dict.setdefault(name, value)
18 if "logger" not in options_dict:
19 options_dict["logger"] = logging.getLogger(cls.__module__)
20 if options_dict["debug"]:
21 options_dict["logger"].setLevel(logging.DEBUG)
23 def __init__(self, data_graph, *args, shacl_graph=None, ont_graph=None, options=None, **kwargs):
24 options = options or {}
25 self._load_default_options(options)
26 self.options = options
27 self.logger = options["logger"]
28 self.debug = options["debug"]
29 self.pre_inferenced = kwargs.pop("pre_inferenced", False)
30 self.inplace = options["inplace"]
31 if not isinstance(data_graph, DataGraph):
32 raise RuntimeError("data_graph must be a DataGraph object") # noqa: TRY004 - reference API
33 self.data_graph = data_graph
34 self._target_graph = None
35 self.ont_graph = ont_graph
36 self.data_graph_is_multigraph = data_graph.is_multigraph()
37 if isinstance(ont_graph, Dataset):
38 ont_graph.default_union = True
39 if shacl_graph is None:
40 shacl_graph = data_graph.clone(identifier="shacl")
41 assert isinstance(shacl_graph, Graph), "shacl_graph must be a rdflib Graph object"
42 self.shacl_graph = ShapesGraph(shacl_graph, self.debug, self.logger)
44 @property
45 def target_graph(self):
46 return self._target_graph
48 def _prepare_focus_nodes(self, target):
49 if self.options.get("focus_nodes"):
50 from . import _expand_names
52 self.options["focus_nodes"] = [
53 URIRef(node) for node in _expand_names(target.impl, self.options["focus_nodes"])
54 ]
56 def mix_in_ontology(self):
57 from . import _prepare_data
58 from .rdf import merge_graph
60 target = self.data_graph if self.inplace else self.data_graph.clone()
61 schema = _prepare_data(Graph(), None, self.ont_graph, None, None, False)
62 destination = (
63 target.impl.graph(URIRef("urn:pyshacl:inoculation"))
64 if self.data_graph_is_multigraph
65 else target.impl
66 )
67 merge_graph(destination, schema)
68 return target
70 def _prepare_target(self, rules=False):
71 from . import _prepare_data, _shared_runtime
72 from .rdf import merge_graph
74 if self.options.get("use_js"):
75 raise NotImplementedError("Unsupported SHACL-JS")
76 if self.options.get("sparql_mode"):
77 raise NotImplementedError("Remote SPARQL validation is not implemented")
78 if self._target_graph is not None:
79 return self._target_graph
80 inference = None if self.pre_inferenced else inference_mode(self.options.get("inference"))
81 if (
82 not rules
83 and self.ont_graph is None
84 and _shared_runtime(self.data_graph.impl) is not None
85 ):
86 # Validation owns private inference and acceptance. Keep the caller's
87 # native owner rather than cloning and materializing a closure here.
88 self._target_graph = self.data_graph
89 return self._target_graph
90 modifies = (
91 rules
92 or self.options.get("advanced")
93 or self.ont_graph is not None
94 or (inference not in (None, "none"))
95 )
96 if self.ont_graph is not None:
97 target = self.mix_in_ontology()
98 else:
99 target = self.data_graph.clone() if modifies and not self.inplace else self.data_graph
100 if not modifies and (not self.inplace):
101 self.inplace = True
102 if _shared_runtime(target.impl) is not None:
103 if inference not in (None, "none"):
104 target.impl.infer(inference=inference)
105 prepared = target.impl
106 else:
107 prepared = _prepare_data(target.impl, None, None, None, inference, False)
108 if prepared is not target.impl:
109 merge_graph(target.impl, prepared)
110 if inference not in (None, "none"):
111 self.pre_inferenced = True
112 self._target_graph = target
113 return target