diff --git a/README.md b/README.md index a6d895a..9713258 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ The editor retains AI generation/modification, saved models, JSON/XMILE import a Native engine improvements add bounded [arrays and reusable modules](docs/STRUCTURED-MODELS.md), [dimensional checks](docs/DIMENSIONAL-ANALYSIS.md), conservative [MDL import](docs/MDL-IMPORT.md), and [local feedback analysis](docs/FEEDBACK-ANALYSIS.md). Use **Source** to edit structured JSON without flattening the saved project. Inspect displays units evidence and can analyze a completed run. The [measured comparison](docs/ENGINE-COMPARISON.md) records a 32–35% improvement over our earlier scalar execution, while Simlin remains faster on those workloads. No Simlin runtime dependency was introduced. +The three previously recorded identifier/import failures are now repaired, with exact names preserved through import, simulation, rename and export. See the [identifier repair and compatibility evidence](docs/IDENTIFIER-IMPORT-FIX.md). + ## Run locally ```bash @@ -67,4 +69,4 @@ PYTHONPATH=. python scripts/check_mcp.py --stdio PYTHONPATH=. python scripts/audit_library.py --full --tracked-only --output operations/library-full.json ``` -Run the application before the MCP client check. Node.js is needed for JavaScript checks. API tests use disposable storage. Every active library model must pass; retired invalid inputs have separate causal-failure regression tests. Three existing XMILE identifier limitations remain expected failures. Offline pytest collection is restricted to tests/; root-level provider scripts are manual probes. Browser acceptance and alternate-engine/device limitations are recorded separately. Never synchronize model directories, databases, credentials or configuration over production. Reviewed per-file repairs and retirements require exact hashes and backups; preserve all other model data. +Run the application before the MCP client check. Node.js is needed for JavaScript checks. API tests use disposable storage. Every active library model must pass; retired invalid inputs have separate causal-failure regression tests. [Literal punctuation, EN SPACE and internal-dollar identifiers](docs/IDENTIFIER-IMPORT-FIX.md) now have passing identity/import/export regressions while unique legacy aliases remain compatible. Offline pytest collection is restricted to tests/; root-level provider scripts are manual probes. Browser acceptance and alternate-engine/device limitations are recorded separately. Never synchronize model directories, databases, credentials or configuration over production. Reviewed per-file repairs and retirements require exact hashes and backups; preserve all other model data. diff --git a/app/core/adaptive.py b/app/core/adaptive.py index 5310fd7..565088a 100644 --- a/app/core/adaptive.py +++ b/app/core/adaptive.py @@ -8,7 +8,7 @@ import ast from fractions import Fraction from scipy.integrate import solve_ivp -from app.core.equations import (EquationError, SafeEvaluator, canonical_name, +from app.core.equations import (EquationError, SafeEvaluator, canonical_name, symbol_identity, resolve_name, decode_identifier, preprocess_equation) from app.core.failure_diagnostics import capacity_error from app.core.model import SimulationResult @@ -19,7 +19,7 @@ class _UncertifiedBoundary(Exception): pass -def _matching_branch_limits(condition, yes, no, aliases): +def _matching_branch_limits(condition, yes, no, aliases, resolve_key=canonical_name): """A bounded algebraic proof, never a numerical sampling heuristic. Expand algebraic aliases, impose each comparison's equality boundary, and @@ -35,7 +35,7 @@ def expand(node, active=()): if budget[0] < 0 or len(active) > 80: raise _UncertifiedBoundary if isinstance(node, ast.Name): - name = canonical_name(decode_identifier(node.id)) + name = resolve_key(decode_identifier(node.id)) if name in aliases: if name in active: raise _UncertifiedBoundary @@ -229,15 +229,19 @@ def state_discontinuities(model): continuous; IF needs matching branch limits. This is a conservative supported syntax contract, not a general proof of existence, uniqueness or accuracy. """ - elements = {canonical_name(e.name): e for e in [*model.variables, *model.flows]} + declared = tuple(e.name for e in (*model.stocks, *model.flows, *model.variables)) + table_declarations = tuple(t.name for t in model.tables) + key = lambda name: symbol_identity(resolve_name(name, declared) or name) + table_key = lambda name: symbol_identity(resolve_name(name, table_declarations) or name) + elements = {symbol_identity(e.name): e for e in [*model.variables, *model.flows]} trees = {name: ast.parse(preprocess_equation(e.equation), mode="eval").body for name,e in elements.items() if e.equation.strip()} - stocks = {canonical_name(s.name) for s in model.stocks} - tables = {canonical_name(t.name): t.interpolation for t in model.tables} + stocks = {symbol_identity(s.name) for s in model.stocks} + tables = {symbol_identity(t.name): t.interpolation for t in model.tables} def references(node): functions = {id(n.func) for n in ast.walk(node) if isinstance(n, ast.Call)} - return {canonical_name(decode_identifier(n.id)) for n in ast.walk(node) + return {key(decode_identifier(n.id)) for n in ast.walk(node) if isinstance(n, ast.Name) and id(n) not in functions} state_names = set(stocks) @@ -245,7 +249,7 @@ def references(node): added = {name for name,tree in trees.items() if references(tree) & state_names} - state_names if not added: break state_names.update(added) - needed = {canonical_name(f.name) for f in model.flows if f.from_stock is not None or f.to_stock is not None} + needed = {symbol_identity(f.name) for f in model.flows if f.from_stock is not None or f.to_stock is not None} for _ in range(len(trees)+1): added = {ref for name in needed if name in trees for ref in references(trees[name]) if ref in trees} - needed if not added: break @@ -259,7 +263,7 @@ def references(node): condition, yes, no = (node.test, node.body, node.orelse) if isinstance(node, ast.IfExp) else (None,None,None) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): function = decode_identifier(node.func.id).upper() - if (canonical_name(function) in tables or function in {"LOOKUP", "WITH_LOOKUP"}) and references(node) & state_names: + if (table_key(function) in tables or function in {"LOOKUP", "WITH_LOOKUP"}) and references(node) & state_names: # Even continuous interpolation can hide an entire narrow # forcing pulse between internal steps. Table knots need # event localization before this RHS contract can expand. @@ -270,7 +274,7 @@ def references(node): findings.append({"element":elements[name].name,"operation":function}) if condition is not None: condition_nodes.update(id(n) for n in ast.walk(condition)) - if references(condition) & state_names and not _matching_branch_limits(condition,yes,no,trees): + if references(condition) & state_names and not _matching_branch_limits(condition,yes,no,trees,key): findings.append({"element":elements[name].name,"operation":"uncertified state-dependent conditional"}) for node in ast.walk(tree): if isinstance(node, (ast.Compare, ast.BoolOp)) and id(node) not in condition_nodes and references(node) & state_names: @@ -287,22 +291,26 @@ def time_discontinuities(model): feedback conditions retain their ordinary ODE meaning. This is a conservative input contract, not a proof that all accepted RHS functions are smooth. """ + declared = tuple(e.name for e in (*model.stocks, *model.flows, *model.variables)) + table_declarations = tuple(t.name for t in model.tables) + key = lambda name: symbol_identity(resolve_name(name, declared) or name) + table_key = lambda name: symbol_identity(resolve_name(name, table_declarations) or name) trees = {} - table_names = {canonical_name(t.name) for t in model.tables} + table_names = {symbol_identity(t.name) for t in model.tables} for element in [*model.variables, *model.flows]: if element.equation.strip(): trees[element.name] = ast.parse(preprocess_equation(element.equation), mode="eval") def references(node): function_nodes = {id(n.func) for n in ast.walk(node) if isinstance(n, ast.Call)} - return {canonical_name(decode_identifier(n.id)) for n in ast.walk(node) + return {key(decode_identifier(n.id)) for n in ast.walk(node) if isinstance(n, ast.Name) and id(n) not in function_nodes} time_names = {"time"} changed = True while changed: changed = False for name, tree in trees.items(): - if canonical_name(name) not in time_names and references(tree) & time_names: - time_names.add(canonical_name(name)) + if key(name) not in time_names and references(tree) & time_names: + time_names.add(key(name)) changed = True findings = [] for name, tree in trees.items(): @@ -315,7 +323,7 @@ def references(node): # do not constrain internal steps. Until table-knot events are # supported, reject explicit TIME-driven lookups rather than # returning a successful but silently missed forcing pulse. - if canonical_name(function) in table_names: + if table_key(function) in table_names: lookup_arguments = node.args elif function == "LOOKUP": lookup_arguments = node.args[1:] # table name is not an input @@ -325,7 +333,7 @@ def references(node): lookup_arguments = [] if any(references(argument) & time_names for argument in lookup_arguments): findings.append({"element": name, "operation": "time-dependent lookup", "function": function}) - if canonical_name(function) not in table_names: + if table_key(function) not in table_names: if function in {"IF", "IF_THEN_ELSE"} and node.args: condition = node.args[0] elif function in {"FLOOR", "CEIL", "ROUND", "SIGN", "MOD"} and references(node) & time_names: diff --git a/app/core/diagnostics.py b/app/core/diagnostics.py index c569324..6e0a13c 100644 --- a/app/core/diagnostics.py +++ b/app/core/diagnostics.py @@ -11,7 +11,7 @@ from collections import Counter from pathlib import Path -from app.core.equations import (SAFE_CONSTANTS, canonical_name, decode_identifier, +from app.core.equations import (SAFE_CONSTANTS, canonical_name, symbol_identity, resolve_name, decode_identifier, equation_features, get_equation_variables, preprocess_equation) from app.core.model import Model, TimeSettings from app.core.limits import MAX_INTERVALS, MAX_RETAINED_VALUES, MAX_WORK_UNITS @@ -41,7 +41,7 @@ def _finite(value): def equation_evidence(model): """Find unresolved names, syntax and sensitive operations without guessing fixes.""" elements = [*model.stocks, *model.flows, *model.variables] - originals = {canonical_name(e.name): e.name for e in elements} + originals = {symbol_identity(e.name): e.name for e in elements} defined = set(originals) | {canonical_name(n) for n in SAFE_CONSTANTS} | {"time", "dt", "time_step"} table_names = {canonical_name(t.name) for t in model.tables} missing, domains, syntax, functions = [], [], [], set() @@ -69,7 +69,10 @@ def equation_evidence(model): stochastic = stochastic or bool(equation_features(equation, [t.name for t in model.tables])["random_calls"]) except Exception as error: syntax.append({"element": element.name, "equation": equation, "error": str(error), "stage": "engine_feature_check"}) - absent = sorted(n for n in get_equation_variables(equation) if canonical_name(n) not in defined and canonical_name(n) not in table_names) + absent = sorted(n for n in get_equation_variables(equation) + if resolve_name(n, [e.name for e in elements]) is None + and symbol_identity(n) not in {*SAFE_CONSTANTS, "time", "dt", "time_step"} + and resolve_name(n, [t.name for t in model.tables]) is None) if absent: missing.append({"element": element.name, "equation": equation, "undefined_symbols": absent}) operations = sorted(calls & {"LOG", "LN", "LOG10", "SQRT", "ASIN", "ACOS", "POWER", "EXP"}) @@ -79,7 +82,7 @@ def equation_evidence(model): operations.append("power") if operations: domains.append({"element": element.name, "equation": equation, "operations_to_review": operations}) - duplicates = sorted(n for n, count in Counter(canonical_name(e.name) for e in elements).items() if count > 1) + duplicates = sorted(n for n, count in Counter(symbol_identity(e.name) for e in elements).items() if count > 1) return {"undefined_symbols": missing, "syntax": syntax, "ambiguous_names": duplicates, "domain_sensitive_equations": domains, "called_functions": sorted(functions), "stochastic": stochastic, diff --git a/app/core/equations.py b/app/core/equations.py index 0d11027..dd764ce 100644 --- a/app/core/equations.py +++ b/app/core/equations.py @@ -339,6 +339,8 @@ def __init__(self, time: float = 0.0, dt: float = 0.25, self.delay_state = delay_state if delay_state is not None else {} self.smooth_state = smooth_state if smooth_state is not None else {} self.table_types = {} + self.declared_symbols = None + self.random_symbol_keys = {} self.equation_key = "" self.random_runtime = random_runtime if random_runtime is not None else RandomRuntime() @@ -399,6 +401,41 @@ def _canonical_name(value): return re.sub(r"_+", "_", normalized) +@lru_cache(maxsize=8192) +def _symbol_identity(value): + import unicodedata + return re.sub(r"[ _\n\u00a0]+", "_", unicodedata.normalize("NFC", value).casefold()) + + +def symbol_identity(value): + """Semantic identity preserves punctuation and non-XMILE whitespace.""" + return _symbol_identity(value) if len(value) <= 1024 else _symbol_identity.__wrapped__(value) + + +def resolve_name(requested, declared): + """Bind against the complete declaration inventory, not available values.""" + if requested in declared: + return requested + identity = symbol_identity(requested) + matches = [name for name in declared if symbol_identity(name) == identity] + if not matches: + legacy = canonical_name(requested) + matches = [name for name in declared if canonical_name(name) == legacy] + if len(matches) > 1: + raise EquationError(f"Ambiguous variable alias: {requested}", code="name_collision") + return matches[0] if matches else None + + +def random_symbol_keys(names): + """Preserve historical streams; newly permitted alias buckets stay distinct.""" + buckets = {} + for name in names: + buckets.setdefault(canonical_name(name), []).append(name) + return {name: (key if len(group) == 1 else ":strict:" + symbol_identity(name).encode("utf-8").hex()) + for key, group in buckets.items() for name in group} + + + class SafeEvaluator(ast.NodeVisitor): """ Safely evaluate mathematical expressions using AST. @@ -427,19 +464,13 @@ def visit_Name(self, node: ast.Name) -> float: return _finite_real(self.context.time) if name_upper in ("DT", "TIME_STEP"): return _finite_real(self.context.dt) - # Then check user variables - try multiple name variations - # to handle underscore vs space naming inconsistencies - for var_name in name_variations: - if var_name in self.variables: - return _finite_real(self.variables[var_name]) - matched = [value for key, value in self.variables.items() if canonical_name(key) == canonical] - if len(matched) == 1: - return _finite_real(matched[0]) - if len(matched) > 1: - raise EquationError(f"Ambiguous variable alias: {name}") - # A declared but unresolved e/pi must wait for its initializer/equation. - # Otherwise dependency retries cache a mathematical constant as if it were - # the authored model quantity. Lazy branches remain evaluated on demand. + inventory = getattr(self.context, "declared_symbols", None) + owner = resolve_name(name, inventory if inventory is not None else self.variables) + if owner is not None: + if owner in self.variables: + return _finite_real(self.variables[owner]) + raise EquationError(f"Unknown variable: {name}", code="unknown_variable", + details={"symbol": name}) if canonical in getattr(self.context, "declared_names", ()): raise EquationError(f"Unknown variable: {name}", code="unknown_variable", details={"symbol": name}) @@ -540,7 +571,7 @@ def _random_value(self, function, node): raise EquationError("RANDOM_BINOMIAL requires integer trials between 0 and 1,000,000") if not 0 <= probability <= 1: raise EquationError("RANDOM_BINOMIAL probability must be between 0 and 1") - key = f"{canonical_name(self.context.equation_key)}:{node.lineno}:{node.col_offset}:binomial" + key = f"{self.context.random_symbol_keys.get(self.context.equation_key, canonical_name(self.context.equation_key))}:{node.lineno}:{node.col_offset}:binomial" return self.context.random_runtime.binomial_sample(key, int(trials), probability, seed, self.context.time) kind = "normal" if function in ("RANDOM_NORMAL", "NORMAL") else "uniform" first, second = parameters @@ -548,7 +579,7 @@ def _random_value(self, function, node): raise EquationError("Uniform minimum must not exceed maximum") if kind == "normal" and second < 0: raise EquationError("Normal standard deviation must be nonnegative") - key = f"{canonical_name(self.context.equation_key)}:{node.lineno}:{node.col_offset}:{kind}" + key = f"{self.context.random_symbol_keys.get(self.context.equation_key, canonical_name(self.context.equation_key))}:{node.lineno}:{node.col_offset}:{kind}" unit = self.context.random_runtime.unit_sample(key, kind, seed, self.context.time) # The convex form avoids max-min overflowing for finite opposite endpoints. if kind == "uniform": @@ -560,12 +591,10 @@ def _random_value(self, function, node): return value def _table_name(self, requested): - if requested in self.context.tables: - return requested - matches = [name for name in self.context.tables if canonical_name(name) == canonical_name(requested)] - if len(matches) > 1: - raise EquationError(f"Ambiguous lookup table alias: {requested}") - return matches[0] if matches else None + try: + return resolve_name(requested, self.context.tables) + except EquationError as exc: + raise EquationError(f"Ambiguous lookup table alias: {requested}") from exc def _handle_context_function(self, func_name: str, args: list) -> float: """Handle functions that require simulation context.""" @@ -815,7 +844,36 @@ def preprocess_equation(equation: str) -> str: import io import tokenize try: - tokens = [token.string for token in tokenize.generate_tokens(io.StringIO(_strip_comments(equation)).readline) + source = _strip_comments(equation) + lexical = list(tokenize.generate_tokens(io.StringIO(source).readline)) + offsets = [0] + for line in source.split(chr(10)): + offsets.append(offsets[-1] + len(line) + 1) + replacements = [] + consumed = -1 + for token in lexical: + if token.type != tokenize.NAME: + continue + start = offsets[token.start[0] - 1] + token.start[1] + if start < consumed: + continue + end = start + while end < len(source): + character = source[end] + if character == "$" or ("A" + character).isidentifier(): + end += 1 + else: + break + value = source[start:end] + if "$" in value: + # Quote only the physically contiguous identifier span. This + # also splits Python NUMBER tokens such as 1e+3 at the operator. + import json + replacements.append((start, end, json.dumps(value, ensure_ascii=False))) + consumed = end + for start, end, replacement in reversed(replacements): + source = source[:start] + replacement + source[end:] + tokens = [token.string for token in tokenize.generate_tokens(io.StringIO(source).readline) if token.type not in (tokenize.ENDMARKER, tokenize.NEWLINE, tokenize.NL, tokenize.INDENT, tokenize.DEDENT)] except (tokenize.TokenError, IndentationError) as exc: @@ -1100,7 +1158,7 @@ def equation_features(equation, table_names=()): tables = {canonical_name(name) for name in table_names} calls = [decode_identifier(node.func.id).upper() for node in ast.walk(tree) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) - and canonical_name(decode_identifier(node.func.id)) not in tables] + and resolve_name(decode_identifier(node.func.id), table_names) is None] return {"random_calls": sum(name in STOCHASTIC_FUNCTIONS for name in calls), "fixed_delay": any(name in ("DELAY", "DELAY_FIXED") for name in calls), "stateful_calls": sorted(set(calls) & STATEFUL_FUNCTIONS), @@ -1143,15 +1201,17 @@ def validate_equation(equation: str, available_variables: list[str], if node.keywords: return False, "Keyword arguments are unsupported" function = decode_identifier(node.func.id) - if function.upper() not in functions and canonical_name(function) not in tables: + if function.upper() not in functions and resolve_name(function, table_names or ()) is None: return False, f"Unknown function: {function}" - if function.upper() == "LOOKUP" and node.args: + if function.upper() == "LOOKUP" and resolve_name(function, table_names or ()) is None and node.args: argument = node.args[0] table = decode_identifier(argument.id) if isinstance(argument, ast.Name) else ( argument.value if isinstance(argument, ast.Constant) and isinstance(argument.value, str) else None) - if table is None or canonical_name(table) not in tables: + if table is None or resolve_name(table, table_names or ()) is None: return False, f"Unknown lookup table: {table}" - unknown = {name for name in _reference_names(tree) if canonical_name(name) not in known} + unknown = {name for name in _reference_names(tree) + if resolve_name(name, available_variables) is None + and symbol_identity(name) not in {*SAFE_CONSTANTS, "time", "dt", "time_step"}} if unknown: return False, f"Unknown variables: {', '.join(sorted(unknown))}" return True, "" diff --git a/app/core/failure_diagnostics.py b/app/core/failure_diagnostics.py index 7b5eb17..362d4e8 100644 --- a/app/core/failure_diagnostics.py +++ b/app/core/failure_diagnostics.py @@ -1,7 +1,7 @@ """Runtime failure causes without evaluating extra branches or repairing inputs.""" import math from pydantic import ValidationError -from app.core.equations import EquationError, canonical_name +from app.core.equations import EquationError, resolve_name def error_code(error): @@ -31,7 +31,7 @@ def stalled_evaluation(model, pending, errors, time, stocks): cycles are reported without assuming a solver or evaluating lazy branches. """ elements = {e.name: e for e in [*model.stocks, *model.variables, *model.flows]} - names = {canonical_name(name): name for name, _ in pending} + names = {name for name, _ in pending} records = {} edges = {} for name, equation in pending: @@ -42,8 +42,8 @@ def stalled_evaluation(model, pending, errors, time, stocks): records[name] = record symbol = record.get("symbol") if record["code"] == "unknown_variable" and symbol is not None: - dependency = names.get(canonical_name(symbol)) - if dependency is not None: + dependency = resolve_name(symbol, elements) + if dependency in names: edges[name] = dependency roots = [record for name, record in records.items() if name not in edges] cycles = {} diff --git a/app/core/feedback_analysis.py b/app/core/feedback_analysis.py index 5983ef4..8358fb1 100644 --- a/app/core/feedback_analysis.py +++ b/app/core/feedback_analysis.py @@ -1,7 +1,7 @@ """Opt-in local feedback response analysis, using the canonical scalar evaluator.""" import math import numpy as np -from app.core.equations import equation_features, EquationError, canonical_name +from app.core.equations import equation_features, EquationError, symbol_identity from app.core.integration import evaluate @@ -28,7 +28,7 @@ def analyze_feedback(model, result, sample_count=8, relative_step=1e-4): requirement='Caller must supply a trajectory from this exact model and effective time settings; an absent hash cannot establish that association.') numeric_model = model.model_copy(update={'variables':[v for v in model.variables if getattr(v,'role',None) != 'report']}) names = [s.name for s in model.stocks] - if len({canonical_name(e.name) for e in elements}) != len(elements): + if len({symbol_identity(e.name) for e in elements}) != len(elements): raise ValueError('Ambiguous element names cannot be analyzed') if model.time_settings.discrete_period is not None: raise ValueError('Continuous-time feedback analysis does not support discrete recurrences') diff --git a/app/core/integration.py b/app/core/integration.py index 9106dec..7a67b38 100644 --- a/app/core/integration.py +++ b/app/core/integration.py @@ -13,7 +13,7 @@ from app.core.execution_order import derive_execution_order from app.core.failure_diagnostics import stalled_evaluation, capacity_error from app.core.equations import (EquationError, safe_eval, SimulationContext, - discover_stateful, advance_stateful, canonical_name, + discover_stateful, advance_stateful, canonical_name, symbol_identity, random_symbol_keys, RandomRuntime, stateful_derivatives, equation_features) @@ -38,6 +38,8 @@ class _EvaluationPlan: equations: tuple initial_equations: tuple declared_names: frozenset + declared_symbols: tuple + random_keys: object tables: object table_types: object @@ -54,6 +56,8 @@ def _make_evaluation_plan(model): id(model), scheduled if scheduled is not None else equations, scheduled_initial if scheduled_initial is not None else initial_equations, frozenset(canonical_name(e.name) for e in (*model.stocks, *model.flows, *model.variables)), + tuple(e.name for e in (*model.stocks, *model.flows, *model.variables)), + MappingProxyType(random_symbol_keys(e.name for e in (*model.stocks, *model.flows, *model.variables))), MappingProxyType({table.name: tuple(tuple(point) for point in table.to_list()) for table in model.tables}), MappingProxyType({table.name: table.interpolation for table in model.tables}), ) @@ -69,6 +73,9 @@ def simulation_context(model, t, dt, delay, smooth, random_runtime=None, input_t # Declared e/pi must wait for authored initializers, never constant fallback. context.declared_names = plan.declared_names if plan else frozenset(canonical_name(element.name) for element in (*model.stocks, *model.flows, *model.variables)) + context.declared_symbols = plan.declared_symbols if plan else tuple( + e.name for e in (*model.stocks, *model.flows, *model.variables)) + context.random_symbol_keys = plan.random_keys if plan else random_symbol_keys(context.declared_symbols) context.table_types = plan.table_types if plan else {table.name: table.interpolation for table in model.tables} context.input_time = t if input_time is None else input_time context.input_snapshot = input_snapshot @@ -161,7 +168,7 @@ def integrate(model: Model, settings: TimeSettings): result = _integrate_numeric(numeric_model, settings, report_work) for variable, tree in reports: del result.values[variable.name] - tables = {canonical_name(t.name) for t in model.tables} + tables = tuple(t.name for t in model.tables) result.report_values = {variable.name: [] for variable, _ in reports} for index, at in enumerate(result.time) if reports else []: values = {name: series[index] for name, series in result.values.items()} @@ -187,9 +194,9 @@ def _integrate_numeric(model: Model, settings: TimeSettings, report_work=0): def _integrate_numeric_planned(model: Model, settings: TimeSettings, report_work=0): elements = [*model.stocks, *model.flows, *model.variables] - names = [canonical_name(e.name) for e in elements] + names = [symbol_identity(e.name) for e in elements] if len(set(names)) != len(names): - raise EquationError("Element names must be unique, including case/space/punctuation aliases", code="name_collision", details={"category": "invalid_structure", "conflicting_names": [e.name for e in elements if names.count(canonical_name(e.name)) > 1]}) + raise EquationError("Element names must be unique, including case and XMILE whitespace aliases", code="name_collision", details={"category": "invalid_structure", "conflicting_names": [e.name for e in elements if names.count(symbol_identity(e.name)) > 1]}) if any(name in {"time", "dt", "time_step"} for name in names): raise EquationError("TIME, DT and TIME_STEP are reserved simulation names", code="reserved_name", details={"category": "invalid_structure", "names": [e.name for e in elements if canonical_name(e.name) in {"time", "dt", "time_step"}]}) reference_owners = {} @@ -198,7 +205,7 @@ def _integrate_numeric_planned(model: Model, settings: TimeSettings, report_work if reference in reference_owners and reference_owners[reference] is not stock: raise EquationError(f"Ambiguous stock reference: '{reference}'") reference_owners[reference] = stock - if len({canonical_name(table.name) for table in model.tables}) != len(model.tables): + if len({symbol_identity(table.name) for table in model.tables}) != len(model.tables): raise EquationError("Lookup table names must be unique") if len(elements) > MAX_ELEMENTS: raise capacity_error("Models are limited to 500 elements per simulation", resource="elements", requested=len(elements), limit=MAX_ELEMENTS) diff --git a/app/core/mdl_parser.py b/app/core/mdl_parser.py index ec40cd0..0de8113 100644 --- a/app/core/mdl_parser.py +++ b/app/core/mdl_parser.py @@ -2,7 +2,7 @@ import json import math import re -from app.core.equations import canonical_name, validate_equation +from app.core.equations import canonical_name, symbol_identity, resolve_name, validate_equation MAX_SOURCE_BYTES = 2_000_000 MAX_RECORDS = 500 @@ -96,7 +96,7 @@ def warn(code, message, record=None): name=re.sub(r"\s+", " ", lhs).strip() if not name or any(c in name for c in "[]:~|=()\\") or name.startswith("*"): fail("Arrays, macros and non-scalar declarations are unsupported", record=number) - key=canonical_name(name) + key=symbol_identity(name) if key in {"time", "dt"}: fail("Variable name conflicts with reserved scalar execution symbol", "duplicate_name", number) if key in names: @@ -143,12 +143,18 @@ def numeric_control(key, default): "time_settings":{"start":start,"end":end,"dt":dt,"units":time_units,"integration_method":"euler"}} # Declared names are matched longest first, case-insensitively; spaces and # underscores share the engine's documented identifier alias convention. - model_names={canonical_name(e["name"]):e["name"] for e in entries} + model_names={symbol_identity(e["name"]):e["name"] for e in entries} candidates=dict(model_names) + # Retain only unambiguous legacy spellings; literal declarations win. + legacy = {} + for name in model_names.values(): + legacy.setdefault(canonical_name(name), []).append(name) + candidates.update({key: names[0] for key,names in legacy.items() + if len(names) == 1 and key not in candidates}) candidates.update({"time":"TIME","time_step":"DT","initial_time":str(start),"final_time":str(end)}) patterns=[] for key,name in candidates.items(): - pattern=r"[ _\s]+".join(re.escape(part) for part in key.split("_")) + pattern=r"[ _\n\u00a0]+".join(re.escape(part) for part in key.split("_")) patterns.append((re.compile(pattern,re.I),key,name)) patterns.sort(key=lambda x:len(x[1]),reverse=True) funcs={"IF THEN ELSE":"IF_THEN_ELSE", @@ -176,10 +182,10 @@ def translate(expr, record): if expr[pos]=='"': m=re.match(r'"(?:[^"\\]|\\.)*"',expr[pos:]) if not m:fail("Unterminated identifier", "invalid_syntax",record) - try:key=canonical_name(json.loads(m.group())) + try:owner=resolve_name(json.loads(m.group()), model_names.values()) except ValueError:fail("Invalid quoted identifier","invalid_syntax",record) - if key not in model_names:fail("Unknown quoted reference",record=record) - output.append(json.dumps(model_names[key]));pos+=len(m.group());continue + if owner is None:fail("Unknown quoted reference",record=record) + output.append(json.dumps(owner));pos+=len(m.group());continue number=re.match(NUMBER,expr[pos:]) # Signs are operators; retaining a signed numeric token is equivalent. if number: @@ -272,7 +278,7 @@ def split_args(body, record): expression=translate(rhs,record) model["variables"].append({"id":"mdl:"+name,"name":name,"equation":expression,"units":units}) all_names=[e["name"] for k in ("stocks","flows","variables") for e in model[k]] - canonical=[canonical_name(n) for n in all_names] + canonical=[symbol_identity(n) for n in all_names] if len(canonical)!=len(set(canonical)): fail("Generated net flow name conflicts with source variable","duplicate_name") for kind in ("stocks","flows","variables"): diff --git a/app/core/model.py b/app/core/model.py index 7253981..7979a76 100644 --- a/app/core/model.py +++ b/app/core/model.py @@ -362,29 +362,9 @@ def generate_connectors_from_equations(model: Model) -> list[Connector]: """ if model.has_structure(): return list(model.connectors) - from app.core.equations import get_equation_variables, canonical_name + from app.core.equations import get_equation_variables, resolve_name - # Build a lookup map for all element names (normalized to handle variations) - element_names = {} # normalized_name -> actual_name - - for stock in model.stocks: - element_names[canonical_name(stock.name)] = stock.name - # Store original name and underscore variant - element_names[stock.name.lower()] = stock.name - element_names[stock.name.lower().replace(" ", "_")] = stock.name - element_names[stock.name.lower().replace("_", " ")] = stock.name - - for flow in model.flows: - element_names[canonical_name(flow.name)] = flow.name - element_names[flow.name.lower()] = flow.name - element_names[flow.name.lower().replace(" ", "_")] = flow.name - element_names[flow.name.lower().replace("_", " ")] = flow.name - - for var in model.variables: - element_names[canonical_name(var.name)] = var.name - element_names[var.name.lower()] = var.name - element_names[var.name.lower().replace(" ", "_")] = var.name - element_names[var.name.lower().replace("_", " ")] = var.name + element_names = tuple(e.name for e in (*model.stocks, *model.flows, *model.variables)) # Collect existing connectors to avoid duplicates existing = set() @@ -396,17 +376,7 @@ def generate_connectors_from_equations(model: Model) -> list[Connector]: def find_element_name(ref: str) -> Optional[str]: """Find the actual element name for a reference.""" - ref_lower = canonical_name(ref) - if ref_lower in element_names: - return element_names[ref_lower] - # Try with underscore substitution - ref_underscore = ref_lower.replace(" ", "_") - if ref_underscore in element_names: - return element_names[ref_underscore] - ref_space = ref_lower.replace("_", " ") - if ref_space in element_names: - return element_names[ref_space] - return None + return resolve_name(ref, element_names) # Process flows for flow in model.flows: diff --git a/app/core/reports.py b/app/core/reports.py index d244384..1a5ddbd 100644 --- a/app/core/reports.py +++ b/app/core/reports.py @@ -7,7 +7,7 @@ import math from app.core.equations import (EquationError, SafeEvaluator, _parse_expression, preprocess_equation, decode_identifier, canonical_name, equation_features, - get_equation_variables, validate_equation) + get_equation_variables, validate_equation, symbol_identity, resolve_name) def report_tree(equation, names, tables): @@ -34,7 +34,7 @@ def check(node): raise EquationError("Report labels must contain valid Unicode scalar text", code="invalid_report") from exc if len(node.value) > 512: raise EquationError("Report labels are limited to 512 characters", code="invalid_report") - elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and decode_identifier(node.func.id).upper() in {"IF", "IF_THEN_ELSE"} and canonical_name(decode_identifier(node.func.id)) not in {canonical_name(t) for t in tables}: + elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and decode_identifier(node.func.id).upper() in {"IF", "IF_THEN_ELSE"} and resolve_name(decode_identifier(node.func.id), tables) is None: if len(node.args) != 3: raise EquationError("Report IF requires condition, true result and false result", code="invalid_report") numeric(node.args[0]); check(node.args[1]); check(node.args[2]) @@ -50,14 +50,15 @@ def prepare_reports(model): reports = [v for v in model.variables if v.role == "report"] if not reports: return model, [] - report_names = {canonical_name(v.name) for v in reports} + report_names = {symbol_identity(v.name) for v in reports} elements = [*model.stocks, *model.flows, *model.variables] - names = [canonical_name(e.name) for e in elements] + names = [symbol_identity(e.name) for e in elements] if len(set(names)) != len(names): - raise EquationError("Element names must be unique, including case/space/punctuation aliases", code="name_collision") - numeric_names = [e.name for e in elements if canonical_name(e.name) not in report_names] + raise EquationError("Element names must be unique, including case and XMILE whitespace aliases", code="name_collision") + numeric_names = [e.name for e in elements if symbol_identity(e.name) not in report_names] for e in elements: - dependencies = {canonical_name(n) for n in get_equation_variables(e.equation)} & report_names + dependencies = {symbol_identity(resolve_name(n, [v.name for v in elements]) or n) + for n in get_equation_variables(e.equation)} & report_names if dependencies: raise EquationError(f"{e.name}: report outputs cannot feed equations ({', '.join(sorted(dependencies))})", code="report_feedback", details={"element": e.name, "category": "invalid_equations"}) prepared = [(v, report_tree(v.equation, numeric_names, [t.name for t in model.tables])) for v in reports] @@ -72,7 +73,7 @@ def evaluate_report(tree, values, context, tables): def visit(node): if isinstance(node, ast.Constant) and isinstance(node.value, str): return node.value - if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and decode_identifier(node.func.id).upper() in {"IF", "IF_THEN_ELSE"} and canonical_name(decode_identifier(node.func.id)) not in tables: + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and decode_identifier(node.func.id).upper() in {"IF", "IF_THEN_ELSE"} and resolve_name(decode_identifier(node.func.id), tables) is None: condition = float(evaluator.visit(node.args[0])) if not math.isfinite(condition): raise EquationError("Report condition must be finite", code="invalid_report") diff --git a/app/core/structured.py b/app/core/structured.py index a9918f2..07b2555 100644 --- a/app/core/structured.py +++ b/app/core/structured.py @@ -3,7 +3,7 @@ from itertools import product import json import re -from app.core.equations import canonical_name +from app.core.equations import symbol_identity from app.core.array_reductions import Budget, Selection, ReductionError, lower_expression MAX_CELLS = 4096 @@ -127,7 +127,7 @@ def add_scope(body, prefix, parent=None, bindings=None, chain=()): key = ",".join(indices) valid_keys.add(key) full = (prefix + "." if prefix else "") + name + ("[" + key + "]" if shape else "") - alias = canonical_name(full) + alias = symbol_identity(full) if alias in expanded_aliases: fail("ambiguous_name", "Expanded names collide under scalar identifier matching", path) expanded_aliases.add(alias) diff --git a/app/core/units.py b/app/core/units.py index 902b397..2679b77 100644 --- a/app/core/units.py +++ b/app/core/units.py @@ -2,7 +2,7 @@ import ast from fractions import Fraction from app.core.equations import (_parse_expression, preprocess_equation, - canonical_name, decode_identifier, EquationError, STATEFUL_FUNCTIONS, STATEFUL_ALIASES) + canonical_name, symbol_identity, resolve_name, decode_identifier, EquationError, STATEFUL_FUNCTIONS, STATEFUL_ALIASES) # None is unknown; literals have contextual units, distinct from dimensionless {}. _LITERAL = object() @@ -107,7 +107,7 @@ def declared(text): for element in elements: current = element.name exact[element.name] = element - aliases.setdefault(canonical_name(element.name), []).append(element) + aliases.setdefault(symbol_identity(element.name), []).append(element) units[element.id] = declared(element.units) for key, entries in aliases.items(): if len(entries) > 1: @@ -120,7 +120,7 @@ def declared(text): table_units = {} for table in model.tables: current = table.name - key = canonical_name(table.name) + key = symbol_identity(table.name) if key in tables: note('unknown', 'ambiguous_table', f'Multiple tables share alias {key}') tables[key] = table @@ -155,10 +155,11 @@ def expr(node, depth=0): name = decode_identifier(node.id) if name.upper() in {'TIME', 'DT', 'TIME_STEP'}: return clock - element = exact.get(name) - matches = aliases.get(canonical_name(name), []) - if element is None and len(matches) == 1: - element = matches[0] + try: + owner = resolve_name(name, exact) + except EquationError: + owner = None + element = exact.get(owner) if element is not None: return units[element.id] if name.upper() in {'TIME', 'DT', 'TIME_STEP'}: @@ -205,17 +206,19 @@ def expr(node, depth=0): if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): name = decode_identifier(node.func.id) upper = name.upper() - if canonical_name(name) in tables or upper in {'LOOKUP', 'WITH_LOOKUP'}: - key = canonical_name(name) + table_owner = resolve_name(name, [t.name for t in model.tables]) + if table_owner is not None or upper in {'LOOKUP', 'WITH_LOOKUP'}: + key = symbol_identity(table_owner) if table_owner is not None else None input_node = node.args[0] if node.args else None if upper == 'LOOKUP' and key not in tables: table_arg = node.args[0] if node.args else None requested = (decode_identifier(table_arg.id) if isinstance(table_arg, ast.Name) else table_arg.value if isinstance(table_arg, ast.Constant) and isinstance(table_arg.value,str) else '') - key = canonical_name(requested) + owner = resolve_name(requested, [t.name for t in model.tables]) + key = symbol_identity(owner) if owner is not None else None input_node = node.args[1] if len(node.args)>1 else None if key in tables: - if len(node.args) != (2 if upper == 'LOOKUP' and canonical_name(name) not in tables else 1): + if len(node.args) != (2 if upper == 'LOOKUP' and table_owner is None else 1): note('unknown', 'function_signature', 'Lookup has an invalid argument count') return None inp, output = table_units[key] @@ -328,4 +331,4 @@ def expr(node, depth=0): return report - + diff --git a/app/core/validation.py b/app/core/validation.py index aa6e31b..21bc127 100644 --- a/app/core/validation.py +++ b/app/core/validation.py @@ -26,16 +26,16 @@ def issue(severity, code, message, element=None, **details): issues.append({"severity": severity, "code": code, "element": element, "message": message, **details}) elements = [*model.stocks, *model.flows, *model.variables] names = [e.name for e in elements] - from app.core.equations import canonical_name + from app.core.equations import symbol_identity for identifier, count in Counter(e.id for e in elements).items(): if count > 1: issue("error", "duplicate_element_id", "Element IDs must be unique for unambiguous edits and selection", identifier) - for name, count in Counter(canonical_name(e.name) for e in elements).items(): + for name, count in Counter(symbol_identity(e.name) for e in elements).items(): if count > 1: - issue("error", "name_collision", "Element names collide after case/space/punctuation normalization", name) + issue("error", "name_collision", "Element names collide after case and XMILE whitespace normalization", name) if name in {"time", "dt", "time_step"}: issue("error", "reserved_name", "TIME, DT and TIME_STEP are reserved simulation names", name) - for name, count in Counter(canonical_name(t.name) for t in model.tables).items(): + for name, count in Counter(symbol_identity(t.name) for t in model.tables).items(): if count > 1: issue("error", "duplicate_lookup_name", "Lookup names must be unique after normalization", name) stock_references = {} diff --git a/app/core/xmile_generator.py b/app/core/xmile_generator.py index 30ee096..26c825c 100644 --- a/app/core/xmile_generator.py +++ b/app/core/xmile_generator.py @@ -8,7 +8,7 @@ from datetime import datetime, timezone import xml.etree.ElementTree as ET from app.core.model import Model -from app.core.equations import preprocess_equation, canonical_name, decode_identifier, restore_identifiers +from app.core.equations import preprocess_equation, canonical_name, resolve_name, decode_identifier, restore_identifiers NS = 'http://docs.oasis-open.org/xmile/ns/XMILE/v1.0' PROVENANCE_NS = 'https://stockandflow.live/ns/model' @@ -61,8 +61,8 @@ def render(node): if name.upper() == 'TIME_STEP': changed = True return 'DT' - if name.lower() == 'e' and canonical_name(name) not in declared: - if canonical_name('EXP') in table_names: + if name.lower() == 'e' and resolve_name(name, declared_names) is None: + if resolve_name('EXP', tables) is not None: raise ValueError('Constant e translation conflicts with graphical function EXP; use JSON') changed = True return 'EXP(1)' @@ -84,7 +84,7 @@ def render(node): args = [render(arg) for arg in node.args] # Runtime graphical-function resolution precedes built-ins. In # particular a table named LOG or SMOOTH must keep that meaning. - if canonical_name(decode_identifier(node.func.id)) in table_names: + if resolve_name(decode_identifier(node.func.id), tables) is not None: if len(args) != 1 or name in arities or name in {'IF', 'THEN', 'ELSE'}: raise ValueError('Graphical function conflicts with XMILE built-ins or has unsupported arity; use JSON') return splice(node) @@ -92,7 +92,7 @@ def render(node): if len(args) not in (1, 2): raise ValueError('LOG requires one or two arguments in XMILE export') changed = True - if canonical_name('LN') in table_names: + if resolve_name('LN', tables) is not None: raise ValueError('LOG translation conflicts with a graphical function named LN; use JSON') return f'LN({args[0]})' if len(args) == 1 else f'(LN({args[0]}) / LN({args[1]}))' if name in ('IF', 'IF_THEN_ELSE'): @@ -107,10 +107,10 @@ def render(node): if target in arities: if len(args) not in arities[target]: raise ValueError(f'{name} signature is unsupported by XMILE export; use JSON') - elif canonical_name(decode_identifier(node.func.id)) not in table_names or len(args) != 1: + elif resolve_name(decode_identifier(node.func.id), tables) is None or len(args) != 1: raise ValueError(f'{name} is unsupported by XMILE export; use JSON') if target != name: - if canonical_name(target) in table_names: + if resolve_name(target, tables) is not None: raise ValueError(f'{name} translation conflicts with graphical function {target}; use JSON') changed = True return f'{target}({", ".join(args)})' @@ -217,7 +217,7 @@ def equation(parent, text, element_name): table_name = decode_identifier(first.id) if isinstance(first, ast.Name) else first.value if isinstance(first, ast.Constant) else None if table_name in tables: argument = restore_identifiers(ast.unparse(expr.args[1])) - text = argument if table_name == element_name else f'{canonical_name(table_name)}({argument})' + text = argument if table_name == element_name else f'{json.dumps(table_name, ensure_ascii=False)}({argument})' except (SyntaxError, ValueError): pass ET.SubElement(parent, 'eqn').text = xmile_equation(text, tables, model.time_settings.random_seed, original_text, declared_names) diff --git a/app/core/xmile_parser.py b/app/core/xmile_parser.py index 004b11f..5ad9041 100644 --- a/app/core/xmile_parser.py +++ b/app/core/xmile_parser.py @@ -3,7 +3,7 @@ from typing import Optional import re from .model import Model, Stock, Flow, Variable, Connector, Position, TimeSettings, LookupTable -from .equations import canonical_name +from .equations import canonical_name, symbol_identity, resolve_name @@ -282,12 +282,12 @@ def import_graph(element, name_value, equation): # Set flow connections based on stock inflows/outflows flow_map = {} for flow in flows: - key = canonical_name(flow.name) + key = symbol_identity(flow.name) if key in flow_map: raise ValueError(f"Ambiguous XMILE flow name: {flow.name}") flow_map[key] = flow def link_flow(flow_name, stock_name, endpoint): - flow = flow_map.get(canonical_name(flow_name)) + flow = flow_map.get(symbol_identity(resolve_name(flow_name, [f.name for f in flows]) or flow_name)) if flow is None: raise ValueError(f"Stock '{stock_name}' references missing flow '{flow_name}'") current = getattr(flow, endpoint) diff --git a/docs/IDENTIFIER-IMPORT-FIX.md b/docs/IDENTIFIER-IMPORT-FIX.md new file mode 100644 index 0000000..33f94f8 --- /dev/null +++ b/docs/IDENTIFIER-IMPORT-FIX.md @@ -0,0 +1,46 @@ +# Identifier import repair + +The three previously recorded identifier failures are fixed. Quoted `A-B` and +`A_B` remain distinct, U+2002 EN SPACE is preserved inside a quoted name, and an +unquoted internal dollar sign works in identifiers such as `Cash$Balance`. +The original regression assertions now pass without expected-failure markers. + +References bind first to an exact declared spelling, then to a unique normalized +identity, then to a unique historical punctuation alias. Normalized identity uses +NFC, case folding, and the existing space/newline/nonbreaking-space/underscore +equivalence. Ambiguous fallback is an error. Binding uses the complete declaration +inventory, so an unresolved exact name cannot accidentally read an already +computed alias. Existing case and standard-whitespace collisions remain errors. + +The same rules cover evaluation, initialization, XMILE flow-name mapping, dependency +graphs, lookup tables, reports, units and related analysis. The conservative +execution scheduler retains its fallback for punctuation-alias collisions. +Historical random streams remain unchanged for existing models; newly permitted +distinct names receive separate stream keys. Dollar tokenization preserves source +boundaries and does not join names across spaces or accept a leading dollar sign. + +The editor preserves these identities when renaming imported elements. It checks +all references before changing the model and cancels a rename that would change +an unrelated reference's binding. Existing saved model IDs and source names are +preserved; no library definitions are changed by this repair. + +## Verification + +- All 1,000 public models completed their full default runs. Before/after hashes + of every time vector, numerical series, report series and effective random seed + are identical; all 1,000 model-file hashes are also unchanged. +- Permanent regressions cover the original three cases, Unicode identity, + declaration order, aliases, random streams, flow endpoints, tables, units, + feedback and JSON/XMILE roundtrips. +- Real file-backed HTTP acceptance imported, fetched, simulated, exported and + reimported each of the three cases. An actual MCP client simulated all six saved + imports. These tests used isolated storage. +- Browser acceptance imported a fixture containing all three cases, renamed one + literal identifier, saved, exported XMILE, reimported and simulated it. The + distinct values remained 1, 2, 4 and 8; their output remained 15 and the + accumulated stock ended at 30 after two time units. + +This repair does not expand the documented scalar XMILE subset into support for +every XMILE construct. Existing structured-model and solver export restrictions +remain explicit. Historical benchmark receipts predate this repair; no new speed +advantage over Simlin is claimed. diff --git a/scripts/check_property_inputs_v16.cjs b/scripts/check_property_inputs_v16.cjs index 5b83f63..4af189c 100644 --- a/scripts/check_property_inputs_v16.cjs +++ b/scripts/check_property_inputs_v16.cjs @@ -43,6 +43,40 @@ assert.equal(rename('")" + 1',')','Right'),'"Right" + 1'); assert.equal(app.identifierKey('Straße'),app.identifierKey('STRASSE')); assert.equal(app.identifierKey('Total__Population'),app.identifierKey('total population')); assert.equal(app.identifierKey('é'),app.identifierKey('e\u0301')); +// Imported literal names remain separate throughout an editor rename. +for (const literal of ['A-B','A\u2002B']) { + use({stocks:[],flows:[],tables:[],connectors:[],variables:[ + {id:'literal',name:literal,equation:'1'}, + {id:'underscore',name:'A_B',equation:'2'}, + {id:'out',name:'Output',equation:JSON.stringify(literal)+'+A_B'}]},'variable','literal'); + assert.equal(app.updateProperty('name','Renamed'),true); + assert.equal(app.model.variables[2].equation,'"Renamed"+A_B'); + assert.equal(app.model.variables[1].name,'A_B'); +} +assert.equal(rename('Cash$Balance+1','Cash$Balance','Cash Total'),'"Cash Total"+1'); +assert.equal(rename('Wall_R_Value+1','Wall R-Value','Insulation'),'"Insulation"+1'); +assert.throws(()=>rename('A__B','A-B','New',[],['A-B','A.B']),/Ambiguous/); +assert.equal(rename('A_B+"A-B"','A-B','New',[],['A-B','A_B']),'A_B+"New"'); +// A rename cannot steal a reference that previously used another name's +// unique legacy alias, or turn an unknown symbol into a different binding. +for (const [existing,reference,renamed] of [ + ['A-B','A_B','A_B'], ['A_B','"A-B"','A-B'], + ['A.B','A__B','A-B'], ['Existing','Missing','Missing'] +]) { + use({stocks:[],flows:[],tables:[],connectors:[],variables:[ + {id:'x',name:'X',equation:'1'}, {id:'other',name:existing,equation:'2'}, + {id:'out',name:'Output',equation:reference+'+X'}]},'variable','x'); + const original=JSON.stringify(app.model); + assert.equal(app.updateProperty('name',renamed),false); + assert.equal(JSON.stringify(app.model),original); +} +// Real table arguments/functions stay in their own namespace even when a +// variable would match their spelling through a legacy alias. +use({stocks:[],flows:[],connectors:[],tables:[{name:'A-B',points:[[0,0],[1,1]]}],variables:[ + {id:'x',name:'A_B',equation:'1'}, + {id:'out',name:'Output',equation:'LOOKUP("A-B",A_B)+"A-B"(A_B)'}]},'variable','x'); +assert.equal(app.updateProperty('name','Renamed'),true); +assert.equal(app.model.variables[1].equation,'LOOKUP("A-B","Renamed")+"A-B"("Renamed")'); for(const name of ['', ' ', 'TIME','dt','Population Growth']){ use();const original=JSON.stringify(app.model); assert.equal(app.updateProperty('name',name),false); diff --git a/static/js/app.js b/static/js/app.js index f366033..3c06482 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -1374,15 +1374,30 @@ const App = { identifierKey(name) { const folded = Array.from(String(name).normalize('NFC'), c => AppRenameCasefold[c] ?? c.toLowerCase()).join(''); - return folded.replace(/[ _\n\u00a0]+/g, '_') - .replace(/[^\p{L}\p{N}_]/gu, '_').replace(/_+/g, '_'); + return folded.replace(/[ _\n\u00a0]+/g, '_'); + }, + + legacyIdentifierKey(name) { + return this.identifierKey(name).replace(/[^\p{L}\p{N}_]/gu, '_').replace(/_+/g, '_'); + }, + + resolveIdentifier(name, declaredNames) { + const names = [...new Set(declaredNames)]; + if (names.includes(name)) return name; + for (const key of [this.identifierKey.bind(this), this.legacyIdentifierKey.bind(this)]) { + const matches = names.filter(candidate => key(candidate) === key(name)); + if (matches.length > 1) throw new Error('Ambiguous identifier “' + name + '”. Rename was cancelled.'); + if (matches.length === 1) return matches[0]; + } + return null; }, // Supported scalar syntax with exact source spans; never global text replace. - renameEquationReferences(equation, oldName, newName, tableNames = []) { + renameEquationReferences(equation, oldName, newName, tableNames = [], declaredNames = [oldName]) { const source = String(equation || ''), tokens = []; const oldKey = this.identifierKey(oldName); const tableKeys = new Set(tableNames.map(name => this.identifierKey(name))); + const renamedNames = declaredNames.map(name => name === oldName ? newName : name); const keywords = new Set(['IF','THEN','ELSE','AND','OR','NOT','TRUE','FALSE']); const builtins = new Set(('SIN COS TAN ASIN ACOS ATAN ATAN2 SIGN EXP LOG LN LOG10 SQRT ABS MIN MAX POW FLOOR CEIL ROUND MOD IF IF_THEN_ELSE RANDOM RANDOM_UNIFORM NORMAL RANDOM_NORMAL STEP PULSE RAMP LOOKUP WITH_LOOKUP DELAY DELAY_FIXED DELAY1 DELAY3 DELAY_N DELAYN SMOOTH SMOOTH3 SMOOTH_N SMTH1 SMTH3 SMTHN').split(' ')); let at = 0; @@ -1430,8 +1445,7 @@ const App = { if (stack.pop()?.close !== token.value) throw new Error('An equation has unmatched brackets. Rename was cancelled.'); } else if (token.kind === 'op' && token.value === ',') { if (stack.length) stack[stack.length-1].argument++; - } else if (token.kind === 'name' && this.identifierKey(token.value) === oldKey && - (token.quoted || !keywords.has(upper))) { + } else if (token.kind === 'name' && (token.quoted || !keywords.has(upper))) { let tableArgument = false; for (let j=stack.length-1;j>=0;j--) { if (stack[j].functionName) { @@ -1439,14 +1453,30 @@ const App = { break; } } - if (tableArgument && !tableKeys.has(oldKey)) - throw new Error('“' + oldName + '” is used as an undeclared lookup table. Resolve that ambiguity before renaming.'); - if (!tableArgument) { - if (next?.value === '[') throw new Error('Array references cannot be safely renamed by this scalar editor.'); - if (next?.value === '(') { - if (!tableKeys.has(oldKey) && !builtins.has(upper)) - throw new Error('“' + oldName + '” is also used as a function. Resolve that ambiguity before renaming.'); - } else edits.push({start:token.start,end:token.end,value:JSON.stringify(newName)}); + // Lookup positions/functions belong to the table namespace, not + // a similarly spelled variable's legacy alias bucket. + if ((tableArgument || next?.value === '(') && + this.resolveIdentifier(token.value, tableNames) !== null) { + previous=token; + continue; + } + const owner = this.resolveIdentifier(token.value, declaredNames); + if (!tableArgument && next?.value !== '(') { + const expected = owner === oldName ? newName : owner; + const reference = owner === oldName ? newName : token.value; + if (this.resolveIdentifier(reference, renamedNames) !== expected) + throw new Error('Renaming would change another equation reference. Rename was cancelled.'); + } + if (owner === oldName) { + if (tableArgument && !tableKeys.has(oldKey)) + throw new Error('“' + oldName + '” is used as an undeclared lookup table. Resolve that ambiguity before renaming.'); + if (!tableArgument) { + if (next?.value === '[') throw new Error('Array references cannot be safely renamed by this scalar editor.'); + if (next?.value === '(') { + if (!tableKeys.has(oldKey) && !builtins.has(upper)) + throw new Error('“' + oldName + '” is also used as a function. Resolve that ambiguity before renaming.'); + } else edits.push({start:token.start,end:token.end,value:JSON.stringify(newName)}); + } } } previous=token; @@ -1478,7 +1508,7 @@ const App = { if (elements.some(other=>other!==element && other.id===oldName)) throw new Error('The old name also identifies another element ID. Resolve that ambiguity before renaming.'); const equations=elements.map(item=>({ - item, equation:this.renameEquationReferences(item.equation,oldName,name,tableNames) + item, equation:this.renameEquationReferences(item.equation,oldName,name,tableNames,elements.map(item=>item.name)) })); const endpoints=[]; const renameReference=value=>!ids.has(value) && value===oldName ? name : value; diff --git a/static/library-validation.json b/static/library-validation.json index ac1a03e..d1cd5cd 100644 --- a/static/library-validation.json +++ b/static/library-validation.json @@ -3,27 +3,27 @@ "library_scope": "Git-tracked public definitions", "engine_sha256": { "app/core/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "app/core/adaptive.py": "56ea274ce6f65712ae574777f50351247374baf658dc88495e862d3bcd1157f0", + "app/core/adaptive.py": "adf4b354c6be4bde74ccebf7dafd4986932760b403905dd818a5f744a98da0d8", "app/core/array_reductions.py": "9881875542167f033142376167e3e13487000aaed84029d157a09e200f2c275b", - "app/core/diagnostics.py": "5734f4d913d81cb4f6a4b825d81517005989ca0d38c78c70ae34ed961f0cbdc9", - "app/core/equations.py": "7c7dff75a0a4c00ec62d347788e7dde32197a68c38b5140ca57f11df2e74e612", + "app/core/diagnostics.py": "414b086be34d0e6f38db47cd745aee7a02121305d44e6d8f6d46a082dd5a3fde", + "app/core/equations.py": "ccfb73d46efa01e920df1e9f1a0b4b1b1956d9186827cfd7a60812cd2d513e3b", "app/core/execution_order.py": "0633452a7a9a5e7d0d3701ff2ea7b496063e6714e4596986f1d057a5ec694820", - "app/core/failure_diagnostics.py": "ef2627d9d6fd73560b1e3d249b394709e76473db1952e36c5c1ad11fe7ad7e79", - "app/core/feedback_analysis.py": "4d85d163a7fea0766d752804320661ca821fa0e5a59a5950da2455bb260c5ab8", - "app/core/integration.py": "29b6649c2771e1684c7298751647d65a6c04b4f54a016be9f4328908d75b2598", + "app/core/failure_diagnostics.py": "14e287ab723e3b8dad5f53c5e4936a2f2616e56892d79a5b804b887d634a2206", + "app/core/feedback_analysis.py": "09531e44af61ee64db8075e104f39737d10b7b9e35b2a9ec8f0f454183ed6aa2", + "app/core/integration.py": "268165bdb70943b4c60909d7b2903145e86a1f2901f2f2a0fb74223b5a616ce7", "app/core/limits.py": "77a726f0764097752534c6bb9c15828a2289a21009daddce882f3fd5abfbfffa", "app/core/local_sensitivity.py": "0ca710f7cc6a076636803df5aa3d74bdaea66429ddab66512a95371ce3ad035a", - "app/core/mdl_parser.py": "f17dbb1a4604efa1f2b63cdff7a172ddfce5d6c2cac549b00d1596d7ddf20e99", - "app/core/model.py": "bf3ccdf20c8e0902686561241d2c27cce8e871e00a93e80c23655a38fbf3eaeb", - "app/core/reports.py": "ce0bf0e79b23297267ad07628e56b4a40229526acd124461309190739de87e2e", + "app/core/mdl_parser.py": "c3d993df4f21c9aca201b781b24c5a000171d7ac399da40e7478da3ab49bed4e", + "app/core/model.py": "e8eaf2ca5680683e82b0fee79865cbd031e84b1a59bdc5d83dd233ae6a99ba09", + "app/core/reports.py": "269aeddbdcc8c2a8957da149ee166f1d3086b1b78074d853a1de5fde68ae1413", "app/core/simulator.py": "dffe1dc1aa2b937a6076ad9c869ae115281ab17f11ddc2e57bc1eaab435a8679", "app/core/source_review.py": "fbb87641cefc9b2ab5f66becffa197993a107522a8d68750b09ae1f0167840ec", - "app/core/structured.py": "44e119463fc6470e867fcfd519099f0ebb2a527a547c88d623ae2b1f7591a5ee", - "app/core/units.py": "7ecf90ab7d42f4859fe2eb4c831a6b92cd91509f7f513e0237c0a0736a58b9ac", - "app/core/validation.py": "fff2e905ea1b581492c4499843bf87a55e810b0ea15ae37c3a46c0e58bd857ff", + "app/core/structured.py": "c85d5b107a18fd2840e57b63c14aa9883d263dfdec49989bbb9b8a3356aebb69", + "app/core/units.py": "155c21e9a68b880f0e2cda2595f871c864f082fb11d9ca8e5413715bc963e3f8", + "app/core/validation.py": "e788f461e59cc7d8a8a96106e572c62a8537f765a5d807c7b28f8b628c327044", "app/core/validator.py": "18ae2762241eebe32f96f603879bca1b716e6c6fc32f3729d85749408dca4a86", - "app/core/xmile_generator.py": "f02bbea8191392ff8700e298d73f354269de2e298ccfd7d2796d86eda4d7b0e4", - "app/core/xmile_parser.py": "72070b639fd2fe664d713a6cf4b53cf25e9dc653692e3ed028f84e97173c9e56" + "app/core/xmile_generator.py": "82c2ecf5afb6c989620c486b8eb87442ee9083b0fdb87c72e0dc1712619ba83c", + "app/core/xmile_parser.py": "f00a657a46a55bcfadc3a63585e0e30d3f7f143ceafad7e8cd3f7cccf15e7c9e" }, "scientific_validation": "not established; inspect assumptions and source references", "counts": { diff --git a/tests/test_engine_inputs_v16.py b/tests/test_engine_inputs_v16.py index b01c71f..ad52689 100644 --- a/tests/test_engine_inputs_v16.py +++ b/tests/test_engine_inputs_v16.py @@ -1,7 +1,7 @@ """Read-only v1.6 input-semantics findings; no engine/model repairs. -Passing tests pin supported/current documented behavior. Strict xfails describe -bounded XMILE identifier gaps; they must not silently weaken legacy alias rules. +Passing tests pin supported/current documented behavior. Regression cases preserve literal identifier distinctions without weakening +existing case/standard-whitespace alias rules. """ import pytest from app.core.equations import EquationError, safe_eval @@ -109,21 +109,18 @@ def test_signed_stocks_are_not_globally_clamped_to_zero(): flows=[{"name":"Withdrawal","equation":"1","from_stock":"x"}]) assert simulate(m).values["Signed Account"][-1]==-1 -@pytest.mark.xfail(strict=True,reason="Unresolved strict-XMILE gap: punctuation is literal in quoted identifiers; legacy aliases collapse it.") def test_strict_xmile_quoted_punctuation_identifiers_should_remain_distinct(): m=output_model(variables=[ {"name":"A-B","equation":"1"},{"name":"A_B","equation":"2"}, {"name":"Output","equation":'"A-B"+"A_B"'}]) assert simulate(m).values["Output"]==[3]*9 -@pytest.mark.xfail(strict=True,reason="Unresolved strict-XMILE gap: en-space must not be equivalent to underscore.") def test_strict_xmile_en_space_is_not_whitespace_alias(): m=output_model(variables=[ {"name":"A B","equation":"1"},{"name":"A_B","equation":"2"}, {"name":"Output","equation":'"A B"+"A_B"'}]) assert simulate(m).values["Output"]==[3]*9 -@pytest.mark.xfail(strict=True,reason="Unresolved strict-XMILE lexer gap: dollar is allowed after the first identifier character.") def test_strict_xmile_unquoted_internal_dollar_identifier(): assert safe_eval("Cash$Balance+1",{"Cash$Balance":2})==3 diff --git a/tests/test_engine_review.py b/tests/test_engine_review.py index 6a88c49..7584d08 100644 --- a/tests/test_engine_review.py +++ b/tests/test_engine_review.py @@ -87,7 +87,7 @@ def test_constants_do_not_shadow_user_parameters(): assert safe_eval("PI", {}) == math.pi -@pytest.mark.parametrize("names", [("A", "a"), ("a-b", "a_b"), ("A B", "A_B")]) +@pytest.mark.parametrize("names", [("A", "a"), ("A B", "A_B")]) def test_colliding_aliases_are_rejected(names): model = Model(variables=[{"name": name, "equation": "1"} for name in names]) with pytest.raises(EquationError, match="unique"): @@ -491,3 +491,10 @@ def test_rk4_budget_counts_hidden_stages_and_all_derivative_evaluations(): time_settings={"end":10000,"dt":1,"integration_method":"rk4"}) with pytest.raises(EquationError,match="RK4 evaluations"): simulate(model) + +def test_punctuation_distinct_names_have_distinct_owners(): + model = Model(variables=[{"name":"a-b","equation":"1"}, + {"name":"a_b","equation":"2"}, + {"name":"Output","equation":'"a-b"+"a_b"'}], + time_settings={"end":1,"dt":.25}) + assert simulate(model).values["Output"] == [3]*5 \ No newline at end of file diff --git a/tests/test_frontend.py b/tests/test_frontend.py index 8d5c186..57809a0 100644 --- a/tests/test_frontend.py +++ b/tests/test_frontend.py @@ -274,6 +274,22 @@ def test_index_html(): assert success, message +def test_property_rename_preserves_identifier_bindings(): + """Run real editor transactions, including strict/legacy alias collisions.""" + executable = shutil.which("node") or shutil.which("nodejs") or shutil.which("node.exe") + assert executable, "Node.js is required for JavaScript behavior checks" + root = Path(__file__).resolve().parents[1] + script = str(root / "scripts/check_property_inputs_v16.cjs") + if os.name != "nt" and executable.lower().endswith(".exe"): + # WSL may discover Windows Node; it needs a Windows/UNC script path. + converter = shutil.which("wslpath") + assert converter, "wslpath is required to run a WSL script with Windows Node" + script = subprocess.check_output([converter, "-w", script], text=True).strip() + result = subprocess.run([executable, script], cwd=root, + capture_output=True, text=True, timeout=30) + assert result.returncode == 0, result.stdout + result.stderr + + def test_js_parser_rejects_balanced_invalid_syntax(tmp_path): script = tmp_path / "invalid.js" script.write_text("const broken = ;", encoding="utf-8") diff --git a/tests/test_identifier_identity.py b/tests/test_identifier_identity.py new file mode 100644 index 0000000..f41e9bf --- /dev/null +++ b/tests/test_identifier_identity.py @@ -0,0 +1,103 @@ +"""Literal names, unique legacy fallback, and complete declaration ownership.""" +import json +import pytest +from app.core.equations import safe_eval,EquationError,SimulationContext,symbol_identity,canonical_name,resolve_name +from app.core.model import Model +from app.core.simulator import simulate +from app.core.units import analyze_units +from app.core.validation import inspect_inputs +from app.core.feedback_analysis import analyze_feedback +from app.core.xmile_generator import generate_xmile +from app.core.xmile_parser import parse_xmile + +@pytest.mark.parametrize("name",["A-B","A\u2002B","Cash$Balance"]) +def test_literal_identity_preserves_punctuation(name): + assert symbol_identity(name)!=symbol_identity(name.replace("-","_").replace("\u2002","_").replace("$","_")) + assert canonical_name(name)==canonical_name(name.replace("-","_").replace("\u2002","_").replace("$","_")) + +@pytest.mark.parametrize("expression,want",[("Cash$1e+3",5),("Cash$1e-3",-1),("Cash$$Balance+1",3),("Cash$e\u0301+1",3)]) +def test_unquoted_dollar_physical_identifier_span(expression,want): + name=expression[:-2] + assert safe_eval(expression,{name:2})==want + +@pytest.mark.parametrize("expression",["$Cash+1","Cash $ Balance+1","Cash$ Balance+1"]) +def test_dollar_does_not_join_whitespace_or_leading_dollar(expression): + with pytest.raises(EquationError):safe_eval(expression,{"Cash$Balance":2}) + +def test_generated_identifier_prefix_cannot_capture_authored_name(): + encoded="__xmile_id_"+b"Cash$Balance".hex() + assert safe_eval(f"Cash$Balance+{encoded}",{"Cash$Balance":2,encoded:7})==9 + assert safe_eval('"Cash$Balance"+1',{"Cash$Balance":2})==3 + +def test_exact_owner_blocks_unresolved_alias_fallback(): + context=SimulationContext();context.declared_symbols=("A-B","A_B") + with pytest.raises(EquationError,match="Unknown variable"): + safe_eval('"A_B"',{"A-B":1},context) + assert safe_eval('"A_B"',{"A-B":1,"A_B":2},context)==2 + with pytest.raises(EquationError,match="Ambiguous"): + safe_eval('"A!B"',{"A-B":1,"A_B":2}) + assert safe_eval("Pre_Service_Teachers+1",{"Pre-Service Teachers":2})==3 + +@pytest.mark.parametrize("solver",["euler","rk4","radau"]) +def test_distinct_state_flow_init_table_units_and_feedback(solver): + m=Model(stocks=[{"id":"x","name":"Store-A","initial_value":0,"equation":'"Initial-A"',"units":"kg"}, + {"id":"y","name":"Store_A","initial_value":0,"equation":'"Initial_A"',"units":"kg"}], + flows=[{"name":"Flow-A","equation":'"Curve-A"(1)',"to_stock":"x","units":"kg/second"}, + {"name":"Flow_A","equation":'"Curve_A"(1)',"to_stock":"y","units":"kg/second"}], + variables=[{"name":"Initial-A","equation":"2","units":"kg"},{"name":"Initial_A","equation":"5","units":"kg"}], + tables=[{"name":"Curve-A","points":[[0,1],[2,1]],"input_units":"1","output_units":"kg/second"}, + {"name":"Curve_A","points":[[0,2],[2,2]],"input_units":"1","output_units":"kg/second"}], + time_settings={"end":1,"dt":.25,"integration_method":solver,"units":"second"}) + before=m.model_dump(mode="json");r=simulate(m) + assert r.values["Store-A"]==pytest.approx([2+t for t in r.time],abs=1e-9) + assert r.values["Store_A"]==pytest.approx([5+2*t for t in r.time],abs=1e-9) + assert inspect_inputs(m)["valid"] and analyze_units(m)["status"]=="consistent" + assert analyze_feedback(m,r)["samples"] + if solver=="radau": + with pytest.raises(ValueError,match="Radau settings"):generate_xmile(m) + out=Model.model_validate_json(m.model_dump_json()) + else: + out=parse_xmile(generate_xmile(m)) + assert simulate(out).values==r.values + assert m.model_dump(mode="json")==before + +def test_forward_exact_dependencies_and_lazy_branch_cycles(): + m=Model(variables=[{"name":"A-B","equation":"Later"},{"name":"A_B","equation":"2"}, + {"name":"Later","equation":"7"},{"name":"Output","equation":'"A-B"+"A_B"'}],time_settings={"end":1,"dt":.25}) + assert simulate(m).values["Output"]==[9]*5 + m.variables[0].equation='IF(1,7,"A_B")';m.variables[1].equation='"A-B"+1' + assert simulate(m).values["A_B"]==[8]*5 + m.variables[0].equation='"A_B"';m.variables[1].equation='"A-B"' + with pytest.raises(EquationError,match="Unresolved"):simulate(m) + +def test_new_legacy_collision_random_streams_are_distinct_and_replayable(): + m=Model(variables=[{"name":"A-B","equation":"RANDOM_UNIFORM(0,1)"},{"name":"A_B","equation":"RANDOM_UNIFORM(0,1)"}], + time_settings={"end":2,"dt":.25,"random_seed":314}) + a,b=simulate(m),simulate(m) + assert a.values==b.values and a.values["A-B"]!=a.values["A_B"] + +def test_reports_do_not_capture_punctuation_distinct_numeric_symbol(): + m=Model(variables=[{"name":"A-B","equation":"2"},{"name":"A_B","role":"report","equation":"'label'"}, + {"name":"Output","equation":'"A-B"+1'}],time_settings={"end":1,"dt":.25}) + r=simulate(m);assert r.values["Output"]==[3]*5 and r.report_values["A_B"]==["label"]*5 + m.variables[2].equation='"A_B"+1' + with pytest.raises(EquationError,match="report outputs"):simulate(m) + +@pytest.mark.parametrize("pair",[("A B","A_B"),("A\u00a0B","A_B"),("e\u0301","é"),("k3","K3")]) +def test_true_semantic_collisions_remain_errors(pair): + m=Model(variables=[{"name":n,"equation":"1"} for n in pair]) + with pytest.raises(EquationError,match="unique"):simulate(m) + + +def test_rng_namespace_cannot_collide_with_authored_symbol(): + names=["A-B","A_B","__strict_symbol_612d62",":strict:612d62"] + m=Model(variables=[{"name":n,"equation":"RANDOM_UNIFORM(0,1)"} for n in names],time_settings={"end":1,"dt":.25,"random_seed":812}) + run=simulate(m);assert run.values==simulate(m).values + assert len({tuple(v) for v in run.values.values()})==4 + + +def test_failure_diagnostics_keep_distinct_blocked_dependency_owners(): + m=Model(variables=[{"name":"A-B","equation":"Missing"},{"name":"A_B","equation":"1/0"},{"name":"First","equation":chr(34)+"A-B"+chr(34)},{"name":"Second","equation":"A_B"}]) + with pytest.raises(EquationError) as error:simulate(m) + blocked={r["element"]:r["waiting_for"] for r in error.value.details["blocked_errors"]} + assert blocked=={"First":"A-B","Second":"A_B"} diff --git a/tests/test_identifier_roundtrip.py b/tests/test_identifier_roundtrip.py new file mode 100644 index 0000000..6f906f7 --- /dev/null +++ b/tests/test_identifier_roundtrip.py @@ -0,0 +1,131 @@ +"""Independent proposed regressions; local only, no filesystem model storage.""" +import pytest +import xml.etree.ElementTree as ET +from fastapi import FastAPI +from fastapi.testclient import TestClient +from app.core.model import Model, generate_connectors_from_equations +from app.core.simulator import simulate +from app.core.xmile_generator import generate_xmile +from app.core.xmile_parser import parse_xmile +from app.api import models as routes + +PAIRS=[("A-B","A_B"),("A\u2002B","A_B"),("Cash$Balance","Other")] + +def authored(first,second): + equation=f'"{first}"+"{second}"' if "$" not in first else f'{first}+"{second}"' + return Model(name="Identifier review", stocks=[{"id":"stock","name":"Stored$Total","initial_value":0}], + flows=[{"name":"Accrual$Rate","equation":equation,"to_stock":"stock"}], + variables=[{"name":first,"equation":"1"},{"name":second,"equation":"2"}, + {"name":"Output","equation":equation}], + time_settings={"start":0,"end":2,"dt":.25}) + +def verify(model,first,second): + result=simulate(model) + assert result.values["Output"]==[3]*9 + assert result.values["Stored$Total"]==pytest.approx([3*t for t in result.time]) + edges={(c.from_element,c.to_element) for c in generate_connectors_from_equations(model)} + for target in ("Output","Accrual$Rate"): + assert {(first,target),(second,target)} <= edges + stock=next(s for s in model.stocks if s.name=="Stored$Total") + flow=next(f for f in model.flows if f.name=="Accrual$Rate") + assert flow.to_stock in (stock.id,stock.name) + +@pytest.mark.parametrize("first,second",PAIRS) +def test_json_roundtrip(first,second): + model=authored(first,second) + restored=Model.model_validate_json(model.model_dump_json()) + assert restored.model_dump()==model.model_dump() + verify(restored,first,second) + +@pytest.mark.parametrize("first,second",PAIRS) +def test_xml_export_import_roundtrip(first,second): + model=authored(first,second) + parsed=parse_xmile(generate_xmile(model)) + assert [(v.name,v.equation) for v in parsed.variables]==[(v.name,v.equation) for v in model.variables] + verify(parsed,first,second) + +@pytest.mark.parametrize("first,second",PAIRS) +def test_actual_import_api_in_memory_only(monkeypatch,first,second): + calls=[] + def create(model): + calls.append(model) + return model + monkeypatch.setattr(routes.storage,"create_model",create) + app=FastAPI() + app.include_router(routes.router,prefix="/api/models") + model=authored(first,second) + root=ET.Element("xmile",version="1.0") + specs=ET.SubElement(root,"sim_specs") + for tag,value in (("start","0"),("stop","2"),("dt",".25")): + ET.SubElement(specs,tag).text=value + variables=ET.SubElement(ET.SubElement(root,"model"),"variables") + stock=ET.SubElement(variables,"stock",name="Stored$Total") + ET.SubElement(stock,"eqn").text="0" + ET.SubElement(stock,"inflow").text="Accrual$Rate" + flow=ET.SubElement(variables,"flow",name="Accrual$Rate") + ET.SubElement(flow,"eqn").text=model.flows[0].equation + for variable in model.variables: + node=ET.SubElement(variables,"aux",name=variable.name) + ET.SubElement(node,"eqn").text=variable.equation + xml=ET.tostring(root,encoding="unicode") + with TestClient(app) as client: + response=client.post("/api/models/import/xmile",files={"file":("review.xmile",xml,"application/xml")}) + assert response.status_code==200,response.text + assert len(calls)==1 + verify(Model.model_validate(response.json()),first,second) + +@pytest.mark.parametrize("first,second",PAIRS) +def test_source_dependency_graph_distinct_bindings(first,second): + model=authored(first,second) + before=[(v.name,v.equation) for v in model.variables] + edges={(c.from_element,c.to_element) for c in generate_connectors_from_equations(model)} + assert [(v.name,v.equation) for v in model.variables]==before + for target in ("Output","Accrual$Rate"): + assert {(first,target),(second,target)} <= edges + +@pytest.mark.parametrize("reverse",[False,True]) +def test_unresolved_exact_declaration_cannot_take_computed_legacy_alias(reverse): + # A-B is deliberately after its alias: dependency planning must wait for9. + variables=[{"name":"A_B","equation":"2"},{"name":"Output","equation":'"A-B"'}, + {"name":"A-B","equation":"Later"},{"name":"Later","equation":"9"}] + if reverse: variables.reverse() + model=Model(variables=variables,time_settings={"start":0,"end":1,"dt":.25}) + for candidate in (model,Model.model_validate_json(model.model_dump_json()),parse_xmile(generate_xmile(model))): + assert simulate(candidate).values["Output"]==[9]*5 + + +def test_punctuation_stock_ids_and_distinct_flow_links_survive_xml(): + model=Model(stocks=[{"id":"id-1","name":"Store-A","initial_value":10}, + {"id":"id_1","name":"Store_A","initial_value":20}], + flows=[{"name":"Flow-A","equation":"1","from_stock":"id-1","to_stock":"id_1"}, + {"name":"Flow_A","equation":"2","from_stock":"id_1","to_stock":"id-1"}], + time_settings={"start":0,"end":1,"dt":.25}) + for candidate in (model,parse_xmile(generate_xmile(model))): + result=simulate(candidate) + assert result.values["Store-A"]==pytest.approx([10+t for t in result.time]) + assert result.values["Store_A"]==pytest.approx([20-t for t in result.time]) + assert set(f.name for f in candidate.flows)=={"Flow-A","Flow_A"} + + +def test_distinct_punctuation_lookup_tables_survive_xml(): + model=Model(tables=[{"name":"Curve-A","points":[[0,1],[1,1]]}, + {"name":"Curve_A","points":[[0,2],[1,2]]}], + variables=[{"name":"Output","equation":'"Curve-A"(TIME)+"Curve_A"(TIME)'}], + time_settings={"start":0,"end":1,"dt":.25}) + for candidate in (model,Model.model_validate_json(model.model_dump_json()),parse_xmile(generate_xmile(model))): + assert {t.name for t in candidate.tables}=={"Curve-A","Curve_A"} + assert simulate(candidate).values["Output"]==[3]*5 + +def test_independent_xml_flow_names_do_not_collapse(): + xml="""01
.25
+ + 10Flow-AFlow_A + 20Flow-AFlow_A + 12 +
""" + model=parse_xmile(xml) + byname={f.name:f for f in model.flows} + assert byname["Flow-A"].from_stock != byname["Flow_A"].from_stock + result=simulate(model) + assert result.values["Store-A"]==pytest.approx([10+t for t in result.time]) + assert result.values["Store_A"]==pytest.approx([20-t for t in result.time])