1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
import sys
from pathlib import Path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root / "src"))
from conventionalrp.core.rules import Rule, RuleEngine
from conventionalrp.core.processor import Processor
def example_1_simple_rules():
rule = Rule(
name="tag_dice_rolls",
condition={"type": "dice_roll"},
action={"type": "add_tag", "tag": "game_mechanics"},
priority=100
)
engine = RuleEngine()
engine.add_rule(rule)
data = {"type": "dice_roll", "content": "[d20 = 18]"}
print(f"Original data: {data}")
result = engine.process(data)
print(f"Processed result: {result}")
print()
def example_2_conditional_rules():
engine = RuleEngine()
# Rule1: High rolls (>=15) get "success" tag
engine.add_rule_dict(
name="high_roll",
condition={
"type": "dice_roll",
"result": {"type": "greater_than", "value": 15}
},
action={"type": "add_tag", "tag": "success"},
priority=100
)
# Rule2: Low rolls (<10) get "failure" tag
engine.add_rule_dict(
name="low_roll",
condition={
"type": "dice_roll",
"result": {"type": "less_than", "value": 10}
},
action={"type": "add_tag", "tag": "failure"},
priority=100
)
test_cases = [
{"type": "dice_roll", "result": 18, "content": "[d20 = 18]"},
{"type": "dice_roll", "result": 5, "content": "[d20 = 5]"},
{"type": "dice_roll", "result": 12, "content": "[d20 = 12]"},
]
for data in test_cases:
result = engine.process(data)
print(f"结果: {data['result']} -> 标签: {result.get('tags', [])}")
print()
def example_3_field_transformation():
engine = RuleEngine()
# Rule: Normalize speaker names
engine.add_rule_dict(
name="normalize_speaker",
condition={"type": "metadata"},
action={
"type": "transform",
"field": "speaker",
"function": "upper"
},
priority=90
)
data = {
"type": "metadata",
"speaker": "艾莉娅",
"timestamp": "2025-10-24 14:30:01"
}
print(f"Original data: {data}")
result = engine.process(data)
print(f"Processed result: {result}")
print()
def example_4_processor_with_rules():
processor = Processor()
# Add rule: Highlight important dialogues
processor.add_rule(Rule(
name="highlight_important_dialogue",
condition={
"type": "dialogue",
"content": {"type": "contains", "value": "重要"}
},
action={"type": "add_tag", "tag": "important"},
priority=100
))
# Add rule: Mark all metadata as processed
processor.add_rule(Rule(
name="mark_metadata",
condition={"type": "metadata"},
action={"type": "set_field", "field": "processed_by", "value": "rule_engine"},
priority=90
))
tokens = [
{"type": "metadata", "speaker": "DM", "timestamp": "2025-10-24"},
{"type": "dialogue", "content": "这是重要的线索"},
{"type": "dialogue", "content": "普通对话"},
{"type": "dice_roll", "result": 20},
]
print(f"Processing {len(tokens)} tokens...")
results = processor.process_tokens(tokens)
for i, result in enumerate(results):
print(f" [{i+1}] {result.get('type')}: "
f"Tags={result.get('tags', [])} "
f"Processed by={result.get('processed_by', 'N/A')}")
print()
def example_5_custom_processor():
processor = Processor()
# Custom processing function: Count characters
def add_char_count(data):
if "content" in data:
data["char_count"] = len(data["content"])
return data
# Custom processing function: Ensure timestamp exists
def ensure_timestamp(data):
if "timestamp" not in data:
from datetime import datetime
data["timestamp"] = datetime.now().isoformat()
return data
processor.add_processor(add_char_count)
processor.add_processor(ensure_timestamp)
test_data = [
{"type": "dialogue", "content": "你好世界"},
{"type": "text", "content": "这是一段很长的文本内容"},
]
results = processor.process_tokens(test_data)
for result in results:
print(f" {result.get('type')}: "
f"Character count={result.get('char_count')} "
f"Timestamp={result.get('timestamp', 'N/A')[:19]}")
print()
def example_6_priority_and_order():
engine = RuleEngine()
engine.add_rule_dict(
name="low_priority",
condition={"type": "test"},
action={"type": "set_field", "field": "processed_by", "value": "low"},
priority=10
)
engine.add_rule_dict(
name="high_priority",
condition={"type": "test"},
action={"type": "set_field", "field": "processed_by", "value": "high"},
priority=100
)
engine.add_rule_dict(
name="medium_priority",
condition={"type": "test"},
action={"type": "set_field", "field": "processed_by", "value": "medium"},
priority=50
)
data = {"type": "test"}
result1 = engine.process(data, apply_all=False)
print(f"Only apply the highest priority matching rule: {result1}")
result2 = engine.process(data, apply_all=True)
print(f"Apply all matching rules: {result2}")
print()
def main():
example_1_simple_rules()
example_2_conditional_rules()
example_3_field_transformation()
example_4_processor_with_rules()
example_5_custom_processor()
example_6_priority_and_order()
if __name__ == "__main__":
main()
|