Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RDFGraph to API #3031

Merged
merged 5 commits into from Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 19 additions & 12 deletions docs/reference/api.rst
Expand Up @@ -20,12 +20,12 @@ Renku Python API

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>`_.

.. _api-project:
.. _api-activity:

``Project``
-----------
``Activity``
------------

.. automodule:: renku.ui.api.models.project
.. automodule:: renku.ui.api.models.activity

.. _api-dataset:

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

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

.. _api-parameter:

``Inputs, Outputs, and Parameters``
-----------------------------------

.. automodule:: renku.ui.api.models.parameter

.. _api-plan:

``Plan, CompositePlan``
-----------------------

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

.. _api-activity:
.. _api-project:

``Activity``
------------
``Project``
-----------

.. automodule:: renku.ui.api.models.activity
.. automodule:: renku.ui.api.models.project

.. _api-parameter:
.. _api-rdfgraph:

``Inputs, Outputs, and Parameters``
-----------------------------------
``RDF Graph``
-------------

.. automodule:: renku.ui.api.models.parameter
.. automodule:: renku.ui.api.graph.rdf
15 changes: 14 additions & 1 deletion renku/ui/api/__init__.py
Expand Up @@ -17,10 +17,23 @@
# limitations under the License.
"""Renku API."""

from renku.ui.api.graph.rdf import RDFGraph
from renku.ui.api.models.activity import Activity
from renku.ui.api.models.dataset import Dataset
from renku.ui.api.models.parameter import Input, Link, Mapping, Output, Parameter
from renku.ui.api.models.plan import CompositePlan, Plan
from renku.ui.api.models.project import Project

__all__ = ("Activity", "CompositePlan", "Dataset", "Input", "Link", "Mapping", "Output", "Parameter", "Plan", "Project")
__all__ = (
"Activity",
"CompositePlan",
"Dataset",
"Input",
"Link",
"Mapping",
"Output",
"Parameter",
"Plan",
"Project",
"RDFGraph",
)
18 changes: 18 additions & 0 deletions renku/ui/api/graph/__init__.py
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
#
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku Graph API."""
77 changes: 77 additions & 0 deletions renku/ui/api/graph/rdf.py
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
#
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku RDF Graph API.

The ``RDFGraph`` class allows for the quick creation of a searchable graph object
based on the project's metadata.

To create the graph and query it:

.. code-block:: python

from renku.ui.api import RDFGraph

g = RDFGraph()
# get a list of contributors to the project
list(g.subjects(object=URIRef("http://schema.org/Person")))

For more information on querying the graph, see the `RDFLib
documentation <https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html>`_.

"""

import json

import pyld
from rdflib import Graph

from renku.command.graph import export_graph_command


class RDFGraph(Graph):
"""RDF Graph of the project's metadata."""

def __init__(self, revision_or_range=None):
"""Instantiate the RDFGraph class.

Args:
revision_or_range(None): Revision or range to generate the graph from. Defaults to ``None``

"""
super().__init__()
self.revision_or_range = revision_or_range
self._build()
self.bind_(self)

def _build(self):
"""Construct the RDF graph representing this Renku project."""
data = json.dumps(
pyld.jsonld.expand(
export_graph_command().build().execute(revision_or_range=self.revision_or_range).output._graph
)
)
self.parse(data=data, format="json-ld")

@staticmethod
def bind_(graph):
"""Bind all the usual namespaces."""
graph.bind("prov", "http://www.w3.org/ns/prov#")
graph.bind("oa", "http://www.w3.org/ns/oa#")
graph.bind("schema", "http://schema.org/")
graph.bind("renku", "https://swissdatasciencecenter.github.io/renku-ontology#")
graph.bind("foaf", "http://xmlns.com/foaf/0.1/")
28 changes: 28 additions & 0 deletions tests/api/test_rdfgraph.py
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# Copyright 2017-2022 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for RDFGraph API."""

from rdflib import URIRef

from renku.ui.api import RDFGraph


def test_get_graph(project):
"""Test generating an RDFGraph."""
g = RDFGraph()
assert URIRef("mailto:renku@datascience.ch") in g.subjects(object=URIRef("http://schema.org/Person"))