build(agent): molt-az#4b796a iteration
This commit is contained in:
parent
952f4503b8
commit
dda27c74ee
|
|
@ -0,0 +1,92 @@
|
||||||
|
"""
|
||||||
|
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"]
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
import time
|
||||||
|
from gridverse.energi_bridge_skeleton import LocalProblem, SharedSignal, PlanDelta
|
||||||
|
|
||||||
|
|
||||||
|
def test_local_problem_basic():
|
||||||
|
lp = LocalProblem(id="lp-001", name="test-problem", variables={"p1": 1.0})
|
||||||
|
d = lp.to_dict()
|
||||||
|
assert d["id"] == "lp-001"
|
||||||
|
assert d["name"] == "test-problem"
|
||||||
|
assert d["variables"]["p1"] == 1.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_shared_signal_basic_and_bump():
|
||||||
|
s = SharedSignal(name="forecast", value=12.5)
|
||||||
|
old_version = s.version
|
||||||
|
s.bump(13.0)
|
||||||
|
assert s.value == 13.0
|
||||||
|
assert s.version == old_version + 1
|
||||||
|
assert isinstance(s.to_dict(), dict)
|
||||||
|
|
||||||
|
|
||||||
|
def test_plan_delta_changes_and_serialization():
|
||||||
|
pd = PlanDelta(changes=[{"action": "set", "key": "v1", "val": 42}])
|
||||||
|
prev_version = pd.version
|
||||||
|
pd.add_change({"action": "update", "key": "v1", "val": 43})
|
||||||
|
assert pd.version == prev_version + 1
|
||||||
|
as_dict = pd.to_dict()
|
||||||
|
assert isinstance(as_dict, dict)
|
||||||
|
assert "changes" in as_dict
|
||||||
Loading…
Reference in New Issue