Search in sources :

Example 1 with FactoryBotContextWrapper

use of won.bot.framework.bot.context.FactoryBotContextWrapper in project webofneeds by researchstudio-sat.

the class InitFactoryAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof InitializeEvent) || !(getEventListenerContext().getBotContextWrapper() instanceof FactoryBotContextWrapper)) {
        logger.error("InitFactoryAction can only handle InitializeEvent with FactoryBotContextWrapper");
        return;
    }
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = ctx.getEventBus();
    FactoryBotContextWrapper botContextWrapper = (FactoryBotContextWrapper) ctx.getBotContextWrapper();
    // create a targeted counter that will publish an event when the target is
    // reached
    // in this case, 0 unfinished atom creations means that all atoms were created
    final TargetCounterDecorator creationUnfinishedCounter = new TargetCounterDecorator(ctx, new CounterImpl("creationUnfinished"), 0);
    BotTrigger createFactoryAtomTrigger = new BotTrigger(ctx, Duration.ofMillis(FACTORYATOMCREATION_DURATION_INMILLIS));
    createFactoryAtomTrigger.activate();
    bus.subscribe(StartFactoryAtomCreationEvent.class, new ActionOnFirstEventListener(ctx, new PublishEventAction(ctx, new StartBotTriggerCommandEvent(createFactoryAtomTrigger))));
    bus.subscribe(BotTriggerEvent.class, new ActionOnTriggerEventListener(ctx, createFactoryAtomTrigger, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (isTooManyMessagesInFlight(messagesInFlightCounter)) {
                return;
            }
            adjustTriggerInterval(createFactoryAtomTrigger, messagesInFlightCounter);
            // defined via spring
            AtomProducer atomProducer = ctx.getAtomProducer();
            Dataset dataset = atomProducer.create();
            if (dataset == null && atomProducer.isExhausted()) {
                bus.publish(new AtomProducerExhaustedEvent());
                bus.unsubscribe(executingListener);
                return;
            }
            URI atomUriFromProducer = null;
            Resource atomResource = WonRdfUtils.AtomUtils.getAtomResource(dataset);
            if (atomResource.isURIResource()) {
                atomUriFromProducer = URI.create(atomResource.getURI());
            }
            if (atomUriFromProducer != null) {
                URI atomURI = botContextWrapper.getURIFromInternal(atomUriFromProducer);
                if (atomURI != null) {
                    bus.publish(new FactoryAtomCreationSkippedEvent());
                } else {
                    bus.publish(new CreateAtomCommandEvent(dataset, botContextWrapper.getFactoryListName()));
                }
            }
        }
    }));
    bus.subscribe(CreateAtomCommandSuccessEvent.class, // decrease the
    new DecrementCounterAction(ctx, creationUnfinishedCounter), // count a successful atom
    new IncrementCounterAction(ctx, atomCreationSuccessfulCounter), // creation
    new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            if (event instanceof CreateAtomCommandSuccessEvent) {
                CreateAtomCommandSuccessEvent atomCreatedEvent = (CreateAtomCommandSuccessEvent) event;
                botContextWrapper.addInternalIdToUriReference(atomCreatedEvent.getAtomUriBeforeCreation(), atomCreatedEvent.getAtomURI());
            }
        }
    });
    bus.subscribe(CreateAtomCommandEvent.class, // execute the atom creation for the atom in the event
    new ExecuteCreateAtomCommandAction(ctx), // increase the
    new IncrementCounterAction(ctx, atomCreationStartedCounter), // increase the
    new IncrementCounterAction(ctx, creationUnfinishedCounter));
    // if an atom is already created we skip the recreation of it and increase the
    // atomCreationSkippedCounter
    bus.subscribe(FactoryAtomCreationSkippedEvent.class, new IncrementCounterAction(ctx, atomCreationSkippedCounter));
    // if a creation failed, we don't want to keep us from keeping the correct count
    bus.subscribe(CreateAtomCommandFailureEvent.class, // decrease the
    new DecrementCounterAction(ctx, creationUnfinishedCounter), // count an unsuccessful atom
    new IncrementCounterAction(ctx, atomCreationFailedCounter));
    // when the atomproducer is exhausted, we stop the creator (trigger) and we have
    // to wait until all unfinished atom creations finish
    // when they do, the InitFactoryFinishedEvent is published
    bus.subscribe(AtomProducerExhaustedEvent.class, new ActionOnFirstEventListener(ctx, new MultipleActions(ctx, new PublishEventAction(ctx, new StopBotTriggerCommandEvent(createFactoryAtomTrigger)), new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            // when we're called, there probably are atom creations
            // unfinished, but there may not be
            // a)
            // first, prepare for the case when there are unfinished
            // atom creations:
            // we register a listener, waiting for the unfinished
            // counter to reach 0
            EventListener waitForUnfinishedAtomsListener = bus.subscribe(TargetCountReachedEvent.class, new ActionOnFirstEventListener(ctx, new TargetCounterFilter(creationUnfinishedCounter), new PublishEventAction(ctx, new InitFactoryFinishedEvent())));
            // now, we can check if we've already reached the target
            if (creationUnfinishedCounter.getCount() <= 0) {
                // ok, turned out we didn't need that listener
                bus.unsubscribe(waitForUnfinishedAtomsListener);
                bus.publish(new InitFactoryFinishedEvent());
            }
        }
    })));
    bus.subscribe(InitFactoryFinishedEvent.class, new ActionOnFirstEventListener(ctx, "factoryCreateStatsLogger", new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(Event event, EventListener executingListener) throws Exception {
            logger.info("FactoryAtomCreation finished: total:{}, successful: {}, failed: {}, skipped: {}", new Object[] { atomCreationStartedCounter.getCount(), atomCreationSuccessfulCounter.getCount(), atomCreationFailedCounter.getCount(), atomCreationSkippedCounter.getCount() });
        }
    }));
    // MessageInFlight counter handling *************************
    bus.subscribe(MessageCommandEvent.class, new IncrementCounterAction(ctx, messagesInFlightCounter));
    bus.subscribe(MessageCommandResultEvent.class, new DecrementCounterAction(ctx, messagesInFlightCounter));
    // if we receive a
    bus.subscribe(MessageCommandFailureEvent.class, new LogMessageCommandFailureAction(ctx));
    // message command
    // failure, log it
    // Start the atom creation stuff
    bus.publish(new StartFactoryAtomCreationEvent());
}
Also used : InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) PublishEventAction(won.bot.framework.eventbot.action.impl.PublishEventAction) StartFactoryAtomCreationEvent(won.bot.framework.eventbot.event.impl.factory.StartFactoryAtomCreationEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CounterImpl(won.bot.framework.eventbot.action.impl.counter.CounterImpl) StopBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StopBotTriggerCommandEvent) URI(java.net.URI) LogMessageCommandFailureAction(won.bot.framework.eventbot.action.impl.wonmessage.execCommand.LogMessageCommandFailureAction) ExecuteCreateAtomCommandAction(won.bot.framework.eventbot.action.impl.wonmessage.execCommand.ExecuteCreateAtomCommandAction) MultipleActions(won.bot.framework.eventbot.action.impl.MultipleActions) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) ActionOnTriggerEventListener(won.bot.framework.eventbot.action.impl.trigger.ActionOnTriggerEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) IncrementCounterAction(won.bot.framework.eventbot.action.impl.counter.IncrementCounterAction) AtomProducer(won.bot.framework.component.atomproducer.AtomProducer) InitFactoryFinishedEvent(won.bot.framework.eventbot.event.impl.factory.InitFactoryFinishedEvent) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) TargetCounterFilter(won.bot.framework.eventbot.filter.impl.TargetCounterFilter) Dataset(org.apache.jena.query.Dataset) FactoryAtomCreationSkippedEvent(won.bot.framework.eventbot.event.impl.factory.FactoryAtomCreationSkippedEvent) Resource(org.apache.jena.rdf.model.Resource) TargetCountReachedEvent(won.bot.framework.eventbot.action.impl.counter.TargetCountReachedEvent) FactoryBotContextWrapper(won.bot.framework.bot.context.FactoryBotContextWrapper) StartBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StartBotTriggerCommandEvent) ActionOnTriggerEventListener(won.bot.framework.eventbot.action.impl.trigger.ActionOnTriggerEventListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) CreateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandSuccessEvent) InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) StartBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StartBotTriggerCommandEvent) StartFactoryAtomCreationEvent(won.bot.framework.eventbot.event.impl.factory.StartFactoryAtomCreationEvent) CreateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandFailureEvent) StopBotTriggerCommandEvent(won.bot.framework.eventbot.action.impl.trigger.StopBotTriggerCommandEvent) AtomProducerExhaustedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomProducerExhaustedEvent) BotTriggerEvent(won.bot.framework.eventbot.action.impl.trigger.BotTriggerEvent) MessageCommandResultEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandResultEvent) MessageCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent) FactoryAtomCreationSkippedEvent(won.bot.framework.eventbot.event.impl.factory.FactoryAtomCreationSkippedEvent) InitFactoryFinishedEvent(won.bot.framework.eventbot.event.impl.factory.InitFactoryFinishedEvent) Event(won.bot.framework.eventbot.event.Event) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) TargetCountReachedEvent(won.bot.framework.eventbot.action.impl.counter.TargetCountReachedEvent) CreateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateAtomCommandEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) DecrementCounterAction(won.bot.framework.eventbot.action.impl.counter.DecrementCounterAction) BotTrigger(won.bot.framework.eventbot.action.impl.trigger.BotTrigger) TargetCounterDecorator(won.bot.framework.eventbot.action.impl.counter.TargetCounterDecorator)

Example 2 with FactoryBotContextWrapper

use of won.bot.framework.bot.context.FactoryBotContextWrapper in project webofneeds by researchstudio-sat.

the class FactoryBot method initializeEventListeners.

@Override
protected final void initializeEventListeners() {
    if (!(super.getBotContextWrapper() instanceof FactoryBotContextWrapper)) {
        logger.error("FactoryBot does not work without a FactoryBotContextWrapper");
        throw new IllegalStateException("FactoryBot does not work without a FactoryBotContextWrapper");
    }
    if (getAtomProducer() == null) {
        logger.error("FactoryBots do not work without a set atomProducer");
        throw new IllegalStateException("FactoryBots do not work without a set atomProducer");
    }
    EventListenerContext ctx = getEventListenerContext();
    BotBehaviour factoryBotInitBehaviour = new FactoryBotInitBehaviour(ctx);
    BotBehaviour factoryBotHintBehaviour = new FactoryBotHintBehaviour(ctx);
    BotBehaviour messageCommandBehaviour = new ExecuteWonMessageCommandBehaviour(ctx);
    BotBehaviour runningBehaviour = new BotBehaviour(ctx) {

        @Override
        protected void onActivate(Optional<Object> message) {
            initializeFactoryEventListeners();
        }
    };
    factoryBotInitBehaviour.onDeactivateActivate(runningBehaviour, factoryBotHintBehaviour, messageCommandBehaviour);
    factoryBotInitBehaviour.activate();
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) Optional(java.util.Optional) FactoryBotInitBehaviour(won.bot.framework.eventbot.behaviour.FactoryBotInitBehaviour) FactoryBotHintBehaviour(won.bot.framework.eventbot.behaviour.FactoryBotHintBehaviour) ExecuteWonMessageCommandBehaviour(won.bot.framework.eventbot.behaviour.ExecuteWonMessageCommandBehaviour) FactoryBotContextWrapper(won.bot.framework.bot.context.FactoryBotContextWrapper)

Example 3 with FactoryBotContextWrapper

use of won.bot.framework.bot.context.FactoryBotContextWrapper in project webofneeds by researchstudio-sat.

the class FactoryHintCheckAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    if (!(getEventListenerContext().getBotContextWrapper() instanceof FactoryBotContextWrapper)) {
        logger.error("FactoryHintCheckAction can only work for FactoryBotContextWrappers");
    } else if (!(event instanceof HintFromMatcherEvent)) {
        logger.error("FactoryHintCheckAction can only handle HintFromMatcherEvent");
        return;
    }
    FactoryBotContextWrapper botContextWrapper = (FactoryBotContextWrapper) getEventListenerContext().getBotContextWrapper();
    Optional<URI> ownUri = Optional.of(((HintFromMatcherEvent) event).getRecipientAtom());
    Optional<URI> requesterUri = Optional.of(((HintFromMatcherEvent) event).getHintTargetAtom());
    if (ownUri.isPresent() && requesterUri.isPresent() && botContextWrapper.isFactoryAtom(ownUri.get())) {
        logger.debug("FactoryHint for factoryURI: " + ownUri.get() + " from the requesterUri: " + requesterUri.get());
        EventBus bus = getEventListenerContext().getEventBus();
        bus.publish(new FactoryHintEvent(requesterUri.get(), ownUri.get()));
    } else {
        logger.warn("NON FactoryHint for URI: " + ownUri + " from the requesterUri: " + requesterUri + " ignore the hint");
    }
}
Also used : HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) FactoryHintEvent(won.bot.framework.eventbot.event.impl.factory.FactoryHintEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) FactoryBotContextWrapper(won.bot.framework.bot.context.FactoryBotContextWrapper)

Aggregations

FactoryBotContextWrapper (won.bot.framework.bot.context.FactoryBotContextWrapper)3 URI (java.net.URI)2 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 Optional (java.util.Optional)1 Dataset (org.apache.jena.query.Dataset)1 Resource (org.apache.jena.rdf.model.Resource)1 AtomProducer (won.bot.framework.component.atomproducer.AtomProducer)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 MultipleActions (won.bot.framework.eventbot.action.impl.MultipleActions)1 PublishEventAction (won.bot.framework.eventbot.action.impl.PublishEventAction)1 CounterImpl (won.bot.framework.eventbot.action.impl.counter.CounterImpl)1 DecrementCounterAction (won.bot.framework.eventbot.action.impl.counter.DecrementCounterAction)1 IncrementCounterAction (won.bot.framework.eventbot.action.impl.counter.IncrementCounterAction)1 TargetCountReachedEvent (won.bot.framework.eventbot.action.impl.counter.TargetCountReachedEvent)1 TargetCounterDecorator (won.bot.framework.eventbot.action.impl.counter.TargetCounterDecorator)1 ActionOnTriggerEventListener (won.bot.framework.eventbot.action.impl.trigger.ActionOnTriggerEventListener)1 BotTrigger (won.bot.framework.eventbot.action.impl.trigger.BotTrigger)1 BotTriggerEvent (won.bot.framework.eventbot.action.impl.trigger.BotTriggerEvent)1 StartBotTriggerCommandEvent (won.bot.framework.eventbot.action.impl.trigger.StartBotTriggerCommandEvent)1