From 2be362db67215388b06cf22ab564438cc3a6f6f8 Mon Sep 17 00:00:00 2001 From: Pavel Kuznetsov Date: Sun, 8 Nov 2026 19:43:40 +0300 Subject: [PATCH] fix: delegate expression evaluation to Fusion and accept random --- Paramood/bridge/formatting.py | 11 +- Paramood/bridge/protocol.py | 5 +- Paramood/core/builtins.py | 4 +- Paramood/core/expression_catalog.json | 1194 +++++++++++++++++++- Paramood/core/expressions.py | 169 +-- Paramood/core/text.py | 33 + Paramood/fusion/adapter.py | 169 +-- Paramood/fusion/expression_engine.py | 211 ++++ Paramood/fusion/variants.py | 5 + README.md | 5 +- docs/EXPRESSION_ACCEPTANCE.md | 43 +- docs/EXPRESSION_REFERENCE.md | 38 +- docs/INTELLISENSE.md | 4 +- docs/releases/1.6.1.md | 16 + frontend/src/bridge.ts | 1 + frontend/src/editor.tsx | 2 +- frontend/src/expression-syntax.test.ts | 8 + frontend/src/expression-syntax.ts | 18 +- frontend/src/main.tsx | 7 +- frontend/src/palettes.test.tsx | 45 +- frontend/src/style.css | 20 + scripts/fusion_expression_parity.py | 160 +++ src/expression-catalog.ts | 6 +- src/expression-language.ts | 93 +- tests/integration/test_batch_adapter.py | 36 + tests/integration/test_variants_adapter.py | 11 + tests/intellisense.test.mjs | 21 + tests/unit/test_expression_builtins.py | 10 +- tests/unit/test_native_quantities.py | 153 +++ 29 files changed, 2256 insertions(+), 242 deletions(-) create mode 100644 Paramood/fusion/expression_engine.py create mode 100644 docs/releases/1.6.1.md create mode 100644 scripts/fusion_expression_parity.py create mode 100644 tests/unit/test_native_quantities.py diff --git a/Paramood/bridge/formatting.py b/Paramood/bridge/formatting.py index 3b38a86..59fb416 100644 --- a/Paramood/bridge/formatting.py +++ b/Paramood/bridge/formatting.py @@ -1,11 +1,10 @@ -from ..core.expressions import SCALAR, UNITS, TextValue +from ..core.expressions import TextValue, unit_definition def display_quantity(quantity, unit): if isinstance(quantity, TextValue): return quantity.value - if not unit and quantity.dimension == SCALAR: - return quantity.value - if unit not in UNITS or UNITS[unit][1] != quantity.dimension: - raise ValueError('Unsupported output unit') - return quantity.value / UNITS[unit][0] + factor, dimension, offset = unit_definition(unit) + if dimension != quantity.dimension: + raise ValueError('Incompatible output unit') + return (quantity.value - offset) / factor diff --git a/Paramood/bridge/protocol.py b/Paramood/bridge/protocol.py index f17ca3c..37f3727 100644 --- a/Paramood/bridge/protocol.py +++ b/Paramood/bridge/protocol.py @@ -3,7 +3,6 @@ from ..core.expressions import ( ExpressionError, LocalEvaluationUnsupported, - NondeterministicExpression, ) @@ -66,9 +65,7 @@ def dispatch(adapter, message): } except ExpressionError as exc: code = ( - 'NONDETERMINISTIC_EXPRESSION' - if isinstance(exc, NondeterministicExpression) - else 'LOCAL_EVALUATION_UNSUPPORTED' + 'LOCAL_EVALUATION_UNSUPPORTED' if isinstance(exc, LocalEvaluationUnsupported) else 'INVALID_EXPRESSION' ) diff --git a/Paramood/core/builtins.py b/Paramood/core/builtins.py index 5d558e9..5a4730b 100644 --- a/Paramood/core/builtins.py +++ b/Paramood/core/builtins.py @@ -8,4 +8,6 @@ ) FUNCTIONS = {item['name']: item for item in CATALOG['functions']} CONSTANT_NAMES = {item['name'] for item in CATALOG['constants']} -UNIT_NAMES = set(CATALOG['units']) +UNIT_NAMES = set(CATALOG['units']) | { + alias for unit in CATALOG['unitDefinitions'] for alias in unit['aliases'] +} diff --git a/Paramood/core/expression_catalog.json b/Paramood/core/expression_catalog.json index 262c116..1095cc3 100644 --- a/Paramood/core/expression_catalog.json +++ b/Paramood/core/expression_catalog.json @@ -261,12 +261,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "floor(-1.5)", "restriction": "" }, @@ -279,12 +279,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "ceil(1.5)", "restriction": "" }, @@ -297,12 +297,12 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, - "returns": "unitless", + "returns": "same as input", "example": "round(-1.5)", "restriction": "Halfway values round away from zero." }, @@ -315,8 +315,8 @@ { "name": "value", "description": "Value to evaluate.", - "type": "unitless", - "dimension": "unitless" + "type": "numeric", + "dimension": "numeric" } ], "arity": 1, @@ -459,7 +459,7 @@ "arity": 0, "returns": "unitless", "example": "random()", - "restriction": "Changes when evaluated. Exact preview and batch application are unavailable; Quick Calculator uses native Fusion evaluation." + "restriction": "Preview shows a sample. Fusion evaluates the original expression again when applied or recomputed; the resulting value can change." }, { "name": "if", @@ -489,7 +489,7 @@ "arity": 3, "returns": "same as branches", "example": "if(2 mm < 3 mm; 10 mm; 20 mm)", - "restriction": "Three arguments; both branches must have matching types and dimensions." + "restriction": "Fusion evaluates only the selected branch. Unitless numeric branches can adopt the other branch dimension." } ], "constants": [ @@ -640,5 +640,1173 @@ "and", "or", "not" + ], + "unitDefinitions": [ + { + "name": "mm", + "dimension": [ + 1, + 0 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cm", + "dimension": [ + 1, + 0 + ], + "factor": 10.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "m", + "dimension": [ + 1, + 0 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hm", + "dimension": [ + 1, + 0 + ], + "factor": 100000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "micron", + "dimension": [ + 1, + 0 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "in", + "dimension": [ + 1, + 0 + ], + "factor": 25.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ft", + "dimension": [ + 1, + 0 + ], + "factor": 304.8, + "offset": 0.0, + "aliases": [] + }, + { + "name": "yd", + "dimension": [ + 1, + 0 + ], + "factor": 914.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mi", + "dimension": [ + 1, + 0 + ], + "factor": 1609344.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "nauticalMile", + "dimension": [ + 1, + 0 + ], + "factor": 1852000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mil", + "dimension": [ + 1, + 0 + ], + "factor": 0.025400000000000002, + "offset": 0.0, + "aliases": [] + }, + { + "name": "rad", + "dimension": [ + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "deg", + "dimension": [ + 0, + 1 + ], + "factor": 0.017453292519943295, + "offset": 0.0, + "aliases": [] + }, + { + "name": "grad", + "dimension": [ + 0, + 1 + ], + "factor": 0.015707963267948967, + "offset": 0.0, + "aliases": [] + }, + { + "name": "s", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "min", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 60.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hr", + "dimension": [ + 0, + 0, + 1 + ], + "factor": 3600.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "acre", + "dimension": [ + 2, + 0 + ], + "factor": 4046856422.4, + "offset": 0.0, + "aliases": [] + }, + { + "name": "circular_mil", + "dimension": [ + 2, + 0 + ], + "factor": 0.0005067074791, + "offset": 0.0, + "aliases": [] + }, + { + "name": "rpm", + "dimension": [ + 0, + 1, + -1 + ], + "factor": 0.10471975511965977, + "offset": 0.0, + "aliases": [] + }, + { + "name": "farad", + "dimension": [ + -2, + 0, + 4, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "coulomb", + "dimension": [ + 0, + 0, + 1, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "S", + "dimension": [ + -2, + 0, + 3, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mho", + "dimension": [ + -2, + 0, + 3, + -1, + 2 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "dol", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "A", + "dimension": [ + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "J", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "erg", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 0.1, + "offset": 0.0, + "aliases": [] + }, + { + "name": "calorie", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 4184000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Btu", + "dimension": [ + 2, + 0, + -2, + 1 + ], + "factor": 1055055852.62, + "offset": 0.0, + "aliases": [] + }, + { + "name": "V", + "dimension": [ + 2, + 0, + -3, + 1, + -1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ccs", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [ + "CCS" + ] + }, + { + "name": "cis", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 16387.064, + "offset": 0.0, + "aliases": [ + "CIS" + ] + }, + { + "name": "cfm", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 471947.44320000004, + "offset": 0.0, + "aliases": [ + "CFM" + ] + }, + { + "name": "cmh", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 277777.77777777775, + "offset": 0.0, + "aliases": [ + "CMH" + ] + }, + { + "name": "gpm", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 63090.19639999999, + "offset": 0.0, + "aliases": [ + "GPM" + ] + }, + { + "name": "lph", + "dimension": [ + 3, + 0, + -1 + ], + "factor": 277.77777777777777, + "offset": 0.0, + "aliases": [ + "LPH" + ] + }, + { + "name": "N", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "dyne", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 0.01, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lbforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 4448.221615260501, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ounceforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 278.01385095, + "offset": 0.0, + "aliases": [] + }, + { + "name": "tonforce", + "dimension": [ + 1, + 0, + -2, + 1 + ], + "factor": 8896443.230521, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Hz", + "dimension": [ + 0, + 0, + -1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lx", + "dimension": [ + -2, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1 + ], + "factor": 1.0000000000000002e-06, + "offset": 0.0, + "aliases": [] + }, + { + "name": "H", + "dimension": [ + 2, + 0, + -2, + 1, + -2 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cd", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "EV", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lm", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Wb", + "dimension": [ + 2, + 0, + -2, + 1, + -1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "maxwell", + "dimension": [ + 2, + 0, + -2, + 1, + -1 + ], + "factor": 0.01, + "offset": 0.0, + "aliases": [] + }, + { + "name": "T", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gamma", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 1e-09, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gauss", + "dimension": [ + 0, + 0, + -2, + 1, + -1 + ], + "factor": 0.0001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "oersted", + "dimension": [ + -1, + 0, + 0, + 0, + 1 + ], + "factor": 0.07957747200000001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "g", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "kg", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "slug", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 14.5939029372, + "offset": 0.0, + "aliases": [] + }, + { + "name": "lbmass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.45359237, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ouncemass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 0.028349523125, + "offset": 0.0, + "aliases": [] + }, + { + "name": "tonmass", + "dimension": [ + 0, + 0, + 0, + 1 + ], + "factor": 907.18474, + "offset": 0.0, + "aliases": [] + }, + { + "name": "pcs", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "W", + "dimension": [ + 2, + 0, + -3, + 1 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "hp", + "dimension": [ + 2, + 0, + -3, + 1 + ], + "factor": 745699871.582, + "offset": 0.0, + "aliases": [] + }, + { + "name": "Pa", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "psi", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 6.894757293178, + "offset": 0.0, + "aliases": [] + }, + { + "name": "psf", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.047880259, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ksi", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 6894.7572931780005, + "offset": 0.0, + "aliases": [] + }, + { + "name": "bar", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 100.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "atm", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 101.325, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.24908891, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ftH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 2.9890669200000004, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mH2O", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 9.806650000000001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mmHg", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 0.133322387415, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inHg", + "dimension": [ + -1, + 0, + -2, + 1 + ], + "factor": 3.386388, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ohm", + "dimension": [ + 2, + 0, + -3, + 1, + -2 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "sr", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mps", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 1000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "fps", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 304.8, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mph", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 447.04, + "offset": 0.0, + "aliases": [] + }, + { + "name": "knots", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 514.4444444444445, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mmpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 0.016666666666666666, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 16.666666666666668, + "offset": 0.0, + "aliases": [] + }, + { + "name": "inpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 0.42333333333333334, + "offset": 0.0, + "aliases": [] + }, + { + "name": "ftpm", + "dimension": [ + 1, + 0, + -1 + ], + "factor": 5.08, + "offset": 0.0, + "aliases": [] + }, + { + "name": "mole", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "K", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "C", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 1.0, + "offset": 273.15, + "aliases": [] + }, + { + "name": "F", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 0.5555555555555429, + "offset": 255.3722222222222, + "aliases": [] + }, + { + "name": "R", + "dimension": [ + 0, + 0, + 0, + 0, + 0, + 1 + ], + "factor": 0.5555555555555556, + "offset": 0.0, + "aliases": [] + }, + { + "name": "poise", + "dimension": [ + -1, + 0, + -1, + 1 + ], + "factor": 0.0001, + "offset": 0.0, + "aliases": [] + }, + { + "name": "l", + "dimension": [ + 3, + 0 + ], + "factor": 1000000.0, + "offset": 0.0, + "aliases": [] + }, + { + "name": "gal", + "dimension": [ + 3, + 0 + ], + "factor": 3785411.784, + "offset": 0.0, + "aliases": [] + }, + { + "name": "qt", + "dimension": [ + 3, + 0 + ], + "factor": 946352.946, + "offset": 0.0, + "aliases": [] + }, + { + "name": "pt", + "dimension": [ + 3, + 0 + ], + "factor": 473176.473, + "offset": 0.0, + "aliases": [] + }, + { + "name": "cup", + "dimension": [ + 3, + 0 + ], + "factor": 236588.2365, + "offset": 0.0, + "aliases": [] + }, + { + "name": "fl_oz", + "dimension": [ + 3, + 0 + ], + "factor": 29573.5295625, + "offset": 0.0, + "aliases": [] + } ] } diff --git a/Paramood/core/expressions.py b/Paramood/core/expressions.py index 741af8d..d5a9bab 100644 --- a/Paramood/core/expressions.py +++ b/Paramood/core/expressions.py @@ -9,15 +9,15 @@ import ast import math import re -from dataclasses import dataclass +from dataclasses import dataclass, replace from typing import ClassVar, Mapping, Tuple from itertools import zip_longest -from .builtins import FUNCTIONS, CONSTANT_NAMES, UNIT_NAMES +from .builtins import CATALOG, FUNCTIONS, CONSTANT_NAMES, UNIT_NAMES from .text import decode_literal, map_code, normalize_literals, segments, split_definitions -Dimension = Tuple[int, ...] # powers of length, angle, and optional time +Dimension = Tuple[float, ...] # powers of length, angle, and optional time SCALAR = (0, 0) LENGTH = (1, 0) ANGLE = (0, 1) @@ -25,24 +25,16 @@ MAX_TEXT_LENGTH = 65536 MAX_BATCH_TEXT_LENGTH = 262144 UNITS = { - 'mm': (1.0, LENGTH), - 'cm': (10.0, LENGTH), - 'm': (1000.0, LENGTH), - 'in': (25.4, LENGTH), - 'ft': (304.8, LENGTH), - 'deg': (math.pi / 180, ANGLE), - 'rad': (1.0, ANGLE), - 'n': (1.0, SCALAR), - 'hm': (100000.0, LENGTH), - 'micron': (0.001, LENGTH), - 'yd': (914.4, LENGTH), - 'mi': (1609344.0, LENGTH), - 'nauticalMile': (1852000.0, LENGTH), - 'mil': (0.0254, LENGTH), - 'grad': (math.pi / 200, ANGLE), - 's': (1.0, (0, 0, 1)), - 'min': (60.0, (0, 0, 1)), - 'hr': (3600.0, (0, 0, 1)), + item['name']: (item['factor'], tuple(item['dimension'])) for item in CATALOG['unitDefinitions'] +} +for item in CATALOG['unitDefinitions']: + for alias in item['aliases']: + UNITS[alias] = (item['factor'], tuple(item['dimension'])) +UNITS['n'] = (1.0, SCALAR) +UNIT_OFFSETS = { + name: item['offset'] + for item in CATALOG['unitDefinitions'] + for name in [item['name'], *item['aliases']] } _UNIT_PATTERN = '|'.join(re.escape(u) for u in sorted(UNIT_NAMES | {'n'}, key=len, reverse=True)) _LITERAL = re.compile( @@ -76,29 +68,61 @@ class LocalEvaluationUnsupported(ExpressionError): """Valid Fusion syntax whose result cannot be established by the local evaluator.""" -class NondeterministicExpression(LocalEvaluationUnsupported): - """A stable preview cannot be promised for an expression containing random().""" - - def _dimension(values): - result = list(values) + result = [round(v) if math.isclose(v, round(v), abs_tol=1e-7) else round(v, 8) for v in values] while len(result) > 2 and result[-1] == 0: result.pop() return tuple(result) def dimension_unit(dimension, length_unit='mm'): - """Serialize length/angle/time dimensions without inferring physical types.""" - if len(dimension) > 3: - raise LocalEvaluationUnsupported('Unsupported physical dimension') + """Serialize native dimensions; all base units come from the Fusion catalog.""" + bases = (length_unit, 'rad', 's', 'kg', 'A', 'K', 'mole', 'cd', 'dol', 'pcs', 'sr') + if len(dimension) > len(bases): + raise LocalEvaluationUnsupported('Unknown Fusion base dimension') numerator, denominator = [], [] - for unit, power in zip((length_unit, 'rad', 's'), dimension): + for unit, power in zip(bases, dimension): if power: magnitude = abs(power) - part = unit if magnitude == 1 else unit + '^' + str(magnitude) + part = unit if magnitude == 1 else unit + '^' + format(magnitude, '.8g') (numerator if power > 0 else denominator).append(part) + if not numerator and denominator: + # Fusion unit strings need a unit numerator, not the numeric token "1". + numerator.append(length_unit) + denominator.insert(0, length_unit) top = '*'.join(numerator) - return (top or '1') + '/' + '/'.join(denominator) if denominator else top + return top + '/' + '/'.join(denominator) if denominator else top + + +def unit_definition(unit): + if not unit: + return 1.0, SCALAR, 0.0 + if unit in UNITS: + factor, dim = UNITS[unit] + return factor, dim, UNIT_OFFSETS.get(unit, 0.0) + quantity = evaluate('1 ' + unit, {}) + if isinstance(quantity, TextValue): + raise ExpressionError('Expected a numeric unit') + return quantity.value, quantity.dimension, 0.0 + + +def stochastic_expression(expression, parameters=None): + tree = _tree(expression) + return any( + isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == 'random' + for n in ast.walk(tree) + ) or any( + getattr((parameters or {}).get(name), 'stochastic', False) for name in _dependencies(tree) + ) + + +def mark_stochastic(expressions, dependencies): + names = {name for name, expr in expressions.items() if stochastic_expression(expr)} + while True: + expanded = names | {name for name, deps in dependencies.items() if deps & names} + if expanded == names: + return names + names = expanded def _assignment(item: str) -> tuple[str, str, str]: @@ -131,11 +155,13 @@ def replace(match): class Quantity: value: float dimension: Dimension = SCALAR + stochastic: bool = False @dataclass(frozen=True) class TextValue: value: str + stochastic: bool = False dimension: ClassVar[str] = TEXT @@ -174,8 +200,6 @@ def code(raw): lambda m: '(' + m.group(1) + '*__unit__(1,"' + m.group(2) + '")**(' + m.group(3) + '))', raw, ) - if re.search(r'\b(' + _UNIT_PATTERN + r')\s*\*\*\s*[A-Za-z_(]', raw): - raise LocalEvaluationUnsupported('Use pow() for nonliteral unit exponents') raw = _LITERAL.sub(lambda m: '__unit__(' + m.group(1) + ',"' + m.group(2) + '")', raw) return re.sub( r'(?<=[\w)])\s+(' + _UNIT_PATTERN + r')\b(?!\s*\()', @@ -211,7 +235,10 @@ def evaluate( """Evaluate supported operations, rejecting unknown units/functions and dimensions.""" if len(expression) > 10000: raise ExpressionError('Expression is too long') - tree = _tree(normalize_expression(expression, parameters)) + expression = normalize_expression(expression, parameters) + if hasattr(native_function, 'evaluate'): + return native_function.evaluate(expression, parameters) + tree = _tree(expression) if sum(1 for _ in ast.walk(tree)) > 2000: raise ExpressionError('Expression is too complex') for call in ast.walk(tree): @@ -222,14 +249,6 @@ def evaluate( ): if len(call.args) != FUNCTIONS[call.func.id]['arity'] or call.keywords: raise ExpressionError('Invalid function arguments') - # Reject random even in an unselected conditional branch: recompute is unstable. - if not allow_nondeterministic and any( - isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == 'random' - for n in ast.walk(tree) - ): - raise NondeterministicExpression( - 'random() has no stable preview. Use Quick Calculator for a native sample.' - ) def logical(node): if isinstance(node, ast.BoolOp) and isinstance(node.op, (ast.And, ast.Or)): @@ -261,13 +280,9 @@ def power(a, b): if abs(exponent) > 10000: raise ExpressionError('Exponent is too large') dim = [round(d * exponent, 8) for d in a.dimension] - if any(not math.isclose(d, round(d), abs_tol=1e-8) for d in dim): - raise LocalEvaluationUnsupported( - 'Fractional resulting dimensions require native Fusion evaluation' - ) if a.value < 0 and exponent != int(exponent): raise ExpressionError('Power must have a real result') - return Quantity(math.pow(a.value, exponent), _dimension(round(d) for d in dim)) + return Quantity(math.pow(a.value, exponent), _dimension(dim)) def calc(node: ast.AST) -> Quantity | TextValue: if isinstance(node, ast.Constant) and isinstance(node.value, str): @@ -359,7 +374,7 @@ def calc(node: ast.AST) -> Quantity | TextValue: if q.dimension != SCALAR: raise ExpressionError('Unit requires a dimensionless number') factor, dim = UNITS[unit] - return Quantity(q.value * factor, dim) + return Quantity(q.value * factor + UNIT_OFFSETS.get(unit, 0), dim) if name not in _FUNCTIONS: raise ExpressionError('Unknown function: ' + name) args = [calc(arg) for arg in node.args] @@ -368,9 +383,11 @@ def calc(node: ast.AST) -> Quantity | TextValue: if len(args) != FUNCTIONS.get(name, {}).get('arity'): raise ExpressionError('Invalid function arguments') if name == 'random': - if native_function and allow_nondeterministic: + if native_function: return native_function(name, args, SCALAR) - raise NondeterministicExpression('random() requires native Fusion evaluation') + raise LocalEvaluationUnsupported( + 'Native Fusion evaluation is required for random()' + ) q = args[0] if name == 'pow': return power(q, args[1]) @@ -379,9 +396,9 @@ def calc(node: ast.AST) -> Quantity | TextValue: raise ExpressionError('Incompatible dimensions') return Quantity((min if name == 'min' else max)(a.value for a in args), q.dimension) if name == 'sqrt': - if any(d % 2 for d in q.dimension) or q.value < 0: + if q.value < 0: raise ExpressionError('Invalid sqrt dimension or value') - return Quantity(math.sqrt(q.value), _dimension(d // 2 for d in q.dimension)) + return Quantity(math.sqrt(q.value), _dimension(d / 2 for d in q.dimension)) if name in {'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh'}: if q.dimension == SCALAR: if native_function: @@ -416,7 +433,7 @@ def calc(node: ast.AST) -> Quantity | TextValue: raise ExpressionError('Result must be finite') if isinstance(result, TextValue) and len(result.value) > MAX_TEXT_LENGTH: raise ExpressionError('Text result exceeds 65536 characters') - return result + return replace(result, stochastic=stochastic_expression(expression, parameters)) def _increment_unit( @@ -452,7 +469,7 @@ def resolve(name: str) -> None: return default_unit if dimension == ANGLE: return 'deg' - raise ExpressionError('Unsupported resulting dimension') + return dimension_unit(dimension, default_unit) def _fold_assignments( @@ -472,8 +489,18 @@ def _fold_assignments( def numeric_literal(name: str, expression: str) -> str: if not _NUMERIC.fullmatch(expression): return expression + if name in existing and isinstance(existing[name], TextValue): + raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) # A scalar's empty display unit is meaningful, not a missing length unit. - unit = 'n' if name in existing and existing[name].dimension == SCALAR else default_unit + unit = ( + ( + 'n' + if existing[name].dimension == SCALAR + else units.get(name) or dimension_unit(existing[name].dimension, default_unit) + ) + if name in existing + else default_unit + ) return expression + ' ' + unit initial: dict[str, str] = {} @@ -503,7 +530,9 @@ def numeric_literal(name: str, expression: str) -> str: if operator in '+-' and _NUMERIC.fullmatch(expression): unit = units.get(name) if unit is None: - unit = _increment_unit(previous, {**initial, **raw}, existing, default_unit) + unit = _increment_unit( + previous, {**initial, **raw}, existing, default_unit, native_function + ) if unit: expression += ' ' + unit expression = replacement + operator + '(' + expression + ')' @@ -557,8 +586,6 @@ def visit(name: str) -> None: text_size += len(value.value) if text_size > MAX_BATCH_TEXT_LENGTH: raise ExpressionError('Combined text results exceed 262144 characters') - if value.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported resulting dimension') if name in existing and existing[name].dimension != value.dimension: raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) fusion_expr = map_code(expr, lambda code: re.sub(r'(?<=\d)\s*n\b', '', code)) @@ -566,7 +593,13 @@ def visit(name: str) -> None: default_unit if value.dimension == LENGTH else ( - 'deg' if value.dimension == ANGLE else ('Text' if value.dimension == TEXT else '') + 'deg' + if value.dimension == ANGLE + else ( + 'Text' + if value.dimension == TEXT + else dimension_unit(value.dimension, default_unit) + ) ) ) if name not in existing: @@ -707,13 +740,11 @@ def plan_batch_snapshot( original_exprs: dict[str, str] = {} original_graph: dict[str, set[str]] = {} for name, item in snapshot.items(): - if item.quantity.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported snapshot dimension: ' + name) if isinstance(item.quantity, TextValue): if item.unit != 'Text': raise ExpressionError('Invalid text snapshot unit: ' + name) elif item.unit: - if item.unit not in UNITS or UNITS[item.unit][1] != item.quantity.dimension: + if unit_definition(item.unit)[1] != item.quantity.dimension: raise ExpressionError('Unsupported snapshot unit: ' + name) elif item.quantity.dimension != SCALAR: raise ExpressionError('Missing snapshot unit: ' + name) @@ -742,7 +773,9 @@ def plan_batch_snapshot( else not isinstance(expected, TextValue) and math.isclose(actual.value, expected.value, rel_tol=1e-10, abs_tol=1e-10) ) - if actual.dimension != expected.dimension or not same_value: + if actual.dimension != expected.dimension or ( + not same_value and not actual.stochastic and not expected.stochastic + ): raise ExpressionError('Inconsistent snapshot value: ' + name) raw = _fold_assignments( text, @@ -781,8 +814,6 @@ def visit(name: str) -> None: if name in raw: expr = raw[name] value = evaluate(expr, values, native_function) - if value.dimension not in (SCALAR, LENGTH, ANGLE, TEXT): - raise ExpressionError('Unsupported resulting dimension') if name in snapshot and snapshot[name].quantity.dimension != value.dimension: raise ExpressionError('Cannot change existing parameter type or dimension: ' + name) unit = ( @@ -791,7 +822,11 @@ def visit(name: str) -> None: else ( 'deg' if value.dimension == ANGLE - else ('Text' if value.dimension == TEXT else '') + else ( + 'Text' + if value.dimension == TEXT + else dimension_unit(value.dimension, default_unit) + ) ) ) if name not in snapshot: diff --git a/Paramood/core/text.py b/Paramood/core/text.py index 6f28b17..9f8e8a5 100644 --- a/Paramood/core/text.py +++ b/Paramood/core/text.py @@ -1,7 +1,40 @@ """Quote-aware Fusion string syntax; never evaluate arbitrary Python input.""" +def normalize_unit_symbols(source): + """Quotes following a number are native inch/foot suffixes, not text starts.""" + result, quote, escaped, previous = [], '', False, '' + for i, char in enumerate(source): + if quote: + result.append(char) + if escaped: + escaped = False + elif char == '\\': + escaped = True + elif char == quote: + quote = '' + previous = char + continue + if char in '\'"' and previous and (previous.isdigit() or previous == ')'): + result.append(' in' if char == '"' else ' ft') + elif char in '\'"': + quote = char + result.append(char) + elif ( + char in 'µ°' + and not (i + 1 < len(source) and source[i + 1].isalpha()) + and not previous.isalpha() + ): + result.append(' micron' if char == 'µ' else ' deg') + else: + result.append(char) + if not char.isspace(): + previous = char + return ''.join(result) + + def segments(source): + source = normalize_unit_symbols(source) start, i = 0, 0 while i < len(source): if source[i] not in '\'"': diff --git a/Paramood/fusion/adapter.py b/Paramood/fusion/adapter.py index 6d08b53..f09df43 100644 --- a/Paramood/fusion/adapter.py +++ b/Paramood/fusion/adapter.py @@ -5,21 +5,22 @@ import math import re import uuid +from dataclasses import replace import adsk.core import adsk.fusion from ..core.expressions import ( ANGLE, - ExpressionError, LENGTH, SCALAR, TextValue, - UNITS, ParameterSnapshot, Quantity, dependency_closure, dimension_unit, + unit_definition, + mark_stochastic, evaluate, normalize_expression, plan_batch, @@ -29,6 +30,7 @@ from ..bridge.protocol import BridgeError from ..bridge.formatting import display_quantity from .variants import VariantsMixin +from .expression_engine import NativeExpressionEngine from ..core.text import map_code, normalize_literals, split_definitions, text_literal @@ -52,19 +54,14 @@ def _value_expression(parameter): raise ValueError('Non-finite parameter value') if not unit: return repr(value) + 'n' - if unit in UNITS and UNITS[unit][1] == LENGTH: - base = value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE: - base = value - else: - raise ValueError('Unsupported parameter unit') - factor = UNITS[unit][0] - converted = base / factor + factor, dimension, offset = unit_definition(unit) + base = value * 10.0 ** dimension[0] + converted = (base - offset) / factor if not math.isfinite(converted): raise ValueError('Non-finite converted value') if abs(converted) < 1e12: integer = round(converted) - if integer * factor == base: + if integer * factor + offset == base: converted = float(integer) return repr(converted) + ' ' + unit @@ -145,21 +142,27 @@ def _quantities(self, design): for parameter in design.allParameters: if _is_text(parameter): result[parameter.name] = TextValue(parameter.textValue) - continue - unit = parameter.unit - if unit not in UNITS and unit != '': - continue - try: - value = parameter.value - if unit == '': - result[parameter.name] = Quantity(value, SCALAR) - elif UNITS[unit][1] == LENGTH: - result[parameter.name] = Quantity(value * 10.0, LENGTH) - elif UNITS[unit][1] == ANGLE: - result[parameter.name] = Quantity(value, ANGLE) - except Exception: - continue - return result + else: + _, dimension, _ = unit_definition(parameter.unit) + result[parameter.name] = Quantity(parameter.value * 10.0 ** dimension[0], dimension) + dynamic = self._stochastic_parameters(design) + return {name: replace(value, stochastic=name in dynamic) for name, value in result.items()} + + @staticmethod + def _stochastic_parameters(design): + expressions = {p.name: p.expression for p in design.allParameters} + dependencies = {} + for parameter in design.allParameters: + # API dependency lists are preferred. Lightweight offline fixtures have none. + native = getattr(parameter, 'dependencyParameters', None) + if native is not None: + dependencies[parameter.name] = {p.name for p in native} + else: + dependencies[parameter.name] = ( + set(re.findall(r'[A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9]*', parameter.expression)) + & expressions.keys() + ) + return mark_stochastic(expressions, dependencies) def _dependency_snapshot(self, design): """Read a strict graph for future update planning; it does not authorize writes.""" @@ -199,14 +202,9 @@ def names_of(items): dimension, value = 'text', parameter.textValue if not isinstance(value, str) or unit != 'Text': raise ValueError('Invalid text parameter: ' + name) - elif unit == '': - dimension, value = SCALAR, parameter.value - elif unit in UNITS and UNITS[unit][1] == LENGTH: - dimension, value = LENGTH, parameter.value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE: - dimension, value = ANGLE, parameter.value else: - raise ValueError('Unsupported parameter unit: ' + name) + _, dimension, _ = unit_definition(unit) + value = parameter.value * 10.0 ** dimension[0] if not text and not math.isfinite(value): raise ValueError('Non-finite parameter value: ' + name) dependencies[name] = names_of(parameter.dependencyParameters) @@ -227,7 +225,11 @@ def names_of(items): for name in by_name: if actual_dependents[name] != expected_dependents[name]: raise ValueError('Inconsistent parameter dependency graph: ' + name) - return result + dynamic = self._stochastic_parameters(design) + return { + name: replace(item, quantity=replace(item.quantity, stochastic=name in dynamic)) + for name, item in result.items() + } except Exception as exc: raise BridgeError( 'INCOMPLETE_SNAPSHOT', 'Cannot verify parameter dependencies: ' + str(exc) @@ -371,6 +373,7 @@ def state(self): def _snapshot_token(self, design, text): parameters = [] + dynamic = self._stochastic_parameters(design) for param in design.allParameters: try: value = param.textValue if _is_text(param) else param.value @@ -378,7 +381,9 @@ def _snapshot_token(self, design, text): value = ( None # Text/unsupported parameter, expression is still part of the snapshot. ) - parameters.append((param.name, param.expression, param.unit, value)) + parameters.append( + (param.name, param.expression, param.unit, None if param.name in dynamic else value) + ) payload = [ self._document_id, text, @@ -405,12 +410,45 @@ def _editable_parameter(self, design, name): else None ) + def _normalize_unit_aliases(self, text, design): + if not callable(getattr(design.fusionUnitsManager, 'formatUnits', None)): + return text + known = {p.name for p in design.allParameters} + known.update( + match.group(1).replace('.', '_') + for match in re.finditer( + r'(?:^|[;\n])\s*([A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9.]*)\s*[+*/-]?=', text + ) + ) + cache = {} + + def normalize(code): + def alias(match): + name = match.group() + if name in known or name[0].isdigit() or name.startswith('.') or '.' in name: + return name + if name not in cache: + try: + cache[name] = design.fusionUnitsManager.formatUnits(name) or name + except RuntimeError: + cache[name] = name + return cache[name] + + return re.sub( + r'(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|[A-Za-zА-Яа-яЁё_][A-Za-zА-Яа-яЁё_0-9]*(?:\.[A-Za-zА-Яа-яЁё_0-9]+)*', + alias, + code, + ) + + return map_code(text, normalize) + def _plan(self, text): design = self._design() if not design: raise BridgeError('NO_DESIGN', 'Open a Fusion design first.') if not isinstance(text, str) or len(text) > 10000: raise BridgeError('BAD_REQUEST', 'Expression is missing or too long.') + text = self._normalize_unit_aliases(text, design) names = [ re.sub(r'[+*/-]$', '', item.split('=', 1)[0].strip()).strip().replace('.', '_') for item in split_definitions(text) @@ -523,44 +561,39 @@ def _execute_new_batch(self, design, ops): return {'created': created_names, 'updated': updated_names} def _native_function(self, design): - """Evaluate preference-sensitive calls only on an explicit bridge request.""" - - def evaluate_call(name, arguments, dimension): - # This callback currently handles unitless arguments and unitless results. - # It never creates temporary parameters or changes the active document. - if dimension != SCALAR or any(arg.dimension != SCALAR for arg in arguments): - raise ExpressionError('Unsupported native function dimension') - expression = name + '(' + ';'.join(format(arg.value, '.17g') for arg in arguments) + ')' - manager = design.fusionUnitsManager - if not manager.isValidExpression(expression, ''): - raise ExpressionError('Fusion cannot validate function: ' + name) - value = manager.evaluateExpression(expression, '') - if not math.isfinite(value): - raise ExpressionError('Fusion returned a non-finite result') - return Quantity(value, dimension) - - return evaluate_call + manager = design.fusionUnitsManager + return ( + NativeExpressionEngine(manager, self._quantities(design)) + if hasattr(manager, 'evaluateExpression') + else None + ) def _verify_value(self, parameter, op): unit = parameter.unit if isinstance(op.quantity, TextValue): - if not _is_text(parameter) or parameter.textValue != op.quantity.value: + if not _is_text(parameter) or ( + not op.quantity.stochastic and parameter.textValue != op.quantity.value + ): raise BridgeError( 'RESULT_MISMATCH', 'Fusion text differs from preview for ' + op.name + '.' ) return if _is_text(parameter): raise BridgeError('TYPE_MISMATCH', 'Cannot assign a number to a text parameter.') - if unit == '' and op.quantity.dimension == SCALAR: - actual = parameter.value - elif unit in UNITS and UNITS[unit][1] == LENGTH and op.quantity.dimension == LENGTH: - actual = parameter.value * 10.0 - elif unit in UNITS and UNITS[unit][1] == ANGLE and op.quantity.dimension == ANGLE: - actual = parameter.value - else: + _, dimension, _ = unit_definition(unit) + if dimension != op.quantity.dimension: raise BridgeError( 'RESULT_MISMATCH', 'Fusion returned an unexpected unit for ' + op.name + '.' ) + actual = parameter.value * 10.0 ** dimension[0] + if not math.isfinite(actual): + raise BridgeError( + 'RESULT_MISMATCH', 'Fusion returned a non-finite value for ' + op.name + '.' + ) + if op.quantity.stochastic: + # The expression and type are verified; the sampled preview value is + # deliberately not compared with a fresh native random evaluation. + return if not math.isclose(actual, op.quantity.value, rel_tol=1e-9, abs_tol=1e-9): raise BridgeError( 'RESULT_MISMATCH', 'Fusion result differs from preview for ' + op.name + '.' @@ -787,6 +820,10 @@ def request(self, action, payload): 'value': display_quantity(op.quantity, op.unit), 'unit': op.unit, 'status': op.status, + 'estimated': op.quantity.stochastic, + 'message': 'Sample value. Fusion will evaluate this expression again when applied.' + if op.quantity.stochastic + else None, } for op in ops ], @@ -818,6 +855,10 @@ def request(self, action, payload): 'value': display_quantity(op.quantity, op.unit), 'unit': op.unit, 'status': op.status, + 'estimated': op.quantity.stochastic, + 'message': 'Sample value. Fusion will evaluate this expression again when applied.' + if op.quantity.stochastic + else None, 'previous': str(display_quantity(op.previous, op.unit)) if op.previous else None, @@ -876,7 +917,9 @@ def request(self, action, payload): if not isinstance(expression, str) or len(expression) > 10000: raise BridgeError('BAD_REQUEST', 'Expression is missing or too long.') quantities = self._quantities(design) - expression = normalize_expression(expression, quantities) + expression = self._normalize_unit_aliases( + normalize_expression(expression, quantities), design + ) quantity = evaluate( expression, quantities, self._native_function(design), allow_nondeterministic=True ) @@ -891,9 +934,7 @@ def request(self, action, payload): dimension_unit(quantity.dimension, design.fusionUnitsManager.defaultLengthUnits), ) manager = design.fusionUnitsManager - if not manager.isValidExpression(expression, unit): - raise BridgeError('INVALID_EXPRESSION', 'Fusion cannot validate this expression.') - value = manager.evaluateExpression(expression, unit) + value = quantity.value / (10.0 ** quantity.dimension[0]) formatted = manager.formatValue(value, unit) if unit else str(value) display_value = ( value diff --git a/Paramood/fusion/expression_engine.py b/Paramood/fusion/expression_engine.py new file mode 100644 index 0000000..f5e0573 --- /dev/null +++ b/Paramood/fusion/expression_engine.py @@ -0,0 +1,211 @@ +"""Native numeric evaluation with symbolic unit inference and no model mutations. + +Fusion evaluates the original formula. The AST only supplies a result-unit hint +and safely substitutes values of parameters that have not been created yet. +""" + +import ast +import math +import re +from itertools import zip_longest + +from ..core.expressions import ( + ANGLE, + SCALAR, + TEXT, + Quantity, + TextValue, + ExpressionError, + _dimension, + _tree, + dimension_unit, + unit_definition, + stochastic_expression, + evaluate as offline_evaluate, +) +from ..core.text import map_code, text_literal + + +class NativeExpressionEngine: + def __init__(self, manager, baseline=None): + self.manager = manager + self.baseline = baseline or {} + + def __call__(self, name, arguments, dimension): + source = name + '(' + ';'.join(self.literal(arg) for arg in arguments) + ')' + return self.numeric(source, dimension) + + @staticmethod + def literal(quantity): + if isinstance(quantity, TextValue): + return text_literal(quantity.value) + unit = dimension_unit(quantity.dimension) + return '(' + format(quantity.value, '.17g') + (' ' + unit if unit else '') + ')' + + def numeric(self, source, dimension): + unit = dimension_unit(dimension) + if not self.manager.isValidExpression(source, unit): + raise ExpressionError('Fusion cannot validate this expression.') + value = self.manager.evaluateExpression(source, unit) + # Fusion's internal length is cm; the planner retains mm, including derived units. + value *= 10.0 ** dimension[0] + if not math.isfinite(value): + raise ExpressionError('Fusion returned a non-finite result') + return Quantity(value, dimension) + + def evaluate(self, expression, parameters): + tree = _tree(expression) + stochastic = stochastic_expression(expression, parameters) + + def render(node): + if isinstance(node, ast.Constant): + return text_literal(node.value) if isinstance(node.value, str) else repr(node.value) + if isinstance(node, ast.Name): + return ( + self.literal(parameters[node.id]) + if node.id in parameters and parameters[node.id] != self.baseline.get(node.id) + else node.id + ) + if isinstance(node, ast.UnaryOp): + op = {ast.USub: '-', ast.UAdd: '+', ast.Not: 'not '}.get(type(node.op)) + if op is not None: + return '(' + op + render(node.operand) + ')' + if isinstance(node, ast.BinOp): + op = { + ast.Add: '+', + ast.Sub: '-', + ast.Mult: '*', + ast.Div: '/', + ast.Pow: '^', + ast.Mod: '%', + }.get(type(node.op)) + if op: + return '(' + render(node.left) + op + render(node.right) + ')' + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and not node.keywords: + if node.func.id == '__unit__': + return '(' + render(node.args[0]) + ' ' + node.args[1].value + ')' + name = 'if' if node.func.id == '__if__' else node.func.id + return name + '(' + ';'.join(render(a) for a in node.args) + ')' + if isinstance(node, ast.Compare) and len(node.ops) == 1: + op = { + ast.Lt: '<', + ast.LtE: '<=', + ast.Gt: '>', + ast.GtE: '>=', + ast.Eq: '==', + ast.NotEq: '<>', + }.get(type(node.ops[0])) + if op: + return '(' + render(node.left) + op + render(node.comparators[0]) + ')' + if isinstance(node, ast.BoolOp): + op = ' and ' if isinstance(node.op, ast.And) else ' or ' + return '(' + op.join(render(a) for a in node.values) + ')' + raise ExpressionError('Invalid expression syntax') + + def dimension(node): + if isinstance(node, ast.Constant): + return TEXT if isinstance(node.value, str) else SCALAR + if isinstance(node, ast.Name): + if node.id in parameters: + return parameters[node.id].dimension + if node.id in {'PI', 'E'}: + return SCALAR + if node.id == 'Gravity': + return (1, 0, -2) + if node.id == 'SpeedOfLight': + return (1, 0, -1) + return unit_definition(self.manager.formatUnits(node.id))[1] + if isinstance(node, ast.UnaryOp): + return dimension(node.operand) + if isinstance(node, ast.BinOp): + a, b = dimension(node.left), dimension(node.right) + if TEXT in (a, b): + return TEXT + if isinstance(node.op, (ast.Add, ast.Sub, ast.Mod)): + return b if a == SCALAR else a + if isinstance(node.op, ast.Pow): + exponent = self.numeric(render(node.right), SCALAR).value + return _dimension(d * exponent for d in a) + sign = -1 if isinstance(node.op, ast.Div) else 1 + return _dimension(x + sign * y for x, y in zip_longest(a, b, fillvalue=0)) + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): + name = node.func.id + if name == '__unit__': + return unit_definition(node.args[1].value)[1] + if name in { + 'sin', + 'cos', + 'tan', + 'sinh', + 'cosh', + 'tanh', + 'sign', + 'random', + 'exp', + 'ln', + 'log', + }: + return SCALAR + if name in {'asin', 'acos', 'atan', 'asinh', 'acosh', 'atanh'}: + return ANGLE + if name == '__if__' and len(node.args) == 3: + branches = [] + for branch in node.args[1:]: + try: + branches.append(dimension(branch)) + except (ExpressionError, RuntimeError): + # Fusion may never evaluate this branch. The complete + # native formula is still validated below before sampling. + continue + return next((d for d in branches if d != SCALAR), SCALAR) + if name == 'pow' and len(node.args) == 2: + exponent = self.numeric(render(node.args[1]), SCALAR).value + return _dimension(d * exponent for d in dimension(node.args[0])) + if name == 'sqrt' and len(node.args) == 1: + return _dimension(d / 2 for d in dimension(node.args[0])) + if node.args: + dims = [dimension(a) for a in node.args] + return next((d for d in dims if d != SCALAR), SCALAR) + return SCALAR + return SCALAR + + inferred = dimension(tree.body) + if inferred == TEXT: + # Text references/concatenation are pure. Native numeric conditions are + # resolved lazily so an unused invalid numeric branch does not veto text. + def text_value(node): + if isinstance(node, ast.Name) and node.id in parameters: + return parameters[node.id] + if ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Name) + and node.func.id == '__if__' + ): + test = 'if(' + render(node.args[0]) + ';1;0)' + branch = node.args[1] if self.numeric(test, SCALAR).value else node.args[2] + return text_value(branch) + if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add): + return TextValue(text_value(node.left).value + text_value(node.right).value) + return offline_evaluate(render(node), {}) + + value = text_value(tree.body) + return TextValue(value.value, stochastic) + + # Preserve the original Fusion syntax and literal unit binding. Substitute + # only references, never function names, units, constants, or quoted text. + identifiers = re.compile(r'(? d.from === from); + const diagnostic = diagnostics.find((d) => d.from === from && d.severity !== 'info'); ranges.push( Decoration.mark({ class: `token-${kind}${diagnostic ? ' token-error' : ''}`, diff --git a/frontend/src/expression-syntax.test.ts b/frontend/src/expression-syntax.test.ts index 69b2549..2a119c9 100644 --- a/frontend/src/expression-syntax.test.ts +++ b/frontend/src/expression-syntax.test.ts @@ -30,3 +30,11 @@ it('preserves escaped quotes while scanning command delimiters', () => { "'a\\'this' + old", ); }); + +it('treats native inch and foot suffixes as units while preserving quoted text', () => { + expect(hasDefinitionSeparator('size=1";other=2')).toBe(true); + expect(hasDefinitionSeparator("size=1';other=2")).toBe(true); + expect(inString('size=1"')).toBe(false); + expect(inString("size=1'")).toBe(false); + expect(inString('caption="1')).toBe(true); +}); diff --git a/frontend/src/expression-syntax.ts b/frontend/src/expression-syntax.ts index 7aa86ea..d2db8d1 100644 --- a/frontend/src/expression-syntax.ts +++ b/frontend/src/expression-syntax.ts @@ -1,7 +1,9 @@ +import { expressionTokens, normalizeUnitSymbols } from '../../src/expression-language'; /** Separators and identifiers inside quoted values are content, not commands. */ export function mapExpressionCode(text: string, transform: (code: string) => string): string { - return text.replace(/'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|[^'"]+/gs, (part) => - part.startsWith("'") || part.startsWith('"') ? part : transform(part), + return normalizeUnitSymbols(text).replace( + /'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|[^'"]+/gs, + (part) => (part.startsWith("'") || part.startsWith('"') ? part : transform(part)), ); } export function hasAssignment(text: string): boolean { @@ -37,14 +39,6 @@ export function hasDefinitionSeparator(text: string): boolean { return found; } export function inString(text: string): boolean { - let quote = ''; - for (let i = 0; i < text.length; i++) { - if (quote && text[i] === '\\') { - i++; - continue; - } - if (quote === text[i]) quote = ''; - else if (!quote && (text[i] === "'" || text[i] === '"')) quote = text[i]; - } - return !!quote; + const last = expressionTokens(text).at(-1); + return !!last && last.kind === 'string' && !/^(['"])(?:\\.|(?!\1).)*\1$/s.test(last.value); } diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx index 35ab2f1..472fc12 100644 --- a/frontend/src/main.tsx +++ b/frontend/src/main.tsx @@ -683,11 +683,16 @@ export function App() {
{item.name} {item.expression} - {item.message && {item.message}} + {item.message && ( + + {item.message} + + )}
{item.previous ? `${item.previous} → ` : ''} + {item.estimated ? '≈ ' : ''} {item.value ?? ''} {item.unit ?? ''} {item.status} diff --git a/frontend/src/palettes.test.tsx b/frontend/src/palettes.test.tsx index fb8dfbc..87ef537 100644 --- a/frontend/src/palettes.test.tsx +++ b/frontend/src/palettes.test.tsx @@ -12,7 +12,7 @@ afterEach(() => { delete window.adsk; window.location.hash = ''; }); -function mount(hash: string) { +function mount(hash: string, sampledPreview = false) { window.location.hash = hash; const calls: { action: string; payload: Record }[] = []; window.adsk = { @@ -23,7 +23,24 @@ function mount(hash: string) { data.action === 'state.get' ? { documentId: 'doc', documentName: 'Design', parameters: [] } : data.action === 'parameters.preview' - ? { valid: false, items: [] } + ? sampledPreview + ? { + valid: true, + snapshotToken: 'sample-preview', + items: [ + { + name: 'roll', + expression: 'random()', + value: 0.25, + unit: '', + status: 'New', + estimated: true, + message: + 'Sample value. Fusion will evaluate this expression again when applied.', + }, + ], + } + : { valid: false, items: [] } : data.action === 'calculator.evaluate' ? { value: 5, formatted: '5' } : {}; @@ -869,3 +886,27 @@ it('waits for complete function calls before native Preview or Calculate request expect(calls.some((call) => call.action === 'parameters.preview')).toBe(true), ); }); + +it('shows sampled previews and applies the original random expression', async () => { + const calls = mount('#parameters', true); + await vi.waitFor(() => expect(host.textContent).toContain('Design')); + const editor = EditorView.findFromDOM(host.querySelector('.cm-content')!)!; + act(() => editor.dispatch({ changes: { from: 0, insert: 'roll=random()' } })); + await vi.waitFor(() => expect(host.textContent).toContain('≈ 0.25')); + expect(host.querySelector('.preview-note')?.textContent).toContain('Sample value'); + const apply = Array.from(host.querySelectorAll('button')).find((b) => + b.textContent?.includes('Apply parameters'), + )!; + expect(apply.disabled).toBe(false); + act(() => apply.click()); + await vi.waitFor(() => + expect( + calls.some( + (c) => + c.action === 'parameters.apply' && + c.payload.text === 'roll=random()' && + c.payload.snapshotToken === 'sample-preview', + ), + ).toBe(true), + ); +}); diff --git a/frontend/src/style.css b/frontend/src/style.css index 50f06a4..9910fa4 100644 --- a/frontend/src/style.css +++ b/frontend/src/style.css @@ -1202,3 +1202,23 @@ kbd { .expression-signature > div + div { margin-top: 4px; } + +.preview-note { + display: block; + color: var(--muted); + font-size: 10px; + white-space: normal; +} + +.preview-row > div:first-child { + min-width: 0; + flex: 1; +} +.preview-row > .row-end { + max-width: 52%; + white-space: normal; + overflow-wrap: anywhere; +} +.preview-row .formula { + overflow-wrap: anywhere; +} diff --git a/scripts/fusion_expression_parity.py b/scripts/fusion_expression_parity.py new file mode 100644 index 0000000..a58bcee --- /dev/null +++ b/scripts/fusion_expression_parity.py @@ -0,0 +1,160 @@ +"""Run in Autodesk Fusion: native expression acceptance in a disposable document. + +The script imports this checkout under an isolated package name, never restarts +an installed add-in, and closes only documents it created. Reports and F3D files +are written to the OS temporary directory, outside the repository. +""" + +import importlib +import importlib.util +import json +import math +import sys +import tempfile +import uuid +from pathlib import Path + + +def run(_context): + import adsk.core + import adsk.fusion + + root = Path(__file__).resolve().parents[1] / 'Paramood' + namespace = 'paramood_native_parity_' + uuid.uuid4().hex + spec = importlib.util.spec_from_file_location( + namespace, root / '__init__.py', submodule_search_locations=[str(root)] + ) + package = importlib.util.module_from_spec(spec) + sys.modules[namespace] = package + spec.loader.exec_module(package) + adapter_module = importlib.import_module(namespace + '.fusion.adapter') + catalog = importlib.import_module(namespace + '.core.builtins').CATALOG + dispatch = importlib.import_module(namespace + '.bridge.protocol').dispatch + app = adsk.core.Application.get() + original = app.activeDocument + document = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType) + document.name = 'Paramood Native Expression Acceptance' + report = {'version': app.version, 'units': [], 'functions': [], 'scenarios': []} + output = Path(tempfile.mkdtemp(prefix='paramood-native-parity-')) + try: + design = adsk.fusion.Design.cast(app.activeProduct) + assert design.userParameters.count == 0 + adapter = adapter_module.FusionAdapter(app) + adapter.in_direct_apply = True + + def apply(text): + count = design.userParameters.count + preview = adapter.request('parameters.preview', {'text': text}) + assert preview['valid'] and design.userParameters.count == count + result = adapter.request( + 'parameters.apply', {'text': text, 'snapshotToken': preview['snapshotToken']} + ) + return preview, result + + for i, unit in enumerate(catalog['units']): + apply('nativeUnit' + str(i) + '=1 ' + unit) + parameter = design.userParameters.itemByName('nativeUnit' + str(i)) + assert parameter and math.isfinite(parameter.value) + report['units'].append(unit) + for function in catalog['functions']: + preview, _ = apply('nativeFunction_' + function['name'] + '=' + function['example']) + assert preview['items'][0]['estimated'] == (function['name'] == 'random') + report['functions'].append(function['name']) + cases = [ + 'areaValue=10 mm*20 mm', + 'volumeValue=2 cm*3 cm*4 cm', + 'cubeRoot=pow(8 mm^3;1/3)', + 'massValue=5 kg', + 'forceValue=2 N', + 'pressureValue=4 Pa', + 'heatValue=-20 C', + 'speedValue=SpeedOfLight', + 'accelerationValue=Gravity', + 'lazyValue=if(1<2;2;1/0)', + 'mixedValue=if(1<2;1 mm;2)', + 'roundedLength=floor(1 mm)', + 'positiveLength=sign(1 mm)', + "caption='Body'", + 'captionCopy=caption', + "caption=caption+' X'", + 'aliasValue=2 centimeter', + 'inchValue=1"', + "footValue=1'", + 'micronValue=2 µ', + 'degreeValue=30 °', + 'randomValue=random();derivedRandom=randomValue*10 mm', + 'randomValue=random()', + 'massValue+=2', + 'areaValue=this*2', + ] + for text in cases: + apply(text) + report['scenarios'].append(text) + random_parameter = design.userParameters.itemByName('randomValue') + assert random_parameter.expression == 'random()' + preview = adapter.request('parameters.preview', {'text': 'randomValue=random()'}) + random_parameter.expression = 'random()' + adapter.request( + 'parameters.apply', + {'text': 'randomValue=random()', 'snapshotToken': preview['snapshotToken']}, + ) + report['random_recompute_keeps_preview_valid'] = True + stale = adapter.request('parameters.preview', {'text': 'areaValue=this*2'}) + design.userParameters.itemByName('areaValue').expression = '500 mm^2' + response = dispatch( + adapter, + { + 'requestId': 'stale-parity', + 'documentId': adapter.document_id, + 'action': 'parameters.apply', + 'payload': {'text': 'areaValue=this*2', 'snapshotToken': stale['snapshotToken']}, + }, + ) + assert not response['ok'] and response['error']['code'] == 'STALE_PREVIEW' + report['deterministic_edit_invalidates_preview'] = True + state = adapter._variants_state(design) + saved = adapter.request( + 'variants.save', + { + 'name': 'Native random', + 'parameters': [adapter._variant_entry(random_parameter)], + 'variantsToken': state['variantsToken'], + 'snapshotToken': state['variantSnapshotToken'], + }, + ) + variant_id = saved['activeVariantId'] + random_parameter.expression = 'random()' + assert adapter._variants_state(design)['activeVariantId'] == variant_id + archive = output / 'parity.f3d' + assert design.exportManager.execute( + design.exportManager.createFusionArchiveExportOptions(str(archive)) + ) + expected = {p.name: (p.expression, p.unit) for p in design.userParameters} + document.close(False) + document = app.importManager.importToNewDocument( + app.importManager.createFusionArchiveImportOptions(str(archive)) + ) + design = adsk.fusion.Design.cast(app.activeProduct) + assert {p.name: (p.expression, p.unit) for p in design.userParameters} == expected + reopened = adapter_module.FusionAdapter(app) + assert reopened._variants_state(design)['activeVariantId'] == variant_id + report['archive_parameters'] = len(expected) + report['random_variant_survives_recompute_and_reopen'] = True + report['passed'] = True + (output / 'report.json').write_text(json.dumps(report, indent=2), encoding='utf-8') + print( + json.dumps( + { + 'report': str(output / 'report.json'), + 'units': len(report['units']), + 'functions': len(report['functions']), + 'scenarios': len(report['scenarios']), + 'passed': True, + } + ) + ) + finally: + if document and document.isValid: + document.close(False) + if original and original.isValid: + original.activate() diff --git a/src/expression-catalog.ts b/src/expression-catalog.ts index a11cf45..18d8be4 100644 --- a/src/expression-catalog.ts +++ b/src/expression-catalog.ts @@ -19,7 +19,11 @@ export interface FusionFunction { } export const fusionFunctions: readonly FusionFunction[] = catalog.functions; export const fusionConstants = catalog.constants; -export const fusionUnits: readonly string[] = [...catalog.units, 'n']; +export const fusionUnits: readonly string[] = [ + ...catalog.units, + ...catalog.unitDefinitions.flatMap((unit) => unit.aliases), + 'n', +]; export const fusionOperators = catalog.operators; export const functionByName = new Map(fusionFunctions.map((f) => [f.name, f])); export const constantByName = new Map(fusionConstants.map((c) => [c.name, c])); diff --git a/src/expression-language.ts b/src/expression-language.ts index 1cc859a..f60f6b8 100644 --- a/src/expression-language.ts +++ b/src/expression-language.ts @@ -14,20 +14,58 @@ export interface ExpressionToken { /** One lexer for incomplete editor text, including scientific notation and escaped strings. */ export function expressionTokens(text: string): ExpressionToken[] { const pattern = - /'(?:\\.|[^'\\])*'?|"(?:\\.|[^"\\])*"?|(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|[A-Za-zА-Яа-яЁё_µ°][A-Za-zА-Яа-яЁё_0-9µ°]*(?:\.[A-Za-zА-Яа-яЁё0-9_]+)*|>=|<=|==|<>|!=|[^\s]/g; - return Array.from(text.matchAll(pattern), (m) => ({ - value: m[0], - from: m.index!, - to: m.index! + m[0].length, - kind: /^['"]/.test(m[0]) - ? 'string' - : /^(?:\d|\.\d)/.test(m[0]) - ? 'number' - : /^[A-Za-zА-Яа-яЁё_µ°]/.test(m[0]) - ? 'identifier' - : 'operator', - })); + /'(?:\\.|[^'\\])*'?|"(?:\\.|[^"\\])*"?|(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|[A-Za-zА-Яа-яЁё_µ°][A-Za-zА-Яа-яЁё_0-9µ°]*(?:\.[A-Za-zА-Яа-яЁё0-9_]+)*|>=|<=|==|<>|!=|[^\s]/y; + const result: ExpressionToken[] = []; + for (let i = 0; i < text.length;) { + if (/\s/.test(text[i])) { + i++; + continue; + } + const previous = result.at(-1); + const symbols: Record = { '"': 'in', "'": 'ft', µ: 'micron', '°': 'deg' }; + if (symbols[text[i]] && (previous?.kind === 'number' || previous?.value === ')')) { + result.push({ value: symbols[text[i]], from: i, to: i + 1, kind: 'identifier' }); + i++; + continue; + } + pattern.lastIndex = i; + const match = pattern.exec(text); + if (!match) { + i++; + continue; + } + const value = match[0]; + result.push({ + value, + from: i, + to: i + value.length, + kind: /^['"]/.test(value) + ? 'string' + : /^(?:\d|\.\d)/.test(value) + ? 'number' + : /^[A-Za-zА-Яа-яЁё_µ°]/.test(value) + ? 'identifier' + : 'operator', + }); + i += value.length; + } + return result; +} +export function normalizeUnitSymbols(text: string): string { + let result = '', + start = 0; + for (const token of expressionTokens(text)) { + result += + text.slice(start, token.from) + + (token.kind === 'identifier' && + ['"', "'", 'µ', '°'].includes(text.slice(token.from, token.to)) + ? ' ' + token.value + : text.slice(token.from, token.to)); + start = token.to; + } + return result + text.slice(start); } + export function statementStart(text: string, pos: number): number { let depth = 0, start = 0; @@ -69,6 +107,7 @@ export interface ExpressionDiagnostic { to: number; message: string; code: string; + severity?: 'error' | 'info'; } /** Diagnose only proven errors. Missing trailing tokens remain a normal editing state. */ export function expressionDiagnostics( @@ -138,10 +177,18 @@ export function expressionDiagnostics( ['min', 'max'].includes(fn.name) && dimensions[0] && dimensions[1] && - dimensions[0] !== dimensions[1] + dimensions[0] !== dimensions[1] && + !dimensions.includes('unitless') ) error(call.token, 'dimension', 'Arguments must have matching dimensions.'); - if (fn.name === 'if' && dimensions[1] && dimensions[2] && dimensions[1] !== dimensions[2]) + if ( + fn.name === 'if' && + dimensions[1] && + dimensions[2] && + dimensions[1] !== dimensions[2] && + dimensions[1] !== 'unitless' && + dimensions[2] !== 'unitless' + ) error( call.token, 'dimension', @@ -180,13 +227,15 @@ export function expressionDiagnostics( continue; // The final identifier may still be a prefix the user is typing. if (i === tokens.length - 1 && t.to === text.length) continue; - error( - t, - tokens[i - 1]?.kind === 'number' ? 'unit' : 'parameter', - tokens[i - 1]?.kind === 'number' - ? `Unknown unit: ${t.value}.` - : `Unknown parameter: ${t.value}.`, - ); + if (tokens[i - 1]?.kind === 'number') + result.push({ + from: t.from, + to: t.to, + code: 'unit', + severity: 'info', + message: `Fusion will validate this unit alias: ${t.value}.`, + }); + else error(t, 'parameter', `Unknown parameter: ${t.value}.`); } return result; } diff --git a/tests/integration/test_batch_adapter.py b/tests/integration/test_batch_adapter.py index ae612a6..b55cef1 100644 --- a/tests/integration/test_batch_adapter.py +++ b/tests/integration/test_batch_adapter.py @@ -1374,3 +1374,39 @@ def test_mixed_snapshot_checks_text_dependency_links(adapter_module): text.dependencyParameters = [numeric] with pytest.raises(adapter_module.BridgeError, match='Inconsistent'): adapter._dependency_snapshot(adapter._design()) + + +def test_stochastic_values_do_not_invalidate_snapshot_but_formulas_and_fixed_values_do( + adapter_module, +): + adapter, parameters = make_adapter(adapter_module) + parameters.entries.update( + { + 'roll': SimpleNamespace(name='roll', expression='random()', unit='', value=0.2), + 'span': SimpleNamespace(name='span', expression='roll*10 mm', unit='mm', value=0.2), + 'width': SimpleNamespace(name='width', expression='10 mm', unit='mm', value=1.0), + } + ) + design = adapter._design() + token = adapter._snapshot_token(design, 'width=20') + parameters.entries['roll'].value = 0.9 + parameters.entries['span'].value = 0.9 + assert adapter._snapshot_token(design, 'width=20') == token + parameters.entries['width'].value = 2.0 + assert adapter._snapshot_token(design, 'width=20') != token + token = adapter._snapshot_token(design, 'width=20') + parameters.entries['roll'].expression = 'random()*2' + assert adapter._snapshot_token(design, 'width=20') != token + + +def test_random_write_preserves_expression_and_checks_type_instead_of_sample_equality( + adapter_module, +): + adapter, parameters = make_adapter(adapter_module) + operations = adapter_module.plan_batch( + 'roll=random()', {}, 'mm', lambda *_: adapter_module.Quantity(0.25) + ) + assert operations[0].quantity.stochastic + adapter._execute_new_batch(adapter._design(), operations) + assert parameters.entries['roll'].expression == 'random()' + assert parameters.entries['roll'].value != operations[0].quantity.value diff --git a/tests/integration/test_variants_adapter.py b/tests/integration/test_variants_adapter.py index 9b418d0..387ced8 100644 --- a/tests/integration/test_variants_adapter.py +++ b/tests/integration/test_variants_adapter.py @@ -358,3 +358,14 @@ def test_variant_identity_survives_compatible_model_unit_changes(model): assert not adapter._variants_state(design)['variants'][0]['parameters'][0]['missing'] params[0].unit = 'deg' assert adapter._variants_state(design)['variants'][0]['parameters'][0]['missing'] + + +def test_random_variant_matches_its_expression_after_recompute(model): + adapter, design, params, _ = model + params[0].expression = 'random()*10 mm' + entry = adapter._variant_entry(params[0]) + variant = {'id': 'sample', 'name': 'Random', 'parameters': [entry]} + params[0].value = 123.0 + assert adapter._variant_matches(design, variant) + params[0].expression = 'random()*20 mm' + assert not adapter._variant_matches(design, variant) diff --git a/tests/intellisense.test.mjs b/tests/intellisense.test.mjs index 5cc56d7..cec7e94 100644 --- a/tests/intellisense.test.mjs +++ b/tests/intellisense.test.mjs @@ -197,3 +197,24 @@ test('native preview waits for complete expressions', () => { for (const input of ['probe=pow(2;3)', 'random()', 'PI', 'min(1;2)', '2+3']) assert.equal(incompleteExpression(input), false, input); }); + +test('native quote and symbol units do not start text literals', () => { + for (const [source, unit] of [ + ['1"', 'in'], + ["1'", 'ft'], + ['2 µ', 'micron'], + ['30 °', 'deg'], + ]) { + const tokens = expressionTokens(source); + assert.equal(tokens.at(-1).value, unit); + assert.equal(tokens.at(-1).kind, 'identifier'); + assert.equal(incompleteExpression(source), false); + } + assert.equal(expressionTokens(JSON.stringify('1"'))[0].kind, 'string'); + assert.equal( + expressionDiagnostics('2 centimeter + 1 cm', new Map()).some((d) => d.severity !== 'info'), + false, + ); + assert.deepEqual(expressionDiagnostics('if(1<2;1 mm;2)', new Map()), []); + assert.deepEqual(expressionDiagnostics('floor(1 mm)', new Map()), []); +}); diff --git a/tests/unit/test_expression_builtins.py b/tests/unit/test_expression_builtins.py index 525903e..6859150 100644 --- a/tests/unit/test_expression_builtins.py +++ b/tests/unit/test_expression_builtins.py @@ -9,7 +9,6 @@ SCALAR, ExpressionError, LocalEvaluationUnsupported, - NondeterministicExpression, Quantity, ParameterSnapshot, evaluate, @@ -34,7 +33,7 @@ def test_every_catalog_function(function): for arg in function['arguments']: assert arg['name'] and arg['type'] and arg['dimension'] and arg['description'] if function['name'] == 'random': - with pytest.raises(NondeterministicExpression): + with pytest.raises(LocalEvaluationUnsupported): evaluate(function['example'], {}) else: result = evaluate(function['example'], {}) @@ -130,9 +129,9 @@ def test_invalid_expression_rejected(expression): @pytest.mark.parametrize('expression', ['random()', 'if(1<2;2;random())']) -def test_random_never_becomes_a_stable_plan(expression): +def test_random_requires_a_native_evaluator_offline(expression): for planner in (plan_batch, plan_batch_snapshot): - with pytest.raises(NondeterministicExpression): + with pytest.raises(LocalEvaluationUnsupported): planner('probe=' + expression, {}, 'mm') @@ -148,8 +147,7 @@ def native(name, args, dimension): assert evaluate('sin(30)', {}, native).value == 0.5 assert calls == [('sin', [Quantity(30)], SCALAR)] assert evaluate('random()', {}, native, allow_nondeterministic=True).value == 0.5 - with pytest.raises(LocalEvaluationUnsupported): - evaluate('1 kg', {}) + assert evaluate('1 kg', {}).dimension == (0, 0, 0, 1) def test_unitless_parameter_and_scientific_notation_are_not_constants(): diff --git a/tests/unit/test_native_quantities.py b/tests/unit/test_native_quantities.py new file mode 100644 index 0000000..6d87d5e --- /dev/null +++ b/tests/unit/test_native_quantities.py @@ -0,0 +1,153 @@ +import math + +import pytest + +from Paramood.core.builtins import CATALOG +from Paramood.core.expressions import ( + LENGTH, + SCALAR, + ExpressionError, + ParameterSnapshot, + Quantity, + TextValue, + dimension_unit, + evaluate, + mark_stochastic, + plan_batch, + plan_batch_snapshot, + unit_definition, +) +from Paramood.bridge.formatting import display_quantity +from Paramood.fusion.expression_engine import NativeExpressionEngine + + +@pytest.mark.parametrize('unit', CATALOG['unitDefinitions'], ids=lambda u: u['name']) +def test_all_native_unit_metadata_and_compounds(unit): + factor, dimension, offset = unit_definition(unit['name']) + assert math.isfinite(factor) and factor > 0 + assert dimension == tuple(unit['dimension']) + assert offset == unit['offset'] + quantity = evaluate('1 ' + unit['name'], {}) + assert quantity.dimension == dimension + assert display_quantity(quantity, unit['name']) == pytest.approx(1) + canonical = dimension_unit(dimension) + assert unit_definition(canonical)[1] == dimension + for alias in unit['aliases']: + assert unit_definition(alias) == unit_definition(unit['name']) + + +@pytest.mark.parametrize( + ('text', 'value', 'unit'), + [ + ('area=10 mm*20 mm', 200, 'mm^2'), + ('root=pow(8 mm^3;1/3)', 2, 'mm'), + ('mass=5 kg', 5, 'kg'), + ('force=2 N', 2, 'N'), + ('pressure=3 Pa', 3, 'Pa'), + ('velocity=SpeedOfLight', 299792458000, 'mm/s'), + ('acceleration=Gravity', 9806.65, 'mm/s^2'), + ], +) +def test_creation_and_snapshot_updates_accept_derived_dimensions(text, value, unit): + op = plan_batch(text, {}, 'mm')[0] + assert op.unit == unit + assert display_quantity(op.quantity, unit) == pytest.approx(value) + snapshot = {op.name: ParameterSnapshot(op.expression, op.unit, op.quantity, set())} + result = plan_batch_snapshot(op.name + '=this*2', snapshot, 'mm')[0] + assert display_quantity(result.quantity, unit) == pytest.approx(value * 2) + + +class NativeManager: + """A strict callback fixture, not native Fusion acceptance.""" + + def __init__(self, results): + self.results = results + self.log = [] + + def isValidExpression(self, expression, unit): + self.log.append(('validate', expression, unit)) + return (expression, unit) in self.results + + def evaluateExpression(self, expression, unit): + self.log.append(('evaluate', expression, unit)) + return self.results[expression, unit] + + def formatUnits(self, unit): + return unit + + +def test_native_engine_evaluates_original_formula_once_and_preserves_references(): + manager = NativeManager({('box_size+2 mm', 'mm'): 1.2}) + baseline = {'box_size': Quantity(10, LENGTH)} + engine = NativeExpressionEngine(manager, baseline) + result = evaluate('box.size+2 mm', baseline, engine) + assert result == Quantity(12, LENGTH) + assert manager.log == [('validate', 'box_size+2 mm', 'mm'), ('evaluate', 'box_size+2 mm', 'mm')] + + +def test_native_fusion_controls_implicit_units_and_lazy_numeric_branches(): + manager = NativeManager({('if(1<2;1 mm;2)', 'mm'): 0.1, ('if(1<2;2;1/0)', ''): 2}) + engine = NativeExpressionEngine(manager) + assert evaluate('if(1<2;1 mm;2)', {}, engine) == Quantity(1, LENGTH) + assert evaluate('if(1<2;2;1/0)', {}, engine) == Quantity(2) + + +def test_random_creation_update_and_dependencies_are_sampled_without_rejection(): + samples = iter([0.2, 0.7, 0.9, 0.4, 0.3, 0.1, 0.8, 0.6, 0.5, 0.2, 0.1, 0.3, 0.4, 0.7, 0.9, 0.2]) + + class Sampler: + def __call__(self, name, args, dimension): + assert name == 'random' + return Quantity(next(samples)) + + native = Sampler() + ops = plan_batch('roll=random();span=roll*10 mm', {}, 'mm', native) + assert [op.quantity.stochastic for op in ops] == [True, True] + assert ops[0].expression == 'random()' and ops[1].expression == 'roll*10 mm' + snapshot = { + 'roll': ParameterSnapshot('random()', '', Quantity(0.8, SCALAR, True), set()), + 'span': ParameterSnapshot('roll*10 mm', 'mm', Quantity(8, LENGTH, True), {'roll'}), + } + updated = plan_batch_snapshot('roll=random()', snapshot, 'mm', native) + assert updated[0].quantity.stochastic and updated[0].expression == 'random()' + assert mark_stochastic( + {'roll': 'random()', 'span': 'roll*10 mm', 'fixed': '2'}, + {'roll': set(), 'span': {'roll'}, 'fixed': set()}, + ) == {'roll', 'span'} + + +def test_unselected_random_branch_is_still_labeled_as_a_sample(): + result = evaluate('if(1<2;2;random())', {}, lambda *_: Quantity(0.5)) + assert result.value == 2 and result.stochastic + + +def test_native_engine_refuses_an_expression_only_when_native_validation_fails(): + engine = NativeExpressionEngine(NativeManager({})) + with pytest.raises(ExpressionError, match='Fusion cannot validate'): + evaluate('1 mm+1 kg', {}, engine) + + +def test_affine_temperature_comes_from_original_native_expression(): + manager = NativeManager({('-20 C', 'K'): 253.15}) + result = evaluate('-20 C', {}, NativeExpressionEngine(manager)) + assert display_quantity(result, 'C') == pytest.approx(-20) + + +@pytest.mark.parametrize( + ('source', 'unit', 'value'), + [('1"', 'in', 1), ("1'", 'ft', 1), ('2 µ', 'micron', 2), ('30 °', 'deg', 30)], +) +def test_native_unit_symbols_preserve_strings_and_batch_boundaries(source, unit, value): + from Paramood.core.text import split_definitions, normalize_literals + + assert len(split_definitions('size=' + source + ';other=2')) == 2 + quantity = evaluate(source, {}) + assert display_quantity(quantity, unit) == pytest.approx(value) + assert normalize_literals("'1\\\"' ") == "'1\"' " + + +def test_native_text_references_and_concatenation_preserve_values(): + parameters = {'caption': TextValue('Body')} + engine = NativeExpressionEngine(NativeManager({}), parameters) + assert evaluate('caption', parameters, engine) == TextValue('Body') + assert evaluate("caption+' X'", parameters, engine) == TextValue('Body X')