kotoba_core/ir/
rule.rs

1//! Rule-IR(DPO型付き属性グラフ書換え)
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use crate::types::*;
6
7/// ガード条件(名前付き述語)
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Guard {
10    pub ref_: String,  // 述語名(例: "deg_ge")
11    pub args: HashMap<String, Value>,
12}
13
14/// グラフパターン要素
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct GraphElement {
17    pub id: String,  // 変数名
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub type_: Option<Label>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub props: Option<Properties>,
22}
23
24/// エッジ定義
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct EdgeDef {
27    pub id: String,
28    pub src: String,
29    pub dst: String,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub type_: Option<Label>,
32}
33
34/// グラフパターン
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct GraphPattern {
37    pub nodes: Vec<GraphElement>,
38    pub edges: Vec<EdgeDef>,
39}
40
41/// 負の条件(NAC: Negative Application Condition)- ルール用
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct RuleNac {
44    pub nodes: Vec<GraphElement>,
45    pub edges: Vec<EdgeDef>,
46}
47
48/// DPOルール定義
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct RuleIR {
51    pub name: String,
52    pub types: HashMap<String, Vec<Label>>,  // 型定義
53    pub lhs: GraphPattern,                   // Left-hand side (L)
54    pub context: GraphPattern,               // Context (K)
55    pub rhs: GraphPattern,                   // Right-hand side (R)
56    pub nacs: Vec<RuleNac>,                   // Negative conditions
57    pub guards: Vec<Guard>,                  // ガード条件
58}
59
60/// ルールマッチ結果
61#[derive(Debug, Clone)]
62pub struct Match {
63    pub mapping: HashMap<String, VertexId>,  // 変数→頂点IDマッピング
64    pub score: f64,                         // マッチスコア
65}
66
67/// 複数マッチ結果
68pub type Matches = Vec<Match>;