Skip to content

Commit

Permalink
Clever lector
Browse files Browse the repository at this point in the history
  • Loading branch information
Pastor committed Oct 6, 2024
1 parent 7f89554 commit 1345eb9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
14 changes: 7 additions & 7 deletions vol1/src/main/java/ru/mifi/practice/vol1/agent/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}

Expand All @@ -85,7 +85,7 @@ public void subscribe(Listener listener) {
public Optional<Replay> 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) {
Expand Down
10 changes: 10 additions & 0 deletions vol1/src/main/java/ru/mifi/practice/vol1/agent/Registrar.java
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface Transport {
Optional<Replay> send(Object target, Message message);

default Optional<Replay> send(Message message) {
return send(this, message);
return send(null, message);
}

interface Message {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,33 @@ public Optional<Agent> 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() {
environment.tick();
}

enum Messages implements Transport.Replay {
NOISE, WAT
NOISE, WAT, SEND_FLY
}

private record Human(String name, Class<? extends Agent> klass) implements Agent.Factory {
Expand Down Expand Up @@ -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<Transport.Replay> call(Transport.Message message) {
return Optional.empty();
}

@Override
public void onEvent(Event event) {
//TODO:
}

@Override
public void tick(Environment.Snapshot snapshot) {
//TODO:
}
}
}

0 comments on commit 1345eb9

Please sign in to comment.