feat: 改进对象系统
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from .atom import Atom
|
||||
from .electron import Electron
|
||||
from .nucleon import Nucleon
|
||||
from .orbital import Orbital
|
||||
from .orbital import Orbital
|
||||
|
||||
@@ -5,7 +5,6 @@ from typing import TypedDict
|
||||
|
||||
import toml
|
||||
|
||||
from heurams.context import config_var
|
||||
from heurams.services.logger import get_logger
|
||||
|
||||
from .electron import Electron
|
||||
@@ -19,11 +18,13 @@ class AtomRegister_runtime(TypedDict):
|
||||
min_rate: int # 最低评分
|
||||
new_activation: bool # 新激活
|
||||
|
||||
|
||||
class AtomRegister(TypedDict):
|
||||
nucleon: Nucleon
|
||||
electron: Electron
|
||||
runtime: AtomRegister_runtime
|
||||
|
||||
|
||||
class Atom:
|
||||
"""
|
||||
统一处理一系列对象的所有信息与持久化:
|
||||
@@ -39,10 +40,10 @@ class Atom:
|
||||
"new_activation": False,
|
||||
}
|
||||
|
||||
def __init__(self, nucleon_obj = None, electron_obj = None, orbital_obj = None):
|
||||
self.ident = nucleon_obj["ident"] # type: ignore
|
||||
def __init__(self, nucleon_obj=None, electron_obj=None, orbital_obj=None):
|
||||
self.ident = nucleon_obj["ident"] # type: ignore
|
||||
self.registry: AtomRegister = { # type: ignore
|
||||
"ident": nucleon_obj["ident"], # type: ignore
|
||||
"ident": nucleon_obj["ident"], # type: ignore
|
||||
"nucleon": nucleon_obj,
|
||||
"electron": electron_obj,
|
||||
"orbital": orbital_obj,
|
||||
@@ -53,7 +54,7 @@ class Atom:
|
||||
self.registry["runtime"]["new_activation"] = True
|
||||
|
||||
def init_runtime(self):
|
||||
self.registry['runtime'] = AtomRegister_runtime(**self.default_runtime)
|
||||
self.registry["runtime"] = AtomRegister_runtime(**self.default_runtime)
|
||||
|
||||
def minimize(self, rating):
|
||||
"""效果等同于 self.registry['runtime']['min_rate'] = min(rating, self.registry['runtime']['min_rate'])
|
||||
@@ -97,4 +98,4 @@ class Atom:
|
||||
def __setitem__(self, key, value):
|
||||
if key == "ident":
|
||||
raise AttributeError("应为只读")
|
||||
self.registry[key] = value
|
||||
self.registry[key] = value
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
from copy import deepcopy
|
||||
from typing import TypedDict
|
||||
|
||||
import heurams.kernel.algorithms as algolib
|
||||
import heurams.services.timer as timer
|
||||
from heurams.context import config_var
|
||||
from heurams.kernel.algorithms import algorithms
|
||||
from heurams.services.logger import get_logger
|
||||
import heurams.kernel.algorithms as algolib
|
||||
from copy import deepcopy
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Electron:
|
||||
"""电子: 单算法支持的记忆数据包装"""
|
||||
|
||||
@@ -89,11 +91,11 @@ class Electron:
|
||||
def __len__(self):
|
||||
"""仅返回当前算法的配置数量"""
|
||||
return len(self.algodata[self.algo.algo_name])
|
||||
|
||||
|
||||
@staticmethod
|
||||
def create_on_electonic_data(electronic_data: tuple, algo_name: str = ""):
|
||||
_data = electronic_data
|
||||
ident = _data[0]
|
||||
algodata = _data[1]
|
||||
ident = ident
|
||||
return Electron(ident, algodata, algo_name)
|
||||
return Electron(ident, algodata, algo_name)
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
from heurams.services.logger import get_logger
|
||||
from copy import deepcopy
|
||||
|
||||
from heurams.services.logger import get_logger
|
||||
from heurams.utils.evalizor import Evalizer
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class Nucleon:
|
||||
"""原子核: 带有运行时隔离的模板化只读材料元数据容器
|
||||
"""
|
||||
"""原子核: 带有运行时隔离的模板化只读材料元数据容器"""
|
||||
|
||||
def __init__(self, ident, payload, common):
|
||||
self.ident = ident
|
||||
env = {
|
||||
"payload": payload
|
||||
}
|
||||
env = {"payload": payload}
|
||||
self.evalizer = Evalizer(environment=env)
|
||||
self.data = self.evalizer(deepcopy((payload | common)))
|
||||
|
||||
@@ -19,10 +19,10 @@ class Nucleon:
|
||||
if key == "ident":
|
||||
return self.ident
|
||||
return self.data[key]
|
||||
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
raise AttributeError("应为只读")
|
||||
|
||||
|
||||
def __delitem__(self, key):
|
||||
raise AttributeError("应为只读")
|
||||
|
||||
@@ -31,12 +31,12 @@ class Nucleon:
|
||||
|
||||
def __contains__(self, key):
|
||||
return key in (self.data)
|
||||
|
||||
|
||||
def get(self, key, default=None):
|
||||
if key in self:
|
||||
return self[key]
|
||||
return default
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return len(self.data)
|
||||
|
||||
@@ -48,5 +48,5 @@ class Nucleon:
|
||||
_data = nucleonic_data
|
||||
payload = _data[1][0]
|
||||
common = _data[1][1]
|
||||
ident = _data[0] #TODO:实现eval
|
||||
return Nucleon(ident, payload, common)
|
||||
ident = _data[0] # TODO:实现eval
|
||||
return Nucleon(ident, payload, common)
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
from heurams.utils.lict import Lict
|
||||
from heurams.utils.evalizor import Evalizer
|
||||
from heurams.utils.lict import Lict
|
||||
|
||||
class Orbital():
|
||||
|
||||
class Orbital:
|
||||
@classmethod
|
||||
def create_orbital(cls, schedule: list, phase_def: dict):
|
||||
phase_def = Lict(initdict=phase_def) # type: ignore
|
||||
phase_def = Lict(initdict=phase_def) # type: ignore
|
||||
orbital = Lict()
|
||||
for i in schedule:
|
||||
orbital[i] = Lict(phase_def[i])
|
||||
return orbital
|
||||
|
||||
@classmethod
|
||||
def create_orbital_on_orbitic_data(cls, orbitic_data):
|
||||
return cls.create_orbital(orbitic_data["schedule"], orbitic_data["phases"])
|
||||
return cls.create_orbital(orbitic_data["schedule"], orbitic_data["phases"])
|
||||
|
||||
Reference in New Issue
Block a user