|
107 | 107 | from typing import TYPE_CHECKING, Any, Collection, Iterable, List, Set, Tuple, Union, cast |
108 | 108 |
|
109 | 109 | from rdflib import OWL, Graph, URIRef |
| 110 | +from rdflib.term import _is_valid_uri |
110 | 111 |
|
111 | 112 | from .rdflib_custom import MappingServiceSPARQLProcessor # type: ignore |
112 | 113 | from ..api import Converter |
@@ -201,37 +202,29 @@ def __init__( |
201 | 202 | self.predicates = _prepare_predicates(predicates) |
202 | 203 | super().__init__(*args, **kwargs) |
203 | 204 |
|
| 205 | + def _expand_pair_all(self, uri_in: str) -> List[URIRef]: |
| 206 | + prefix, identifier = self.converter.parse_uri(uri_in) |
| 207 | + if prefix is None or identifier is None: |
| 208 | + return [] |
| 209 | + uris = cast(Collection[str], self.converter.expand_pair_all(prefix, identifier)) |
| 210 | + # do _is_valid_uri check because some configurations e.g. from Bioregistry might |
| 211 | + # produce invalid URIs e.g., containing spaces |
| 212 | + return [URIRef(uri) for uri in uris if _is_valid_uri(uri)] |
| 213 | + |
204 | 214 | def triples( |
205 | 215 | self, triple: Tuple[URIRef, URIRef, URIRef] |
206 | 216 | ) -> Iterable[Tuple[URIRef, URIRef, URIRef]]: |
207 | 217 | """Generate triples, overriden to dynamically generate mappings based on this graph's converter.""" |
208 | 218 | subj_query, pred_query, obj_query = triple |
209 | | - if pred_query not in self.predicates: |
210 | | - return |
211 | | - if subj_query is None and obj_query is None: |
212 | | - return # can't generate based on this |
213 | | - if subj_query is None and obj_query is not None: |
214 | | - prefix, identifier = self.converter.parse_uri(obj_query) |
215 | | - if prefix is None or identifier is None: |
216 | | - return |
217 | | - subjects = [ |
218 | | - URIRef(sub) |
219 | | - for sub in cast(Collection[str], self.converter.expand_pair_all(prefix, identifier)) |
220 | | - ] |
221 | | - for subj, pred in itt.product(subjects, self.predicates): |
222 | | - yield subj, pred, obj_query |
223 | | - elif subj_query is not None and obj_query is None: |
224 | | - prefix, identifier = self.converter.parse_uri(subj_query) |
225 | | - if prefix is None or identifier is None: |
226 | | - return |
227 | | - objects = [ |
228 | | - URIRef(obj) |
229 | | - for obj in cast(Collection[str], self.converter.expand_pair_all(prefix, identifier)) |
230 | | - ] |
231 | | - for obj, pred in itt.product(objects, self.predicates): |
232 | | - yield subj_query, pred, obj |
233 | | - else: # subj_query is not None and obj_query is not None |
234 | | - return # too much specification? maybe just return one triple then? |
| 219 | + if pred_query in self.predicates: |
| 220 | + if subj_query is None and obj_query is not None: |
| 221 | + subjects = self._expand_pair_all(obj_query) |
| 222 | + for subj, pred in itt.product(subjects, self.predicates): |
| 223 | + yield subj, pred, obj_query |
| 224 | + elif subj_query is not None and obj_query is None: |
| 225 | + objects = self._expand_pair_all(subj_query) |
| 226 | + for obj, pred in itt.product(objects, self.predicates): |
| 227 | + yield subj_query, pred, obj |
235 | 228 |
|
236 | 229 |
|
237 | 230 | def get_flask_mapping_blueprint( |
|
0 commit comments