use of won.bot.framework.component.needproducer.NeedProducer in project webofneeds by researchstudio-sat.
the class AbstractCompositeNeedProducer method selectNonExhaustedNeedFactory.
private NeedProducer selectNonExhaustedNeedFactory() {
NeedProducer delegate = null;
// keep fetching delegates, and remove them from the list if they are exhausted
while ((delegate = selectActiveNeedFactory()) != null && delegate.isExhausted()) {
// here we have a non-null delegate that is exhausted. Remove it
this.needFactories.remove(delegate);
delegate = null;
}
// here, a non-null delegate will not be exhausted. If it is null, we're completely exhausted
return delegate;
}
use of won.bot.framework.component.needproducer.NeedProducer 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;
}
final boolean usedForTesting = this.usedForTesting;
final boolean doNotMatch = this.doNotMatch;
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 need creations means that all needs were created
final TargetCounterDecorator creationUnfinishedCounter = new TargetCounterDecorator(ctx, new CounterImpl("creationUnfinished"), 0);
BotTrigger createFactoryNeedTrigger = new BotTrigger(ctx, Duration.ofMillis(FACTORYNEEDCREATION_DURATION_INMILLIS));
createFactoryNeedTrigger.activate();
bus.subscribe(StartFactoryNeedCreationEvent.class, new ActionOnFirstEventListener(ctx, new PublishEventAction(ctx, new StartBotTriggerCommandEvent(createFactoryNeedTrigger))));
bus.subscribe(BotTriggerEvent.class, new ActionOnTriggerEventListener(ctx, createFactoryNeedTrigger, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (isTooManyMessagesInFlight(messagesInFlightCounter)) {
return;
}
adjustTriggerInterval(createFactoryNeedTrigger, messagesInFlightCounter);
// defined via spring
NeedProducer needProducer = ctx.getNeedProducer();
Dataset dataset = needProducer.create();
if (dataset == null && needProducer.isExhausted()) {
bus.publish(new NeedProducerExhaustedEvent());
bus.unsubscribe(executingListener);
return;
}
URI needUriFromProducer = null;
Resource needResource = WonRdfUtils.NeedUtils.getNeedResource(dataset);
if (needResource.isURIResource()) {
needUriFromProducer = URI.create(needResource.getURI());
}
if (needUriFromProducer != null) {
URI needURI = botContextWrapper.getURIFromInternal(needUriFromProducer);
if (needURI != null) {
bus.publish(new FactoryNeedCreationSkippedEvent());
} else {
bus.publish(new CreateNeedCommandEvent(dataset, botContextWrapper.getFactoryListName(), usedForTesting, doNotMatch));
}
}
}
}));
bus.subscribe(CreateNeedCommandSuccessEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, // decrease the creationUnfinishedCounter
new DecrementCounterAction(ctx, creationUnfinishedCounter), // count a successful need creation
new IncrementCounterAction(ctx, needCreationSuccessfulCounter), new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (event instanceof CreateNeedCommandSuccessEvent) {
CreateNeedCommandSuccessEvent needCreatedEvent = (CreateNeedCommandSuccessEvent) event;
botContextWrapper.addInternalIdToUriReference(needCreatedEvent.getNeedUriBeforeCreation(), needCreatedEvent.getNeedURI());
}
}
})));
bus.subscribe(CreateNeedCommandEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, // execute the need creation for the need in the event
new ExecuteCreateNeedCommandAction(ctx), // increase the needCreationStartedCounter to signal another pending creation
new IncrementCounterAction(ctx, needCreationStartedCounter), // increase the creationUnfinishedCounter to signal another pending creation
new IncrementCounterAction(ctx, creationUnfinishedCounter))));
// if a need is already created we skip the recreation of it and increase the needCreationSkippedCounter
bus.subscribe(FactoryNeedCreationSkippedEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, needCreationSkippedCounter)));
// if a creation failed, we don't want to keep us from keeping the correct count
bus.subscribe(CreateNeedCommandFailureEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, // decrease the creationUnfinishedCounter
new DecrementCounterAction(ctx, creationUnfinishedCounter), // count an unsuccessful need creation
new IncrementCounterAction(ctx, needCreationFailedCounter))));
// when the needproducer is exhausted, we stop the creator (trigger) and we have to wait until all unfinished need creations finish
// when they do, the InitFactoryFinishedEvent is published
bus.subscribe(NeedProducerExhaustedEvent.class, new ActionOnFirstEventListener(ctx, new MultipleActions(ctx, new PublishEventAction(ctx, new StopBotTriggerCommandEvent(createFactoryNeedTrigger)), new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
// when we're called, there probably are need creations unfinished, but there may not be
// a)
// first, prepare for the case when there are unfinished need creations:
// we register a listener, waiting for the unfinished counter to reach 0
EventListener waitForUnfinishedNeedsListener = new ActionOnFirstEventListener(ctx, new TargetCounterFilter(creationUnfinishedCounter), new PublishEventAction(ctx, new InitFactoryFinishedEvent()));
bus.subscribe(TargetCountReachedEvent.class, waitForUnfinishedNeedsListener);
// 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(waitForUnfinishedNeedsListener);
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("FactoryNeedCreation finished: total:{}, successful: {}, failed: {}, skipped: {}", new Object[] { needCreationStartedCounter.getCount(), needCreationSuccessfulCounter.getCount(), needCreationFailedCounter.getCount(), needCreationSkippedCounter.getCount() });
}
}));
// MessageInFlight counter handling *************************
bus.subscribe(MessageCommandEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, messagesInFlightCounter)));
bus.subscribe(MessageCommandResultEvent.class, new ActionOnEventListener(ctx, new DecrementCounterAction(ctx, messagesInFlightCounter)));
// if we receive a message command failure, log it
bus.subscribe(MessageCommandFailureEvent.class, new ActionOnEventListener(ctx, new LogMessageCommandFailureAction(ctx)));
// Start the need creation stuff
bus.publish(new StartFactoryNeedCreationEvent());
}
use of won.bot.framework.component.needproducer.NeedProducer 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 need producer (e.g. if it should exhaust) in the SolrNeedIndexerAppConfiguration file
NeedProducer needProducer = ctx.getBean(RoundRobinCompositeNeedProducer.class);
while (!needProducer.isExhausted()) {
// && needs < 20) {
Dataset ds = needProducer.create();
try {
TestNeedQueryFactory needQuery = new TestNeedQueryFactory(ds);
String query = needQuery.createQuery();
System.out.println("execute query: " + query);
SolrDocumentList docs = queryExecutor.executeNeedQuery(query, null, new BasicNeedQueryFactory(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 matchedNeedId = doc.getFieldValue("id").toString();
System.out.println("Score: " + score + ", Id: " + matchedNeedId);
}
System.out.println("All docs: ");
System.out.println("======================");
for (SolrDocument doc : docs) {
String score = doc.getFieldValue("score").toString();
String matchedNeedId = doc.getFieldValue("id").toString();
System.out.println("Score: " + score + ", Id: " + matchedNeedId);
}
} catch (SolrException e) {
System.err.println(e);
}
}
System.exit(0);
}
use of won.bot.framework.component.needproducer.NeedProducer in project webofneeds by researchstudio-sat.
the class SolrNeedIndexer method main.
public static void main(String[] args) throws IOException, InterruptedException, JsonLdError {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SolrTestAppConfiguration.class);
NeedIndexer indexer = ctx.getBean(NeedIndexer.class);
// set the options of the need producer (e.g. if it should exhaust) in the SolrNeedIndexerAppConfiguration file
NeedProducer needProducer = ctx.getBean(RoundRobinCompositeNeedProducer.class);
Model needModel = new NeedModelWrapper(needProducer.create()).copyNeedModel(NeedGraphType.NEED);
int needs = 0;
while (!needProducer.isExhausted()) {
// indexer.indexNeedModel(needModel, UUID.randomUUID().toString(), true);
Dataset ds = DatasetFactory.createTxnMem();
ds.addNamedModel("https://node.matchat.org/won/resource/need/test#need", needModel);
NeedModelWrapper needModelWrapper = new NeedModelWrapper(needModel, null);
needModel = needModelWrapper.normalizeNeedModel();
indexer.indexNeedModel(needModel, SolrMatcherEvaluation.createNeedId(ds), true);
needs++;
if (needs % 100 == 0) {
System.out.println("Indexed " + needs + " needs.");
}
needModel = new NeedModelWrapper(needProducer.create()).copyNeedModel(NeedGraphType.NEED);
}
System.out.println("Indexed " + needs + " needs.");
System.exit(0);
}
use of won.bot.framework.component.needproducer.NeedProducer in project webofneeds by researchstudio-sat.
the class AbstractCompositeNeedProducer method create.
@Override
public synchronized Dataset create() {
logger.debug("starting to produce a need model");
NeedProducer delegate = selectNonExhaustedNeedFactory();
if (delegate == null) {
logger.warn("cannot produce a need model - all need factories are exhausted");
// we're exhausted
return null;
}
return delegate.create();
}
Aggregations