use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class AbstractCreateAtomAction method createWonMessage.
/**
* @deprecated wonNodeUri parameter is obsolete, flags should be set within
* atomDataset
*/
@Deprecated
protected WonMessage createWonMessage(URI atomURI, URI wonNodeURI, Dataset atomDataset, final boolean usedForTesting, final boolean doNotMatch) throws WonMessageBuilderException {
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
if (doNotMatch) {
atomModelWrapper.addFlag(WONMATCH.NoHintForMe);
atomModelWrapper.addFlag(WONMATCH.NoHintForCounterpart);
}
if (usedForTesting) {
atomModelWrapper.addFlag(WONMATCH.UsedForTesting);
}
RdfUtils.replaceBaseURI(atomDataset, atomURI.toString(), true);
return WonMessageBuilder.createAtom().atom(atomURI).content().dataset(atomModelWrapper.copyDatasetWithoutSysinfo()).build();
}
use of won.protocol.util.AtomModelWrapper 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());
}
use of won.protocol.util.AtomModelWrapper 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());
}
use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class TrigFileAtomProducer method readAtomFromFile.
@Override
public synchronized Dataset readAtomFromFile(final File file) throws IOException {
logger.debug("processing as turtle file: {} ", file);
try (FileInputStream fis = new FileInputStream(file)) {
Dataset dataset = DatasetFactory.createGeneral();
RDFDataMgr.read(dataset, fis, RDFFormat.TRIG.getLang());
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(dataset);
return atomModelWrapper.copyDatasetWithoutSysinfo();
} catch (Exception e) {
logger.error("could not parse trig from file {} ", file, e);
throw e;
}
}
use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class SolrMatcherActor method processActiveAtomEvent.
protected void processActiveAtomEvent(AtomEvent atomEvent) throws IOException, SolrServerException, JsonLdError {
log.info("Start processing active atom event {}", atomEvent);
// check if the atom has doNotMatch flag, then do not use it for querying or
// indexing
Dataset dataset = atomEvent.deserializeAtomDataset();
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(dataset);
if (atomModelWrapper.flag(WONMATCH.NoHintForMe) && atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
log.info("Discarding received atom due to flags match:NoHintForMe and match:NoHintForCounterpart: {}", atomEvent);
return;
}
// check if atom has a sparql query attached
if (atomModelWrapper.sparqlQuery()) {
log.debug("Atom {} has a sparql query, omitting this atom in Solr matcher", atomModelWrapper.getAtomUri());
return;
}
// check if atom is usedForTesting only
boolean usedForTesting = atomModelWrapper.flag(WONMATCH.UsedForTesting);
SolrMatcherQueryExecutor queryExecutor = (usedForTesting ? testQueryExecuter : defaultQueryExecuter);
// create another query depending if the current atom is "WhatsAround" or a
// default atom
String queryString = null;
if (atomModelWrapper.flag(WONMATCH.WhatsAround)) {
// WhatsAround doesnt match on terms only other atoms in close location are
// boosted
WhatsAroundQueryFactory qf = new WhatsAroundQueryFactory(dataset);
queryString = qf.createQuery();
} else if (atomModelWrapper.flag(WONMATCH.WhatsNew)) {
WhatsNewQueryFactory qf = new WhatsNewQueryFactory(dataset);
queryString = qf.createQuery();
} else {
// default query matches content terms (of fields title, description and tags)
// with different weights
// and gives an additional multiplicative boost for geographically closer atoms
DefaultAtomQueryFactory qf = new DefaultAtomQueryFactory(dataset);
queryString = qf.createQuery();
}
// add filters to the query: default filters are
// - atom status active
// - creation date overlap 1 month
// - OR-filtering for matching contexts if any were specified
// now create three slightly different queries for different lists of atoms:
// 1) atoms without NoHintForCounterpart => hints for current atom
// 2) atoms without NoHintForSelf, excluding WhatsAround atoms => hints for
// atoms in index that are not WhatsAround
// 3) atoms without NoHintForSelf that are only WhatsAround atoms => hints for
// atoms in index that are WhatsAround
// to achieve this use a different filters for these queries
// case 1) atoms without NoHintForCounterpart => hints for current atom
List<String> filterQueries = new LinkedList<>();
filterQueries.add(new AtomStateQueryFactory(dataset).createQuery());
filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_COUNTERPART)).createQuery());
if (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
}
if (!atomModelWrapper.flag(WONMATCH.NoHintForMe)) {
// execute the query
log.info("query Solr endpoint {} for atom {} and atom list 1 (without NoHintForCounterpart)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHints(), null, filterQueries.toArray(new String[filterQueries.size()]));
if (docs != null) {
// perform knee detection depending on current atom is WhatsAround/WhatsNew or
// not)
boolean kneeDetection = atomModelWrapper.flag(WONMATCH.WhatsNew) || atomModelWrapper.flag(WONMATCH.WhatsAround) ? false : true;
// generate hints for current atom (only generate hints for current atom,
// suppress hints for matched atoms,
BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, false, true, kneeDetection);
log.info("Create {} hints for atom {} and atom list 1 (without NoHintForCounterpart)", events.getHintEvents().size(), atomEvent);
// publish hints to current atom
if (events.getHintEvents().size() != 0) {
getSender().tell(events, getSelf());
}
} else {
log.warning("No results found for atom list 1 (without NoHintForCounterpart) query of atom ", atomEvent);
}
}
// case 2) atoms without NoHintForSelf, excluding WhatsAround atoms => hints for
// atoms in index that are not WhatsAround
filterQueries = new LinkedList<>();
filterQueries.add(new AtomStateQueryFactory(dataset).createQuery());
filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_ME)).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_AROUND)).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_NEW)).createQuery());
if (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
}
if (!atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
// execute the query
log.info("query Solr endpoint {} for atom {} and atom list 2 (without NoHintForSelf, excluding WhatsAround atoms)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHintsForCounterparts(), null, filterQueries.toArray(new String[filterQueries.size()]));
if (docs != null) {
// generate hints for matched atoms (suppress hints for current atom, only
// generate hints for matched atoms, perform knee detection)
BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, true, false, true);
log.info("Create {} hints for atom {} and atom list 2 (without NoHintForSelf, excluding WhatsAround atoms)", events.getHintEvents().size(), atomEvent);
// publish hints to current atom
if (events.getHintEvents().size() != 0) {
getSender().tell(events, getSelf());
}
} else {
log.warning("No results found for atom list 2 (without NoHintForSelf, excluding WhatsAround atoms) query of atom ", atomEvent);
}
}
// case 3) atoms without NoHintForSelf that are only WhatsAround atoms => hints
// for atoms in index that are WhatsAround
filterQueries = new LinkedList<>();
filterQueries.add(new AtomStateQueryFactory(dataset).createQuery());
filterQueries.add(new CreationDateQueryFactory(dataset, 1, ChronoUnit.MONTHS).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.NOT, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.NO_HINT_FOR_ME)).createQuery());
filterQueries.add(new BooleanQueryFactory(BooleanQueryFactory.BooleanOperator.OR, new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_AROUND), new HasFlagQueryFactory(HasFlagQueryFactory.FLAGS.WHATS_NEW)).createQuery());
if (atomModelWrapper.getMatchingContexts() != null && atomModelWrapper.getMatchingContexts().size() > 0) {
filterQueries.add(new MatchingContextQueryFactory(atomModelWrapper.getMatchingContexts()).createQuery());
}
if (!atomModelWrapper.flag(WONMATCH.NoHintForCounterpart)) {
// hints for WhatsAround Atoms should not have the keywords from title,
// description, tags etc.
// this can prevent to actually find WhatsAround atoms.
// Instead create a WhatsAround query (query without keywords, just location) to
// find other WhatsAround atoms
queryString = (new WhatsAroundQueryFactory(dataset)).createQuery();
// execute the query
log.info("query Solr endpoint {} for atom {} and atom list 3 (without NoHintForSelf that are only WhatsAround atoms)", config.getSolrEndpointUri(usedForTesting), atomEvent.getUri());
SolrDocumentList docs = queryExecutor.executeAtomQuery(queryString, config.getMaxHintsForCounterparts(), null, filterQueries.toArray(new String[filterQueries.size()]));
if (docs != null) {
// generate hints for matched atoms (suppress hints for current atom, only
// generate hints for matched atoms, do not perform knee detection)
BulkHintEvent events = hintBuilder.generateHintsFromSearchResult(docs, atomEvent, atomModelWrapper, true, false, false);
log.info("Create {} hints for atom {} and atom list 3 (without NoHintForSelf that are only WhatsAround atoms)", events.getHintEvents().size(), atomEvent);
// publish hints to current atom
if (events.getHintEvents().size() != 0) {
getSender().tell(events, getSelf());
}
} else {
log.warning("No results found for atom list 3 (without NoHintForSelf that are only WhatsAround atoms) query of atom ", atomEvent);
}
}
// index atom
log.info("Add atom event content {} to solr index", atomEvent);
atomIndexer.index(dataset);
}
Aggregations