Search in sources :

Example 1 with AtomCreatedEvent

use of won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent 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 AtomCreatedEvent

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

the class CreateAtomWithSocketsAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    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);
    int i = 1;
    for (URI socket : sockets) {
        atomModelWrapper.addSocket(atomUriBeforeCreation.toString() + "#socket" + i, socket.toString());
        i++;
    }
    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, wonNodeUri, atomDatasetWithSockets);
    createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
    // remember the atom URI so we can react to success/failure responses
    ctx.getBotContextWrapper().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 });
        ctx.getBotContextWrapper().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) 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) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomCreationFailedEvent(won.bot.framework.eventbot.event.AtomCreationFailedEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)

Example 3 with AtomCreatedEvent

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

the class ServiceAtomBehaviour method onActivate.

@Override
protected void onActivate(Optional<Object> message) {
    logger.debug("activating ServiceAtomBehaviour");
    final EventListenerContext ctx = this.context;
    subscribeWithAutoCleanup(InitializeEvent.class, new ActionOnEventListener(ctx, new AbstractCreateAtomAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            logger.debug("Initializing the BotServiceAtom...");
            URI serviceAtomUri = serviceAtomContext.getServiceAtomUri();
            if (serviceAtomUri == null) {
                logger.debug("BotServiceAtom does not exist, creating a new one...");
                createServiceAtom();
            } else {
                logger.debug("BotServiceAtom exists atomUri: {} checking validity...", serviceAtomUri);
                Dataset serviceBotDataSet = ctx.getLinkedDataSource().getDataForPublicResource(serviceAtomUri);
                if (serviceBotDataSet == null) {
                    logger.debug("BotServiceAtom can't be retrieved, creating a new one...");
                    createServiceAtom();
                } else {
                    ServiceAtomModelWrapper oldServiceAtomWrapper = new ServiceAtomModelWrapper(serviceBotDataSet);
                    if (!Objects.equals(oldServiceAtomWrapper.getServiceAtomContent(), serviceAtomContent)) {
                        logger.debug("BotServiceAtom is outdated, modifying the BotServiceAtom");
                        modifyServiceAtom(oldServiceAtomWrapper);
                    } else {
                        logger.info("#####################################################################################");
                        logger.info("BotServiceAtom is still up to date, atom URI is {}", serviceAtomUri);
                        logger.info("#####################################################################################");
                    }
                }
            }
        }

        private void createServiceAtom() {
            final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
            WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
            final URI atomUri = wonNodeInformationService.generateAtomURI(wonNodeUri);
            Dataset botServiceDataset = new ServiceAtomModelWrapper(atomUri, serviceAtomContent).copyDataset();
            logger.debug("creating BotServiceAtom on won node {} with content: {} ", wonNodeUri, RdfUtils.toString(Prefixer.setPrefixes(botServiceDataset)));
            WonMessage createAtomMessage = createWonMessage(atomUri, botServiceDataset);
            createAtomMessage = ctx.getWonMessageSender().prepareMessage(createAtomMessage);
            ctx.getBotContextWrapper().rememberAtomUri(atomUri);
            EventBus bus = ctx.getEventBus();
            EventListener successCallback = event -> {
                logger.info("#####################################################################################");
                logger.info("BotServiceAtom creation successful, new atom URI is {}", atomUri);
                logger.info("#####################################################################################");
                serviceAtomContext.setServiceAtomUri(atomUri);
                bus.publish(new AtomCreatedEvent(atomUri, wonNodeUri, botServiceDataset, null));
            };
            EventListener failureCallback = event -> {
                String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
                logger.error("BotServiceAtom creation failed for atom URI {}, original message URI: {}", atomUri, textMessage);
                ctx.getBotContextWrapper().removeAtomUri(atomUri);
            };
            EventBotActionUtils.makeAndSubscribeResponseListener(createAtomMessage, successCallback, failureCallback, ctx);
            logger.debug("registered listeners for response to message URI {}", createAtomMessage.getMessageURI());
            ctx.getWonMessageSender().sendMessage(createAtomMessage);
            logger.debug("BotServiceAtom creation message sent with message URI {}", createAtomMessage.getMessageURI());
        }

        private void modifyServiceAtom(ServiceAtomModelWrapper oldServiceAtomModelWrapper) {
            final URI atomUri = URI.create(oldServiceAtomModelWrapper.getAtomUri());
            // Replace placeholder socketUris with existing socketUris
            // if socketType was present before modification
            Map<String, String> oldServiceAtomSocketsInverse = oldServiceAtomModelWrapper.getServiceAtomContent().getSockets().entrySet().stream().collect(Collectors.toMap(socketEntry -> socketEntry.getValue(), socketEntry -> socketEntry.getKey()));
            serviceAtomContent.setSockets(serviceAtomContent.getSockets().entrySet().stream().collect(Collectors.toMap(socketEntry -> oldServiceAtomSocketsInverse.containsKey(socketEntry.getValue()) ? oldServiceAtomSocketsInverse.get(socketEntry.getValue()) : socketEntry.getKey(), socketEntry -> socketEntry.getValue())));
            ServiceAtomModelWrapper serviceAtomModelWrapper = new ServiceAtomModelWrapper(atomUri, serviceAtomContent);
            final URI wonNodeUri = URI.create(oldServiceAtomModelWrapper.getWonNodeUri());
            Dataset botServiceDataset = serviceAtomModelWrapper.copyDataset();
            logger.debug("creating BotServiceAtom on won node {} with content: {} ", wonNodeUri, RdfUtils.toString(Prefixer.setPrefixes(botServiceDataset)));
            RdfUtils.replaceBaseURI(botServiceDataset, atomUri.toString(), true);
            ServiceAtomModelWrapper modifiedAtomModelWrapper = new ServiceAtomModelWrapper(botServiceDataset);
            WonMessage modifyAtomMessage = WonMessageBuilder.replace().atom(atomUri).content().dataset(modifiedAtomModelWrapper.copyDatasetWithoutSysinfo()).build();
            modifyAtomMessage = ctx.getWonMessageSender().prepareMessage(modifyAtomMessage);
            ctx.getBotContextWrapper().rememberAtomUri(atomUri);
            EventBus bus = ctx.getEventBus();
            EventListener successCallback = event -> {
                logger.info("#####################################################################################");
                logger.info("BotServiceAtom modification successful, atom URI is {}", atomUri);
                logger.info("#####################################################################################");
                serviceAtomContext.setServiceAtomUri(atomUri);
                bus.publish(new AtomCreatedEvent(atomUri, wonNodeUri, botServiceDataset, null));
            };
            EventListener failureCallback = event -> {
                String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event).getFailureMessage());
                logger.error("BotServiceAtom modification failed for atom URI {}, original message URI: {}", atomUri, textMessage);
                ctx.getBotContextWrapper().removeAtomUri(atomUri);
            };
            EventBotActionUtils.makeAndSubscribeResponseListener(modifyAtomMessage, successCallback, failureCallback, ctx);
            logger.debug("registered listeners for response to message URI {}", modifyAtomMessage.getMessageURI());
            ctx.getWonMessageSender().sendMessage(modifyAtomMessage);
            logger.debug("BotServiceAtom creation message sent with message URI {}", modifyAtomMessage.getMessageURI());
        }
    }));
    subscribeWithAutoCleanup(AtomCreatedEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (event instanceof AtomCreatedEvent) {
                AtomCreatedEvent atomCreatedEvent = (AtomCreatedEvent) event;
                URI botServiceAtomUri = serviceAtomContext.getServiceAtomUri();
                URI createdAtomUri = atomCreatedEvent.getAtomURI();
                if (!Objects.equals(createdAtomUri, botServiceAtomUri)) {
                    if (ctx.getBotContextWrapper().isAtomKnown(createdAtomUri)) {
                        logger.debug("Atom ({}) is known, must be one we created..., dataset: {}", createdAtomUri, RdfUtils.toString(Prefixer.setPrefixes(atomCreatedEvent.getAtomDataset())));
                        Optional<URI> createdAtomHoldableSocketUri = WonLinkedDataUtils.getSocketsOfType(createdAtomUri, WXHOLD.HoldableSocket.asURI(), ctx.getLinkedDataSource()).stream().findFirst();
                        if (createdAtomHoldableSocketUri.isPresent()) {
                            logger.debug("Atom ({}) has the holdableSocket, connect botServiceAtom ({}) with this atom", createdAtomUri, botServiceAtomUri);
                            Optional<URI> botServiceAtomHolderSocketUri = WonLinkedDataUtils.getSocketsOfType(botServiceAtomUri, WXHOLD.HolderSocket.asURI(), ctx.getLinkedDataSource()).stream().findFirst();
                            logger.debug("Connecting atom ({}) - botServiceAtom ({})", createdAtomUri, botServiceAtomUri);
                            URI senderSocketURI = createdAtomHoldableSocketUri.orElseThrow(() -> new IllegalStateException("no suitable socket for connecting on created atom " + createdAtomUri));
                            URI recipientSocketURI = botServiceAtomHolderSocketUri.orElseThrow(() -> new IllegalStateException("no suitable socket for connecting on bot service atom " + botServiceAtomUri));
                            WonMessage connectToServiceAtomMessage = WonMessageBuilder.connect().sockets().sender(senderSocketURI).recipient(recipientSocketURI).content().text("Automated Connect to Service Atom").build();
                            ctx.getWonMessageSender().prepareAndSendMessage(connectToServiceAtomMessage);
                        } else {
                            logger.debug("Atom ({}) does not have a holdable Socket, no connect action required", createdAtomUri);
                        }
                    } else {
                        logger.debug("Atom ({}) is not known, must be someone elses...", createdAtomUri);
                    }
                } else {
                    logger.debug("BotServiceAtomCreated, no connect action required");
                }
            }
        }
    }));
    subscribeWithAutoCleanup(ConnectFromOtherAtomEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (event instanceof ConnectFromOtherAtomEvent) {
                ConnectFromOtherAtomEvent connectFromOtherAtomEvent = (ConnectFromOtherAtomEvent) event;
                URI botServiceAtomUri = serviceAtomContext.getServiceAtomUri();
                URI senderAtomUri = connectFromOtherAtomEvent.getWonMessage().getSenderAtomURI();
                URI targetAtomUri = connectFromOtherAtomEvent.getWonMessage().getRecipientAtomURI();
                logger.debug("Possibly accept the connection Request with holderFacet<->holdableFacet if atom is BotServiceAtom");
                if (ctx.getBotContextWrapper().isAtomKnown(senderAtomUri) && ctx.getBotContextWrapper().isAtomKnown(targetAtomUri) && Objects.equals(targetAtomUri, botServiceAtomUri)) {
                    logger.debug("Both Atoms belong to you, you might want to accept the connect, socketUri: {}, targetSocketUri: {}", connectFromOtherAtomEvent.getWonMessage().getSenderSocketURI(), connectFromOtherAtomEvent.getWonMessage().getRecipientSocketURI());
                    URI senderSocketUri = connectFromOtherAtomEvent.getWonMessage().getSenderSocketURI();
                    URI targetSocketUri = connectFromOtherAtomEvent.getWonMessage().getRecipientSocketURI();
                    Optional<URI> senderSocketTypeUri = WonLinkedDataUtils.getTypeOfSocket(senderSocketUri, ctx.getLinkedDataSource());
                    Optional<URI> targetSocketTypeUri = WonLinkedDataUtils.getTypeOfSocket(targetSocketUri, ctx.getLinkedDataSource());
                    if (senderSocketTypeUri.isPresent() && targetSocketTypeUri.isPresent() && Objects.equals(senderSocketTypeUri.get(), WXHOLD.HoldableSocket.asURI()) && Objects.equals(targetSocketTypeUri.get(), WXHOLD.HolderSocket.asURI())) {
                        logger.debug("Accepting connect request from atom ({}) to serviceAtom ({})", senderAtomUri, targetAtomUri);
                        WonMessage openServiceAtomMessage = WonMessageBuilder.connect().sockets().reactingTo(connectFromOtherAtomEvent.getWonMessage()).direction().fromOwner().content().text("Automated Open from Service Atom").build();
                        ctx.getWonMessageSender().prepareAndSendMessage(openServiceAtomMessage);
                    }
                } else {
                    logger.debug("At least one of the two Atoms is not known, or the targetAtomUri is not the botServiceAtomUri, ignore the connect request");
                }
            }
        }
    }));
}
Also used : AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) LoggerFactory(org.slf4j.LoggerFactory) EventBus(won.bot.framework.eventbot.bus.EventBus) ConnectFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherAtomEvent) WonMessage(won.protocol.message.WonMessage) InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) WXHOLD(won.protocol.vocabulary.WXHOLD) Map(java.util.Map) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonNodeInformationService(won.protocol.service.WonNodeInformationService) Logger(org.slf4j.Logger) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) Prefixer(won.protocol.util.Prefixer) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) MethodHandles(java.lang.invoke.MethodHandles) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) AbstractCreateAtomAction(won.bot.framework.eventbot.action.impl.atomlifecycle.AbstractCreateAtomAction) Collectors(java.util.stream.Collectors) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonRdfUtils(won.protocol.util.WonRdfUtils) Objects(java.util.Objects) Event(won.bot.framework.eventbot.event.Event) RdfUtils(won.protocol.util.RdfUtils) Optional(java.util.Optional) EventListener(won.bot.framework.eventbot.listener.EventListener) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) AbstractCreateAtomAction(won.bot.framework.eventbot.action.impl.atomlifecycle.AbstractCreateAtomAction) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) ConnectFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherAtomEvent) WonMessage(won.protocol.message.WonMessage) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) ConnectFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherAtomEvent) InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) Event(won.bot.framework.eventbot.event.Event) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)

Example 4 with AtomCreatedEvent

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

the class MatchingLoadTestMonitorAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    Stopwatch stopwatch = SimonManager.getStopwatch("atomHintFullRoundtrip");
    if (event instanceof AtomCreatedEvent) {
        Split split = stopwatch.start();
        atomSplits.put(((AtomCreatedEvent) event).getAtomURI().toString(), split);
        logger.info("RECEIVED EVENT {} for uri {}", event, ((AtomCreatedEvent) event).getAtomURI().toString());
        long startTime = System.currentTimeMillis();
        String atomUri = ((AtomCreatedEvent) event).getAtomURI().toString();
        atomEventStartTime.put(atomUri, startTime);
    } else if (event instanceof AtomHintFromMatcherEvent) {
        String atomUri = ((AtomHintFromMatcherEvent) event).getRecipientAtom().toString();
        logger.info("RECEIVED EVENT {} for uri {}", event, atomUri);
        long hintReceivedTime = System.currentTimeMillis();
        atomSplits.get(atomUri).stop();
        hintEventReceivedTime.computeIfAbsent(atomUri, k -> new LinkedList<>());
        hintEventReceivedTime.get(atomUri).add(hintReceivedTime);
    } else if (event instanceof SocketHintFromMatcherEvent) {
        String atomUri = ((SocketHintFromMatcherEvent) event).getRecipientSocket().toString();
        logger.info("RECEIVED EVENT {} for uri {}", event, atomUri);
        long hintReceivedTime = System.currentTimeMillis();
        atomSplits.get(atomUri).stop();
        hintEventReceivedTime.computeIfAbsent(atomUri, k -> new LinkedList<>());
        hintEventReceivedTime.get(atomUri).add(hintReceivedTime);
    }
    if (startTestTime == -1) {
        startTestTime = System.currentTimeMillis();
    }
    logger.info("Number of Atoms: {}", atomEventStartTime.size());
    logger.info("Number of Hints: {}", getTotalHints());
    logger.info("Number of Atoms with Hints: {}", getAtomsWithHints());
    logger.info("Average Duration: {}", getAverageHintDuration());
    logger.info("Minimum Duration: {}", getMinHintDuration());
    logger.info("Maximum Duration: {}", getMaxHintDuration());
    logger.info("Atoms with Hints per Second: {}", getAtomsWithAtomsPerSecond(startTestTime));
    logger.info("Hints per Second: {}", getHintsPerSecondThroughput(startTestTime));
}
Also used : AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) java.util(java.util) Logger(org.slf4j.Logger) Split(org.javasimon.Split) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) SocketHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.SocketHintFromMatcherEvent) Stopwatch(org.javasimon.Stopwatch) SimonManager(org.javasimon.SimonManager) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.AtomHintFromMatcherEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) SocketHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.SocketHintFromMatcherEvent) Stopwatch(org.javasimon.Stopwatch) Split(org.javasimon.Split) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) AtomHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.AtomHintFromMatcherEvent)

Aggregations

MethodHandles (java.lang.invoke.MethodHandles)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)4 Event (won.bot.framework.eventbot.event.Event)4 AtomCreatedEvent (won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 URI (java.net.URI)3 Dataset (org.apache.jena.query.Dataset)3 EventBotActionUtils (won.bot.framework.eventbot.action.EventBotActionUtils)3 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)3 WonMessage (won.protocol.message.WonMessage)3 WonNodeInformationService (won.protocol.service.WonNodeInformationService)3 Prefixer (won.protocol.util.Prefixer)3 RdfUtils (won.protocol.util.RdfUtils)3 WonRdfUtils (won.protocol.util.WonRdfUtils)3 StringUtils (org.apache.commons.lang3.StringUtils)2 Resource (org.apache.jena.rdf.model.Resource)2 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)2 AtomCreationFailedEvent (won.bot.framework.eventbot.event.AtomCreationFailedEvent)2