def single(graph: str, entity: str, as_element: Union[str, None], limit: int) -> Dict[str, List[Dict[str, str]]]: """Finds triples which the given entity is a part of (as subject, predicate, object, or any)""" results = {'subject': None, 'predicate': None, 'object': None} if as_element is None or as_element == 'subject': client.setQuery('SELECT DISTINCT ?predicate ?predicateLabel ?object ?objectLabel FROM <%s> WHERE {' '<%s> ?predicate ?object ' 'OPTIONAL { ?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?predicateLabel } ' 'OPTIONAL { ?object <http://www.w3.org/2000/01/rdf-schema#label> ?objectLabel } ' '}' % (graph, entity)) results['subject'] = client.query().convert()['results']['bindings'][:limit] if as_element is None or as_element == 'predicate': client.setQuery('SELECT DISTINCT ?subject ?subjectLabel ?object ?objectLabel WHERE {' '?subject <%s> ?object ' 'OPTIONAL { ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?subjectLabel } ' 'OPTIONAL { ?object <http://www.w3.org/2000/01/rdf-schema#label> ?objectLabel } ' '}' % entity) results['predicate'] = client.query().convert()['results']['bindings'][:limit] if as_element is None or as_element == 'object': client.setQuery('SELECT DISTINCT ?subject ?subjectLabel ?predicate ?predicateLabel WHERE {' '?subject ?predicate <%s> ' 'OPTIONAL { ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?subjectLabel } ' 'OPTIONAL { ?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?predicateLabel } ' '}' % entity) results['object'] = client.query().convert()['results']['bindings'][:limit] return results
def single(triple: Tuple[str, str, str], graph: str) -> str: """Deletes a triple from the graph database""" client.setQuery('DELETE DATA { GRAPH <%s> {<%s> <%s> <%s>} }' % (graph, triple[0], triple[1], triple[2])) try: return client.query().convert( )['results']['bindings'][0]['callret-0']['value'] except Exception: return str(client.query().convert())
def single(triple: Tuple[str, str, str], graph: str) -> bool: """Checks whether a triple exists in the graph""" client.setQuery( 'SELECT DISTINCT ?predicate WHERE { GRAPH <%s> {<%s> ?predicate <%s>} }' % (graph, triple[0], triple[2])) response = client.query().convert()['results']['bindings'] for item in response: if triple[1] == item['predicate']['value']: return True return False
def fetch(perfect_match: bool): if perfect_match: terms = ['"%s"' % term for term in label.strip().split(' ')] else: # Each word in the search term needs at least four characters to be included in partial search # The asterisk (*) indicates partial search terms = ['"%s*"' % term if len(term) > 3 else '"%s"' % term for term in label.strip().split(' ')] # Search terms are joined by 'AND' operators client.setQuery('SELECT DISTINCT ?entity, ?label, ?type FROM <%s> WHERE {' '?entity <http://www.w3.org/2000/01/rdf-schema#label> ?label ; a ?type .' '?label bif:contains \'%s\' %s} %s' % (graph, ' and '.join(terms), language_filter, limit_filter)) return client.query().convert()['results']['bindings']