feat(ingest): support boxed conditional/filter/iterator/list element forms
public
feat(ingest): support boxed conditional/filter/iterator/list element forms Add four new parse_*_decisions() functions that translate DMN 1.5 boxed-expression XML elements (<conditional>, <filter>, <for>/<some>/<every>, <list>) into the equivalent FEEL text the engine already evaluates. Wire them as four additional dispatch branches (forms 5–8) in extract_cases(), and add BOXED_PREDICATE_DEVIATION_SKIPS for three cases where feel-core's documented behaviour (ADR-24 filter drop, ADR-13/14 quantifier short-circuit) differs from the TCK expectation. Also fix a latent bug in parse_value_elem: an explicit xsi:type="xsd:string" now takes priority over the text-heuristic boolean check, so a <value xsi:type="xsd:string"> true</value> is correctly returned as the string "true" rather than the boolean true. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | a7153537040d13b86af65f331041071beb43b412 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 0823ae2d0a3f |
| Children | |
| Pushlog | by timo via ssh |
modified tools/ingest_tck.py +251 −9
| @@ -812,6 +812,27 @@ | ||
| 812 | 812 | "0083-feel-unicode:decision_007:decision_007", # {🐎: "😀"} |
| 813 | 813 | }) |
| 814 | 814 | |
| 815 | +# ADR-24 (filter) and ADR-13/ADR-14 (quantifier short-circuit / sticky provenance): | |
| 816 | +# Three cases where feel-core's documented design produces a result that differs from | |
| 817 | +# the TCK expectation for a non-true predicate / short-circuiting quantifier. | |
| 818 | +# | |
| 819 | +# 1151-boxed-filter:004 — [1,2,3,4,5]["not a boolean"] | |
| 820 | +# TCK expects null; feel-core returns [] (non-true predicate silently drops the | |
| 821 | +# element per ADR-24 "Filter scope … Non-true predicate results drop the element"). | |
| 822 | +# | |
| 823 | +# 1151-boxed-filter:005 — [1,2,3,4,5][if (item<=3) then true else "not a boolean"] | |
| 824 | +# TCK expects null; feel-core returns [1,2,3] (same ADR-24 rule). | |
| 825 | +# | |
| 826 | +# 1153-boxed-some:006 — some element in [1,2] satisfies if (element=2) then true else … | |
| 827 | +# TCK expects null; feel-core returns true because `some` short-circuits on the | |
| 828 | +# first true (ADR-13/14 sticky-provenance: the dominating true wins regardless of | |
| 829 | +# a subsequent non-boolean taint). | |
| 830 | +BOXED_PREDICATE_DEVIATION_SKIPS: frozenset[str] = frozenset({ | |
| 831 | + "1151-boxed-filter:004:decision004", | |
| 832 | + "1151-boxed-filter:005:decision005", | |
| 833 | + "1153-boxed-some:006:decision006", | |
| 834 | +}) | |
| 835 | + | |
| 815 | 836 | |
| 816 | 837 | def apply_deviations(case: Case) -> None: |
| 817 | 838 | """Run every registry rule whose predicate matches `case` (mutates in place).""" |
| @@ -1219,6 +1240,160 @@ | ||
| 1219 | 1240 | return result |
| 1220 | 1241 | |
| 1221 | 1242 | |
| 1243 | +def parse_conditional_decisions(dmn_path: Path) -> dict[str, tuple[str, str, str]]: | |
| 1244 | + """Parse ``<conditional>`` form decisions; return a mapping from decision name | |
| 1245 | + to ``(if_expr, then_expr, else_expr)`` where each expression is the FEEL text | |
| 1246 | + of the corresponding ``<literalExpression><text>`` child. | |
| 1247 | + | |
| 1248 | + Only decisions whose direct logic child is ``<conditional>`` are returned. | |
| 1249 | + Decisions with a missing or malformed child are silently omitted.""" | |
| 1250 | + root = ET.parse(dmn_path).getroot() | |
| 1251 | + result: dict[str, tuple[str, str, str]] = {} | |
| 1252 | + for elem in root.iter(): | |
| 1253 | + if _local(elem.tag) != "decision": | |
| 1254 | + continue | |
| 1255 | + name = elem.get("name") | |
| 1256 | + cond = _first_child(elem, "conditional") | |
| 1257 | + if name is None or cond is None: | |
| 1258 | + continue | |
| 1259 | + if_branch = _first_child(cond, "if") | |
| 1260 | + then_branch = _first_child(cond, "then") | |
| 1261 | + else_branch = _first_child(cond, "else") | |
| 1262 | + if if_branch is None or then_branch is None or else_branch is None: | |
| 1263 | + continue | |
| 1264 | + def _text_of(branch: ET.Element) -> Optional[str]: | |
| 1265 | + lit = _first_child(branch, "literalExpression") | |
| 1266 | + if lit is None: | |
| 1267 | + return None | |
| 1268 | + t = _first_child(lit, "text") | |
| 1269 | + return t.text.strip() if t is not None and t.text is not None else None | |
| 1270 | + if_expr = _text_of(if_branch) | |
| 1271 | + then_expr = _text_of(then_branch) | |
| 1272 | + else_expr = _text_of(else_branch) | |
| 1273 | + if if_expr is None or then_expr is None or else_expr is None: | |
| 1274 | + continue | |
| 1275 | + result[name] = (if_expr, then_expr, else_expr) | |
| 1276 | + return result | |
| 1277 | + | |
| 1278 | + | |
| 1279 | +def parse_filter_decisions(dmn_path: Path) -> dict[str, tuple[str, str]]: | |
| 1280 | + """Parse ``<filter>`` form decisions; return a mapping from decision name to | |
| 1281 | + ``(in_expr, match_expr)`` where each expression is the FEEL text of the | |
| 1282 | + corresponding ``<literalExpression><text>`` child. | |
| 1283 | + | |
| 1284 | + Only decisions whose direct logic child is ``<filter>`` are returned.""" | |
| 1285 | + root = ET.parse(dmn_path).getroot() | |
| 1286 | + result: dict[str, tuple[str, str]] = {} | |
| 1287 | + for elem in root.iter(): | |
| 1288 | + if _local(elem.tag) != "decision": | |
| 1289 | + continue | |
| 1290 | + name = elem.get("name") | |
| 1291 | + filt = _first_child(elem, "filter") | |
| 1292 | + if name is None or filt is None: | |
| 1293 | + continue | |
| 1294 | + in_branch = _first_child(filt, "in") | |
| 1295 | + match_branch = _first_child(filt, "match") | |
| 1296 | + if in_branch is None or match_branch is None: | |
| 1297 | + continue | |
| 1298 | + def _text_of_lit(branch: ET.Element) -> Optional[str]: | |
| 1299 | + lit = _first_child(branch, "literalExpression") | |
| 1300 | + if lit is None: | |
| 1301 | + return None | |
| 1302 | + t = _first_child(lit, "text") | |
| 1303 | + return t.text.strip() if t is not None and t.text is not None else None | |
| 1304 | + in_expr = _text_of_lit(in_branch) | |
| 1305 | + match_expr = _text_of_lit(match_branch) | |
| 1306 | + if in_expr is None or match_expr is None: | |
| 1307 | + continue | |
| 1308 | + result[name] = (in_expr, match_expr) | |
| 1309 | + return result | |
| 1310 | + | |
| 1311 | + | |
| 1312 | +def parse_iterator_decisions( | |
| 1313 | + dmn_path: Path, | |
| 1314 | +) -> dict[str, tuple[str, str, str, str]]: | |
| 1315 | + """Parse ``<for>``, ``<some>``, and ``<every>`` form decisions; return a | |
| 1316 | + mapping from decision name to ``(kind, var, in_expr, body_expr)`` where: | |
| 1317 | + | |
| 1318 | + * ``kind`` is ``"for"``, ``"some"``, or ``"every"``. | |
| 1319 | + * ``var`` is the ``iteratorVariable`` attribute value. | |
| 1320 | + * ``in_expr`` is the FEEL text of the ``<in><literalExpression><text>`` cell. | |
| 1321 | + * ``body_expr`` is the FEEL text of the ``<return>`` (for ``for``) or | |
| 1322 | + ``<satisfies>`` (for ``some``/``every``) cell. | |
| 1323 | + | |
| 1324 | + Only decisions whose direct logic child is one of the three iterator elements | |
| 1325 | + are returned. Decisions with missing attributes or cells are silently omitted.""" | |
| 1326 | + _ITERATOR_TAGS = {"for": "return", "some": "satisfies", "every": "satisfies"} | |
| 1327 | + root = ET.parse(dmn_path).getroot() | |
| 1328 | + result: dict[str, tuple[str, str, str, str]] = {} | |
| 1329 | + for elem in root.iter(): | |
| 1330 | + if _local(elem.tag) != "decision": | |
| 1331 | + continue | |
| 1332 | + name = elem.get("name") | |
| 1333 | + if name is None: | |
| 1334 | + continue | |
| 1335 | + iter_elem: Optional[ET.Element] = None | |
| 1336 | + kind: Optional[str] = None | |
| 1337 | + for tag in _ITERATOR_TAGS: | |
| 1338 | + child = _first_child(elem, tag) | |
| 1339 | + if child is not None: | |
| 1340 | + iter_elem = child | |
| 1341 | + kind = tag | |
| 1342 | + break | |
| 1343 | + if iter_elem is None or kind is None: | |
| 1344 | + continue | |
| 1345 | + var = iter_elem.get("iteratorVariable") | |
| 1346 | + if not var: | |
| 1347 | + continue | |
| 1348 | + in_branch = _first_child(iter_elem, "in") | |
| 1349 | + body_tag = _ITERATOR_TAGS[kind] | |
| 1350 | + body_branch = _first_child(iter_elem, body_tag) | |
| 1351 | + if in_branch is None or body_branch is None: | |
| 1352 | + continue | |
| 1353 | + def _text_of_iter(branch: ET.Element) -> Optional[str]: | |
| 1354 | + lit = _first_child(branch, "literalExpression") | |
| 1355 | + if lit is None: | |
| 1356 | + return None | |
| 1357 | + t = _first_child(lit, "text") | |
| 1358 | + return t.text.strip() if t is not None and t.text is not None else None | |
| 1359 | + in_expr = _text_of_iter(in_branch) | |
| 1360 | + body_expr = _text_of_iter(body_branch) | |
| 1361 | + if in_expr is None or body_expr is None: | |
| 1362 | + continue | |
| 1363 | + result[name] = (kind, var, in_expr, body_expr) | |
| 1364 | + return result | |
| 1365 | + | |
| 1366 | + | |
| 1367 | +def parse_list_decisions(dmn_path: Path) -> dict[str, list[str]]: | |
| 1368 | + """Parse ``<list>`` form decisions (boxed list literal); return a mapping from | |
| 1369 | + decision name to an ordered list of FEEL text strings, one per direct | |
| 1370 | + ``<literalExpression>`` child of the ``<list>`` element. | |
| 1371 | + | |
| 1372 | + Only decisions whose direct logic child is a ``<list>`` are returned. | |
| 1373 | + Rows with a missing or empty ``<text>`` are silently omitted from the row | |
| 1374 | + list; decisions with zero valid rows are excluded.""" | |
| 1375 | + root = ET.parse(dmn_path).getroot() | |
| 1376 | + result: dict[str, list[str]] = {} | |
| 1377 | + for elem in root.iter(): | |
| 1378 | + if _local(elem.tag) != "decision": | |
| 1379 | + continue | |
| 1380 | + name = elem.get("name") | |
| 1381 | + lst = _first_child(elem, "list") | |
| 1382 | + if name is None or lst is None: | |
| 1383 | + continue | |
| 1384 | + rows: list[str] = [] | |
| 1385 | + for child in lst: | |
| 1386 | + if _local(child.tag) != "literalExpression": | |
| 1387 | + continue | |
| 1388 | + t = _first_child(child, "text") | |
| 1389 | + if t is None or not t.text: | |
| 1390 | + continue | |
| 1391 | + rows.append(t.text.strip()) | |
| 1392 | + if rows: | |
| 1393 | + result[name] = rows | |
| 1394 | + return result | |
| 1395 | + | |
| 1396 | + | |
| 1222 | 1397 | def _inline_bkms( |
| 1223 | 1398 | call_expr: str, |
| 1224 | 1399 | bkm_names: frozenset[str], |
| @@ -1269,7 +1444,14 @@ | ||
| 1269 | 1444 | xsi_type = value_elem.get(f"{_XSI}type", "") |
| 1270 | 1445 | raw = value_elem.text or "" |
| 1271 | 1446 | text = raw.strip() |
| 1272 | - if text in ("true", "false") or xsi_type.endswith("boolean"): | |
| 1447 | + # A declared string type is authoritative: a string result like "2" must not | |
| 1448 | + # be mis-read as the number 2 (e.g. `string(2)` returns the string "2"). It must | |
| 1449 | + # also win over the boolean text heuristic so `<value xsi:type="xsd:string">true</value>` | |
| 1450 | + # is the string "true", not the boolean true. Keep the raw text un-stripped — a | |
| 1451 | + # string result may legitimately be whitespace (e.g. `substring("foob r", -2, 1) = " "`). | |
| 1452 | + if xsi_type.endswith("string"): | |
| 1453 | + return raw, "string" | |
| 1454 | + if xsi_type.endswith("boolean") or text in ("true", "false"): | |
| 1273 | 1455 | return text == "true", "boolean" |
| 1274 | 1456 | # Temporal types (ADR-18). The lexical text is the canonical ISO-8601 form |
| 1275 | 1457 | # feel-core round-trips. Check dateTime before date/time (it ends with "time" |
| @@ -1284,12 +1466,6 @@ | ||
| 1284 | 1466 | return text, "time" |
| 1285 | 1467 | if low.endswith("duration"): |
| 1286 | 1468 | return text, "duration" |
| 1287 | - # A declared string type is authoritative: a string result like "2" must not | |
| 1288 | - # be mis-read as the number 2 (e.g. `string(2)` returns the string "2"). Keep | |
| 1289 | - # the raw text un-stripped — a string result may legitimately be whitespace | |
| 1290 | - # (e.g. `substring("foob r", -2, 1) = " "`). | |
| 1291 | - if xsi_type.endswith("string"): | |
| 1292 | - return raw, "string" | |
| 1293 | 1469 | # decimal / double / integer, or anything that parses as a number |
| 1294 | 1470 | try: |
| 1295 | 1471 | return float(text), "number" |
| @@ -1378,13 +1554,18 @@ | ||
| 1378 | 1554 | # Parse type infrastructure for coerce_to resolution. |
| 1379 | 1555 | item_defs = parse_item_definitions(dmn_path) |
| 1380 | 1556 | decision_typerefs = parse_decision_typerefs(dmn_path, item_defs) |
| 1381 | - # Parse decision expressions (four forms). | |
| 1557 | + # Parse decision expressions (eight forms). | |
| 1382 | 1558 | decisions = parse_decisions(dmn_path) |
| 1383 | 1559 | context_decisions = parse_context_decisions(dmn_path) |
| 1384 | 1560 | context_result_decisions = parse_context_result_decisions(dmn_path) |
| 1385 | 1561 | bkms = parse_bkms(dmn_path, item_defs) |
| 1386 | 1562 | knowledge_reqs = parse_knowledge_requirements(dmn_path) |
| 1387 | 1563 | invocation_decisions = parse_invocation_decisions(dmn_path, item_defs) |
| 1564 | + # Boxed element-form decisions (DMN 1.5 §10.2.1.5 / §10.2.1.8–10). | |
| 1565 | + conditional_decisions = parse_conditional_decisions(dmn_path) | |
| 1566 | + filter_decisions = parse_filter_decisions(dmn_path) | |
| 1567 | + iterator_decisions = parse_iterator_decisions(dmn_path) | |
| 1568 | + list_decisions = parse_list_decisions(dmn_path) | |
| 1388 | 1569 | model_stem = dmn_path.stem |
| 1389 | 1570 | |
| 1390 | 1571 | root = ET.parse(test_path).getroot() |
| @@ -1424,12 +1605,16 @@ | ||
| 1424 | 1605 | case_id = f"{model_stem}:{tc_id}:{name}" |
| 1425 | 1606 | |
| 1426 | 1607 | # ----------------------------------------------------------------- # |
| 1427 | - # Resolve the decision to an expression (four forms): # | |
| 1608 | + # Resolve the decision to an expression (eight forms): # | |
| 1428 | 1609 | # # |
| 1429 | 1610 | # 1. Literal-expression decision (possibly BKM-backed) # |
| 1430 | 1611 | # 2. Boxed-context decision with no result cell # |
| 1431 | 1612 | # 3. Boxed-context decision with a trailing result cell (fd_*) # |
| 1432 | 1613 | # 4. Invocation-form decision # |
| 1614 | + # 5. Boxed-conditional decision (<conditional> element form) # | |
| 1615 | + # 6. Boxed-filter decision (<filter> element form) # | |
| 1616 | + # 7. Boxed-iterator decision (<for>/<some>/<every> element forms) # | |
| 1617 | + # 8. Boxed-list decision (<list> element form) # | |
| 1433 | 1618 | # ----------------------------------------------------------------- # |
| 1434 | 1619 | |
| 1435 | 1620 | if name in decisions: |
| @@ -1532,6 +1717,55 @@ | ||
| 1532 | 1717 | # coerce_to priority: invocation typeRef > BKM return typeRef > variable typeRef |
| 1533 | 1718 | case_coerce_to = inv_coerce_to or bkm_return_type2 or decision_typerefs.get(name) |
| 1534 | 1719 | |
| 1720 | + elif name in conditional_decisions: | |
| 1721 | + # Boxed conditional: if (IF) then (THEN) else (ELSE) | |
| 1722 | + if_expr, then_expr, else_expr = conditional_decisions[name] | |
| 1723 | + assembled = f"if ({if_expr}) then ({then_expr}) else ({else_expr})" | |
| 1724 | + if not fits_slice_grammar(assembled, input_names): | |
| 1725 | + skipped.append((case_id, f"outside slice grammar: {assembled!r}")) | |
| 1726 | + continue | |
| 1727 | + expression = assembled | |
| 1728 | + case_coerce_to = decision_typerefs.get(name) | |
| 1729 | + | |
| 1730 | + elif name in filter_decisions: | |
| 1731 | + # Boxed filter: (IN)[MATCH] (predicate uses `item`) | |
| 1732 | + in_expr, match_expr = filter_decisions[name] | |
| 1733 | + assembled = f"({in_expr})[{match_expr}]" | |
| 1734 | + # `item` is always bound in filter scope; include it so the slice | |
| 1735 | + # detector does not reject its implicit use inside `match_expr`. | |
| 1736 | + if not fits_slice_grammar(assembled, input_names | frozenset({"item"})): | |
| 1737 | + skipped.append((case_id, f"outside slice grammar: {assembled!r}")) | |
| 1738 | + continue | |
| 1739 | + expression = assembled | |
| 1740 | + case_coerce_to = decision_typerefs.get(name) | |
| 1741 | + | |
| 1742 | + elif name in iterator_decisions: | |
| 1743 | + # Boxed iterator: for VAR in (IN) return (BODY) | |
| 1744 | + # some/every VAR in (IN) satisfies (BODY) | |
| 1745 | + kind, var, in_expr, body_expr = iterator_decisions[name] | |
| 1746 | + if kind == "for": | |
| 1747 | + assembled = f"for {var} in ({in_expr}) return ({body_expr})" | |
| 1748 | + else: | |
| 1749 | + assembled = f"{kind} {var} in ({in_expr}) satisfies ({body_expr})" | |
| 1750 | + # The iterator variable is bound inside the body; include it in the | |
| 1751 | + # slice check so the grammar detector treats it as known. | |
| 1752 | + iter_bound = input_names | frozenset({var}) | |
| 1753 | + if not fits_slice_grammar(assembled, iter_bound): | |
| 1754 | + skipped.append((case_id, f"outside slice grammar: {assembled!r}")) | |
| 1755 | + continue | |
| 1756 | + expression = assembled | |
| 1757 | + case_coerce_to = decision_typerefs.get(name) | |
| 1758 | + | |
| 1759 | + elif name in list_decisions: | |
| 1760 | + # Boxed list literal: [row1, row2, …] | |
| 1761 | + rows = list_decisions[name] | |
| 1762 | + assembled = "[" + ", ".join(rows) + "]" | |
| 1763 | + if not fits_slice_grammar(assembled, input_names): | |
| 1764 | + skipped.append((case_id, f"outside slice grammar: {assembled!r}")) | |
| 1765 | + continue | |
| 1766 | + expression = assembled | |
| 1767 | + case_coerce_to = decision_typerefs.get(name) | |
| 1768 | + | |
| 1535 | 1769 | else: |
| 1536 | 1770 | continue |
| 1537 | 1771 | |
| @@ -1569,6 +1803,14 @@ | ||
| 1569 | 1803 | |
| 1570 | 1804 | if case_id in NON_XID_NAME_SKIPS: |
| 1571 | 1805 | skipped.append((case_id, "emoji / non-XID bare name not in the slice (ADR-17)")) |
| 1806 | + continue | |
| 1807 | + | |
| 1808 | + if case_id in BOXED_PREDICATE_DEVIATION_SKIPS: | |
| 1809 | + skipped.append(( | |
| 1810 | + case_id, | |
| 1811 | + "boxed predicate deviation: non-true filter predicate drops element " | |
| 1812 | + "(ADR-24) / some short-circuits on true before non-boolean taint (ADR-13/14)", | |
| 1813 | + )) | |
| 1572 | 1814 | continue |
| 1573 | 1815 | |
| 1574 | 1816 | expected, expected_type = parse_expected(result_node) |