A powerful command-line interface that converts natural language queries into SPARQL using OWL ontologies. Built with the SPARC methodology (Specification, Pseudocode, Architecture, Refinement, Completion) for systematic development.
- ๐ฃ๏ธ Natural Language Queries: "meetings with John Smith", "todos assigned to Sarah"
- ๐ง Automatic Pattern Generation: Creates query patterns from OWL ontology structure
- โก Interactive & Command Modes: Both REPL and single-command execution
- ๐ฏ No SPARQL Knowledge Required: Users query in plain English
- ๐ Intelligent Suggestions: Provides corrections for failed queries
- ๐ Debug Mode: Shows pattern matching and SPARQL generation details
- ๐ SPARQL Endpoint Execution: Execute queries against any SPARQL endpoint
- ๐ Named Graph Support: Query specific graphs or datasets
- ๐ Multiple Output Formats: JSON, CSV, Table, Rich terminal, Turtle
- ๐ Authentication Support: Basic and Digest authentication for secured endpoints
- ๐๏ธ Extensible Architecture: Clean separation of concerns, fully tested
# Install directly from source
pip install -e .
# Or install required dependencies
pip install rdflib click# Query with an ontology file
kb-query --ontology data/kb-vocabulary.ttl "meetings with John Smith"
# Show generated SPARQL
kb-query --ontology data/kb-vocabulary.ttl --show-sparql "todos with Sarah"
# Execute query against SPARQL endpoint
kb-query --ontology data/kb-vocabulary.ttl -x -e http://localhost:3030/kb/sparql "meetings with John"
# Query specific named graphs
kb-query -o data/kb-vocabulary.ttl -x -e http://localhost:3030/dataset/sparql \
-g http://example.org/graph1 -g http://example.org/graph2 "meetings"
# Query with default graph
kb-query -o data/kb-vocabulary.ttl -x -e http://localhost:3030/dataset/sparql \
--default-graph http://example.org/default "todos"
# Test endpoint connection
kb-query --test-connection -e http://dbpedia.org/sparql
# Different output formats
kb-query -o data/kb-vocabulary.ttl -x -e http://localhost:3030/kb/sparql -f json "meetings"
kb-query -o data/kb-vocabulary.ttl -x -e http://localhost:3030/kb/sparql -f csv "todos"
# Interactive mode
kb-query --ontology data/kb-vocabulary.ttl --interactive
# List available query patterns
kb-query --ontology data/kb-vocabulary.ttl --list-patterns$ kb-query --ontology data/kb-vocabulary.ttl --interactive --show-sparql
KB Query Interactive Mode
Type 'help' for commands, 'exit' to quit
----------------------------------------
kb-query: meetings with John Smith
โ Query understood
Generated SPARQL:
----------------------------------------
SELECT ?meeting ?person_name
WHERE {
?meeting <http://example.org/kb#hasAttendee> ?person .
?person <http://xmlns.com/foaf/0.1/name> ?person_name .
FILTER (lcase(str(?person_name)) = lcase("John Smith"))
}
----------------------------------------
Execution time: 0.001s
kb-query: exit
Goodbye!$ kb-query -o data/kb-vocabulary.ttl --show-sparql -g http://example.org/mydata "meetings"
โ Query understood
Generated SPARQL:
----------------------------------------
SELECT ?meeting ?person_name
WHERE {
GRAPH <http://example.org/mydata> {
?meeting <http://example.org/kb#hasAttendee> ?person .
?person <http://xmlns.com/foaf/0.1/name> ?person_name .
}
}
----------------------------------------The system automatically generates patterns from your OWL ontology. For example, with the included sample ontology:
| Query Pattern | Example |
|---|---|
meetings with {person} |
"meetings with John Smith" |
{person}'s meetings |
"John's meetings" |
todos with {person} |
"todos with Sarah" |
{person}'s todos |
"Sarah's todos" |
notes with {person} |
"notes with John" |
meetings tagged {tag} |
"meetings tagged project" |
Built with a clean 4-layer architecture:
- CLI Layer: Interactive REPL + Command-line interface
- Service Layer: Query orchestration and business logic
- Core Layer: Grammar engine, pattern matching, SPARQL building
- Infrastructure Layer: OWL parsing, caching, HTTP clients
- Grammar Engine: Parses OWL ontologies and generates natural language patterns
- Pattern Matcher: Fuzzy matching of user queries to patterns with entity extraction
- SPARQL Builder: Template-based SPARQL generation with injection prevention
- Query Service: Orchestrates the complete query processing pipeline
The project follows Test-Driven Development with comprehensive test coverage:
# Run all tests
python -m pytest
# Run with coverage
python -m pytest --cov=kb_query
# Run specific test categories
python -m pytest tests/unit/ # Unit tests
python -m pytest tests/integration/ # Integration tests
python -m pytest tests/e2e/ # End-to-end testsTest Statistics:
- 17 unit tests (100% passing)
- 79% code coverage (target: 80%+)
- TDD approach throughout development
kb_query/
โโโ cli/ # Command-line interface
โ โโโ main.py # Main CLI entry point
โโโ core/ # Core business logic
โ โโโ entities.py # Data structures
โ โโโ grammar.py # OWL parsing & pattern generation
โ โโโ matcher.py # Query pattern matching
โ โโโ builder.py # SPARQL query building
โโโ services/ # Service orchestration
โ โโโ query.py # Main query service
โโโ exceptions.py # Exception hierarchy
tests/
โโโ unit/ # Unit tests
โโโ integration/ # Integration tests
โโโ e2e/ # End-to-end tests
docs/
โโโ adr/ # Architecture Decision Records
โโโ sparc/ # SPARC methodology documentation
โโโ architecture.md # System architecture
โโโ api-reference.md # API documentation
โโโ examples.md # Usage examples
This project was developed using the SPARC methodology:
- โ Specification: Requirements, user stories, success criteria
- โ Pseudocode: High-level algorithmic design
- โ Architecture: System design, interfaces, diagrams
- โ Refinement: TDD implementation, iterative improvement
- โ Completion: Integration, testing, documentation
# Install development dependencies
pip install -e .[dev]
# Run tests with coverage
python -m pytest --cov=kb_query --cov-report=html
# Run specific component tests
python -m pytest tests/unit/test_grammar_engine.py -v
python -m pytest tests/unit/test_pattern_matcher.py -v- Create your OWL ontology file in Turtle format
- Ensure proper class and property definitions with domains/ranges
- Use the CLI to see generated patterns:
kb-query --ontology your-file.ttl --list-patterns
@prefix kb: <http://example.org/kb#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
# Classes
kb:Meeting a owl:Class .
kb:Person a owl:Class .
# Properties (generates patterns automatically)
kb:hasAttendee a owl:ObjectProperty ;
rdfs:domain kb:Meeting ;
rdfs:range foaf:Person .- Architecture Overview - System design and components
- API Reference - Detailed API documentation
- Query Examples - Usage patterns and examples
- Architecture Decision Records - Design decisions and rationale
- SPARC Documentation - Development methodology artifacts
- Follow TDD: Write tests first, then implement
- Update Documentation: Keep docs current with changes
- Use Type Hints: Full type annotations required
- Run Tests: Ensure all tests pass before submitting
# Development setup
git clone <repository>
cd knowledgebase-query-system
pip install -e .[dev]
# Make changes, add tests
python -m pytest # Ensure tests pass- Systematic Development: Complete SPARC methodology application
- High Test Coverage: 79% coverage with 17 comprehensive tests
- Production Ready: Clean architecture with proper error handling
- User Friendly: Natural language interface requires no SPARQL knowledge
- Extensible: Easy to add new ontologies and query patterns
- Configuration Profiles: Multiple endpoint support with authentication
- Result Export: CSV, JSON output formats
- Query History: Persistent query history and favorites
- Advanced Patterns: Support for aggregation and complex queries
- Web Interface: Browser-based query interface
- Performance Optimization: Caching and query optimization
MIT License - see LICENSE file for details.
Built using modern Python best practices:
- rdflib for OWL/RDF processing
- click for CLI interface
- pytest for testing framework
- SPARC methodology for systematic development