json-model-compiler 2.0b0__py3-none-any.whl
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.
- json_model/README.md +32 -0
- json_model/__init__.py +18 -0
- json_model/analyze.py +409 -0
- json_model/clang.py +563 -0
- json_model/convert.py +430 -0
- json_model/data/clang_entry.c +21 -0
- json_model/data/clang_free.c +10 -0
- json_model/data/clang_init.c +11 -0
- json_model/data/clang_pcre2_fun.c +6 -0
- json_model/data/clang_re2_fun.c +5 -0
- json_model/data/javascript_exe.js +4 -0
- json_model/data/javascript_foot.js +9 -0
- json_model/data/javascript_free.js +9 -0
- json_model/data/javascript_head.js +1 -0
- json_model/data/javascript_init.js +11 -0
- json_model/data/javascript_main.js +5 -0
- json_model/data/plpgsql_entry.sql +16 -0
- json_model/data/python_entry.py +11 -0
- json_model/data/python_exe.py +1 -0
- json_model/data/python_free.py +6 -0
- json_model/data/python_init.py +9 -0
- json_model/data/python_main.py +4 -0
- json_model/javascript.py +310 -0
- json_model/language.py +748 -0
- json_model/model.py +1084 -0
- json_model/mtypes.py +42 -0
- json_model/objmerge.py +165 -0
- json_model/optim.py +486 -0
- json_model/plpgsql.py +422 -0
- json_model/python.py +269 -0
- json_model/recurse.py +127 -0
- json_model/resolver.py +106 -0
- json_model/runtime/META.json +30 -0
- json_model/runtime/Makefile +21 -0
- json_model/runtime/__init__.py +32 -0
- json_model/runtime/json-model.c +1054 -0
- json_model/runtime/json-model.h +183 -0
- json_model/runtime/json_model--2.0.sql +261 -0
- json_model/runtime/json_model.control +2 -0
- json_model/runtime/main.c +473 -0
- json_model/runtime/support.py +485 -0
- json_model/runtime/types.py +16 -0
- json_model/script.py +585 -0
- json_model/url_cache.py +82 -0
- json_model/utils.py +623 -0
- json_model/xstatic.py +1444 -0
- json_model_compiler-2.0b0.dist-info/METADATA +201 -0
- json_model_compiler-2.0b0.dist-info/RECORD +51 -0
- json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
- json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
- json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
json_model/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# JSON Model Compiler
|
|
2
|
+
|
|
3
|
+
## CLOC
|
|
4
|
+
|
|
5
|
+
- Compiler: 5400 py-locs, including 850 for the 3 code generators (C, Python and JavaScript)
|
|
6
|
+
- C runtime: 1250 c-locs
|
|
7
|
+
- Python runtime: 375 py-locs
|
|
8
|
+
- JS runtime: 350 js-locs
|
|
9
|
+
|
|
10
|
+
## Files
|
|
11
|
+
|
|
12
|
+
- [model](./model.py) model representation and preprocessing
|
|
13
|
+
- [xstatic](./xstatic.py) model compilation
|
|
14
|
+
- [language](./language.py) code generator abstraction
|
|
15
|
+
- [python](./python.py) Python code generator
|
|
16
|
+
- [clang](./clang.py) C code generator
|
|
17
|
+
- [javascript](./javascript.py) JS code generator
|
|
18
|
+
- [resolver](./resolver.py)
|
|
19
|
+
[url_cache](./url_cache.py) URL management
|
|
20
|
+
- [script](./script.py) frontend
|
|
21
|
+
- [convert](./convert.py) conversion to JSON Schema
|
|
22
|
+
- [utils](./utils.py)
|
|
23
|
+
[analyze](./analyze.py)
|
|
24
|
+
[mtypes](./mtypes.py)
|
|
25
|
+
[objmerge](./objmerge.py)
|
|
26
|
+
[optim](./optim.py)
|
|
27
|
+
[recurse](./recurse.py)
|
|
28
|
+
various utilities
|
|
29
|
+
- [data/](./data/) partial source code helpers for code generation
|
|
30
|
+
- [runtime/](./runtime/) C and Python runtimes
|
|
31
|
+
- [../js_runtime/](../js_runtime/) JS runtime as a npm module
|
|
32
|
+
|
json_model/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#
|
|
2
|
+
# convenient types, classes and functions to process models
|
|
3
|
+
#
|
|
4
|
+
from .mtypes import Jsonable
|
|
5
|
+
from .runtime.types import Report, EntryCheckFun
|
|
6
|
+
from .model import JsonModel
|
|
7
|
+
from .script import (
|
|
8
|
+
jmc_script,
|
|
9
|
+
model_from_json, model_from_url, model_from_str,
|
|
10
|
+
model_checker, model_checker_from_url, model_checker_from_json,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__ALL__ = [
|
|
14
|
+
Jsonable, Report, EntryCheckFun, JsonModel,
|
|
15
|
+
jmc_script,
|
|
16
|
+
model_from_json, model_from_url, model_from_str,
|
|
17
|
+
model_checker, model_checker_from_url, model_checker_from_json,
|
|
18
|
+
]
|
json_model/analyze.py
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Analyses
|
|
3
|
+
#
|
|
4
|
+
import re
|
|
5
|
+
from .mtypes import UnknownModel, ModelPath, ModelType, ModelFilter
|
|
6
|
+
from .utils import log, CONST_RE, is_regex
|
|
7
|
+
from .recurse import recModel, allFlt, noRwt
|
|
8
|
+
from .model import JsonModel
|
|
9
|
+
|
|
10
|
+
# JsonModel = typing.NewType("JsonModel", None)
|
|
11
|
+
type Constants = bool|int|float|str
|
|
12
|
+
type ModelRef = list[str]
|
|
13
|
+
|
|
14
|
+
def references(jm: JsonModel) -> dict[str, set[int]]:
|
|
15
|
+
"""Get direct references between models.
|
|
16
|
+
|
|
17
|
+
local name in model -> set of model ids that are referenced
|
|
18
|
+
"""
|
|
19
|
+
assert jm._is_root
|
|
20
|
+
|
|
21
|
+
def directRefs(model: ModelType) -> set[int]:
|
|
22
|
+
"""Recursive collect references."""
|
|
23
|
+
|
|
24
|
+
refs: set[int] = set()
|
|
25
|
+
|
|
26
|
+
def drRwt(m: ModelType, p: ModelPath) -> ModelType:
|
|
27
|
+
if isinstance(m, str) and jm._isRef(m):
|
|
28
|
+
target = jm.resolveRef(m, p)
|
|
29
|
+
refs.add(target._id)
|
|
30
|
+
return m
|
|
31
|
+
|
|
32
|
+
recModel(model, allFlt, drRwt, True)
|
|
33
|
+
|
|
34
|
+
return refs
|
|
35
|
+
|
|
36
|
+
contains = {}
|
|
37
|
+
|
|
38
|
+
for name, model in jm._defs.items():
|
|
39
|
+
contains[name] = directRefs(model._model)
|
|
40
|
+
|
|
41
|
+
contains[""] = directRefs(jm._model)
|
|
42
|
+
|
|
43
|
+
return contains
|
|
44
|
+
|
|
45
|
+
# def reachable(self):
|
|
46
|
+
# """Build reachable defs."""
|
|
47
|
+
#
|
|
48
|
+
# # should consider **all** loaded models!?
|
|
49
|
+
# contains = self.references()
|
|
50
|
+
# reached = copy.deepcopy(contains)
|
|
51
|
+
#
|
|
52
|
+
# # simplistic transitive closure computation
|
|
53
|
+
# changed = True
|
|
54
|
+
# while changed:
|
|
55
|
+
# changed = False
|
|
56
|
+
# for ref, refs in reached.items():
|
|
57
|
+
# for r in refs:
|
|
58
|
+
# if r in reached and not reached[r].issubset(refs):
|
|
59
|
+
# changed = True
|
|
60
|
+
# refs.update(reached[r])
|
|
61
|
+
#
|
|
62
|
+
# return reached
|
|
63
|
+
|
|
64
|
+
def check(jm: JsonModel, assertion: ModelFilter, what: str = "?", short: bool = False) -> bool:
|
|
65
|
+
"""Check an arbitrary local assertion on a model."""
|
|
66
|
+
|
|
67
|
+
okay = True
|
|
68
|
+
|
|
69
|
+
def checkFlt(model: ModelType, path: ModelPath) -> bool:
|
|
70
|
+
nonlocal okay
|
|
71
|
+
if not assertion(model, path):
|
|
72
|
+
log.debug(f"{jm._id}: check {what} failed at {path}")
|
|
73
|
+
okay = False
|
|
74
|
+
return okay if short else True
|
|
75
|
+
|
|
76
|
+
recModel(jm._model, checkFlt, noRwt, True)
|
|
77
|
+
|
|
78
|
+
return okay
|
|
79
|
+
|
|
80
|
+
# FIXME must tell why it is unhappy!
|
|
81
|
+
def valid(jm: JsonModel, path: ModelPath = [], root: bool = True) -> bool:
|
|
82
|
+
"""Check JSON Model structural (and slightly more) validity."""
|
|
83
|
+
|
|
84
|
+
is_valid = True
|
|
85
|
+
|
|
86
|
+
def finiteRef(model: str) -> bool:
|
|
87
|
+
ref, recref, recid, ljm = model, [], [], jm
|
|
88
|
+
while isinstance(ref, str) and jm._isRef(ref) and ref not in recref:
|
|
89
|
+
ljm = ljm.resolveRef(ref, path + recref)
|
|
90
|
+
recref.append(ref)
|
|
91
|
+
recid.append(ljm._id)
|
|
92
|
+
ref = ljm._model
|
|
93
|
+
finite = not jm._isRef(ref) or ref == "$#" # Hmmm…
|
|
94
|
+
if not finite:
|
|
95
|
+
log.debug(f"infinite recursion on {model}: {recref} ({recid})")
|
|
96
|
+
return finite
|
|
97
|
+
|
|
98
|
+
def validFlt(model: ModelType, path: ModelPath) -> bool:
|
|
99
|
+
nonlocal is_valid
|
|
100
|
+
match model:
|
|
101
|
+
case None:
|
|
102
|
+
pass
|
|
103
|
+
case bool():
|
|
104
|
+
is_valid &= model
|
|
105
|
+
case int():
|
|
106
|
+
is_valid &= model in (-1, 0, 1)
|
|
107
|
+
case float():
|
|
108
|
+
is_valid &= model in (-1.0, 0.0, 1.0)
|
|
109
|
+
case str():
|
|
110
|
+
if model == "":
|
|
111
|
+
pass
|
|
112
|
+
elif model[0] == "=":
|
|
113
|
+
is_valid &= re.match(CONST_RE, model) is not None
|
|
114
|
+
elif model[0] == "/":
|
|
115
|
+
try:
|
|
116
|
+
pattern, ropts = model[1:].rsplit("/", 1)
|
|
117
|
+
is_valid &= (ropts == "" or ropts.isalpha()) and is_regex(pattern)
|
|
118
|
+
except Exception:
|
|
119
|
+
is_valid = False
|
|
120
|
+
elif model[0] == "$":
|
|
121
|
+
pass
|
|
122
|
+
else: # TODO more checks
|
|
123
|
+
pass
|
|
124
|
+
case list():
|
|
125
|
+
pass
|
|
126
|
+
case dict():
|
|
127
|
+
if "#" in model:
|
|
128
|
+
is_valid &= isinstance(model["#"], str)
|
|
129
|
+
if "@" in model:
|
|
130
|
+
if "!" in model:
|
|
131
|
+
is_valid &= isinstance(model["!"], bool)
|
|
132
|
+
for op in ("=", "!=", "<", "<=", ">", ">="):
|
|
133
|
+
if op in model:
|
|
134
|
+
is_valid &= isinstance(model[op], (int, float, str))
|
|
135
|
+
# TODO reject any other props! TODO allow .mo and other extensions?
|
|
136
|
+
for k in model.keys():
|
|
137
|
+
is_valid &= \
|
|
138
|
+
k in ("@", "!", "=", "!=", "<", "<=", ">=", ">", "#", "$", "%", "~")
|
|
139
|
+
elif "|" in model:
|
|
140
|
+
is_valid &= isinstance(model["|"], list)
|
|
141
|
+
# no other keys in recurse
|
|
142
|
+
elif "^" in model:
|
|
143
|
+
is_valid &= isinstance(model["^"], list)
|
|
144
|
+
# no other keys in recurse
|
|
145
|
+
elif "&" in model:
|
|
146
|
+
is_valid &= isinstance(model["&"], list)
|
|
147
|
+
# no other keys in recurse
|
|
148
|
+
elif "+" in model:
|
|
149
|
+
is_valid &= isinstance(model["+"], list)
|
|
150
|
+
# no other keys in recurse
|
|
151
|
+
else: # check object
|
|
152
|
+
props = set() # property name collisions
|
|
153
|
+
for p, m in model.items():
|
|
154
|
+
if p == "#":
|
|
155
|
+
continue
|
|
156
|
+
is_valid &= isinstance(p, str)
|
|
157
|
+
if p and p[0] not in ("$", "/"):
|
|
158
|
+
name = p[1:] if p[0] in ("?", "_", "!") else p
|
|
159
|
+
is_valid &= name not in props
|
|
160
|
+
props.add(name)
|
|
161
|
+
# more checks on p if p[0] == "$"
|
|
162
|
+
case _:
|
|
163
|
+
is_valid = False
|
|
164
|
+
return True
|
|
165
|
+
|
|
166
|
+
if is_valid and jm._defs:
|
|
167
|
+
for name, jmr in jm._defs.items():
|
|
168
|
+
is_valid &= finiteRef("$" + name)
|
|
169
|
+
|
|
170
|
+
if is_valid:
|
|
171
|
+
try:
|
|
172
|
+
check(jm, validFlt, "JSON Model Structural Validity")
|
|
173
|
+
except AssertionError as e:
|
|
174
|
+
log.error(e, exc_info=jm._debug > 0)
|
|
175
|
+
is_valid = False
|
|
176
|
+
if jm._debug:
|
|
177
|
+
raise
|
|
178
|
+
|
|
179
|
+
# TODO also check other instances?!
|
|
180
|
+
|
|
181
|
+
return is_valid
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# ultimate type of predefs
|
|
186
|
+
_UTYPE = {
|
|
187
|
+
"$ANY": None, "$NONE": None,
|
|
188
|
+
"$NULL": type(None),
|
|
189
|
+
"$BOOL": bool, "$BOOLEAN": bool,
|
|
190
|
+
"$I32": int, "$U32": int, "$I64": int, "$U64": int, "$INT": int, "$INTEGER": int,
|
|
191
|
+
"$F32": float, "$F64": float, "$FLOAT": float, "$NUMBER": float,
|
|
192
|
+
"$STRING": str,
|
|
193
|
+
"$URL": str, "$URI": str, "$EMAIL": str,
|
|
194
|
+
"$REGEX": str, "$EXREG": str,
|
|
195
|
+
"$DATE": str, "$TIME": str, "$DATETIME": str,
|
|
196
|
+
"$UUID": str,
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
# TODO return a set of types?
|
|
201
|
+
def ultimate_type(jm: JsonModel, model: ModelType) -> type|None:
|
|
202
|
+
"""Get the utimate type by following definitions.
|
|
203
|
+
|
|
204
|
+
Return _None_ if unknown, eg several are possible.
|
|
205
|
+
Note: this is also return of the type system is not feasible, eg "&(int, str)"
|
|
206
|
+
|
|
207
|
+
FIXME recursion
|
|
208
|
+
"""
|
|
209
|
+
match model:
|
|
210
|
+
case str():
|
|
211
|
+
if model == "" or model[0] not in ("$", "="):
|
|
212
|
+
return type(model)
|
|
213
|
+
elif model[0] == "=": # constants
|
|
214
|
+
return (type(None) if model == "=null" else
|
|
215
|
+
bool if model in ("=true", "=false") else
|
|
216
|
+
float if "." in model else
|
|
217
|
+
int)
|
|
218
|
+
elif model in _UTYPE:
|
|
219
|
+
return _UTYPE[model]
|
|
220
|
+
else:
|
|
221
|
+
m = jm.resolveRef(model, ["<?>"]) # type: ignore
|
|
222
|
+
return ultimate_type(m, m._model)
|
|
223
|
+
case dict():
|
|
224
|
+
if "@" in model:
|
|
225
|
+
return ultimate_type(jm, model["@"])
|
|
226
|
+
elif "|" in model:
|
|
227
|
+
assert isinstance(models := model["|"], list)
|
|
228
|
+
types = set(ultimate_type(jm, m) for m in models)
|
|
229
|
+
if len(types) == 1:
|
|
230
|
+
return types.pop()
|
|
231
|
+
else:
|
|
232
|
+
return None
|
|
233
|
+
elif "^" in model:
|
|
234
|
+
assert isinstance(models := model["^"], list)
|
|
235
|
+
types = set(ultimate_type(jm, m) for m in models)
|
|
236
|
+
if len(types) == 1:
|
|
237
|
+
return types.pop()
|
|
238
|
+
else:
|
|
239
|
+
return None
|
|
240
|
+
elif "&" in model:
|
|
241
|
+
assert isinstance(models := model["&"], list)
|
|
242
|
+
types = set(ultimate_type(jm, m) for m in models)
|
|
243
|
+
if len(types) == 1:
|
|
244
|
+
return types.pop()
|
|
245
|
+
else:
|
|
246
|
+
# possibly not feasible
|
|
247
|
+
return None
|
|
248
|
+
else:
|
|
249
|
+
return type(model) # dict
|
|
250
|
+
case _: # None, bool, int, float, list
|
|
251
|
+
return type(model)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
# model predef to standard model
|
|
255
|
+
_UMODEL = {
|
|
256
|
+
"$BOOL": True, "$NULL": None,
|
|
257
|
+
"$I32": -1, "$U32": 0, "$I64": -1, "$U64": 0, "$INTEGER": -1,
|
|
258
|
+
"$F32": -1.0, "$F64": -1.0, "$NUMBER": -1.0,
|
|
259
|
+
"$STRING": "",
|
|
260
|
+
# stop recursion on (some) predefs
|
|
261
|
+
"$ANY": "$ANY",
|
|
262
|
+
"$NONE": "$NONE",
|
|
263
|
+
"$URL": "$URL",
|
|
264
|
+
"$REGEX": "$REGEX",
|
|
265
|
+
"$EXREG": "$EXREG",
|
|
266
|
+
"$DATE": "$DATE",
|
|
267
|
+
"$TIME": "$TIME",
|
|
268
|
+
"$DATETIME": "$DATETIME",
|
|
269
|
+
"$UUID": "$UUID",
|
|
270
|
+
"$URI": "$URI",
|
|
271
|
+
"$EMAIL": "$EMAIL",
|
|
272
|
+
# to be continued…
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
def ultimate_model(jm: JsonModel, model: ModelType, constrained=True, strict=False) -> ModelType:
|
|
276
|
+
"""Look for the real model, beyond references."""
|
|
277
|
+
log.debug(f"ultimate_model on {model}")
|
|
278
|
+
match model:
|
|
279
|
+
case str():
|
|
280
|
+
if model == "" or model[0] not in ("$", "="): # / returns as is
|
|
281
|
+
return model
|
|
282
|
+
elif model[0] == "=":
|
|
283
|
+
return model
|
|
284
|
+
else: # follow definition if possible
|
|
285
|
+
assert model[0] == "$"
|
|
286
|
+
if model in _UMODEL:
|
|
287
|
+
m = _UMODEL[model]
|
|
288
|
+
else:
|
|
289
|
+
m = jm.resolveRef(model, [])._model
|
|
290
|
+
# handle some predefs (again)
|
|
291
|
+
if isinstance(m, str) and m in _UMODEL:
|
|
292
|
+
return _UMODEL[m]
|
|
293
|
+
# else try recursing
|
|
294
|
+
return ultimate_model(jm, m) if m != UnknownModel else m # type: ignore
|
|
295
|
+
case dict():
|
|
296
|
+
if "@" in model:
|
|
297
|
+
if strict:
|
|
298
|
+
if set(model.keys()).issubset(["@", "#", "~", "%", "$"]):
|
|
299
|
+
return ultimate_model(jm, model["@"])
|
|
300
|
+
else:
|
|
301
|
+
return UnknownModel # type: ignore
|
|
302
|
+
elif constrained:
|
|
303
|
+
return ultimate_model(jm, model["@"])
|
|
304
|
+
else:
|
|
305
|
+
return UnknownModel # type: ignore
|
|
306
|
+
elif "|" in model:
|
|
307
|
+
return UnknownModel # type: ignore
|
|
308
|
+
else:
|
|
309
|
+
return model
|
|
310
|
+
case _:
|
|
311
|
+
return model
|
|
312
|
+
|
|
313
|
+
def evalModel(jm: JsonModel, model: ModelType):
|
|
314
|
+
"""Tell if an ultimate model value has a constant."""
|
|
315
|
+
# FIXME should it detect @ eq?
|
|
316
|
+
v = ultimate_model(jm, model)
|
|
317
|
+
if v == UnknownModel:
|
|
318
|
+
# FIXME ?!
|
|
319
|
+
return None
|
|
320
|
+
tv = type(v)
|
|
321
|
+
if tv is str:
|
|
322
|
+
assert isinstance(v, str) # useless pyright hint
|
|
323
|
+
if v == "":
|
|
324
|
+
return None # FIXME
|
|
325
|
+
elif v == "=null":
|
|
326
|
+
return None
|
|
327
|
+
elif re.search(r"^=(true|false)$", v) is not None:
|
|
328
|
+
return True if v == "=true" else False
|
|
329
|
+
elif re.search(r"^=-?\d+$", v) is not None:
|
|
330
|
+
return int(v[1:])
|
|
331
|
+
elif re.search(r"^=", v) is not None:
|
|
332
|
+
return float(v[1:])
|
|
333
|
+
elif v[0] == "_":
|
|
334
|
+
return v[1:]
|
|
335
|
+
else:
|
|
336
|
+
return v
|
|
337
|
+
else:
|
|
338
|
+
return None
|
|
339
|
+
|
|
340
|
+
def disjunct_analyse(jm: JsonModel, model: ModelType, mpath: ModelPath) -> \
|
|
341
|
+
tuple[str, type, list, list]|None:
|
|
342
|
+
"""Return the optimized check function if possible."""
|
|
343
|
+
# FIXME if there is a ^, the preprocessor will have detected the discriminant
|
|
344
|
+
# and turned it into a |.
|
|
345
|
+
assert isinstance(model, dict) and "|" in model
|
|
346
|
+
# first filter out
|
|
347
|
+
utype = ultimate_type(jm, model)
|
|
348
|
+
if utype is not dict:
|
|
349
|
+
log.debug(f"ultimate type not a dict: {utype}")
|
|
350
|
+
return None
|
|
351
|
+
# get models
|
|
352
|
+
assert isinstance(models := model["|"], list)
|
|
353
|
+
models = [ultimate_model(jm, m) for m in models]
|
|
354
|
+
# should not happen, just in case
|
|
355
|
+
if len(models) < 2:
|
|
356
|
+
log.debug("not enough models for a disjunction")
|
|
357
|
+
return None
|
|
358
|
+
# we may have some doubts yet (eg | in |)
|
|
359
|
+
if any(filter(lambda m: not isinstance(m, dict), models)):
|
|
360
|
+
log.debug("some models are not objects")
|
|
361
|
+
return None
|
|
362
|
+
# only objects: collect their direct mandatory properties
|
|
363
|
+
all_props: list[set[str]] = [
|
|
364
|
+
set(k for k in m.keys() if k and k[0] not in ("$", "?", "/"))
|
|
365
|
+
for m in models if isinstance(m, dict)
|
|
366
|
+
]
|
|
367
|
+
# get cleaned props and their constant values
|
|
368
|
+
all_const_props: list[dict[str, Constants]] = []
|
|
369
|
+
for props, model in zip(all_props, models):
|
|
370
|
+
consts: dict[str, Constants] = {}
|
|
371
|
+
assert isinstance(model, dict)
|
|
372
|
+
for prop in props:
|
|
373
|
+
val = evalModel(jm, model[prop])
|
|
374
|
+
if val is not None:
|
|
375
|
+
key = prop[1:] if prop[0] in ("_", "!") else prop
|
|
376
|
+
consts[key] = val
|
|
377
|
+
all_const_props.append(consts)
|
|
378
|
+
# tag candidates must:
|
|
379
|
+
# - appear in all alternate models
|
|
380
|
+
candidates: set[str] = set(all_const_props[0].keys())
|
|
381
|
+
for props in all_const_props[1:]:
|
|
382
|
+
candidates.intersection_update(props.keys())
|
|
383
|
+
if not candidates:
|
|
384
|
+
log.debug("no property with constant values")
|
|
385
|
+
return None
|
|
386
|
+
# - have the same type
|
|
387
|
+
candidates_typed: set[str] = set()
|
|
388
|
+
for prop in candidates:
|
|
389
|
+
if len(set(type(consts[prop]) for consts in all_const_props)) == 1:
|
|
390
|
+
candidates_typed.add(prop)
|
|
391
|
+
if not candidates_typed:
|
|
392
|
+
log.debug("no proprety with same type constants")
|
|
393
|
+
return None
|
|
394
|
+
# - have distinct constant values
|
|
395
|
+
candidates_distinct: set[str] = set()
|
|
396
|
+
for prop in candidates_typed:
|
|
397
|
+
if len(set(consts[prop] for consts in all_const_props)) == len(all_const_props):
|
|
398
|
+
candidates_distinct.add(prop)
|
|
399
|
+
if not candidates_distinct:
|
|
400
|
+
log.debug("no property with distinct values")
|
|
401
|
+
return None
|
|
402
|
+
if len(candidates_distinct) > 1:
|
|
403
|
+
log.warning(f"several disjunctive properties: {candidates_distinct}")
|
|
404
|
+
# one tag with one type and only constants found!
|
|
405
|
+
tag_name = candidates_distinct.pop()
|
|
406
|
+
tag_type = ultimate_type(jm, all_const_props[0][tag_name])
|
|
407
|
+
assert tag_type in (bool, int, float, str)
|
|
408
|
+
|
|
409
|
+
return tag_name, tag_type, models, all_const_props
|