Kotoba Rewrite
Advanced graph rewriting engine for the Kotoba graph processing system. Implements formal graph transformation techniques including Double Pushout (DPO) rewriting and rule-based transformations.
๐ฏ Overview
Kotoba Rewrite serves as the graph transformation layer, providing formal methods for applying complex graph transformations. It implements the Double Pushout (DPO) approach for mathematically sound graph rewriting with pattern matching, rule application, and strategy execution.
๐๏ธ Architecture
Core Components
RewriteEngine (engine.rs)
// Main rewrite engine coordinating matching and application
RuleMatcher (matcher.rs)
// Graph pattern matching using subgraph isomorphism
;
RuleApplier (applier.rs)
// Safe application of transformation rules
;
๐งฎ Double Pushout (DPO) Algorithm
The rewrite engine implements the formal Double Pushout approach:
L โ[m]โ K โ[r]โ R
โ โ โ
L'โ[m']โ K' โ[r']โ R'
Mathematical Foundation:
- L: Left-hand side (pattern to match in host graph)
- K: Interface (elements preserved during transformation)
- R: Right-hand side (result pattern)
- m, m': Match morphisms (injective mappings)
- r, r': Rule morphisms (structure-preserving mappings)
๐ Quality Metrics
| Metric | Status |
|---|---|
| Compilation | โ Clean (no warnings) |
| Tests | โ Comprehensive test suite |
| Documentation | โ Complete API docs |
| Performance | โ Efficient pattern matching |
| Correctness | โ Formal DPO semantics |
| Safety | โ Transactional operations |
๐ง Usage
Basic Rule Application
use *;
use ;
use GraphRef;
// Create rewrite engine
let engine = new;
// Define transformation rule
let rule = RuleIR ;
// Apply transformation strategy
let strategy = StrategyIR ;
let graph = new;
let patch = engine.rewrite?;
Pattern Matching
use RuleMatcher;
// Find all matches for a pattern
let matcher = new;
let catalog = empty;
let matches = matcher.find_matches?;
for match_result in matches
Strategy Composition
// Compose multiple strategies
let complex_strategy = StrategyIR ;
๐ Ecosystem Integration
Kotoba Rewrite is the transformation foundation:
| Crate | Purpose | Integration |
|---|---|---|
kotoba-core |
Required | RuleIR, StrategyIR, Pattern definitions |
kotoba-graph |
Required | Graph data structures for transformation |
kotoba-execution |
Optional | Query execution with rewrite strategies |
kotoba-storage |
Optional | Persistence of transformation rules |
kotoba-server |
Optional | Graph transformation REST APIs |
๐งช Testing
Test Coverage:
- โ RewriteEngine creation and basic operations
- โ RuleMatcher and RuleApplier component tests
- โ RuleIR and StrategyIR structure validation
- โ Pattern creation and manipulation
- โ Patch operations and transformations
- โ Match result handling
- โ Catalog integration
- โ Empty graph transformation scenarios
๐ Performance
- Efficient Pattern Matching: Optimized subgraph isomorphism algorithms
- Transactional Safety: Atomic transformation operations with rollback
- Memory Efficient: Minimal memory overhead for large graph transformations
- Composable Strategies: Chain multiple transformations without intermediate copies
- Lazy Evaluation: On-demand computation for large transformation spaces
๐ Security
- Type Safety: Strongly typed transformation rules prevent invalid operations
- Memory Safety: Rust guarantees prevent buffer overflows and memory corruption
- Transactional Integrity: All-or-nothing transformation application
- Access Control: Rule-based authorization for transformation permissions
๐ API Reference
Core Types
- [
RewriteEngine] - Main rewrite coordination engine - [
RuleMatcher] - Graph pattern matching component - [
RuleApplier] - Safe rule application component - [
RuleIR] - Intermediate representation of rewrite rules - [
StrategyIR] - Transformation strategy definitions - [
Pattern] - Graph patterns for matching and transformation - [
Patch] - Atomic graph modification operations - [
Match] - Pattern matching results with variable bindings
Transformation Strategies
- [
StrategyOp::Once] - Apply rule exactly once - [
StrategyOp::Exhaust] - Apply rule until no more matches - [
StrategyOp::While] - Apply rule while condition holds - [
StrategyOp::Seq] - Sequential strategy composition - [
StrategyOp::Choice] - Alternative strategy selection - [
StrategyOp::Priority] - Priority-based strategy execution
๐ค Contributing
See the main Kotoba repository for contribution guidelines.
๐ License
Licensed under MIT OR Apache-2.0. See LICENSE for details.