use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class EventBotActionUtils method makeAndSubscribeResponseListener.
// ************************************************ EventListener ***************************************************
/**
* Creates a listener that waits for the response to the specified message. If a SuccessResponse is received,
* the successCallback is executed, if a FailureResponse is received, the failureCallback is executed.
* @param outgoingMessage
* @param successCallback
* @param failureCallback
* @param context
* @return
*/
public static EventListener makeAndSubscribeResponseListener(final WonMessage outgoingMessage, final EventListener successCallback, final EventListener failureCallback, EventListenerContext context) {
// create an event listener that processes the response to the wonMessage we're about to send
EventListener listener = new ActionOnFirstEventListener(context, OriginalMessageUriResponseEventFilter.forWonMessage(outgoingMessage), new BaseEventBotAction(context) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
if (event instanceof SuccessResponseEvent) {
successCallback.onEvent(event);
} else if (event instanceof FailureResponseEvent) {
failureCallback.onEvent(event);
}
}
});
context.getEventBus().subscribe(SuccessResponseEvent.class, listener);
context.getEventBus().subscribe(FailureResponseEvent.class, listener);
return listener;
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class ExecuteCreateNeedCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof CreateNeedCommandEvent))
return;
CreateNeedCommandEvent createNeedCommandEvent = (CreateNeedCommandEvent) event;
Dataset needDataset = createNeedCommandEvent.getNeedDataset();
List<URI> facets = createNeedCommandEvent.getFacets();
if (needDataset == null) {
logger.warn("CreateNeedCommandEvent did not contain a need model, aborting need creation");
getEventListenerContext().getEventBus().publish(new NeedCreationAbortedEvent(null, null, createNeedCommandEvent, "CreateNeedCommandEvent did not contain a need model, aborting need creation"));
return;
}
URI needUriFromProducer = null;
Resource needResource = WonRdfUtils.NeedUtils.getNeedResource(needDataset);
if (needResource.isURIResource()) {
needUriFromProducer = URI.create(needResource.getURI().toString());
RdfUtils.replaceBaseURI(needDataset, needResource.getURI());
} else {
RdfUtils.replaceBaseResource(needDataset, needResource);
}
final URI needUriBeforeCreation = needUriFromProducer;
NeedModelWrapper needModelWrapper = new NeedModelWrapper(needDataset);
for (URI facetURI : facets) {
WonRdfUtils.FacetUtils.addFacet(needModelWrapper.getNeedModel(), facetURI);
}
final Dataset needDatasetWithFacets = needModelWrapper.copyDataset();
final URI wonNodeUri = getEventListenerContext().getNodeURISource().getNodeURI();
logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(needDatasetWithFacets), 150));
WonNodeInformationService wonNodeInformationService = getEventListenerContext().getWonNodeInformationService();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, needDatasetWithFacets, createNeedCommandEvent.isUsedForTesting(), createNeedCommandEvent.isDoNotMatch());
// remember the need URI so we can react to success/failure responses
EventBotActionUtils.rememberInList(getEventListenerContext(), needURI, createNeedCommandEvent.getUriListName());
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
getEventListenerContext().getEventBus().publish(new CreateNeedCommandSuccessEvent(needURI, needUriBeforeCreation, createNeedCommandEvent));
}
};
EventListener failureCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
getEventListenerContext().getEventBus().publish(new CreateNeedCommandFailureEvent(needURI, needUriBeforeCreation, createNeedCommandEvent, textMessage));
EventBotActionUtils.removeFromList(getEventListenerContext(), needURI, createNeedCommandEvent.getUriListName());
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, getEventListenerContext());
logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
getEventListenerContext().getWonMessageSender().sendWonMessage(createNeedMessage);
logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class ExecuteDeactivateNeedCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof DeactivateNeedCommandEvent))
return;
DeactivateNeedCommandEvent deactivateNeedCommandEvent = (DeactivateNeedCommandEvent) event;
EventListenerContext ctx = getEventListenerContext();
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
WonMessage deactivateNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri);
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
bus.publish(new DeactivateNeedCommandSuccessEvent(needURI, deactivateNeedCommandEvent));
}
};
EventListener failureCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
logger.debug("need creation failed for need URI {}, original message URI {}: {}", new Object[] { needURI, ((FailureResponseEvent) event).getOriginalMessageURI(), textMessage });
bus.publish(new DeactivateNeedCommandFailureEvent(needURI, deactivateNeedCommandEvent, textMessage));
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(deactivateNeedMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", deactivateNeedMessage.getMessageURI());
ctx.getWonMessageSender().sendWonMessage(deactivateNeedMessage);
logger.debug("need creation message sent with message URI {}", deactivateNeedMessage.getMessageURI());
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class BehaviourBarrier method onActivate.
@Override
protected void onActivate(Optional<Object> message) {
Set<BotBehaviour> deactivatedBehaviours = Collections.synchronizedSet(new HashSet<>());
subscribeWithAutoCleanup(BotBehaviourDeactivatedEvent.class, new ActionOnEventListener(context, new EventFilter() {
@Override
public boolean accept(Event event) {
if (!(event instanceof BotBehaviourDeactivatedEvent))
return false;
return behavioursToWaitFor.contains(((BotBehaviourDeactivatedEvent) event).getBehaviour());
}
}, new BaseEventBotAction(context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
synchronized (behavioursToWaitFor) {
deactivatedBehaviours.add(((BotBehaviourDeactivatedEvent) event).getBehaviour());
if (deactivatedBehaviours.containsAll(behavioursToWaitFor)) {
behavioursToStart.forEach(behaviour -> behaviour.activate());
deactivate();
}
}
}
}));
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class BotBehaviour method cleanup.
private final synchronized void cleanup() {
onCleanup();
for (EventListener eventListener : activeListeners) {
context.getEventBus().unsubscribe(eventListener);
}
activeListeners.clear();
active.set(false);
}
Aggregations