diff --git a/docs/reference/api.rst b/docs/reference/api.rst index 31be0fc729..4e7e463f85 100644 --- a/docs/reference/api.rst +++ b/docs/reference/api.rst @@ -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 `_. -.. _api-project: +.. _api-activity: -``Project`` ------------ +``Activity`` +------------ -.. automodule:: renku.ui.api.models.project +.. automodule:: renku.ui.api.models.activity .. _api-dataset: @@ -34,6 +34,13 @@ 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`` @@ -41,16 +48,16 @@ The following sections describe the Renku Python API. If you work with the R pro .. 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 diff --git a/renku/ui/api/__init__.py b/renku/ui/api/__init__.py index 17f7fc8204..2c8524c295 100644 --- a/renku/ui/api/__init__.py +++ b/renku/ui/api/__init__.py @@ -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", +) diff --git a/renku/ui/api/graph/__init__.py b/renku/ui/api/graph/__init__.py new file mode 100644 index 0000000000..7a31cc9b6a --- /dev/null +++ b/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.""" diff --git a/renku/ui/api/graph/rdf.py b/renku/ui/api/graph/rdf.py new file mode 100644 index 0000000000..89d8497b58 --- /dev/null +++ b/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 `_. + +""" + +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/") diff --git a/tests/api/test_rdfgraph.py b/tests/api/test_rdfgraph.py new file mode 100644 index 0000000000..8fdd1db414 --- /dev/null +++ b/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"))