kotoba_core/ir/
strategy.rs

1//! Strategy-IR(極小戦略表現)
2
3use serde::{Deserialize, Serialize};
4use crate::types::*;
5
6/// 戦略演算子
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(tag = "op")]
9pub enum StrategyOp {
10    /// 1回だけ適用
11    Once {
12        rule: String,  // ルール名またはハッシュ
13    },
14
15    /// 適用可能になるまで繰り返し
16    Exhaust {
17        rule: String,
18        #[serde(default)]
19        order: Order,
20        #[serde(skip_serializing_if = "Option::is_none")]
21        measure: Option<String>,
22    },
23
24    /// 条件付き繰り返し
25    While {
26        rule: String,
27        pred: String,  // 述語名
28        #[serde(default)]
29        order: Order,
30    },
31
32    /// 順次実行
33    Seq {
34        strategies: Vec<Box<StrategyOp>>,
35    },
36
37    /// 選択実行(最初に成功したもの)
38    Choice {
39        strategies: Vec<Box<StrategyOp>>,
40    },
41
42    /// 優先順位付き選択
43    Priority {
44        strategies: Vec<PrioritizedStrategy>,
45    },
46}
47
48/// 優先順位付き戦略
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct PrioritizedStrategy {
51    pub strategy: Box<StrategyOp>,
52    pub priority: i32,
53}
54
55/// 適用順序
56#[derive(Debug, Clone, Serialize, Deserialize, Default)]
57pub enum Order {
58    #[default]
59    #[serde(rename = "topdown")]
60    TopDown,
61
62    #[serde(rename = "bottomup")]
63    BottomUp,
64
65    #[serde(rename = "fair")]
66    Fair,
67}
68
69/// 戦略IR
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct StrategyIR {
72    pub strategy: StrategyOp,
73}
74
75/// 戦略実行結果
76#[derive(Debug, Clone)]
77pub struct StrategyResult {
78    pub applied_count: usize,
79    pub final_graph: GraphRef_,
80    pub patches: Vec<crate::ir::patch::Patch>,
81}
82
83/// 外部述語/測度トレイト
84pub trait Externs {
85    /// 次数が指定値以上かチェック
86    fn deg_ge(&self, v: VertexId, k: u32) -> bool;
87
88    /// エッジ数が非増加かチェック(停止測度)
89    fn edge_count_nonincreasing(&self, g0: &GraphRef_, g1: &GraphRef_) -> bool;
90
91    /// カスタム述語
92    fn custom_pred(&self, name: &str, args: &[Value]) -> bool;
93}