Ontology Examples
This page provides practical examples of working with ontologies in the Graphora client library.
Creating and Registering an Ontology
The following example demonstrates how to create an ontology definition and register it with Graphora:
from graphora import GraphoraClient
# Initialize the client with user ID (required)
client = GraphoraClient(
base_url="https://api.graphora.io",
user_id="your-user-id", # Required for all API calls
api_key="your-api-key"
)
# Define your ontology
ontology_yaml = """
version: 1
entities:
Person:
properties:
name:
type: string
unique: true
required: true
age:
type: integer
email:
type: string
index: true
relationships:
works_at:
target: Company
cardinality: many_to_one
knows:
target: Person
cardinality: many_to_many
Company:
properties:
name:
type: string
unique: true
required: true
industry:
type: string
founded_year:
type: integer
relationships:
has_subsidiary:
target: Company
cardinality: one_to_many
"""
# Register the ontology
response = client.register_ontology(ontology_yaml)
print(f"Ontology registered with ID: {response.id}")
Loading an Ontology from a File
You can also load an ontology from a YAML file:
from graphora import GraphoraClient
# Initialize the client with user ID (required)
client = GraphoraClient(
base_url="https://api.graphora.io",
user_id="your-user-id", # Required for all API calls
api_key="your-api-key"
)
# Load the ontology from a file
with open("ontology.yaml", "r") as f:
ontology_yaml = f.read()
# Register the ontology
response = client.register_ontology(ontology_yaml)
print(f"Ontology registered with ID: {response.id}")
Retrieving an Existing Ontology
You can retrieve a previously registered ontology by its ID:
from graphora import GraphoraClient
# Initialize the client with user ID (required)
client = GraphoraClient(
base_url="https://api.graphora.io",
user_id="your-user-id", # Required for all API calls
api_key="your-api-key"
)
# Retrieve an ontology by ID
ontology_id = "your-ontology-id"
ontology_yaml = client.get_ontology(ontology_id)
print("Retrieved ontology:")
print(ontology_yaml)
Updating an Existing Ontology
To update an existing ontology, you can use the same register_ontology
method with an updated definition:
from graphora import GraphoraClient
# Initialize the client with user ID (required)
client = GraphoraClient(
base_url="https://api.graphora.io",
user_id="your-user-id", # Required for all API calls
api_key="your-api-key"
)
# Define your updated ontology
updated_ontology_yaml = """
version: 2
entities:
Person:
properties:
name:
type: string
unique: true
required: true
age:
type: integer
email:
type: string
index: true
phone: # New property added
type: string
relationships:
works_at:
target: Company
cardinality: many_to_one
knows:
target: Person
cardinality: many_to_many
Company:
properties:
name:
type: string
unique: true
required: true
industry:
type: string
founded_year:
type: integer
website: # New property added
type: string
relationships:
has_subsidiary:
target: Company
cardinality: one_to_many
"""
# Register the updated ontology
response = client.register_ontology(updated_ontology_yaml)
print(f"Updated ontology registered with ID: {response.id}")
These examples demonstrate the basic operations you can perform with ontologies in the Graphora client library. For more detailed information about ontology structure and options, see the Ontology Concepts page.