RDF Parser
Bases: Parser
Parsers RDF Graphs into typedlogic theories.
Example:
>>> parser = RDFParser()
>>> import rdflib as rdflib
>>> from rdflib.namespace import RDF, RDFS
>>> g = rdflib.Graph()
>>> EX = rdflib.Namespace("http://example.org/")
>>> _ = g.add((EX.hasPet, RDFS.domain, EX.Human))
>>> _ = g.add((EX.hasPet, RDFS.range, EX.Animal))
>>> _ = g.add((EX.Fred, EX.hasPet, EX.Fido))
>>> theory = parser.parse(g)
>>> preds = [pd.predicate for pd in theory.predicate_definitions]
>>> for pred in sorted(preds):
... print(pred)
<BLANKLINE>
...
SubClassOf
SubPropertyOf
Triple
...
Note that the predicates here are predicates from the theory of RDF, RDFS, and OWL-Full. "user-defined" predicates (e.g. hasPet) are not in this list, as these are mapped to instances of Triple.
>>> facts = [fact.as_sexpr() for fact in theory.ground_terms]
>>> for fact in sorted(facts):
... print(fact)
['Triple', rdflib.term.URIRef('http://example.org/Fred'), rdflib.term.URIRef('http://example.org/hasPet'), rdflib.term.URIRef('http://example.org/Fido')]
['Triple', rdflib.term.URIRef('http://example.org/hasPet'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#domain'), rdflib.term.URIRef('http://example.org/Human')]
['Triple', rdflib.term.URIRef('http://example.org/hasPet'), rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#range'), rdflib.term.URIRef('http://example.org/Animal')]
After this other facts can be inferred using a solver.
>>> from typedlogic.registry import get_solver
>>> s = get_solver("souffle")
>>> s.add(theory)
>>> model = s.model()
>>> assert model
>>> from typedlogic.integrations.frameworks.rdflib.rdfs import Type
>>> for fact in sorted(model.retrieve(Type)):
... print(fact)
Type(http://example.org/Fido, http://example.org/Animal)
Type(http://example.org/Fred, http://example.org/Human)
Source code in src/typedlogic/integrations/frameworks/rdflib/rdf_parser.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
parse(source, format='ttl', **kwargs)
Parse am RDF Graph to a theory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
Union[Path, str, TextIO, Graph]
|
rdflib Graph pr a path to a graph |
required |
format
|
rdflib accepted format |
'ttl'
|
|
kwargs
|
|
{}
|
Returns:
Type | Description |
---|---|
Theory
|
Parsed theory |
Source code in src/typedlogic/integrations/frameworks/rdflib/rdf_parser.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|