40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from typing import Any, Dict
|
|
|
|
from .models import LocalProblem
|
|
|
|
|
|
class DSLParser:
|
|
@staticmethod
|
|
def parse_local_problem(text: str) -> LocalProblem:
|
|
# Extremely small DSL parody: JSON-like DSL but still parseable
|
|
# Example:
|
|
# LocalProblem { id: lp-001, neighborhood: Downtown, capacity: 1000, equity_budget: 0.1, tasks: [{type: evac, constraints: {}}] }
|
|
try:
|
|
# naive: extract JSON-like portion between first '{' and last '}'
|
|
start = text.find("{")
|
|
end = text.rfind("}")
|
|
if start == -1 or end == -1:
|
|
raise ValueError("Invalid DSL format")
|
|
payload = text[start : end + 1]
|
|
data = json.loads(payload)
|
|
except Exception:
|
|
# fallback to a simple default LocalProblem
|
|
data = {
|
|
"id": "lp-000",
|
|
"neighborhood": "Unknown",
|
|
"tasks": [],
|
|
"capacity": 0,
|
|
"equity_budget": 0.0,
|
|
}
|
|
# Build LocalProblem from dict keys
|
|
return LocalProblem.from_dict({
|
|
"id": data.get("id", "lp-000"),
|
|
"neighborhood": data.get("neighborhood", "Unknown"),
|
|
"tasks": data.get("tasks", []),
|
|
"capacity": data.get("capacity", 0),
|
|
"equity_budget": data.get("equity_budget", 0.0),
|
|
})
|