Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Reader: RDF to entities

The reader converts the triples of a document into model entities, in two layers that work together. A Python pipeline runs six phases. A declarative mapping, the YAML reader.yaml, says what each class and predicate means, and the pipeline reads it; a reader class implements only what YAML cannot say. The entities end up in artefact.entities.

This notebook inspects both, as they ship with LODE. The imports from lode.reader and lode.profiles below are for people who debug LODE: they are not part of the stable API of the Python API.

Methods to inspect a reader

MethodWhat it gives
lode.reader.read_into(artefact, source)Runs the pipeline and fills the artefact. You do not call it: lode.read and the Artefact classes do.
artefact.readerThe reader that built the artefact.
profiles.resolve(profile.reader, profile.package)The reader class of a profile, without reading anything.
reader_class.phase1_classify_from_predicates … phase6_create_statementsThe six phases. vars(reader_class) tells which ones a reader overrides.
ConfigManager(profile).configThe merged mapper: base.yaml plus the reader.yaml of the profile.
config_manager.get_type_mapping()The entries of the RDF classes, parsed, with the model classes resolved.
config_manager.get_property_mapping()The same for the predicates.
config_manager.get_group_axioms()The axioms about several entities and the methods that handle them (phase 4).
config_manager.get_punning_priority()Which class wins when an IRI has more than one.

Examples

One for each method of the table.

lode.reader.read_into(artefact, source)

OwlReader 488 entities

artefact.reader

An artefact keeps the reader that built it.

OwlReader extends Reader

profiles.resolve(profile.reader, profile.package)

The profile knows where the reader class of each format is.

the profile says: .reader:OwlReader
lode.profiles.owl.reader.OwlReader

The six phases

The reader fills the entities in six phases, one method each, and read_into calls them in order. The table is built from the class itself, so it cannot go out of date.

Loading...

A format changes only the phases it overrides:

OwlReader    ['phase1_classify_from_predicates', 'phase2_create_from_types', 'phase5_fallback']
SkosReader   ['phase1_classify_from_predicates']

ConfigManager(profile).config

ConfigManager holds the mapper of a format: base.yaml, shared by every format, with the reader.yaml of the profile merged on top. The reader asks it what a predicate means and never reads YAML itself. The result is a dictionary:

name             str (4)
namespaces       list (5)
mapper           dict (98)
enricher         dict (4)
punning_priority list (8)

config_manager.get_type_mapping() and get_property_mapping()

mapper is what you will read most: one entry for every RDF class and predicate, which the reader sees parsed, with the model classes already resolved. What each key of an entry means is in Extending LODE, step 1.

37 classes, 61 predicates

An entry says what a predicate means. Follow one triple, ex:Cat rdfs:subClassOf ex:Animal, through the entry of rdfs:subClassOf:

YAML  : {'is': 'predicate', 'inferred_class': 'Concept', 'target_classes': ['Concept'], 'setters': [{'set_is_sub_concept_of': 'Concept'}]}
parsed: {'is': 'predicate', 'inferred_class': <class 'lode.models.concept.Concept'>, 'target_classes': [<class 'lode.models.concept.Concept'>], 'setters': [{'set_is_sub_concept_of': <class 'lode.models.concept.Concept'>}]}

phase 1: the subject becomes a Concept
phase 3: the object is resolved to a Concept and passed to set_is_sub_concept_of

Add a super concept to is_sub_concept_of and register self as its sub concept.

The inverse link is set only when the target supports it (a Restriction or
TruthFunction may not have the attribute).

The setter is a method of the model class: it fills is_sub_concept_of and registers the subject on the object as well.

When a setter is not enough the entry names a handler, a method of the reader class. The reader calls it instead of the setter. rdfs:domain has one in OWL:

{'is': 'predicate', 'target_classes': ['Property'], 'setters': [{'set_has_domain': 'Concept'}], 'handler': 'handle_domain'}
Handle rdfs:domain: link the property to its domain and the domain back to the property.

A domain that cannot become a Concept (it is cached as something else) is
forced into one, because of OWL punning.

config_manager.get_group_axioms()

enricher names the methods for axioms about several entities (phase 4).

<http://www.w3.org/2003/11/swrl#Imp> -> handle_swrl_imp
<http://www.w3.org/2002/07/owl#AllDisjointClasses> -> process_all_disjoint_classes
<http://www.w3.org/2002/07/owl#AllDifferent> -> process_all_different
<http://www.w3.org/2002/07/owl#AllDisjointProperties> -> process_all_disjoint_properties

config_manager.get_punning_priority()

When an IRI is used for more than one kind of entity (punning), the first class of the list wins.

['Model', 'Individual', 'Concept', 'Relation', 'Attribute', 'Annotation', 'Property', 'Datatype']

How LODE uses it

lode.read and the Artefact classes call read_into, which builds the reader that the profile names and runs its phases. The reader stores the entities, instances of the classes of the model, in the artefact.

Writing your own reader

This page only inspects the readers that ship with LODE. You do not extend one of them: a new format brings its own mapper and, only where YAML is not enough, its own reader class. To write them, go to Extending LODE: Step 1, write the mapper and Step 2, write the reader class.