let domains author their own pass-2 estimator as formulas, not Python
Pass 2 previously only knew one estimator: a hardcoded physics model that matches dimensions literally named platform/actuator/energy_storage. Any domain outside that shape (e.g. archery) got all-zero estimates and failed every combo. Domains can now declare free variables and per-metric formulas as data instead; a safe AST-based evaluator (engine/formula.py, no eval()) resolves declared entity properties via dep(key, constraint_type) and generalizes the existing hand-nested mass-budget search into an N-variable recursive optimizer. Fully additive -- the legacy platform/actuator/ energy_storage path is untouched and still runs unchanged for every domain that declares no formulas. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
from flask import Blueprint, flash, redirect, render_template, request, url_for
|
||||
|
||||
from physcom.models.domain import Domain, MetricBound
|
||||
from physcom.models.domain import Domain, FreeVariable, MetricBound, MetricFormula
|
||||
from physcom_web.app import get_repo
|
||||
|
||||
bp = Blueprint("domains", __name__, url_prefix="/domains")
|
||||
@@ -117,3 +117,96 @@ def metric_delete(domain_id: int, metric_id: int):
|
||||
flash("Metric removed.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_metrics_table.html", domain=domain)
|
||||
|
||||
|
||||
# ── Free variable CRUD (HTMX partials) ───────────────────────
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/free-vars/add", methods=["POST"])
|
||||
def free_var_add(domain_id: int):
|
||||
repo = get_repo()
|
||||
name = request.form["name"].strip()
|
||||
floor_formula = request.form.get("floor_formula", "").strip()
|
||||
ceiling_formula = request.form.get("ceiling_formula", "").strip()
|
||||
try:
|
||||
sort_order = int(request.form.get("sort_order", "0"))
|
||||
except ValueError:
|
||||
sort_order = 0
|
||||
if not name or not floor_formula or not ceiling_formula:
|
||||
flash("Name, floor formula, and ceiling formula are required.", "error")
|
||||
else:
|
||||
fv = FreeVariable(
|
||||
name=name, floor_formula=floor_formula, ceiling_formula=ceiling_formula,
|
||||
sort_order=sort_order,
|
||||
)
|
||||
repo.add_free_variable(domain_id, fv)
|
||||
flash(f"Free variable '{name}' added.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_free_vars_table.html", domain=domain)
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/edit", methods=["POST"])
|
||||
def free_var_edit(domain_id: int, fv_id: int):
|
||||
repo = get_repo()
|
||||
try:
|
||||
sort_order = int(request.form.get("sort_order", "0"))
|
||||
except ValueError:
|
||||
sort_order = 0
|
||||
fv = FreeVariable(
|
||||
name=request.form["name"].strip(),
|
||||
floor_formula=request.form.get("floor_formula", "").strip(),
|
||||
ceiling_formula=request.form.get("ceiling_formula", "").strip(),
|
||||
sort_order=sort_order,
|
||||
)
|
||||
repo.update_free_variable(fv_id, fv)
|
||||
flash("Free variable updated.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_free_vars_table.html", domain=domain)
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/free-vars/<int:fv_id>/delete", methods=["POST"])
|
||||
def free_var_delete(domain_id: int, fv_id: int):
|
||||
repo = get_repo()
|
||||
repo.delete_free_variable(fv_id)
|
||||
flash("Free variable removed.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_free_vars_table.html", domain=domain)
|
||||
|
||||
|
||||
# ── Metric formula CRUD (HTMX partials) ──────────────────────
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/formulas/add", methods=["POST"])
|
||||
def formula_add(domain_id: int):
|
||||
repo = get_repo()
|
||||
metric_name = request.form["metric_name"].strip()
|
||||
formula = request.form.get("formula", "").strip()
|
||||
if not metric_name or not formula:
|
||||
flash("Metric name and formula are required.", "error")
|
||||
else:
|
||||
repo.add_metric_formula(domain_id, MetricFormula(metric_name=metric_name, formula=formula))
|
||||
flash(f"Formula for '{metric_name}' added.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_formulas_table.html", domain=domain)
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/edit", methods=["POST"])
|
||||
def formula_edit(domain_id: int, formula_id: int):
|
||||
repo = get_repo()
|
||||
mf = MetricFormula(
|
||||
metric_name=request.form["metric_name"].strip(),
|
||||
formula=request.form.get("formula", "").strip(),
|
||||
)
|
||||
repo.update_metric_formula(formula_id, mf)
|
||||
flash("Formula updated.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_formulas_table.html", domain=domain)
|
||||
|
||||
|
||||
@bp.route("/<int:domain_id>/formulas/<int:formula_id>/delete", methods=["POST"])
|
||||
def formula_delete(domain_id: int, formula_id: int):
|
||||
repo = get_repo()
|
||||
repo.delete_metric_formula(formula_id)
|
||||
flash("Formula removed.", "success")
|
||||
domain = repo.get_domain_by_id(domain_id)
|
||||
return render_template("domains/_formulas_table.html", domain=domain)
|
||||
|
||||
Reference in New Issue
Block a user