Skip to content

Classes:

  • AttrLoaderProtocol –

    Protocol for functions that load model attributes.

  • ModelBase –

    Base object for RDF graph models, enforcing all models to have an URI as ID and types.

  • ModelLoader –

    Class for dynamically adding functions to load different model attributes.

Functions:

AttrLoaderProtocol

Bases: Protocol

Protocol for functions that load model attributes.

ModelBase

ModelBase(node_id, graph=None, types=None)

Base object for RDF graph models, enforcing all models to have an URI as ID and types.

Attributes:

  • id (URIRef) –

    the model's ID as an URI

  • types (set[URIRef]) –

    the model's types

Parameters:

  • node_id (URIRef) –

    URI of the model node in the graph

  • graph (Graph | None, default: None ) –

    RDF graph for loading types if types is not specified

  • types (set[URIRef] | None, default: None ) –

    the model's types

Methods:

  • get_attr –

    Get an attribute value.

  • has_attr –

    Check if the model has an attribute.

  • set_attr –

    Set an attribute value.

Source code in src/rdf_utils/models/common.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def __init__(
    self, node_id: URIRef, graph: Graph | None = None, types: set[URIRef] | None = None
) -> None:
    self.id = node_id
    if types is not None:
        self.types = types
    else:
        assert graph is not None, (
            f"ModelBase.__init__: node '{node_id}': neither 'graph' nor 'types' specified"
        )
        self.types = get_node_types(graph=graph, node_id=node_id)
    assert len(self.types) > 0, f"node '{self.id}' has no type"

    self._attributes = {}
    self._ns_manager = None
    if graph is not None:
        self._ns_manager = graph.namespace_manager

get_attr

get_attr(key)

Get an attribute value.

Source code in src/rdf_utils/models/common.py
69
70
71
72
73
74
def get_attr(self, key: URIRef) -> Any | None:
    """Get an attribute value."""
    if key not in self._attributes:
        return None

    return self._attributes[key]

has_attr

has_attr(key)

Check if the model has an attribute.

Source code in src/rdf_utils/models/common.py
61
62
63
def has_attr(self, key: URIRef) -> bool:
    """Check if the model has an attribute."""
    return key in self._attributes

set_attr

set_attr(key, val)

Set an attribute value.

Source code in src/rdf_utils/models/common.py
65
66
67
def set_attr(self, key: URIRef, val: Any) -> None:
    """Set an attribute value."""
    self._attributes[key] = val

ModelLoader

ModelLoader()

Class for dynamically adding functions to load different model attributes.

Methods:

  • load_attributes –

    Load all attributes in the graph into a model with the registered loaders.

  • register –

    Add a new attribute loader function.

Source code in src/rdf_utils/models/common.py
91
92
def __init__(self) -> None:
    self._loaders = []

load_attributes

load_attributes(graph, model, **kwargs)

Load all attributes in the graph into a model with the registered loaders.

Parameters:

  • graph (Graph) –

    RDF graph for loading attributes

  • model (ModelBase) –

    Model object to load attributes into

  • kwargs (Any, default: {} ) –

    any keyword arguments to pass into the loader functions

Source code in src/rdf_utils/models/common.py
102
103
104
105
106
107
108
109
110
111
def load_attributes(self, graph: Graph, model: ModelBase, **kwargs: Any) -> None:
    """Load all attributes in the graph into a model with the registered loaders.

    Parameters:
        graph: RDF graph for loading attributes
        model: Model object to load attributes into
        kwargs: any keyword arguments to pass into the loader functions
    """
    for loader in self._loaders:
        loader(graph=graph, model=model, **kwargs)

register

register(loader)

Add a new attribute loader function.

Parameters:

Source code in src/rdf_utils/models/common.py
 94
 95
 96
 97
 98
 99
100
def register(self, loader: AttrLoaderProtocol) -> None:
    """Add a new attribute loader function.

    Parameters:
        loader: attribute loader function
    """
    self._loaders.append(loader)

get_node_types

get_node_types(graph, node_id)

Get all types of a node in an RDF graph.

Parameters:

  • graph (Graph) –

    RDF graph to look up node types from

  • node_id (URIRef) –

    URIRef of target node

Returns:

  • set[URIRef] –

    A set of the node's types as URIRef's

Source code in src/rdf_utils/models/common.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def get_node_types(graph: Graph, node_id: URIRef) -> set[URIRef]:
    """Get all types of a node in an RDF graph.

    Parameters:
        graph: RDF graph to look up node types from
        node_id: URIRef of target node

    Returns:
        A set of the node's types as URIRef's
    """
    types = set()
    for type_id in graph.objects(subject=node_id, predicate=RDF.type):
        assert isinstance(type_id, URIRef), f"type '{type_id}' of node '{node_id}' not a URIRef"
        types.add(type_id)
    return types