Skip to content
Snippets Groups Projects
Commit 32e9a0e8 authored by Simon Spinner's avatar Simon Spinner
Browse files

Introduce easier way to define notifications.

parent aabe1f9d
No related branches found
No related tags found
No related merge requests found
package tools.descartes.prisma.core.scopes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
......@@ -8,9 +9,11 @@ import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import edu.kit.ipd.descartes.core.NamedElement;
import edu.kit.ipd.descartes.mm.adaptationpoints.AdaptationPointDescriptions;
import edu.kit.ipd.descartes.mm.adaptationpoints.AdaptationpointsFactory;
import tools.descartes.prisma.core.MessageBus;
import tools.descartes.prisma.core.MessageBus.ExchangeType;
import tools.descartes.prisma.core.MessageBus.Listener;
import tools.descartes.prisma.core.ModelRepository;
import tools.descartes.prisma.core.Transaction;
......@@ -39,9 +42,38 @@ public abstract class AgentScope extends Scope {
private AdaptationPointDescriptions adaptationPoints;
private final SystemGlobalScope systemScope;
private final Map<String, AgentScope> scopeCache = new HashMap<>();
private final List<NotificationDefinition> notifications = new ArrayList<>();
protected class DelegationListener implements Listener {
protected class NotificationDefinition {
private final Class<?> key;
private final String exchange;
private final ExchangeType type;
public NotificationDefinition(Class<?> key, String exchange, ExchangeType type) {
this.key = key;
this.exchange = exchange;
this.type = type;
}
public void create() {
getMessageBus().createExchange(exchange, type);
}
public boolean isActive(Object o) {
return key.isInstance(o);
}
public void send(String notificationType, Object o) {
getMessageBus().publishMessage(exchange, getRoutingKey(o), "",
new ModelNotification(getModelRepository(), notificationType, key, o));
}
public String getRoutingKey(Object o) {
return ((NamedElement) o).getName();
}
}
protected class DelegationListener implements Listener {
@Override
public void newMessage(String routingKey, Map<String, Object> headers, String message) {
ModelNotification notification = new ModelNotification(getModelRepository(), headers);
......@@ -255,10 +287,11 @@ public abstract class AgentScope extends Scope {
}
public void sendNotifications(String notificationType, EObject object) {
}
protected void sendNotificationMessage(String exchange, String routingKey, ModelNotification notification) {
getMessageBus().publishMessage(exchange, routingKey, "", notification);
for (NotificationDefinition curNotification : notifications) {
if (curNotification.isActive(object)) {
curNotification.send(notificationType, object);
}
}
}
public void addNotificationHandler(String exchange, String routingKey, final AgentController agent) {
......@@ -273,4 +306,8 @@ public abstract class AgentScope extends Scope {
});
}
protected void declareNotification(NotificationDefinition notification) {
this.notifications.add(notification);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment