Search in sources :

Example 1 with NeedCreatedEventForMatcher

use of won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher in project webofneeds by researchstudio-sat.

the class CreateEchoNeedWithFacetsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    String replyText = "";
    if (!(event instanceof NeedCreatedEventForMatcher)) {
        logger.error("CreateEchoNeedWithFacetsAction can only handle NeedCreatedEventForMatcher");
        return;
    }
    final URI reactingToNeedUri = ((NeedCreatedEventForMatcher) event).getNeedURI();
    final Dataset needDataset = ((NeedCreatedEventForMatcher) event).getNeedData();
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper(needDataset);
    String titleString = needModelWrapper.getSomeTitleFromIsOrAll("en", "de");
    if (titleString != null) {
        replyText = titleString;
    } else {
        replyText = "Your Posting (" + reactingToNeedUri.toString() + ")";
    }
    WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
    final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
    final URI needURI = wonNodeInformationService.generateNeedURI(wonNodeUri);
    needModelWrapper = new DefaultNeedModelWrapper(needURI.toString());
    needModelWrapper.createContentNode(NeedContentPropertyType.IS_AND_SEEKS, needURI.toString());
    needModelWrapper.setTitle(NeedContentPropertyType.IS_AND_SEEKS, "RE: " + replyText);
    needModelWrapper.setDescription(NeedContentPropertyType.IS_AND_SEEKS, "This is a need automatically created by the EchoBot.");
    for (URI facetUri : facets) {
        needModelWrapper.addFacetUri(facetUri.toString());
    }
    final Dataset echoNeedDataset = needModelWrapper.copyDataset();
    logger.debug("creating need on won node {} with content {} ", wonNodeUri, StringUtils.abbreviate(RdfUtils.toString(echoNeedDataset), 150));
    WonMessage createNeedMessage = createWonMessage(wonNodeInformationService, needURI, wonNodeUri, echoNeedDataset);
    // 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);
            ctx.getEventBus().publish(new NeedCreatedEvent(needURI, wonNodeUri, echoNeedDataset, 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);
            ctx.getEventBus().publish(new NeedCreationFailedEvent(wonNodeUri));
        }
    };
    EventBotActionUtils.makeAndSubscribeResponseListener(createNeedMessage, successCallback, failureCallback, ctx);
    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());
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) Dataset(org.apache.jena.query.Dataset) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) WonNodeInformationService(won.protocol.service.WonNodeInformationService) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) URI(java.net.URI) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Example 2 with NeedCreatedEventForMatcher

use of won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher in project webofneeds by researchstudio-sat.

the class LastSeenNeedsMatcherBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // subscribe this bot with the WoN nodes' 'new need' topic
    RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
    this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
    bus.subscribe(ActEvent.class, this.matcherRegistrator);
    RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
    ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
    bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
    bus.subscribe(NeedCreatedEventForMatcher.class, new ActionOnEventListener(ctx, "lastSeenNeedsMatcher", new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            NeedCreatedEventForMatcher needCreatedEvent = (NeedCreatedEventForMatcher) event;
            URI currentNeedURI = needCreatedEvent.getNeedURI();
            URI lastNeedURI = lastNeedUriReference.getAndSet(currentNeedURI);
            URI originator = matcherUri;
            if (lastNeedURI == null) {
                logger.info("First invocation. Remembering {} for matching it later", currentNeedURI);
                return;
            } else {
                logger.info("Sending hint for {} and {}", currentNeedURI, lastNeedURI);
            }
            ctx.getMatcherProtocolNeedServiceClient().hint(currentNeedURI, lastNeedURI, 0.5, originator, null, createWonMessage(currentNeedURI, lastNeedURI, 0.5, originator));
            ctx.getMatcherProtocolNeedServiceClient().hint(lastNeedURI, currentNeedURI, 0.5, originator, null, createWonMessage(lastNeedURI, currentNeedURI, 0.5, originator));
        }
    }));
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) RegisterMatcherAction(won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) MatcherRegisterFailedEvent(won.bot.framework.eventbot.event.impl.matcher.MatcherRegisterFailedEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) RandomDelayedAction(won.bot.framework.eventbot.action.impl.RandomDelayedAction) URI(java.net.URI)

Example 3 with NeedCreatedEventForMatcher

use of won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher 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());
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) NeedCreatedEventForDebugConnect(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) NeedCreatedEventForDebugHint(won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugHint) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) NeedCreatedEventForMatcher(won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher) WonMessage(won.protocol.message.WonMessage) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) ConnectDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent) NeedCreationFailedEvent(won.bot.framework.eventbot.event.NeedCreationFailedEvent) HintDebugCommandEvent(won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)

Aggregations

URI (java.net.URI)3 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)3 Event (won.bot.framework.eventbot.event.Event)3 NeedCreatedEventForMatcher (won.bot.framework.eventbot.event.impl.matcher.NeedCreatedEventForMatcher)3 EventListener (won.bot.framework.eventbot.listener.EventListener)3 Dataset (org.apache.jena.query.Dataset)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 NeedCreationFailedEvent (won.bot.framework.eventbot.event.NeedCreationFailedEvent)2 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)2 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)2 WonMessage (won.protocol.message.WonMessage)2 WonNodeInformationService (won.protocol.service.WonNodeInformationService)2 DefaultNeedModelWrapper (won.protocol.util.DefaultNeedModelWrapper)2 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 RandomDelayedAction (won.bot.framework.eventbot.action.impl.RandomDelayedAction)1 RegisterMatcherAction (won.bot.framework.eventbot.action.impl.matcher.RegisterMatcherAction)1 NeedSpecificEvent (won.bot.framework.eventbot.event.NeedSpecificEvent)1 ConnectDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.ConnectDebugCommandEvent)1 HintDebugCommandEvent (won.bot.framework.eventbot.event.impl.debugbot.HintDebugCommandEvent)1 NeedCreatedEventForDebugConnect (won.bot.framework.eventbot.event.impl.debugbot.NeedCreatedEventForDebugConnect)1