Skip to content

Functions:

  • get_attr_path –

    Get a previously loaded execution path from a model object.

  • get_path_of_node –

    Get the execution path associated with an RDF node.

  • load_attr_path –

    Load a resource path from an RDF graph into a model object.

get_attr_path

get_attr_path(model)

Get a previously loaded execution path from a model object.

Parameters:

  • model (ModelBase) –

    Model object containing the loaded path.

Returns:

  • str –

    The model path as a string.

Raises:

  • ValueError –

    If the model is not a ResourceWithPath.

Source code in src/rdf_utils/models/execution.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def get_attr_path(model: ModelBase) -> str:
    """Get a previously loaded execution path from a model object.

    Parameters:
        model: Model object containing the loaded path.

    Returns:
        The model path as a string.

    Raises:
        ValueError: If the model is not a `ResourceWithPath`.
    """
    if URI_EXEC_TYPE_RES_PATH not in model.types:
        raise ValueError()

    return str(model.get_attr(key=URI_EXEC_PRED_PATH))

get_path_of_node

get_path_of_node(graph, node_id)

Get the execution path associated with an RDF node.

Parameters:

  • graph (Graph) –

    RDF graph containing the node.

  • node_id (URIRef) –

    URI of the node whose path should be loaded.

Returns:

  • str –

    The node path as a string.

Raises:

  • TypeError –

    If the node path is missing or is not an RDF literal.

Source code in src/rdf_utils/models/execution.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def get_path_of_node(graph: Graph, node_id: URIRef) -> str:
    """Get the execution path associated with an RDF node.

    Parameters:
        graph: RDF graph containing the node.
        node_id: URI of the node whose path should be loaded.

    Returns:
        The node path as a string.

    Raises:
        TypeError: If the node path is missing or is not an RDF literal.
    """
    path = graph.value(subject=node_id, predicate=URI_EXEC_PRED_PATH)
    if not isinstance(path, Literal):
        raise TypeError(
            f"node '{node_id.n3(graph.namespace_manager)}' has no edge 'path' to a literal: {path}"
        )

    return str(path.toPython())

load_attr_path

load_attr_path(graph, model, **kwargs)

Load a resource path from an RDF graph into a model object.

Models without the ResourceWithPath type are left unchanged.

Parameters:

  • graph (Graph) –

    RDF graph containing the model data.

  • model (ModelBase) –

    Model object to update.

  • kwargs (Any, default: {} ) –

    Additional loader arguments, ignored by this loader.

Raises:

  • ValueError –

    If a ResourceWithPath model has no 'path' predicate linking to a literal.

Source code in src/rdf_utils/models/execution.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def load_attr_path(graph: Graph, model: ModelBase, **kwargs: Any) -> None:
    """Load a resource path from an RDF graph into a model object.

    Models without the `ResourceWithPath` type are left unchanged.

    Parameters:
        graph: RDF graph containing the model data.
        model: Model object to update.
        kwargs: Additional loader arguments, ignored by this loader.

    Raises:
        ValueError: If a `ResourceWithPath` model has no 'path' predicate linking to a literal.
    """
    if URI_EXEC_TYPE_RES_PATH not in model.types:
        return

    path = get_path_of_node(graph=graph, node_id=model.id)
    model.set_attr(key=URI_EXEC_PRED_PATH, val=path)