Query, inspect and visualize ontologies encoded via RDF and OWL.
—
Ontospy provides comprehensive command-line tools for interactive ontology exploration, analysis, and documentation generation. The CLI interface includes commands for scanning ontologies, generating documentation, managing local libraries, and launching interactive shells.
The primary command-line interface providing access to all ontospy functionality.
def main_cli():
"""
Main CLI entry point handling command routing and argument parsing.
Available commands:
- scan: Analyze RDF sources and display statistics
- gendocs: Generate documentation and visualizations
- lib: Manage local ontology library
- shell: Launch interactive Python shell
- ser: Serialize RDF data to different formats
- utils: Utility functions and tools
"""Command Line Usage:
# Basic usage
ontospy [command] [options] [arguments]
# Show help
ontospy --help
ontospy [command] --help
# Quick test utility
ontospy_quicktestAnalyze RDF sources and generate comprehensive reports about ontological content and structure.
def scan():
"""
Scan and analyze RDF sources with detailed reporting.
Supports:
- Local files and directories
- Remote URLs and SPARQL endpoints
- Multiple RDF formats
- Statistical analysis and summaries
- Entity enumeration and relationship mapping
"""Usage Examples:
# Scan local ontology file
ontospy scan ontology.owl
# Scan remote ontology
ontospy scan http://xmlns.com/foaf/0.1/
# Scan with verbose output
ontospy scan --verbose ontology.rdf
# Scan SPARQL endpoint
ontospy scan --endpoint http://dbpedia.org/sparql
# Output raw RDF data
ontospy scan --raw ontology.ttl
# Include individuals in analysis
ontospy scan --individuals ontology.owl
# Specify RDF format
ontospy scan --format turtle data.rdfGenerate comprehensive documentation and visualizations from ontologies.
def gendocs():
"""
Generate ontology documentation and visualizations.
Features:
- Multiple output formats (HTML, markdown, interactive)
- Customizable themes and styling
- Interactive visualization types
- Batch processing capabilities
- Template customization options
"""Usage Examples:
# Generate default HTML documentation
ontospy gendocs ontology.owl
# Specify output directory
ontospy gendocs ontology.owl --outputdir ./docs
# Use specific visualization type
ontospy gendocs ontology.owl --type 2 # HTML multi-page
# Apply custom theme
ontospy gendocs ontology.owl --theme flatly
# Set custom title
ontospy gendocs ontology.owl --title "My Ontology"
# Interactive type selection
ontospy gendocs ontology.owl --interactive
# Generate multiple formats
ontospy gendocs ontology.owl --type 1,2,3 # HTML single, multi, markdownManage local ontology repositories with caching, indexing, and quick access functionality.
def lib():
"""
Manage local ontology library and cache.
Operations:
- Initialize local repository
- Cache frequently used ontologies
- List cached ontologies with metadata
- Search and filter library contents
- Update and refresh cached data
- Remove outdated cache entries
"""Usage Examples:
# Initialize local library
ontospy lib --init
# List cached ontologies
ontospy lib --list
# Search library contents
ontospy lib --search "foaf"
# Add ontology to library
ontospy lib --add http://xmlns.com/foaf/0.1/
# Remove from library
ontospy lib --remove "foaf"
# Update cached ontology
ontospy lib --update http://xmlns.com/foaf/0.1/
# Show library statistics
ontospy lib --stats
# Clear library cache
ontospy lib --clearLaunch enhanced Python shell environment with ontospy preloaded and ontology-specific utilities.
def shell():
"""
Launch interactive Python shell with ontospy environment.
Features:
- Preloaded ontospy modules and classes
- Ontology-specific helper functions
- Enhanced tab completion for ontology exploration
- Built-in documentation and examples
- Quick access to common analysis patterns
"""Usage Examples:
# Launch interactive shell
ontospy shell
# Launch with preloaded ontology
ontospy shell ontology.owl
# Shell with remote ontology
ontospy shell http://xmlns.com/foaf/0.1/
# Launch with specific namespace preferences
ontospy shell --lang en ontology.owlShell Environment:
# Available in shell session
import ontospy
# Preloaded if ontology specified
g = ontospy.Ontospy("your-ontology.owl", build_all=True)
# Quick exploration commands
g.stats() # Show statistics
g.printClassTree() # Print class hierarchy
g.all_classes # Access all classes
g.all_properties # Access all properties
# Helper functions available
help(ontospy) # Built-in help
examples() # Show usage examples (if available)Convert between RDF formats and generate serialized output with format validation.
def ser():
"""
Serialize RDF data between different formats.
Supported formats:
- Turtle (.ttl)
- RDF/XML (.rdf, .xml)
- N-Triples (.nt)
- N3 (.n3)
- JSON-LD (.jsonld)
- Trig (.trig)
- NQuads (.nq)
"""Usage Examples:
# Convert RDF/XML to Turtle
ontospy ser --input ontology.rdf --output ontology.ttl --format turtle
# Convert Turtle to JSON-LD
ontospy ser --input data.ttl --output data.jsonld --format json-ld
# Pretty-print RDF data
ontospy ser --input ontology.owl --format turtle --pretty
# Validate RDF syntax
ontospy ser --input data.rdf --validate
# Convert with base URI
ontospy ser --input ontology.ttl --output ontology.rdf --format xml --base http://example.org/Additional utility commands for ontology development and maintenance workflows.
def utils():
"""
Utility functions for ontology development workflows.
Available utilities:
- Ontology validation and syntax checking
- Namespace analysis and cleanup
- Dead link detection for imported ontologies
- Duplicate detection and cleanup
- Performance profiling for large ontologies
"""Usage Examples:
# Validate ontology syntax
ontospy utils --validate ontology.owl
# Check for dead imports
ontospy utils --check-imports ontology.owl
# Analyze namespace usage
ontospy utils --namespaces ontology.rdf
# Find duplicate definitions
ontospy utils --duplicates ontology.ttl
# Profile ontology loading performance
ontospy utils --profile ontology.owlThe CLI commands are implemented using core action functions that can also be used programmatically.
from ontospy.core.actions import action_analyze
def action_analyze(sources, endpoint=None, print_opts=False,
verbose=False, extra=False, raw=False,
individuals=False, format=""):
"""
Core analysis function used by scan command.
Parameters:
- sources (list): List of RDF sources to analyze
- endpoint (str): SPARQL endpoint URL
- print_opts (bool): Print analysis options
- verbose (bool): Enable verbose output
- extra (bool): Include extra analysis details
- raw (bool): Output raw RDF data
- individuals (bool): Include individual analysis
- format (str): Specify RDF format
Returns:
Ontospy: Analyzed ontology graph object
"""# Set default output directory
export ONTOSPY_OUTPUT_DIR=/path/to/docs
# Configure library location
export ONTOSPY_LIBRARY=/path/to/library
# Set default theme
export ONTOSPY_DEFAULT_THEME=flatly
# Configure cache behavior
export ONTOSPY_CACHE_DISABLE=falseOntospy looks for configuration in:
~/.ontospy/config.ini - Global configuration./ontospy.ini - Project-specific configuration# 1. Initialize local library
ontospy lib --init
# 2. Add frequently used ontologies to library
ontospy lib --add http://xmlns.com/foaf/0.1/
ontospy lib --add http://purl.org/dc/terms/
# 3. Analyze new ontology
ontospy scan my-ontology.owl --verbose
# 4. Generate comprehensive documentation
ontospy gendocs my-ontology.owl \\
--outputdir ./docs \\
--type 2 \\
--theme cerulean \\
--title "My Domain Ontology"
# 5. Launch interactive exploration
ontospy shell my-ontology.owl
# 6. Convert to different format
ontospy ser --input my-ontology.owl \\
--output my-ontology.ttl \\
--format turtle
# 7. Validate and check ontology
ontospy utils --validate my-ontology.owl
ontospy utils --check-imports my-ontology.owlCLI functionality can be integrated into Python scripts and automation workflows.
import subprocess
import ontospy
# Use CLI commands from Python
def analyze_ontology(file_path):
result = subprocess.run([
'ontospy', 'scan', file_path, '--verbose'
], capture_output=True, text=True)
return result.stdout
# Or use core functions directly
from ontospy.core.actions import action_analyze
def analyze_programmatically(sources):
g = action_analyze(sources, verbose=True)
return g
# Generate docs programmatically
from ontospy.gendocs.actions import build_visualization
def generate_docs(ontology_path, output_dir):
g = ontospy.Ontospy(ontology_path, build_all=True)
build_visualization(g, viz_index=2, path=output_dir)Install with Tessl CLI
npx tessl i tessl/pypi-ontospy