Skip to content

Commit

Permalink
add script to perform an example SPARQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
simleo committed Oct 19, 2023
1 parent 5e47d26 commit 1a606a8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/examples/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2023 CRS4.
#
# 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.

"""\
Perform a sample query on a Workflow Run RO-Crate.
The query returns all actions together with their instruments.
"""

from pathlib import Path
import argparse
import rdflib


query = """\
PREFIX schema: <http://schema.org/>
SELECT ?action ?instrument
WHERE {
?action a schema:CreateAction .
?action schema:instrument ?instrument
}
"""


def main(args):
args.crate = Path(args.crate)
g = rdflib.Graph()
g.parse(args.crate / "ro-crate-metadata.json")
qres = g.query(query)
for row in qres:
print(f"action: {row.action}")
print(f"instrument: {row.instrument}")
print()


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument("crate", metavar="RO-Crate", help="RO-Crate directory")
main(parser.parse_args())
1 change: 1 addition & 0 deletions docs/examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rdflib~=7.0.0

0 comments on commit 1a606a8

Please sign in to comment.