use of won.bot.framework.component.atomproducer.AtomProducer 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());
}
use of won.bot.framework.component.atomproducer.AtomProducer in project webofneeds by researchstudio-sat.
the class AbstractCompositeAtomProducer method create.
@Override
public synchronized Dataset create() {
logger.debug("starting to produce an atom model");
AtomProducer delegate = selectNonExhaustedAtomFactory();
if (delegate == null) {
logger.warn("cannot produce an atom model - all atom factories are exhausted");
// we're exhausted
return null;
}
return delegate.create();
}
use of won.bot.framework.component.atomproducer.AtomProducer in project webofneeds by researchstudio-sat.
the class SolrAtomIndexer method main.
public static void main(String[] args) throws IOException, InterruptedException, JsonLdError {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SolrTestAppConfiguration.class);
AtomIndexer indexer = ctx.getBean(AtomIndexer.class);
// set the options of the atom producer (e.g. if it should exhaust) in the
// SolrAtomIndexerAppConfiguration file
AtomProducer atomProducer = ctx.getBean(RoundRobinCompositeAtomProducer.class);
Model atomModel = new AtomModelWrapper(atomProducer.create()).copyAtomModel(AtomGraphType.ATOM);
int atoms = 0;
while (!atomProducer.isExhausted()) {
// indexer.indexAtomModel(atomModel, UUID.randomUUID().toString(), true);
Dataset ds = DatasetFactory.createTxnMem();
ds.addNamedModel("https://node.matchat.org/won/resource/atom/test#atom", atomModel);
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomModel, null);
atomModel = atomModelWrapper.normalizeAtomModel();
indexer.indexAtomModel(atomModel, SolrMatcherEvaluation.createAtomId(ds), true);
atoms++;
if (atoms % 100 == 0) {
System.out.println("Indexed " + atoms + " atoms.");
}
atomModel = new AtomModelWrapper(atomProducer.create()).copyAtomModel(AtomGraphType.ATOM);
}
System.out.println("Indexed " + atoms + " atoms.");
System.exit(0);
}
use of won.bot.framework.component.atomproducer.AtomProducer in project webofneeds by researchstudio-sat.
the class SolrMatcherQueryTest method main.
public static void main(String[] args) throws IOException, InterruptedException, JsonLdError, SolrServerException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SolrTestAppConfiguration.class);
HintBuilder hintBuilder = ctx.getBean(HintBuilder.class);
// DefaultMatcherQueryExecuter queryExecutor =
// ctx.getBean(DefaultMatcherQueryExecuter.class);
TestMatcherQueryExecutor queryExecutor = ctx.getBean(TestMatcherQueryExecutor.class);
// set the options of the atom producer (e.g. if it should exhaust) in the
// SolrAtomIndexerAppConfiguration file
AtomProducer atomProducer = ctx.getBean(RoundRobinCompositeAtomProducer.class);
while (!atomProducer.isExhausted()) {
// && atoms < 20) {
Dataset ds = atomProducer.create();
try {
TestAtomQueryFactory atomQuery = new TestAtomQueryFactory(ds);
String query = atomQuery.createQuery();
System.out.println("execute query: " + query);
SolrDocumentList docs = queryExecutor.executeAtomQuery(query, 20, null, new BasicAtomQueryFactory(ds).createQuery());
SolrDocumentList matchedDocs = hintBuilder.calculateMatchingResults(docs);
System.out.println("Found docs: " + ((docs != null) ? docs.size() : 0) + ", keep docs: " + ((matchedDocs != null) ? matchedDocs.size() : 0));
if (docs == null) {
continue;
}
System.out.println("Keep docs: ");
System.out.println("======================");
for (SolrDocument doc : matchedDocs) {
String score = doc.getFieldValue("score").toString();
String matchedAtomId = doc.getFieldValue("id").toString();
System.out.println("Score: " + score + ", Id: " + matchedAtomId);
}
System.out.println("All docs: ");
System.out.println("======================");
for (SolrDocument doc : docs) {
String score = doc.getFieldValue("score").toString();
String matchedAtomId = doc.getFieldValue("id").toString();
System.out.println("Score: " + score + ", Id: " + matchedAtomId);
}
} catch (SolrException e) {
System.err.println(e);
}
}
System.exit(0);
}
use of won.bot.framework.component.atomproducer.AtomProducer in project webofneeds by researchstudio-sat.
the class AbstractCompositeAtomProducer method selectNonExhaustedAtomFactory.
private AtomProducer selectNonExhaustedAtomFactory() {
AtomProducer delegate;
// keep fetching delegates, and remove them from the list if they are exhausted
while ((delegate = selectActiveAtomFactory()) != null && delegate.isExhausted()) {
// here we have a non-null delegate that is exhausted. Remove it
this.atomFactories.remove(delegate);
delegate = null;
}
// completely exhausted
return delegate;
}
Aggregations