-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_ontology.py
More file actions
446 lines (419 loc) · 16 KB
/
java_ontology.py
File metadata and controls
446 lines (419 loc) · 16 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
"""Shared valid role and capability label sets for Java indexers and MCP.
Used by `ast_java` inference, brownfield config validation in `graph_enrich`,
and resolver steps for `@CodebaseRole` / `@CodebaseCapability`."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Literal
from ast_java import (
ROLE_ANNOTATIONS,
_INJECTED_TYPES_TO_CAPABILITY,
_METHOD_ANN_TO_CAPABILITY,
_SUPERTYPE_TO_CAPABILITY,
_TYPE_ANN_TO_CAPABILITY,
)
# Roles: Spring stereotype values plus DTO from `infer_role_for_type`.
VALID_ROLES: frozenset[str] = frozenset((*ROLE_ANNOTATIONS.values(), "DTO"))
VALID_CAPABILITIES: frozenset[str] = frozenset(
{
*_METHOD_ANN_TO_CAPABILITY.values(),
*_TYPE_ANN_TO_CAPABILITY.values(),
*_INJECTED_TYPES_TO_CAPABILITY.values(),
*_SUPERTYPE_TO_CAPABILITY.values(),
}
)
VALID_ROUTE_FRAMEWORKS: frozenset[str] = frozenset((
"spring_mvc",
"webflux",
))
VALID_ROUTE_KINDS: frozenset[str] = frozenset((
"http_endpoint",
"http_consumer",
"kafka_topic",
"rabbit_queue",
"jms_destination",
"stream_binding",
))
VALID_CLIENT_KINDS: frozenset[str] = frozenset((
"feign_method",
"rest_template",
"web_client",
))
VALID_PRODUCER_KINDS: frozenset[str] = frozenset((
"kafka_send",
"stream_bridge_send",
))
VALID_HTTP_CALL_STRATEGIES: frozenset[str] = frozenset((
"feign_method",
"rest_template",
"web_client",
"unresolved",
))
VALID_ASYNC_CALL_STRATEGIES: frozenset[str] = frozenset((
"kafka_template",
"stream_bridge",
"rabbit_template",
"jms_template",
"unresolved",
))
VALID_HTTP_CALL_MATCHES: frozenset[str] = frozenset((
"cross_service",
"intra_service",
"ambiguous",
"phantom",
"unresolved",
))
VALID_RESOLVE_REASONS: frozenset[str] = frozenset((
"exact_id",
"exact_fqn",
"fqn_suffix",
"short_name",
"route_template",
"route_method_path",
"client_target",
"client_target_path",
"producer_topic",
"producer_topic_prefix",
))
# Brownfield / fallback edge resolution strategies (hints v2 neighbors fuzzy signal).
# ``phantom`` / ``chained_receiver`` are not CALLS edge strategies after PR-3 (receiver
# failures live on ``UnresolvedCallSite``); they remain on HTTP/ASYNC match literals only.
FUZZY_STRATEGY_SET: frozenset[str] = frozenset({
"layer_c_source",
"layer_b_fqn",
"overload_ambiguous",
"implicit_super",
})
VALID_UNRESOLVED_CALL_REASONS: frozenset[str] = frozenset({
"phantom_unresolved_receiver",
"chained_receiver",
})
# Union of fuzzy + non-fuzzy resolver strategies that may appear on graph edges
# carrying a `strategy` column (brownfield layers, codebase stubs, call-graph tiers,
# HTTP/async dispatch literals). Used by `EdgeSpec.brownfield_resolver_sourced`.
BROWNFIELD_RESOLVER_STRATEGY_SET: frozenset[str] = frozenset({
*FUZZY_STRATEGY_SET,
# Receiver-tier / HTTP match literals — not CALLS edge strategies after PR-3 UCS facet.
"phantom",
"chained_receiver",
"layer_b_ann",
"layer_a_meta",
"codebase_route",
"codebase_client",
"codebase_producer",
"annotation",
"spel",
"constant_ref",
*VALID_HTTP_CALL_STRATEGIES,
*VALID_ASYNC_CALL_STRATEGIES,
*VALID_CLIENT_KINDS,
*VALID_PRODUCER_KINDS,
"import_map",
"static_import",
"static_import_wildcard",
"constructor",
"method_reference",
"this_super",
"unique_type_name",
"suffix",
"same_module",
})
NodeKind = Literal["Symbol", "Route", "Client", "Producer"]
Cardinality = Literal["many_to_many", "many_to_one", "one_to_many", "one_to_one"]
@dataclass(frozen=True)
class EdgeAttr:
name: str
kuzu_type: str
purpose: str
@dataclass(frozen=True)
class EdgeSpec:
name: str
src: NodeKind
dst: NodeKind
cardinality: Cardinality
brownfield_resolver_sourced: bool
attrs: tuple[EdgeAttr, ...]
purpose: str
typical_traversals: dict[str, str]
member_only: bool = False
_SYMBOL_TYPE_TRAVERSAL = (
"neighbors(['{id}'],'out',['DECLARES']) "
"then neighbors(member_ids,'{direction}',['{edge}'])"
)
_COMPOSED_MEMBER_TYPE_TRAVERSAL = (
"neighbors(['{id}'],'out',['DECLARES.{edge}']) — or "
"neighbors(['{id}'],'out',['DECLARES']) then neighbors(member_ids,'{direction}',['{edge}'])"
)
EDGE_SCHEMA: dict[str, EdgeSpec] = {
"EXTENDS": EdgeSpec(
name="EXTENDS",
src="Symbol",
dst="Symbol",
cardinality="many_to_one",
brownfield_resolver_sourced=False,
attrs=(
EdgeAttr("dst_name", "STRING", "raw supertype name as written in source"),
EdgeAttr("dst_fqn", "STRING", "best-effort resolved FQN of the supertype"),
EdgeAttr("resolved", "BOOLEAN", "True iff dst_fqn was resolved to an in-graph Symbol"),
),
purpose="class or interface direct supertype relation",
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="EXTENDS"),
"member_subject": "neighbors(['{id}'],'out',['EXTENDS'])",
"alien_subject": "EXTENDS connects Symbol → Symbol; use a type or member Symbol id",
},
),
"IMPLEMENTS": EdgeSpec(
name="IMPLEMENTS",
src="Symbol",
dst="Symbol",
cardinality="many_to_many",
brownfield_resolver_sourced=False,
attrs=(
EdgeAttr("dst_name", "STRING", "raw interface name as written in source"),
EdgeAttr("dst_fqn", "STRING", "best-effort resolved FQN of the interface"),
EdgeAttr("resolved", "BOOLEAN", "True iff dst_fqn was resolved to an in-graph Symbol"),
),
purpose="class implements interface relation",
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="IMPLEMENTS"),
"member_subject": "neighbors(['{id}'],'out',['IMPLEMENTS'])",
"alien_subject": "IMPLEMENTS connects Symbol → Symbol; use a type or member Symbol id",
},
),
"INJECTS": EdgeSpec(
name="INJECTS",
src="Symbol",
dst="Symbol",
cardinality="many_to_many",
brownfield_resolver_sourced=False,
attrs=(
EdgeAttr("dst_name", "STRING", "raw injected type name as written in source"),
EdgeAttr("dst_fqn", "STRING", "best-effort resolved FQN of the injected type"),
EdgeAttr("resolved", "BOOLEAN", "True iff dst_fqn was resolved to an in-graph Symbol"),
EdgeAttr("mechanism", "STRING", "injection mechanism literal (constructor, field, setter, …)"),
EdgeAttr("annotation", "STRING", "injection annotation simple name when present"),
EdgeAttr("field_or_param", "STRING", "field or parameter name for the injection site"),
),
purpose="dependency injection edge from declaring type to injected type",
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="INJECTS"),
"member_subject": "neighbors(['{id}'],'in',['INJECTS'])",
"alien_subject": "INJECTS connects Symbol → Symbol; use a type Symbol id",
},
),
"DECLARES": EdgeSpec(
name="DECLARES",
src="Symbol",
dst="Symbol",
cardinality="one_to_many",
brownfield_resolver_sourced=False,
attrs=(),
purpose="type declares member Symbol (method, constructor, nested type)",
typical_traversals={
"type_subject": "neighbors(['{id}'],'out',['DECLARES'])",
"member_subject": "neighbors(['{id}'],'in',['DECLARES'])",
"alien_subject": "DECLARES connects Symbol → Symbol; use a type Symbol id for outbound members",
},
),
"OVERRIDES": EdgeSpec(
name="OVERRIDES",
src="Symbol",
dst="Symbol",
cardinality="many_to_one",
brownfield_resolver_sourced=False,
attrs=(),
purpose="subtype method overrides supertype declared method with matching signature",
member_only=True,
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="OVERRIDES"),
"member_subject": "neighbors(['{id}'],'out',['OVERRIDES'])",
"alien_subject": "OVERRIDES connects method Symbol → method Symbol",
},
),
"CALLS": EdgeSpec(
name="CALLS",
src="Symbol",
dst="Symbol",
cardinality="many_to_many",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("call_site_line", "INT64", "source line of the call site"),
EdgeAttr("call_site_byte", "INT64", "source byte offset of the call site"),
EdgeAttr("arg_count", "INT64", "argument count at the call site (-1 for method references)"),
EdgeAttr("confidence", "DOUBLE", "resolver confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "call-graph resolution strategy literal"),
EdgeAttr("source", "STRING", "call-graph source tag"),
EdgeAttr("resolved", "BOOLEAN", "True iff callee Symbol was resolved in-graph"),
EdgeAttr(
"callee_declaring_role",
"STRING",
"role of the Symbol that declares the callee method",
),
),
purpose="intra-codebase method call from caller method to callee method",
member_only=True,
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="CALLS"),
"member_subject": "neighbors(['{id}'],'out',['CALLS'])",
"alien_subject": "CALLS connects method Symbol → method Symbol",
},
),
"EXPOSES": EdgeSpec(
name="EXPOSES",
src="Symbol",
dst="Route",
cardinality="one_to_one",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("confidence", "DOUBLE", "route extraction confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "route resolution strategy literal"),
),
purpose="declaring method exposes an inbound HTTP or messaging Route",
member_only=True,
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(id="{id}", direction="{direction}", edge="EXPOSES"),
"member_subject": "neighbors(['{id}'],'out',['EXPOSES'])",
"alien_subject": "EXPOSES connects method Symbol → Route; use a method Symbol id",
},
),
"DECLARES_CLIENT": EdgeSpec(
name="DECLARES_CLIENT",
src="Symbol",
dst="Client",
cardinality="one_to_many",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("confidence", "DOUBLE", "client declaration confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "client resolution strategy literal"),
),
purpose="method declares an outbound HTTP client call site",
member_only=True,
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(
id="{id}", direction="{direction}", edge="DECLARES_CLIENT",
),
"member_subject": "neighbors(['{id}'],'out',['DECLARES_CLIENT'])",
"alien_subject": "DECLARES_CLIENT connects method Symbol → Client",
},
),
"DECLARES_PRODUCER": EdgeSpec(
name="DECLARES_PRODUCER",
src="Symbol",
dst="Producer",
cardinality="one_to_many",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("confidence", "DOUBLE", "producer declaration confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "producer resolution strategy literal"),
),
purpose="method declares an outbound async producer call site",
member_only=True,
typical_traversals={
"type_subject": _SYMBOL_TYPE_TRAVERSAL.format(
id="{id}", direction="{direction}", edge="DECLARES_PRODUCER",
),
"member_subject": "neighbors(['{id}'],'out',['DECLARES_PRODUCER'])",
"alien_subject": "DECLARES_PRODUCER connects method Symbol → Producer",
},
),
"HTTP_CALLS": EdgeSpec(
name="HTTP_CALLS",
src="Client",
dst="Route",
cardinality="many_to_many",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("confidence", "DOUBLE", "pass6 match confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "HTTP call resolution strategy literal"),
EdgeAttr("method_call", "STRING", "HTTP method of the call site"),
EdgeAttr("raw_uri", "STRING", "uninterpolated URI template from the call site"),
EdgeAttr("match", "STRING", "cross_service|intra_service|ambiguous|phantom|unresolved"),
),
purpose="resolved HTTP call from a declared Client to a target route",
typical_traversals={
"type_subject": (
"neighbors(['{id}'],'out',['DECLARES']) "
"then neighbors(member_ids,'out',['DECLARES_CLIENT']) "
"then neighbors(client_ids,'out',['HTTP_CALLS'])"
),
"member_subject": (
"neighbors(['{id}'],'out',['DECLARES_CLIENT']) "
"then neighbors(client_ids,'out',['HTTP_CALLS'])"
),
"route_subject": (
"neighbors(['{id}'],'in',['HTTP_CALLS']) "
"then neighbors(client_ids,'in',['DECLARES_CLIENT']) for declaring method"
),
"alien_subject": (
"HTTP_CALLS connects Client→Route; use DECLARES_CLIENT from a method Symbol, "
"or neighbors(client_id,'out',['HTTP_CALLS']) from a Client id"
),
},
),
"ASYNC_CALLS": EdgeSpec(
name="ASYNC_CALLS",
src="Producer",
dst="Route",
cardinality="many_to_many",
brownfield_resolver_sourced=True,
attrs=(
EdgeAttr("confidence", "DOUBLE", "pass6 match confidence in [0.0, 1.0]"),
EdgeAttr("strategy", "STRING", "async call resolution strategy literal"),
EdgeAttr("direction", "STRING", "produce|consume async direction literal"),
EdgeAttr("raw_topic", "STRING", "uninterpolated topic template from the call site"),
EdgeAttr("match", "STRING", "cross_service|intra_service|ambiguous|phantom|unresolved"),
),
purpose="resolved async call from a declared Producer to a topic route",
typical_traversals={
"type_subject": (
"neighbors(['{id}'],'out',['DECLARES']) "
"then neighbors(member_ids,'out',['DECLARES_PRODUCER']) "
"then neighbors(producer_ids,'out',['ASYNC_CALLS'])"
),
"member_subject": (
"neighbors(['{id}'],'out',['DECLARES_PRODUCER']) "
"then neighbors(producer_ids,'out',['ASYNC_CALLS'])"
),
"route_subject": (
"neighbors(['{id}'],'in',['ASYNC_CALLS']) "
"then neighbors(producer_ids,'in',['DECLARES_PRODUCER']) for declaring method"
),
"alien_subject": (
"ASYNC_CALLS connects Producer→Route; use DECLARES_PRODUCER from a method Symbol, "
"or neighbors(producer_id,'out',['ASYNC_CALLS']) from a Producer id"
),
},
),
}
ResolveReason = Literal[
"exact_id",
"exact_fqn",
"fqn_suffix",
"short_name",
"route_template",
"route_method_path",
"client_target",
"client_target_path",
"producer_topic",
"producer_topic_prefix",
]
__all__ = [
"VALID_ROLES",
"VALID_CAPABILITIES",
"VALID_ROUTE_FRAMEWORKS",
"VALID_ROUTE_KINDS",
"VALID_CLIENT_KINDS",
"VALID_PRODUCER_KINDS",
"VALID_HTTP_CALL_STRATEGIES",
"VALID_ASYNC_CALL_STRATEGIES",
"VALID_HTTP_CALL_MATCHES",
"VALID_RESOLVE_REASONS",
"FUZZY_STRATEGY_SET",
"BROWNFIELD_RESOLVER_STRATEGY_SET",
"NodeKind",
"Cardinality",
"EdgeAttr",
"EdgeSpec",
"EDGE_SCHEMA",
"ResolveReason",
]