| Crates.io | kotoba-rewrite |
| lib.rs | kotoba-rewrite |
| version | 0.1.22 |
| created_at | 2025-09-14 07:45:58.888157+00 |
| updated_at | 2025-09-19 19:14:21.706011+00 |
| description | Advanced graph rewriting engine implementing DPO transformations |
| homepage | https://github.com/com-junkawasaki/kotoba |
| repository | https://github.com/com-junkawasaki/kotoba |
| max_upload_size | |
| id | 1838442 |
| size | 81,288 |
Advanced graph rewriting engine for the Kotoba graph processing system. Implements formal graph transformation techniques including Double Pushout (DPO) rewriting and rule-based transformations.
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.
engine.rs)// Main rewrite engine coordinating matching and application
#[derive(Debug)]
pub struct RewriteEngine {
matcher: RuleMatcher,
applier: RuleApplier,
}
impl RewriteEngine {
pub fn new() -> Self;
pub fn match_rule(&self, graph: &GraphRef, rule: &RuleIR, catalog: &Catalog) -> Result<Vec<Match>>;
pub fn rewrite(&self, graph: &GraphRef, rule: &RuleIR, strategy: &StrategyIR) -> Result<Patch>;
}
matcher.rs)// Graph pattern matching using subgraph isomorphism
pub struct RuleMatcher;
impl RuleMatcher {
pub fn find_matches(&self, graph: &GraphRef, rule: &RuleIR, catalog: &Catalog) -> Result<Vec<Match>>;
}
applier.rs)// Safe application of transformation rules
pub struct RuleApplier;
impl RuleApplier {
pub fn apply_patch(&self, graph: &GraphRef, patch: &Patch) -> Result<GraphRef>;
}
The rewrite engine implements the formal Double Pushout approach:
L โ[m]โ K โ[r]โ R
โ โ โ
L'โ[m']โ K' โ[r']โ R'
Mathematical Foundation:
| 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 |
use kotoba_rewrite::prelude::*;
use kotoba_core::{types::*, ir::*};
use kotoba_graph::graph::GraphRef;
// Create rewrite engine
let engine = RewriteEngine::new();
// Define transformation rule
let rule = RuleIR {
name: "add_friendship_label".to_string(),
lhs: Pattern {
nodes: vec![("person".to_string(), "Person".to_string())],
edges: vec![("friendship".to_string(), "person".to_string(), "person".to_string(), "FOLLOWS".to_string())],
},
rhs: Pattern {
nodes: vec![("person".to_string(), "Person".to_string())],
edges: vec![("friendship".to_string(), "person".to_string(), "person".to_string(), "FRIEND".to_string())],
},
conditions: vec![],
};
// Apply transformation strategy
let strategy = StrategyIR {
name: "exhaustive_update".to_string(),
strategy: StrategyOp::Exhaust {
rule: "add_friendship_label".to_string(),
order: None,
measure: None,
},
};
let graph = GraphRef::new(Graph::empty());
let patch = engine.rewrite(&graph, &rule, &strategy)?;
use kotoba_rewrite::rewrite::matcher::RuleMatcher;
// Find all matches for a pattern
let matcher = RuleMatcher::new();
let catalog = Catalog::empty();
let matches = matcher.find_matches(&graph, &rule, &catalog)?;
for match_result in matches {
println!("Found match: {:?}", match_result.mapping);
}
// Compose multiple strategies
let complex_strategy = StrategyIR {
name: "multi_step_rewrite".to_string(),
strategy: StrategyOp::Seq {
strategies: vec![
StrategyIR {
name: "normalize_labels".to_string(),
strategy: StrategyOp::Exhaust {
rule: "normalize_labels".to_string(),
order: None,
measure: None,
},
},
StrategyIR {
name: "remove_duplicates".to_string(),
strategy: StrategyOp::While {
rule: "remove_duplicates".to_string(),
pred: None,
order: None,
},
},
],
},
};
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 |
cargo test -p kotoba-rewrite
Test Coverage:
RewriteEngine] - Main rewrite coordination engineRuleMatcher] - Graph pattern matching componentRuleApplier] - Safe rule application componentRuleIR] - Intermediate representation of rewrite rulesStrategyIR] - Transformation strategy definitionsPattern] - Graph patterns for matching and transformationPatch] - Atomic graph modification operationsMatch] - Pattern matching results with variable bindingsStrategyOp::Once] - Apply rule exactly onceStrategyOp::Exhaust] - Apply rule until no more matchesStrategyOp::While] - Apply rule while condition holdsStrategyOp::Seq] - Sequential strategy compositionStrategyOp::Choice] - Alternative strategy selectionStrategyOp::Priority] - Priority-based strategy executionSee the main Kotoba repository for contribution guidelines.
Licensed under MIT OR Apache-2.0. See LICENSE for details.