Skip to content

Commit

Permalink
EnvironmentView interface renamed to EnvironmentListener, NQueensBoar…
Browse files Browse the repository at this point in the history
…d cleaned up.
  • Loading branch information
RuedigerLunde committed Jun 1, 2019
1 parent c3708ef commit 3a78145
Show file tree
Hide file tree
Showing 40 changed files with 268 additions and 322 deletions.
22 changes: 11 additions & 11 deletions aima-core/src/main/java/aima/core/agent/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,26 @@ default void stepUntilDone() {
double getPerformanceMeasure(Agent<?, ?> agent);

/**
* Add a view on the Environment.
* Add a listener which is notified about environment changes.
*
* @param view
* the EnvironmentView to be added.
* @param listener
* the listener to be added.
*/
void addEnvironmentView(EnvironmentView<? super P, ? super A> view);
void addEnvironmentListener(EnvironmentListener<? super P, ? super A> listener);

/**
* Remove a view on the Environment.
* Remove a listener.
*
* @param view
* the EnvironmentView to be removed.
* @param listener
* the listener to be removed.
*/
void removeEnvironmentView(EnvironmentView<? super P, ? super A> view);
void removeEnvironmentListener(EnvironmentListener<? super P, ? super A> listener);

/**
* Notify all registered EnvironmentViews of a message.
* Notify all environment listeners of a message.
*
* @param msg
* the message to notify the registered EnvironmentViews with.
* the message to notify the registered listeners with.
*/
void notifyViews(String msg);
void notify(String msg);
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
package aima.core.agent;

/**
* Allows external applications/logic to view the interaction of Agent(s) with
* an Environment.
*
* @param <P> Type which is used to represent percepts
* @param <A> Type which is used to represent actions
* @author Ravi Mohan
* @author Ciaran O'Reilly
* @author Mike Stampone
* @author Ruediger Lunde
*/
public interface EnvironmentView<P, A> {
/**
* A simple notification message from an object in the Environment.
*
* @param msg
* the message received.
*/
void notify(String msg);

/**
* Indicates an Agent has been added to the environment and what it
* perceives initially.
*
* @param agent
* the Agent just added to the Environment.
* @param source
* the Environment to which the agent was added.
*/
void agentAdded(Agent<?, ?> agent, Environment<?, ?> source);

/**
* Indicates the Environment has changed as a result of an Agent's action.
*
* @param agent
* the Agent that performed the Action.
* @param percept
* the Percept the Agent received from the environment.
* @param action
* the Action the Agent performed.
* @param source
* the Environment in which the agent has acted.
*/
void agentActed(Agent<?, ?> agent, P percept, A action, Environment<?, ?> source);
}
package aima.core.agent;

/**
* Allows applications to analyze and visualize the interaction of Agent(s) with an Environment.
*
* @param <P> Type which is used to represent percepts
* @param <A> Type which is used to represent actions
* @author Ravi Mohan
* @author Ciaran O'Reilly
* @author Mike Stampone
* @author Ruediger Lunde
*/
public interface EnvironmentListener<P, A> {
/**
* A simple notification message from an object in the Environment.
*
* @param msg
* the message received.
*/
void notify(String msg);

/**
* Indicates an Agent has been added to the environment and what it
* perceives initially.
*
* @param agent
* the Agent just added to the Environment.
* @param source
* the Environment to which the agent was added.
*/
void agentAdded(Agent<?, ?> agent, Environment<?, ?> source);

/**
* Indicates the Environment has changed as a result of an Agent's action.
*
* @param agent
* the Agent that performed the Action.
* @param percept
* the Percept the Agent received from the environment.
* @param action
* the Action the Agent performed.
* @param source
* the Environment in which the agent has acted.
*/
void agentActed(Agent<?, ?> agent, P percept, A action, Environment<?, ?> source);
}

This file was deleted.

16 changes: 16 additions & 0 deletions aima-core/src/main/java/aima/core/agent/Notifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package aima.core.agent;

/**
* @author Ciaran O'Reilly
* @author Ruediger Lunde
*
*/
public interface Notifier {
/**
* A simple notification message, to be forwarded to someone.
*
* @param msg
* the message to be forwarded.
*/
void notify(String msg);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
* @author Ruediger Lunde
*/
public abstract class AbstractEnvironment<P, A> implements Environment<P, A>,
EnvironmentViewNotifier {
Notifier {

// Note: Use LinkedHashSet's in order to ensure order is respected.
// get methods provide access to these elements via a List interface.
protected Set<Agent<? super P, ? extends A>> agents = new LinkedHashSet<>();
protected Set<EnvironmentObject> envObjects = new LinkedHashSet<>();

protected Set<EnvironmentView<? super P, ? super A>> views = new LinkedHashSet<>();
protected Set<EnvironmentListener<? super P, ? super A>> listeners = new LinkedHashSet<>();
protected Map<Agent<?, ?>, Double> performanceMeasures = new LinkedHashMap<>();


Expand All @@ -34,7 +34,7 @@ public abstract class AbstractEnvironment<P, A> implements Environment<P, A>,
public void addAgent(Agent<? super P, ? extends A> agent) {
agents.add(agent);
addEnvironmentObject(agent);
notifyViews(agent);
notify(agent);
}

@Override
Expand Down Expand Up @@ -73,7 +73,7 @@ public void step() {
Optional<? extends A> anAction = agent.execute(percept);
if (anAction.isPresent()) {
executeAction(agent, anAction.get());
notifyViews(agent, percept, anAction.get());
notify(agent, percept, anAction.get());
} else {
executeNoOp(agent);
}
Expand Down Expand Up @@ -122,18 +122,18 @@ public double getPerformanceMeasure(Agent<?, ?> agent) {
}

@Override
public void addEnvironmentView(EnvironmentView<? super P, ? super A> ev) {
views.add(ev);
public void addEnvironmentListener(EnvironmentListener<? super P, ? super A> listener) {
listeners.add(listener);
}

@Override
public void removeEnvironmentView(EnvironmentView ev) {
views.remove(ev);
public void removeEnvironmentListener(EnvironmentListener listener) {
listeners.remove(listener);
}

@Override
public void notifyViews(String msg) {
views.forEach(view -> view.notify(msg));
public void notify(String msg) {
listeners.forEach(listener -> listener.notify(msg));
}

//
Expand All @@ -143,11 +143,11 @@ protected void updatePerformanceMeasure(Agent<?, ?> forAgent, double addTo) {
performanceMeasures.put(forAgent, getPerformanceMeasure(forAgent) + addTo);
}

protected void notifyViews(Agent<?, ?> agent) {
views.forEach(view -> view.agentAdded(agent, this));
protected void notify(Agent<?, ?> agent) {
listeners.forEach(listener -> listener.agentAdded(agent, this));
}

protected void notifyViews(Agent<?, ?> agent, P percept, A action) {
views.forEach(view -> view.agentActed(agent, percept, action, this));
protected void notify(Agent<?, ?> agent, P percept, A action) {
listeners.forEach(listener -> listener.agentActed(agent, percept, action, this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import aima.core.agent.Agent;
import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView;
import aima.core.agent.EnvironmentListener;

/**
* Environment view implementation which logs performed action and
* Environment listener implementation which logs performed action and
* provides a comma-separated String with all actions performed so far.
*
* @author Ruediger Lunde
*/
public class SimpleActionTracker implements EnvironmentView<Object, Object> {
public class SimpleActionTracker implements EnvironmentListener<Object, Object> {
protected final StringBuilder actions = new StringBuilder();

public String getActions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import aima.core.agent.Agent;
import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView;
import aima.core.agent.EnvironmentListener;

/**
* Simple environment view which uses the standard output stream to inform about
* Simple environment listener which uses the standard output stream to inform about
* relevant events.
*
* @author Ruediger Lunde
*/
public class SimpleEnvironmentView implements EnvironmentView<Object, Object> {
public class SimpleEnvironmentView implements EnvironmentListener<Object, Object> {
@Override
public void notify(String msg) {
System.out.println("Message: " + msg);
Expand Down
10 changes: 5 additions & 5 deletions aima-core/src/main/java/aima/core/environment/map/MapAgent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aima.core.environment.map;

import aima.core.agent.EnvironmentViewNotifier;
import aima.core.agent.Notifier;
import aima.core.agent.impl.DynamicPercept;
import aima.core.agent.impl.DynamicState;
import aima.core.search.agent.ProblemSolvingAgent;
Expand Down Expand Up @@ -33,7 +33,7 @@ public class MapAgent extends ProblemSolvingAgent<DynamicPercept, String, MoveTo

private SearchForActions<String, MoveToAction> search;
private Function<String, ToDoubleFunction<Node<String, MoveToAction>>> hFnFactory;
protected EnvironmentViewNotifier notifier;
protected Notifier notifier;

public MapAgent(Map map, SearchForActions<String, MoveToAction> search, String goal) {
this.map = map;
Expand Down Expand Up @@ -63,7 +63,7 @@ public MapAgent(Map map, SearchForActions<String, MoveToAction> search, List<Str
}

/** Sets a notifier which gets informed about decisions of the agent */
public MapAgent setNotifier(EnvironmentViewNotifier notifier) {
public MapAgent setNotifier(Notifier notifier) {
this.notifier = notifier;
return this;
}
Expand All @@ -85,7 +85,7 @@ protected Optional<Object> formulateGoal() {
if (hFnFactory != null && search instanceof Informed)
((Informed<String, MoveToAction>) search).setHeuristicFunction(hFnFactory.apply(goal));
if (notifier != null)
notifier.notifyViews("Current location: In(" + state.getAttribute(AttNames.AGENT_LOCATION)
notifier.notify("Current location: In(" + state.getAttribute(AttNames.AGENT_LOCATION)
+ "), Goal: In(" + goal + ")");
}
return Optional.ofNullable(goal);
Expand All @@ -101,7 +101,7 @@ protected Problem<String, MoveToAction> formulateProblem(Object goal) {
protected Optional<List<MoveToAction>> search(Problem<String, MoveToAction> problem) {
Optional<List<MoveToAction>> result = search.findActions(problem);
if (notifier != null)
notifier.notifyViews("Search" + search.getMetrics());
notifier.notify("Search" + search.getMetrics());
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package aima.core.environment.map;

import aima.core.agent.EnvironmentViewNotifier;
import aima.core.agent.Notifier;
import aima.core.agent.impl.DynamicPercept;
import aima.core.agent.impl.DynamicState;
import aima.core.search.agent.SimpleProblemSolvingAgent;
Expand Down Expand Up @@ -29,7 +29,7 @@ public class SimpleMapAgent extends SimpleProblemSolvingAgent<DynamicPercept, St
private SearchForActions<String, MoveToAction> search;
private List<String> goals;
private int nextGoalPos = 0;
private EnvironmentViewNotifier notifier;
private Notifier notifier;

/** Randomly generates goals forever. */
public SimpleMapAgent(Map map, SearchForActions<String, MoveToAction> search) {
Expand All @@ -51,7 +51,7 @@ public SimpleMapAgent(Map map, SearchForActions<String, MoveToAction> search, Li
}

/** Sets a notifier which gets informed about decisions of the agent */
public SimpleMapAgent setNotifier(EnvironmentViewNotifier notifier) {
public SimpleMapAgent setNotifier(Notifier notifier) {
this.notifier = notifier;
return this;
}
Expand All @@ -72,7 +72,7 @@ protected Optional<Object> formulateGoal() {
else if (nextGoalPos < goals.size())
goal = goals.get(nextGoalPos++);
if (goal != null && notifier != null)
notifier.notifyViews("CurrentLocation=In(" + state.getAttribute(AttNames.AGENT_LOCATION)
notifier.notify("CurrentLocation=In(" + state.getAttribute(AttNames.AGENT_LOCATION)
+ "), Goal=In(" + goal + ")");
return Optional.ofNullable(goal);
}
Expand All @@ -86,7 +86,7 @@ protected Problem<String, MoveToAction> formulateProblem(Object goal) {
protected Optional<List<MoveToAction>> search(Problem<String, MoveToAction> problem) {
Optional<List<MoveToAction>> result = search.findActions(problem);
if (notifier != null)
notifier.notifyViews("Search" + search.getMetrics());
notifier.notify("Search" + search.getMetrics());
return result;
}
}
Loading

0 comments on commit 3a78145

Please sign in to comment.