OntologyYaml
What it is
- Utility for translating an RDFLib
Graphcontaining ontology classes, object properties, and individuals into a YAML-friendly Pythondictstructure. - Designed to extract classes (notably BFO), ABI individuals, and their relations, then normalize common URI prefixes.
Public API
class OntologyYaml
__init__(triple_store_service: ITripleStoreService)- Stores a triple store service dependency (currently not used by the static translation method).
@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 = [])- Translates an RDFLib
Graphinto a YAML-ready dictionary with keys:prefixesclassesentities
- Translates an RDFLib
class Translator
translate(graph, class_colors_mapping, top_level_class, display_relations_names, yaml_properties)- Orchestrates translation by loading triples/classes/properties/individuals and producing YAML data via
create_yaml(...).
- Orchestrates translation by loading triples/classes/properties/individuals and producing YAML data via
- Other methods exist but are internal helpers (no leading underscore consistency, but intended for internal use):
load_triples(g)load_classes()compute_class_levels(cls_id)load_object_properties()load_individuals()map_oprop_labels()create_yaml(class_color, display_relations_names, yaml_properties)get_linked_classes(cls_id, rel_type=None)(used internally for domain/range expression trees)
Configuration/Dependencies
- Requires:
rdflib(Graph,URIRef,RDF,RDFS,OWL)pydashas_naas_abi_core.loggernaas_abi_core.services.triple_store.TripleStorePorts.ITripleStoreService
- Prefix normalization performed in output:
xsd,abi,bfo,cco
- Notes on data expectations in the RDF graph:
- Classes are detected via
rdf:type owl:Class - Object properties via
rdf:type owl:ObjectProperty(labels used to find relation predicates on individuals) - Individuals via
rdf:type owl:NamedIndividual - Individuals are only emitted as YAML
entitiesif their URI contains"/abi/"
- Classes are detected via
Usage
from rdflib import Graph
from naas_abi_core.utils.OntologyYaml import OntologyYaml
g = Graph()
# Populate `g` with ontology + individuals data...
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_yamlwill raise anAttributeErrorunlessTranslatoris fixed or externally patched to have__triple_store_service.- Default argument
class_colors_mapping: dict = {}is mutable. - Only the first
subclassOfentry is used when emitting a classis_arelation (subclass[0]). - Relation extraction for individuals depends on object property labels (
rdfs:label); it then looks for those label strings as keys in the individual’s predicate-mapped dict (i.e., the label must match the mapped predicate name inself.mappingoutput).