|
7 | 7 | ## Usage |
8 | 8 |
|
9 | 9 | ```python |
10 | | -from ontoenv import Config, OntoEnv |
| 10 | +from ontoenv import OntoEnv |
11 | 11 | from rdflib import Graph |
12 | 12 |
|
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 |
18 | 43 | 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") |
35 | 52 | ``` |
0 commit comments