# actors/sensor.py from actor import Actor import random class Sensor(Actor): def __init__(self, sid, cx_pid, name, vector_length, fanout_pids, scape=None): super().__init__(f"Sensor-{sid}") self.sid = sid self.cx_pid = cx_pid self.sname = name self.vl = vector_length self.fanout = fanout_pids self.scape = scape async def run(self): while True: print("sensor running...") msg = await self.inbox.get() tag = msg[0] print("got sensor message: ", msg) if tag == "sync": vec = await self._sense() print("sensed vec: ", vec) for pid in self.fanout: await pid.send(("forward", self.sid, vec)) elif tag == "terminate": return async def _sense(self): if self.sname == "rng": return [random.random() for _ in range(self.vl)] elif self.sname == "xor_GetInput" and self.scape: print("TODO") await self.scape.send(("sense", self.sid, self)) msg = await self.inbox.get() if msg[0] == "percept": return msg[1] else: return [0.0] * self.vl else: return [0.0] * self.vl