92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import time
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
@dataclass
|
|
class LocalProblemIR:
|
|
assets: Dict[str, float] = field(default_factory=dict)
|
|
objectives: Dict[str, Any] = field(default_factory=dict)
|
|
risk_budgets: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class SharedVariableIR:
|
|
variables: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class PlanDeltaIR:
|
|
changes: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
@dataclass
|
|
class AuditLogEntry:
|
|
timestamp: float
|
|
action: str
|
|
details: str
|
|
|
|
|
|
class AuditLog:
|
|
def __init__(self) -> None:
|
|
self.entries: List[AuditLogEntry] = []
|
|
|
|
def add(self, action: str, details: str) -> None:
|
|
self.entries.append(AuditLogEntry(time.time(), action, details))
|
|
|
|
def to_json(self) -> str:
|
|
# minimal JSON-like representation
|
|
items = [
|
|
{"timestamp": e.timestamp, "action": e.action, "details": e.details}
|
|
for e in self.entries
|
|
]
|
|
return str(items)
|
|
|
|
|
|
def parse_dsl(text: str) -> Dict[str, Any]:
|
|
assets: Dict[str, float] = {}
|
|
objectives: Dict[str, Any] = {}
|
|
risk_budgets: Dict[str, Any] = {}
|
|
current: str | None = None
|
|
for raw_line in text.splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
if line.lower().startswith("assets:"):
|
|
current = "assets"
|
|
continue
|
|
if line.lower().startswith("objectives:"):
|
|
current = "objectives"
|
|
continue
|
|
if line.lower().startswith("risk_budgets:") or line.lower().startswith("risk budgets:"):
|
|
current = "risk_budgets"
|
|
continue
|
|
if line.endswith(":"):
|
|
# section placeholder
|
|
current = line[:-1].lower()
|
|
continue
|
|
# Parse key: value lines within sections
|
|
if ":" in line:
|
|
key, val = [p.strip() for p in line.split(":", 1)]
|
|
try:
|
|
num = float(val)
|
|
v = num
|
|
except ValueError:
|
|
v = val
|
|
if current == "assets":
|
|
assets[key] = v
|
|
elif current == "objectives":
|
|
objectives[key] = v
|
|
elif current == "risk_budgets" or current == "risk budgets":
|
|
risk_budgets[key] = v
|
|
else:
|
|
# default to assets-like mapping
|
|
assets[key] = v
|
|
ir = {
|
|
"objects": LocalProblemIR(assets=assets, objectives=objectives, risk_budgets=risk_budgets),
|
|
"morphisms": SharedVariableIR(variables={}),
|
|
"plan_delta": PlanDeltaIR(changes={}),
|
|
"audit_log": AuditLog(),
|
|
}
|
|
return ir
|