use of won.bot.framework.eventbot.event.NeedSpecificEvent in project webofneeds by researchstudio-sat.
the class ConnectWithAssociatedNeedAction method doRun.
@Override
public void doRun(Event event, EventListener executingListener) {
if (!(event instanceof NeedSpecificEvent)) {
logger.error("ConnectWithAssociatedNeedAction can only handle NeedSpecificEvents");
return;
}
final URI myNeedUri = ((NeedSpecificEvent) event).getNeedURI();
final URI remoteNeedUri = getEventListenerContext().getBotContextWrapper().getUriAssociation(myNeedUri);
try {
getEventListenerContext().getWonMessageSender().sendWonMessage(createWonMessage(myNeedUri, remoteNeedUri));
} catch (Exception e) {
logger.warn("could not connect " + myNeedUri + " and " + remoteNeedUri, e);
}
}
use of won.bot.framework.eventbot.event.NeedSpecificEvent in project webofneeds by researchstudio-sat.
the class ExecuteSendMessageCommandAction method makeLogMessageString.
private String makeLogMessageString(Event event) {
StringBuilder sb = new StringBuilder();
MessageCommandEvent messageCommandEvent = (MessageCommandEvent) event;
sb.append("sending message of type ").append(messageCommandEvent.getWonMessageType());
if (event instanceof NeedSpecificEvent) {
sb.append(" on behalf of need ").append(((NeedSpecificEvent) event).getNeedURI());
}
if (event instanceof RemoteNeedSpecificEvent) {
sb.append(" to need ").append(((RemoteNeedSpecificEvent) event).getRemoteNeedURI());
}
if (event instanceof ConnectionSpecificEvent) {
sb.append(" in connection ").append(((ConnectionSpecificEvent) event).getConnectionURI());
}
return sb.toString();
}
use of won.bot.framework.eventbot.event.NeedSpecificEvent in project webofneeds by researchstudio-sat.
the class CreateDebugNeedWithFacetsAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
String replyText = "";
URI reactingToNeedUriTmp = null;
Dataset needDataset = null;
if (event instanceof NeedSpecificEvent) {
reactingToNeedUriTmp = ((NeedSpecificEvent) event).getNeedURI();
} else {
logger.warn("could not process non-need specific event {}", event);
return;
}
if (event instanceof NeedCreatedEventForMatcher) {
needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
} else if (event instanceof HintDebugCommandEvent) {
reactingToNeedUriTmp = ((HintDebugCommandEvent) event).getRemoteNeedURI();
} else if (event instanceof ConnectDebugCommandEvent) {
reactingToNeedUriTmp = ((ConnectDebugCommandEvent) event).getRemoteNeedURI();
} else {
logger.error("CreateEchoNeedWithFacetsAction cannot handle " + event.getClass().getName());
return;
}
final URI reactingToNeedUri = reactingToNeedUriTmp;
String titleString = null;
boolean createNeed = true;
if (needDataset != null) {
DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
createNeed = needModelWrapper.hasFlag(WON.USED_FOR_TESTING) && !needModelWrapper.hasFlag(WON.NO_HINT_FOR_ME);
}
// if create need is false do not continue the debug need creation
if (!createNeed)
return;
if (titleString != null) {
if (isInitialForConnect) {
replyText = "Debugging with initial connect: " + titleString;
} else if (isInitialForHint) {
replyText = "Debugging with initial hint: " + titleString;
} else {
replyText = "Debugging: " + titleString;
}
} else {
replyText = "Debug Need No. " + counter.increment();
}
EventListenerContext ctx = getEventListenerContext();
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, replyText);
needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the DebugBot.");
for (URI facet : facets) {
needModelWrapper.addFacetUri(facet.toString());
}
final Dataset debugNeedDataset = needModelWrapper.copyDataset();
final Event origEvent = event;
logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(debugNeedDataset), 150));
WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, debugNeedDataset);
// remember the need URI so we can react to success/failure responses
EventBotActionUtils.rememberInList(ctx, needURI, uriListName);
EventListener successCallback = new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
logger.debug("need creation successful, new need URI is {}", needURI);
// save the mapping between the original and the reaction in to the context.
getEventListenerContext().getBotContextWrapper().addUriAssociation(reactingToNeedUri, needURI);
if ((origEvent instanceof HintDebugCommandEvent) || isInitialForHint) {
bus.publish(new NeedCreatedEventForDebugHint(needURI, wonNodeUri, debugNeedDataset, null));
} else if ((origEvent instanceof ConnectDebugCommandEvent) || isInitialForConnect) {
bus.publish(new NeedCreatedEventForDebugConnect(needURI, wonNodeUri, debugNeedDataset, null));
} else {
bus.publish(new NeedCreatedEvent(needURI, wonNodeUri, debugNeedDataset, null));
}
}
};
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 });
EventBotActionUtils.removeFromList(ctx, needURI, uriListName);
bus.publish(new NeedCreationFailedEvent(wonNodeUri));
}
};
EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", createNeedMessage.getMessageURI());
ctx.getWonMessageSender().sendWonMessage(createNeedMessage);
logger.debug("need creation message sent with message URI {}", createNeedMessage.getMessageURI());
}
use of won.bot.framework.eventbot.event.NeedSpecificEvent in project webofneeds by researchstudio-sat.
the class HintAssociatedNeedAction method doRun.
@Override
public void doRun(Event event, EventListener executingListener) {
if (!(event instanceof NeedSpecificEvent)) {
logger.error("HintAssociatedNeedAction can only handle NeedSpecificEvents");
return;
}
final URI myNeedUri = ((NeedSpecificEvent) event).getNeedURI();
final URI remoteNeedUri = getEventListenerContext().getBotContextWrapper().getUriAssociation(myNeedUri);
try {
logger.info("Sending hint for {} and {}", myNeedUri, remoteNeedUri);
getEventListenerContext().getMatcherProtocolNeedServiceClient().hint(remoteNeedUri, myNeedUri, 0.9, matcherURI, null, createWonMessage(remoteNeedUri, myNeedUri, 0.9, matcherURI));
} catch (Exception e) {
logger.warn("could not send hint for " + myNeedUri + " to " + remoteNeedUri, e);
}
}
Aggregations