use of won.bot.framework.bot.context.BotContextWrapper in project webofneeds by researchstudio-sat.
the class CreateAtomAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
EventListenerContext ctx = getEventListenerContext();
BotContextWrapper botContextWrapper = ctx.getBotContextWrapper();
if (ctx.getAtomProducer().isExhausted()) {
logger.info("the bot's atom producer is exhausted.");
ctx.getEventBus().publish(new AtomProducerExhaustedEvent());
return;
}
final Dataset atomDataset = ctx.getAtomProducer().create();
if (atomDataset == null) {
logger.warn("atomproducer failed to produce an atom model, aborting atom creation");
return;
}
URI atomUriFromProducer = null;
Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(atomDataset);
if (atomResource.isURIResource()) {
atomUriFromProducer = URI.create(atomResource.getURI());
RdfUtils.replaceBaseURI(atomDataset, atomResource.getURI(), true);
} else {
RdfUtils.replaceBaseResource(atomDataset, atomResource, true);
}
final URI atomUriBeforeCreation = atomUriFromProducer;
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
final Dataset atomDatasetWithSockets = atomModelWrapper.copyDatasetWithoutSysinfo();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
logger.debug("creating atom on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(Prefixer.setPrefixes(atomDatasetWithSockets)), 150));
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
final URI atomURI = wonNodeInformationService.generateAtomURI(wonNodeUri);
WonMessage createAtomMessage = createWonMessage(atomURI, atomDatasetWithSockets);
createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
// remember the atom URI so we can react to success/failure responses
botContextWrapper.rememberAtomUri(atomURI);
EventListener successCallback = event12 -> {
logger.debug("atom creation successful, new atom URI is {}", atomURI);
ctx.getEventBus().publish(new AtomCreatedEvent(atomURI, wonNodeUri, atomDatasetWithSockets, null, atomUriBeforeCreation));
};
EventListener failureCallback = event1 -> {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event1).getFailureMessage());
logger.debug("atom creation failed for atom URI {}, original message URI {}: {}", new Object[] { atomURI, ((FailureResponseEvent) event1).getOriginalMessageURI(), textMessage });
botContextWrapper.removeAtomUri(atomURI);
ctx.getEventBus().publish(new AtomCreationFailedEvent(wonNodeUri, atomUriBeforeCreation));
};
EventBotActionUtils.makeAndSubscribeResponseListener(createAtomMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", createAtomMessage.getMessageURI());
ctx.getWonMessageSender().sendMessage(createAtomMessage);
logger.debug("atom creation message sent with message URI {}", createAtomMessage.getMessageURI());
}
use of won.bot.framework.bot.context.BotContextWrapper in project webofneeds by researchstudio-sat.
the class ServiceAtomCreatedAtomRelationFilter method accept.
@Override
public boolean accept(Event event) {
BotContextWrapper botContextWrapper = getContext().getBotContextWrapper();
if (botContextWrapper instanceof ServiceAtomContext && event instanceof MessageEvent) {
ServiceAtomContext serviceAtomContext = (ServiceAtomContext) botContextWrapper;
URI serviceAtomUri = serviceAtomContext.getServiceAtomUri();
if (Objects.nonNull(serviceAtomUri)) {
MessageEvent messageEvent = (MessageEvent) event;
WonMessage wonMessage = messageEvent.getWonMessage();
if (Objects.equals(wonMessage.getRecipientAtomURI(), serviceAtomUri)) {
URI senderAtomUri = wonMessage.getSenderAtomURI();
return getContext().getBotContextWrapper().isAtomKnown(senderAtomUri);
} else if (Objects.equals(wonMessage.getSenderAtomURI(), serviceAtomUri)) {
URI recipientAtomUri = wonMessage.getRecipientAtomURI();
return getContext().getBotContextWrapper().isAtomKnown(recipientAtomUri);
}
return false;
}
}
return false;
}
use of won.bot.framework.bot.context.BotContextWrapper in project webofneeds by researchstudio-sat.
the class ServiceAtomFilter method accept.
@Override
public boolean accept(Event event) {
BotContextWrapper botContextWrapper = getContext().getBotContextWrapper();
if (botContextWrapper instanceof ServiceAtomContext) {
ServiceAtomContext serviceAtomContext = (ServiceAtomContext) botContextWrapper;
URI serviceAtomUri = serviceAtomContext.getServiceAtomUri();
if (Objects.nonNull(serviceAtomUri)) {
if (event instanceof MessageEvent) {
MessageEvent messageEvent = (MessageEvent) event;
WonMessage wonMessage = messageEvent.getWonMessage();
return Objects.equals(wonMessage.getSenderAtomURI(), serviceAtomUri) || Objects.equals(wonMessage.getRecipientAtomURI(), serviceAtomUri);
}
boolean accept = false;
if (event instanceof AtomSpecificEvent) {
AtomSpecificEvent specificEvent = (AtomSpecificEvent) event;
accept = Objects.equals(specificEvent.getAtomURI(), serviceAtomUri);
}
if (!accept && event instanceof TargetAtomSpecificEvent) {
TargetAtomSpecificEvent specificEvent = (TargetAtomSpecificEvent) event;
accept = Objects.equals(specificEvent.getTargetAtomURI(), serviceAtomUri);
}
return accept;
}
}
return false;
}
Aggregations