OntologyYaml
What it is
Utilities to translate an RDFLib Graph (ontology + individuals) into a YAML-ready Python dict containing:
- namespace
prefixes - a set of selected
classes(BFO by default + classes referenced by ABI individuals) entities(individuals under the/abi/namespace) with relations and selected data properties
Public API
class OntologyYaml
__init__(triple_store_service: ITripleStoreService)- Stores a triple store service (note: currently not used by the static conversion entrypoint).
@staticmethod rdf_to_yaml(graph, class_colors_mapping: dict = {}, top_level_class: str = "http://purl.obolibrary.org/obo/BFO_0000001", display_relations_names: bool = True, yaml_properties: list = []) -> dict- Converts an RDF graph to a YAML-like dictionary using an internal
Translator.
- Converts an RDF graph to a YAML-like dictionary using an internal
class Translator
Intended internal implementation used by OntologyYaml.rdf_to_yaml.
translate(graph, class_colors_mapping, top_level_class, display_relations_names, yaml_properties) -> dict- Orchestrates parsing and YAML structure creation.
- Other methods are internal helpers (not prefixed with
_consistently, but effectively internal to the workflow):load_triples(g),load_classes(),compute_class_levels(cls_id),load_object_properties(),load_individuals(),map_oprop_labels(),create_yaml(...)get_linked_classes(cls_id, rel_type=None)for resolving OWL list constructs used in domain/range expressions (unionOf,intersectionOf,complementOf).
Configuration/Dependencies
- Python packages
rdflib(Graph,URIRef, and vocab termsRDF,RDFS,OWL)pydash(imported as_)
- Project dependencies
naas_abi_core.loggernaas_abi_core.services.triple_store.TripleStorePorts.ITripleStoreService
Usage
from rdflib import Graph
from naas_abi_core.utils.OntologyYaml import OntologyYaml
g = Graph()
# Populate `g` with ontology triples and individuals...
yaml_data = OntologyYaml.rdf_to_yaml(g)
print(yaml_data.keys()) # dict_keys(['prefixes', 'classes', 'entities'])
Caveats
OntologyYaml.rdf_to_yaml()instantiatesTranslator()with no arguments, butTranslator.__init__referencesself.__triple_store_service.get_schema_graph()even though__triple_store_serviceis not defined inTranslator. As written, callingrdf_to_yaml()will raise an exception unlessTranslatoris modified or__triple_store_serviceis injected some other way.- Only predicates present in an internal URI-to-name mapping are kept when loading triples; all other predicates are ignored.
- Only individuals whose URI contains
"/abi/"are emitted asentities. - Class inclusion in output:
- all BFO classes (
"BFO_"in URI) are included by default - additional classes are pulled in only if they are on the type chain of emitted ABI individuals (walking
is_avia the firstsubclassOf).
- all BFO classes (
- The function mutates the provided
class_colors_mappingby adding random colors for newly encountered classes.