@diagrammo/dgmo 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,178 @@
1
+ // ============================================================
2
+ // Participant Type Inference Engine
3
+ // ============================================================
4
+ //
5
+ // Data-driven rules table that infers participant type from name.
6
+ // First match wins. Infrastructure overrides come before suffix
7
+ // rules to prevent false positives (e.g. "Router" → networking,
8
+ // not actor despite the "-er" suffix).
9
+ // ============================================================
10
+
11
+ import type { ParticipantType } from './parser';
12
+
13
+ /**
14
+ * A single inference rule: regex pattern → participant type.
15
+ */
16
+ interface InferenceRule {
17
+ pattern: RegExp;
18
+ type: ParticipantType;
19
+ }
20
+
21
+ /**
22
+ * Ordered rules table. First match wins.
23
+ *
24
+ * Priority order:
25
+ * 1. Infrastructure overrides (prevent false actor matches)
26
+ * 2. Networking patterns
27
+ * 3. Database patterns
28
+ * 4. Cache patterns
29
+ * 5. Queue/Messaging patterns
30
+ * 6. Actor patterns (suffix + exact)
31
+ * 7. Frontend patterns
32
+ * 8. Service patterns
33
+ * 9. External patterns
34
+ */
35
+ const PARTICIPANT_RULES: readonly InferenceRule[] = [
36
+ // ── 1. Infrastructure overrides ─────────────────────────
37
+ // These names end in -er/-or but are NOT actors
38
+ { pattern: /^.*Router$/i, type: 'networking' },
39
+ { pattern: /^.*Scheduler$/i, type: 'service' },
40
+ { pattern: /^.*Dispatcher$/i, type: 'service' },
41
+ { pattern: /^.*Balancer$/i, type: 'networking' },
42
+ { pattern: /^.*Controller$/i, type: 'service' },
43
+ { pattern: /^.*Handler$/i, type: 'service' },
44
+ { pattern: /^.*Processor$/i, type: 'service' },
45
+ { pattern: /^.*Connector$/i, type: 'service' },
46
+ { pattern: /^.*Adapter$/i, type: 'service' },
47
+ { pattern: /^.*Provider$/i, type: 'service' },
48
+ { pattern: /^.*Manager$/i, type: 'service' },
49
+ { pattern: /^.*Orchestrator$/i, type: 'service' },
50
+ { pattern: /^.*Monitor$/i, type: 'service' },
51
+ { pattern: /^.*Resolver$/i, type: 'service' },
52
+ { pattern: /^.*Logger$/i, type: 'service' },
53
+ { pattern: /^.*Server$/i, type: 'service' },
54
+ { pattern: /^.*Broker$/i, type: 'queue' },
55
+ { pattern: /^.*Worker$/i, type: 'service' },
56
+ { pattern: /^.*Consumer$/i, type: 'service' },
57
+ { pattern: /^.*Producer$/i, type: 'service' },
58
+ { pattern: /^.*Publisher$/i, type: 'service' },
59
+ { pattern: /^.*Subscriber$/i, type: 'service' },
60
+ { pattern: /^.*Listener$/i, type: 'service' },
61
+
62
+ // ── 2. Networking patterns ──────────────────────────────
63
+ { pattern: /Gateway/i, type: 'networking' },
64
+ { pattern: /GW$/i, type: 'networking' },
65
+ { pattern: /Proxy/i, type: 'networking' },
66
+ { pattern: /LB$/i, type: 'networking' },
67
+ { pattern: /LoadBalancer/i, type: 'networking' },
68
+ { pattern: /CDN/i, type: 'networking' },
69
+ { pattern: /Firewall/i, type: 'networking' },
70
+ { pattern: /WAF$/i, type: 'networking' },
71
+ { pattern: /DNS/i, type: 'networking' },
72
+ { pattern: /Ingress/i, type: 'networking' },
73
+
74
+ // ── 3. Database patterns ────────────────────────────────
75
+ { pattern: /DB$/i, type: 'database' },
76
+ { pattern: /Database/i, type: 'database' },
77
+ { pattern: /Datastore/i, type: 'database' },
78
+ { pattern: /Store$/i, type: 'database' },
79
+ { pattern: /Storage/i, type: 'database' },
80
+ { pattern: /Repo$/i, type: 'database' },
81
+ { pattern: /Repository/i, type: 'database' },
82
+ { pattern: /SQL/i, type: 'database' },
83
+ { pattern: /Postgres/i, type: 'database' },
84
+ { pattern: /MySQL/i, type: 'database' },
85
+ { pattern: /Mongo/i, type: 'database' },
86
+ { pattern: /Dynamo/i, type: 'database' },
87
+
88
+ // ── 4. Cache patterns ──────────────────────────────────
89
+ { pattern: /Cache/i, type: 'cache' },
90
+ { pattern: /Redis/i, type: 'cache' },
91
+ { pattern: /Memcache/i, type: 'cache' },
92
+ // CDN already matched by networking above
93
+
94
+ // ── 5. Queue/Messaging patterns ─────────────────────────
95
+ { pattern: /Queue/i, type: 'queue' },
96
+ { pattern: /MQ$/i, type: 'queue' },
97
+ { pattern: /SQS/i, type: 'queue' },
98
+ { pattern: /Kafka/i, type: 'queue' },
99
+ { pattern: /RabbitMQ/i, type: 'queue' },
100
+ { pattern: /EventBus/i, type: 'queue' },
101
+ { pattern: /MessageBus/i, type: 'queue' },
102
+ { pattern: /Bus$/i, type: 'queue' },
103
+ { pattern: /Topic/i, type: 'queue' },
104
+ { pattern: /Stream$/i, type: 'queue' },
105
+ { pattern: /SNS/i, type: 'queue' },
106
+ { pattern: /PubSub/i, type: 'queue' },
107
+
108
+ // ── 6. Actor patterns ──────────────────────────────────
109
+ // Exact matches first
110
+ { pattern: /^Admin$/i, type: 'actor' },
111
+ { pattern: /^User$/i, type: 'actor' },
112
+ { pattern: /^Customer$/i, type: 'actor' },
113
+ { pattern: /^Client$/i, type: 'actor' },
114
+ { pattern: /^Agent$/i, type: 'actor' },
115
+ { pattern: /^Person$/i, type: 'actor' },
116
+ { pattern: /^Buyer$/i, type: 'actor' },
117
+ { pattern: /^Seller$/i, type: 'actor' },
118
+ { pattern: /^Guest$/i, type: 'actor' },
119
+ { pattern: /^Visitor$/i, type: 'actor' },
120
+ { pattern: /^Operator$/i, type: 'actor' },
121
+ { pattern: /^Alice$/i, type: 'actor' },
122
+ { pattern: /^Bob$/i, type: 'actor' },
123
+ { pattern: /^Charlie$/i, type: 'actor' },
124
+ // Suffix rules (after infrastructure overrides filtered above)
125
+ { pattern: /User$/i, type: 'actor' },
126
+ { pattern: /Actor$/i, type: 'actor' },
127
+ { pattern: /Analyst$/i, type: 'actor' },
128
+
129
+ // ── 7. Frontend patterns ────────────────────────────────
130
+ { pattern: /App$/i, type: 'frontend' },
131
+ { pattern: /Application/i, type: 'frontend' },
132
+ { pattern: /Mobile/i, type: 'frontend' },
133
+ { pattern: /iOS/i, type: 'frontend' },
134
+ { pattern: /Android/i, type: 'frontend' },
135
+ { pattern: /Web/i, type: 'frontend' },
136
+ { pattern: /Browser/i, type: 'frontend' },
137
+ { pattern: /Frontend/i, type: 'frontend' },
138
+ { pattern: /UI$/i, type: 'frontend' },
139
+ { pattern: /Dashboard/i, type: 'frontend' },
140
+ { pattern: /CLI$/i, type: 'frontend' },
141
+ { pattern: /Terminal/i, type: 'frontend' },
142
+
143
+ // ── 8. Service patterns ─────────────────────────────────
144
+ { pattern: /Service/i, type: 'service' },
145
+ { pattern: /Svc$/i, type: 'service' },
146
+ { pattern: /API$/i, type: 'service' },
147
+ { pattern: /Lambda/i, type: 'service' },
148
+ { pattern: /Function$/i, type: 'service' },
149
+ { pattern: /Fn$/i, type: 'service' },
150
+ { pattern: /Job$/i, type: 'service' },
151
+ { pattern: /Cron/i, type: 'service' },
152
+ { pattern: /Microservice/i, type: 'service' },
153
+
154
+ // ── 9. External patterns ────────────────────────────────
155
+ { pattern: /External/i, type: 'external' },
156
+ { pattern: /Ext$/i, type: 'external' },
157
+ { pattern: /ThirdParty/i, type: 'external' },
158
+ { pattern: /3P$/i, type: 'external' },
159
+ { pattern: /Vendor/i, type: 'external' },
160
+ ];
161
+
162
+ /**
163
+ * Infer participant type from a name using the ordered rules table.
164
+ * Returns 'default' if no rule matches.
165
+ */
166
+ export function inferParticipantType(name: string): ParticipantType {
167
+ for (const rule of PARTICIPANT_RULES) {
168
+ if (rule.pattern.test(name)) {
169
+ return rule.type;
170
+ }
171
+ }
172
+ return 'default';
173
+ }
174
+
175
+ /**
176
+ * Number of rules in the table. Exported for test assertions.
177
+ */
178
+ export const RULE_COUNT = PARTICIPANT_RULES.length;