feat: add response type
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
from .common import Resolver, ResultPriority, ResolverRequest
|
||||
from .common import Resolver
|
||||
from .lut import LutResolver
|
||||
from .astar import AStarResolver
|
||||
|
||||
__all__ = [
|
||||
'Resolver',
|
||||
'ResultPriority',
|
||||
'ResolverRequest',
|
||||
'LutResolver',
|
||||
'AStarResolver'
|
||||
]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from typing import Iterator
|
||||
from .common import Resolver, ResolverRequest, ResultPriority
|
||||
from .common import Resolver, Request, ResultPriority
|
||||
from ..dataset import DatasetCollection
|
||||
from ..common import Circuit
|
||||
|
||||
@@ -12,5 +12,5 @@ class AStarResolver(Resolver):
|
||||
pass
|
||||
|
||||
|
||||
def resolve(self, request: ResolverRequest) -> Iterator[Circuit]:
|
||||
def resolve(self, request: Request) -> Iterator[Circuit]:
|
||||
pass
|
||||
|
||||
@@ -1,38 +1,5 @@
|
||||
import enum
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterator
|
||||
from ..common import DeviceKind, Circuit
|
||||
|
||||
|
||||
class ResultPriority(enum.Enum):
|
||||
"""
|
||||
The priority of the result.
|
||||
"""
|
||||
|
||||
LESS_DEVICES = enum.auto()
|
||||
"""Less devices is the first priority."""
|
||||
MORE_ACCURACY = enum.auto()
|
||||
"""More accuracy is the first priority."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class ResolverRequest:
|
||||
"""
|
||||
All request infomation for the resolver.
|
||||
"""
|
||||
|
||||
device_kind: DeviceKind
|
||||
"""The kind of device to resolve."""
|
||||
target_value: float
|
||||
"""The target value of the device."""
|
||||
tolerance: float
|
||||
"""The tolerance of the device in absolute value."""
|
||||
result_priority: ResultPriority
|
||||
"""The priority of the result."""
|
||||
count_limit: int
|
||||
"""The limited count of results."""
|
||||
|
||||
from ..query import Request, Response
|
||||
|
||||
class Resolver(ABC):
|
||||
"""
|
||||
@@ -40,5 +7,5 @@ class Resolver(ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def resolve(self, request: ResolverRequest) -> Iterator[Circuit]:
|
||||
def resolve(self, request: Request) -> Response:
|
||||
pass
|
||||
|
||||
@@ -2,9 +2,10 @@ import heapq
|
||||
from itertools import chain, product
|
||||
from typing import Iterable, Iterator
|
||||
from functools import cached_property
|
||||
from .common import Resolver, ResolverRequest, ResultPriority
|
||||
from .common import Resolver
|
||||
from ..dataset import DatasetCollection, Dataset
|
||||
from ..common import Circuit, DeviceKind, JointKind, LcrConnException
|
||||
from ..common import Circuit, DeviceKind, JointKind
|
||||
from ..query import Request, Response
|
||||
|
||||
|
||||
class LutItem:
|
||||
@@ -67,13 +68,13 @@ class ResultBucket(Iterable[LutItem]):
|
||||
def score(self) -> float:
|
||||
"""The score associated with this item."""
|
||||
return self.__score
|
||||
|
||||
|
||||
@property
|
||||
def item(self) -> LutItem:
|
||||
"""The underlying LutItem."""
|
||||
return self.__item
|
||||
|
||||
def __lt__(self, other: 'ResultBucket.ResultBucketItem') -> bool:
|
||||
def __lt__(self, other: "ResultBucket.ResultBucketItem") -> bool:
|
||||
# heapq is a min-heap: it always pops the smallest element.
|
||||
# We invert the comparison so that an item with a larger score
|
||||
# is considered "smaller", effectively turning the min-heap
|
||||
@@ -179,7 +180,7 @@ class LutResolver(Resolver):
|
||||
)
|
||||
]
|
||||
|
||||
def resolve(self, request: ResolverRequest) -> Iterator[Circuit]:
|
||||
def resolve(self, request: Request) -> Response:
|
||||
# Fetch LUT by device kind
|
||||
lut: list[LutItem]
|
||||
match request.device_kind:
|
||||
@@ -202,4 +203,4 @@ class LutResolver(Resolver):
|
||||
bucket.insert(item, difference)
|
||||
|
||||
# Return result
|
||||
return map(lambda item: item.circuit, bucket)
|
||||
return Response(request, map(lambda item: item.circuit, bucket))
|
||||
|
||||
Reference in New Issue
Block a user