Search in sources :

Example 1 with BotContextWrapper

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());
}
Also used : WonNodeInformationService(won.protocol.service.WonNodeInformationService) AtomCreationFailedEvent(won.bot.framework.eventbot.event.AtomCreationFailedEvent) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) Logger(org.slf4j.Logger) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) Prefixer(won.protocol.util.Prefixer) BotContextWrapper(won.bot.framework.bot.context.BotContextWrapper) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) StringUtils(org.apache.commons.lang3.StringUtils) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonMessage(won.protocol.message.WonMessage) WonRdfUtils(won.protocol.util.WonRdfUtils) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Event(won.bot.framework.eventbot.event.Event) RdfUtils(won.protocol.util.RdfUtils) Resource(org.apache.jena.rdf.model.Resource) EventListener(won.bot.framework.eventbot.listener.EventListener) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) WonNodeInformationService(won.protocol.service.WonNodeInformationService) URI(java.net.URI) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) WonMessage(won.protocol.message.WonMessage) AtomModelWrapper(won.protocol.util.AtomModelWrapper) BotContextWrapper(won.bot.framework.bot.context.BotContextWrapper) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomCreationFailedEvent(won.bot.framework.eventbot.event.AtomCreationFailedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)

Example 2 with BotContextWrapper

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;
}
Also used : MessageEvent(won.bot.framework.eventbot.event.MessageEvent) WonMessage(won.protocol.message.WonMessage) BotContextWrapper(won.bot.framework.bot.context.BotContextWrapper) URI(java.net.URI) ServiceAtomContext(won.bot.framework.extensions.serviceatom.ServiceAtomContext)

Example 3 with BotContextWrapper

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;
}
Also used : WonMessage(won.protocol.message.WonMessage) BotContextWrapper(won.bot.framework.bot.context.BotContextWrapper) URI(java.net.URI) ServiceAtomContext(won.bot.framework.extensions.serviceatom.ServiceAtomContext)

Aggregations

URI (java.net.URI)3 BotContextWrapper (won.bot.framework.bot.context.BotContextWrapper)3 WonMessage (won.protocol.message.WonMessage)3 ServiceAtomContext (won.bot.framework.extensions.serviceatom.ServiceAtomContext)2 MethodHandles (java.lang.invoke.MethodHandles)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Dataset (org.apache.jena.query.Dataset)1 Resource (org.apache.jena.rdf.model.Resource)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 EventBotActionUtils (won.bot.framework.eventbot.action.EventBotActionUtils)1 AtomCreationFailedEvent (won.bot.framework.eventbot.event.AtomCreationFailedEvent)1 Event (won.bot.framework.eventbot.event.Event)1 MessageEvent (won.bot.framework.eventbot.event.MessageEvent)1 AtomCreatedEvent (won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent)1 AtomProducerExhaustedEvent (won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent)1 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1 WonNodeInformationService (won.protocol.service.WonNodeInformationService)1