Skip to content

Commit b8784d5

Browse files
authored
feat: add RDFGraph to API (#3031)
1 parent 6a2e5ac commit b8784d5

File tree

5 files changed

+156
-13
lines changed

5 files changed

+156
-13
lines changed

docs/reference/api.rst

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Renku Python API
2020

2121
The following sections describe the Renku Python API. If you work with the R programming language, you can also use this API through the reticulate package. For more information, visit `our dedicated tutorial <https://renkulab.io/projects/learn-renku/renku-api-from-r>`_.
2222

23-
.. _api-project:
23+
.. _api-activity:
2424

25-
``Project``
26-
-----------
25+
``Activity``
26+
------------
2727

28-
.. automodule:: renku.ui.api.models.project
28+
.. automodule:: renku.ui.api.models.activity
2929

3030
.. _api-dataset:
3131

@@ -34,23 +34,30 @@ The following sections describe the Renku Python API. If you work with the R pro
3434

3535
.. automodule:: renku.ui.api.models.dataset
3636

37+
.. _api-parameter:
38+
39+
``Inputs, Outputs, and Parameters``
40+
-----------------------------------
41+
42+
.. automodule:: renku.ui.api.models.parameter
43+
3744
.. _api-plan:
3845

3946
``Plan, CompositePlan``
4047
-----------------------
4148

4249
.. automodule:: renku.ui.api.models.plan
4350

44-
.. _api-activity:
51+
.. _api-project:
4552

46-
``Activity``
47-
------------
53+
``Project``
54+
-----------
4855

49-
.. automodule:: renku.ui.api.models.activity
56+
.. automodule:: renku.ui.api.models.project
5057

51-
.. _api-parameter:
58+
.. _api-rdfgraph:
5259

53-
``Inputs, Outputs, and Parameters``
54-
-----------------------------------
60+
``RDF Graph``
61+
-------------
5562

56-
.. automodule:: renku.ui.api.models.parameter
63+
.. automodule:: renku.ui.api.graph.rdf

renku/ui/api/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,23 @@
1717
# limitations under the License.
1818
"""Renku API."""
1919

20+
from renku.ui.api.graph.rdf import RDFGraph
2021
from renku.ui.api.models.activity import Activity
2122
from renku.ui.api.models.dataset import Dataset
2223
from renku.ui.api.models.parameter import Input, Link, Mapping, Output, Parameter
2324
from renku.ui.api.models.plan import CompositePlan, Plan
2425
from renku.ui.api.models.project import Project
2526

26-
__all__ = ("Activity", "CompositePlan", "Dataset", "Input", "Link", "Mapping", "Output", "Parameter", "Plan", "Project")
27+
__all__ = (
28+
"Activity",
29+
"CompositePlan",
30+
"Dataset",
31+
"Input",
32+
"Link",
33+
"Mapping",
34+
"Output",
35+
"Parameter",
36+
"Plan",
37+
"Project",
38+
"RDFGraph",
39+
)

renku/ui/api/graph/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
4+
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5+
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
"""Renku Graph API."""

renku/ui/api/graph/rdf.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
4+
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5+
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
"""Renku RDF Graph API.
19+
20+
The ``RDFGraph`` class allows for the quick creation of a searchable graph object
21+
based on the project's metadata.
22+
23+
To create the graph and query it:
24+
25+
.. code-block:: python
26+
27+
from renku.ui.api import RDFGraph
28+
29+
g = RDFGraph()
30+
# get a list of contributors to the project
31+
list(g.subjects(object=URIRef("http://schema.org/Person")))
32+
33+
For more information on querying the graph, see the `RDFLib
34+
documentation <https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html>`_.
35+
36+
"""
37+
38+
import json
39+
40+
import pyld
41+
from rdflib import Graph
42+
43+
from renku.command.graph import export_graph_command
44+
45+
46+
class RDFGraph(Graph):
47+
"""RDF Graph of the project's metadata."""
48+
49+
def __init__(self, revision_or_range=None):
50+
"""Instantiate the RDFGraph class.
51+
52+
Args:
53+
revision_or_range(None): Revision or range to generate the graph from. Defaults to ``None``
54+
55+
"""
56+
super().__init__()
57+
self.revision_or_range = revision_or_range
58+
self._build()
59+
self.bind_(self)
60+
61+
def _build(self):
62+
"""Construct the RDF graph representing this Renku project."""
63+
data = json.dumps(
64+
pyld.jsonld.expand(
65+
export_graph_command().build().execute(revision_or_range=self.revision_or_range).output._graph
66+
)
67+
)
68+
self.parse(data=data, format="json-ld")
69+
70+
@staticmethod
71+
def bind_(graph):
72+
"""Bind all the usual namespaces."""
73+
graph.bind("prov", "http://www.w3.org/ns/prov#")
74+
graph.bind("oa", "http://www.w3.org/ns/oa#")
75+
graph.bind("schema", "http://schema.org/")
76+
graph.bind("renku", "https://swissdatasciencecenter.github.io/renku-ontology#")
77+
graph.bind("foaf", "http://xmlns.com/foaf/0.1/")

tests/api/test_rdfgraph.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
4+
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5+
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
"""Tests for RDFGraph API."""
19+
20+
from rdflib import URIRef
21+
22+
from renku.ui.api import RDFGraph
23+
24+
25+
def test_get_graph(project):
26+
"""Test generating an RDFGraph."""
27+
g = RDFGraph()
28+
assert URIRef("mailto:[email protected]") in g.subjects(object=URIRef("http://schema.org/Person"))

0 commit comments

Comments
 (0)