93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
"""
|
|
Toy EnergiBridge DSL sketch
|
|
|
|
This module provides a minimal, self-contained set of classes inspired by
|
|
GridVerse primitives (LocalProblem, SharedVariables/DualVariables, PlanDelta)
|
|
to bootstrap interoperability ideas for the MVP. It is intentionally lightweight
|
|
and designed to be extended by future adapters and codegen paths.
|
|
|
|
Note: This is not a production bridge implementation. It exists to give the
|
|
community a concrete starting point for DSL sketches and simple tests.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Dict, List, Optional
|
|
import time
|
|
|
|
|
|
@dataclass
|
|
class LocalProblem:
|
|
"""A minimal representation of a local optimization task.
|
|
|
|
Attributes:
|
|
id: Unique identifier for the local problem instance.
|
|
name: Human-friendly name.
|
|
variables: A dictionary representing problem variables and their current values.
|
|
"""
|
|
|
|
id: str
|
|
name: str
|
|
variables: Dict[str, Any] = field(default_factory=dict)
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"variables": self.variables,
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class SharedSignal:
|
|
"""A cross-domain signal (shared variable) used by adapters.
|
|
|
|
This toy representation captures a value along with a version and a timestamp
|
|
to model delta-sync style semantics in a Tiny CDS (contract data space).
|
|
"""
|
|
|
|
name: str
|
|
value: Any
|
|
version: int = 0
|
|
timestamp: float = field(default_factory=time.time)
|
|
|
|
def bump(self, new_value: Any) -> None:
|
|
self.value = new_value
|
|
self.version += 1
|
|
self.timestamp = time.time()
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"name": self.name,
|
|
"value": self.value,
|
|
"version": self.version,
|
|
"timestamp": self.timestamp,
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class PlanDelta:
|
|
"""Incremental optimization plan deltas with metadata."""
|
|
|
|
changes: List[Dict[str, Any]]
|
|
timestamp: float = field(default_factory=time.time)
|
|
version: int = 0
|
|
nonce: Optional[str] = None
|
|
|
|
def add_change(self, change: Dict[str, Any]) -> None:
|
|
self.changes.append(change)
|
|
self.version += 1
|
|
self.timestamp = time.time()
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"changes": self.changes,
|
|
"timestamp": self.timestamp,
|
|
"version": self.version,
|
|
"nonce": self.nonce,
|
|
}
|
|
|
|
|
|
__all__ = ["LocalProblem", "SharedSignal", "PlanDelta"]
|