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 dynimcally 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)

Bases: object

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 (Optional[Graph], default: None ) –

    RDF graph for loading types if types is not specified

  • types (Optional[set[URIRef]], 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def __init__(
    self, node_id: URIRef, graph: Optional[Graph] = None, types: Optional[set[URIRef]] = 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' or '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 = {}

get_attr

get_attr(key)

Get an attribute value.

Source code in src/rdf_utils/models/common.py
62
63
64
65
66
67
def get_attr(self, key: URIRef) -> Optional[Any]:
    """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
54
55
56
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
58
59
60
def set_attr(self, key: URIRef, val: Any) -> None:
    """Set an attribute value."""
    self._attributes[key] = val

ModelLoader

ModelLoader()

Bases: object

Class for dynimcally 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
79
80
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
90
91
92
93
94
95
96
97
98
99
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
82
83
84
85
86
87
88
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
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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