Skip to content

Commit 23608c4

Browse files
authored
Merge pull request #12 from gtfierro/gtf-python-changes
Python changes
2 parents a9d8330 + 1a28e24 commit 23608c4

File tree

8 files changed

+492
-123
lines changed

8 files changed

+492
-123
lines changed

.github/workflows/artifacts.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
name: Artifacts
22

33
on:
4-
push:
5-
branches:
6-
- main
7-
- next
84
release:
95
types:
106
- published

.github/workflows/ci.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- '**' # Triggers on push to all branches
7+
pull_request:
8+
branches:
9+
- '**' # Triggers on pull request to all branches
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
build_and_test:
17+
runs-on: ubuntu-latest
18+
steps:
19+
#- run: cd python && python -m unittest test.py
20+
- uses: actions/checkout@v4
21+
with:
22+
submodules: true
23+
- name: Install uv
24+
uses: astral-sh/setup-uv@v5
25+
with:
26+
enable-cache: true
27+
- name: Set up Python
28+
run: uv python install
29+
- uses: ./.github/actions/setup-rust
30+
with:
31+
target: aarch64-unknown-linux-gnu
32+
version: stable
33+
- run: |
34+
sudo apt-get update && sudo apt-get install -y g++-aarch64-linux-gnu libssl-dev
35+
mkdir .cargo
36+
echo -e "[target.aarch64-unknown-linux-gnu]\nlinker = \"aarch64-linux-gnu-gcc\"" >> .cargo/config.toml
37+
- name: Build rust stuff
38+
run: cargo build --workspace --release
39+
- name: Run rust tests
40+
run: cargo test --workspace --release
41+
- name: Build python package
42+
run: uv run maturin build --release --features abi3
43+
working-directory: ./python
44+
- name: Test python package
45+
run: uv run python -m unittest test.py
46+
working-directory: ./python
47+

lib/src/api.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ use petgraph::graph::{Graph as DiGraph, NodeIndex};
2222
use std::collections::{HashMap, HashSet, VecDeque};
2323
use std::fs;
2424

25-
/// Searches for the .ontoenv directory in the current directory and then recursively up its parent directories.
25+
/// Searches for the .ontoenv directory in the given directory and then recursively up its parent directories.
2626
/// Returns the path to the directory containing the .ontoenv directory if found.
27-
pub fn find_ontoenv_root() -> Option<PathBuf> {
28-
let start_dir = std::env::current_dir().ok()?;
29-
let mut current_dir = Some(start_dir.as_path());
27+
pub fn find_ontoenv_root_from(start_dir: &Path) -> Option<PathBuf> {
28+
let mut current_dir = Some(start_dir);
3029
while let Some(dir) = current_dir {
3130
if dir.join(".ontoenv").is_dir() {
3231
return Some(dir.to_path_buf());
@@ -36,6 +35,13 @@ pub fn find_ontoenv_root() -> Option<PathBuf> {
3635
None
3736
}
3837

38+
/// Searches for the .ontoenv directory in the current directory and then recursively up its parent directories.
39+
/// Returns the path to the directory containing the .ontoenv directory if found.
40+
pub fn find_ontoenv_root() -> Option<PathBuf> {
41+
let start_dir = std::env::current_dir().ok()?;
42+
find_ontoenv_root_from(&start_dir)
43+
}
44+
3945
/// These are the different ways to refer to an ontology: either
4046
/// by a location (file or URL), or the name of the graph (IRI)
4147
pub enum ResolveTarget {

python/README.md

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,46 @@
77
## Usage
88

99
```python
10-
from ontoenv import Config, OntoEnv
10+
from ontoenv import OntoEnv
1111
from rdflib import Graph
1212

13-
cfg = Config(["../brick"], strict=False, offline=True)
14-
15-
# make environment
16-
env = OntoEnv(cfg)
17-
13+
# creates a new environment in the current directory, or loads
14+
# an existing one. To use a different directory, pass the 'path'
15+
# argument: OntoEnv(path="/path/to/env")
16+
# OntoEnv() will discover ontologies in the current directory and
17+
# its subdirectories
18+
env = OntoEnv()
19+
20+
# add an ontology from a file path.
21+
# env.add returns the name of the ontology, which is its URI
22+
# e.g. "https://brickschema.org/schema/1.4-rc1/Brick"
23+
brick_name = env.add("../brick/Brick.ttl")
24+
print(f"Added ontology {brick_name}")
25+
26+
# get the graph of the ontology we just added
27+
# env.get returns an rdflib.Graph
28+
brick_graph = env.get(brick_name)
29+
print(f"Brick graph has {len(brick_graph)} triples")
30+
31+
# get the full closure of the ontology, including all of its imports
32+
# also returns an rdflib.Graph
33+
brick_closure_graph = env.get_closure(brick_name)
34+
print(f"Brick closure has {len(brick_closure_graph)} triples")
35+
36+
# you can also add ontologies from a URL
37+
rec_name = env.add("https://w3id.org/rec/rec.ttl")
38+
rec_graph = env.get(rec_name)
39+
print(f"REC graph has {len(rec_graph)} triples")
40+
41+
# if you have an rdflib.Graph with an owl:Ontology declaration,
42+
# you can transitively import its dependencies into the graph
1843
g = Graph()
19-
# put the transitive owl:imports closure into 'g'
20-
env.get_closure("https://brickschema.org/schema/1.4-rc1/Brick", g)
21-
22-
# or, get the graph directly
23-
g = env.get_closure("https://brickschema.org/schema/1.4-rc1/Brick")
24-
25-
brick = Graph()
26-
brick.parse("Brick.ttl", format="turtle")
27-
# transitively import dependencies into the 'brick' graph, using the owl:imports declarations
28-
env.import_dependencies(brick)
29-
30-
# pull Brick graph out of environment
31-
brick = env.get_graph("https://brickschema.org/schema/1.4-rc1/Brick")
32-
33-
# import graphs by name
34-
env.import_graph(brick, "https://w3id.org/rec")
44+
# this graph just has one triple: the ontology declaration for Brick
45+
g.parse(data="""
46+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
47+
<https://brickschema.org/schema/1.4-rc1/Brick> a owl:Ontology .
48+
""")
49+
# this will load all of the owl:imports of the Brick ontology into 'g'
50+
env.import_dependencies(g)
51+
print(f"Graph with imported dependencies has {len(g)} triples")
3552
```

python/example.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from ontoenv import Config, OntoEnv, version
2+
from rdflib import Graph
3+
print(version)
4+
5+
6+
cfg = Config(["../brick"], strict=False, offline=False)
7+
8+
print("Make env")
9+
env = OntoEnv(cfg)
10+
print(env)
11+
print("get brick")
12+
g = Graph()
13+
env.get_closure("https://brickschema.org/schema/1.4-rc1/Brick", g)
14+
print(len(g))
15+
16+
print("get brick 2")
17+
brick = Graph()
18+
brick.parse("../brick/Brick.ttl", format="turtle")
19+
env.import_dependencies(brick)
20+
print(len(brick))
21+
brick_name = env.add("https://brickschema.org/schema/1.4/Brick.ttl")
22+
print(f"Added {brick_name}")
23+
del env
24+
25+
print("new env")
26+
env2 = OntoEnv()
27+
print(env2.store_path())
28+
29+
print("get brick again from URL")
30+
brick = env2.get("https://brickschema.org/schema/1.4/Brick")
31+
print(len(brick))
32+
print(brick)
33+
print(type(brick))
34+
35+
print("brick closure", env2.list_closure("https://brickschema.org/schema/1.4/Brick"))
36+
37+
env2.import_graph(brick, "https://w3id.org/rec")
38+
brick.serialize("test.ttl", format="turtle")
39+
40+
print("qudtqk deps", env2.get_dependents('http://qudt.org/2.1/vocab/quantitykind'))
41+
42+
# get an rdflib.Dataset (https://rdflib.readthedocs.io/en/stable/apidocs/rdflib.html#rdflib.Dataset)
43+
ds = env2.to_rdflib_dataset()
44+
for graphname in ds.graphs():
45+
graph = ds.graph(graphname)
46+
print(f"Graph {graphname} has {len(graph)} triples")

python/pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ build-backend = "maturin"
2222

2323
[dependency-groups]
2424
dev = [
25+
"maturin>=1.9.0",
2526
"pytest>=8.3.5",
2627
]
28+
29+
[tool.uv]
30+
# Rebuild package when any rust files change
31+
cache-keys = [{file = "pyproject.toml"}, {file = "Cargo.toml"}, {file = "**/*.rs"}]

0 commit comments

Comments
 (0)