81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
# morphology.py
|
|
import time
|
|
from typing import Any, Callable, Dict, List, Union
|
|
|
|
MorphologyType = Union[str, Callable[[str], List[Dict[str, Any]]]]
|
|
|
|
|
|
def generate_id() -> float:
|
|
now = time.time()
|
|
return 1.0 / now
|
|
|
|
|
|
def get_InitSensor(morphology: MorphologyType) -> Dict[str, Any]:
|
|
sensors = get_Sensors(morphology)
|
|
if not sensors:
|
|
raise ValueError("Morphology has no sensors.")
|
|
return sensors[0]
|
|
|
|
|
|
def get_InitActuator(morphology: MorphologyType) -> Dict[str, Any]:
|
|
actuators = get_Actuators(morphology)
|
|
if not actuators:
|
|
raise ValueError("Morphology has no actuators.")
|
|
return actuators[0]
|
|
|
|
|
|
def get_Sensors(morphology: MorphologyType) -> List[Dict[str, Any]]:
|
|
fn = _resolve_morphology(morphology)
|
|
return fn("sensors")
|
|
|
|
|
|
def get_Actuators(morphology: MorphologyType) -> List[Dict[str, Any]]:
|
|
fn = _resolve_morphology(morphology)
|
|
return fn("actuators")
|
|
|
|
|
|
def _resolve_morphology(morphology: MorphologyType) -> Callable[[str], List[Dict[str, Any]]]:
|
|
if callable(morphology):
|
|
return morphology
|
|
|
|
# 2) String -> Registry
|
|
if isinstance(morphology, str):
|
|
reg = {
|
|
"xor_mimic": xor_mimic,
|
|
}
|
|
if morphology in reg:
|
|
return reg[morphology]
|
|
raise ValueError(f"Unknown morphology name: {morphology}")
|
|
|
|
try:
|
|
# Ist es ein Modul mit einer Funktion 'xor_mimic'?
|
|
if hasattr(morphology, "xor_mimic") and callable(getattr(morphology, "xor_mimic")):
|
|
return getattr(morphology, "xor_mimic")
|
|
except Exception:
|
|
pass
|
|
|
|
raise TypeError("morphology must be a callable, a module with 'xor_mimic', or a registered string key")
|
|
|
|
|
|
def xor_mimic(kind: str) -> List[Dict[str, Any]]:
|
|
if kind == "sensors":
|
|
return [
|
|
{
|
|
"id": generate_id(),
|
|
"name": "xor_GetInput",
|
|
"vector_length": 2,
|
|
"scape": {"private": "xor_sim"}
|
|
}
|
|
]
|
|
elif kind == "actuators":
|
|
return [
|
|
{
|
|
"id": generate_id(),
|
|
"name": "xor_SendOutput",
|
|
"vector_length": 1,
|
|
"scape": {"private": "xor_sim"}
|
|
}
|
|
]
|
|
else:
|
|
raise ValueError(f"xor_mimic: unsupported kind '{kind}', expected 'sensors' or 'actuators'")
|