From 1345eb94add94353e050491a5c49c788a2e3820f Mon Sep 17 00:00:00 2001 From: Pastor Date: Mon, 7 Oct 2024 00:22:54 +0300 Subject: [PATCH] Clever lector --- .../mifi/practice/vol1/agent/Environment.java | 14 +++---- .../mifi/practice/vol1/agent/Registrar.java | 10 +++++ .../mifi/practice/vol1/agent/Transport.java | 2 +- .../practice/vol1/agent/model/Institute.java | 39 ++++++++++++++++++- 4 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 vol1/src/main/java/ru/mifi/practice/vol1/agent/Registrar.java diff --git a/vol1/src/main/java/ru/mifi/practice/vol1/agent/Environment.java b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Environment.java index b43649a..fe4136d 100644 --- a/vol1/src/main/java/ru/mifi/practice/vol1/agent/Environment.java +++ b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Environment.java @@ -11,14 +11,10 @@ import static ru.mifi.practice.vol1.agent.Transport.Message; import static ru.mifi.practice.vol1.agent.Transport.Replay; -public interface Environment extends Using { +public interface Environment extends Using, Registrar { Snapshot snapshot(); - void register(Agent.Factory factory); - - void register(Agent.Iterator iterator); - void subscribe(Listener listener); default void tick() { @@ -63,7 +59,11 @@ public void register(Agent.Factory factory) { } private void register(Agent agent) { - agents.put(agent.id(), agent); + Object id = agent.id(); + if (agents.containsKey(id)) { + throw new IllegalArgumentException("Agent " + id + " is already registered"); + } + agents.put(id, agent); subscribe(agent); } @@ -85,7 +85,7 @@ public void subscribe(Listener listener) { public Optional receive(Object target, Object source, Message message) { if (target == this || target == null) { listeners.forEach(listener -> listener.onEvent( - new EventMessage(message, this, source))); + new EventMessage(message, this, source))); } Agent agent = agents.get(target); if (agent != null) { diff --git a/vol1/src/main/java/ru/mifi/practice/vol1/agent/Registrar.java b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Registrar.java new file mode 100644 index 0000000..b6120f0 --- /dev/null +++ b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Registrar.java @@ -0,0 +1,10 @@ +package ru.mifi.practice.vol1.agent; + +import static ru.mifi.practice.vol1.agent.Agent.Factory; +import static ru.mifi.practice.vol1.agent.Agent.Iterator; + +public interface Registrar { + void register(Factory factory); + + void register(Iterator iterator); +} diff --git a/vol1/src/main/java/ru/mifi/practice/vol1/agent/Transport.java b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Transport.java index 765ad08..adb0bbd 100644 --- a/vol1/src/main/java/ru/mifi/practice/vol1/agent/Transport.java +++ b/vol1/src/main/java/ru/mifi/practice/vol1/agent/Transport.java @@ -7,7 +7,7 @@ public interface Transport { Optional send(Object target, Message message); default Optional send(Message message) { - return send(this, message); + return send(null, message); } interface Message { diff --git a/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java b/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java index 054e52e..ab53da9 100644 --- a/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java +++ b/vol1/src/main/java/ru/mifi/practice/vol1/agent/model/Institute.java @@ -30,6 +30,25 @@ public Optional next(Transport transport) { return Optional.of(humans[index++].create(transport)); } }); + this.environment.subscribe(new Event.Listener() { + @Override + public void onEvent(Event event) { + if (event instanceof Event.EventMessage eventMessage + && eventMessage.message() instanceof Messages message + && eventMessage.source() != this && message == Messages.SEND_FLY) { + environment.register(new Agent.Factory() { + private int flyCounter = 0; + + @Override + public Agent create(Transport transport) { + return new Fly(String.format("fly_%d", ++flyCounter), + eventMessage.source(), transport); + } + }); + } + } + }); + this.environment.subscribe(System.out::println); } public void tick() { @@ -37,7 +56,7 @@ public void tick() { } enum Messages implements Transport.Replay { - NOISE, WAT + NOISE, WAT, SEND_FLY } private record Human(String name, Class klass) implements Agent.Factory { @@ -123,4 +142,22 @@ public void tick(Environment.Snapshot snapshot) { throw new UnsupportedOperationException(); } } + + private record Fly(String id, Object owner, Transport transport) implements Agent { + + @Override + public Optional call(Transport.Message message) { + return Optional.empty(); + } + + @Override + public void onEvent(Event event) { + //TODO: + } + + @Override + public void tick(Environment.Snapshot snapshot) { + //TODO: + } + } }