use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class SparqlMatcherActor method executeQuery.
/**
* Executes the query, optionally only searching in the atomToCheck.
*
* @param q
* @param atomToCheck
* @param atomURI - the URI of the atom we are matching for
* @return
*/
private Stream<ScoredAtom> executeQuery(Op q, String atomURI) {
Query compiledQuery = OpAsQuery.asQuery(q);
// if we were given an atomToCheck, restrict the query result to that uri so
// that
// we get exactly one result if that uri is found for the atom
List<Var> valuesBlockVariables = new ArrayList<>();
// bind the ?thisAtom variable to the atom we are matching for
BindingHashMap bindingMap = new BindingHashMap();
bindingMap.add(thisAtom, new ResourceImpl(atomURI.toString()).asNode());
valuesBlockVariables.add(thisAtom);
compiledQuery.setValuesDataBlock(valuesBlockVariables, Collections.singletonList(bindingMap));
// make sure we order by score, if present, and we limit the results
if (compiledQuery.getProjectVars().contains(scoreName)) {
compiledQuery.addOrderBy(scoreName, Query.ORDER_DESCENDING);
}
if (!compiledQuery.hasLimit() || compiledQuery.getLimit() > config.getLimitResults() * 5) {
compiledQuery.setLimit(config.getLimitResults() * 5);
}
compiledQuery.setOffset(0);
compiledQuery.setDistinct(true);
if (log.isDebugEnabled()) {
log.debug("executeQuery query: {}", new Object[] { compiledQuery });
}
List<ScoredAtomUri> foundUris = new LinkedList<>();
// process query results iteratively
try (QueryExecution execution = QueryExecutionFactory.sparqlService(config.getSparqlEndpoint(), compiledQuery)) {
ResultSet result = execution.execSelect();
while (result.hasNext()) {
QuerySolution querySolution = result.next();
RDFNode atomUriNode = querySolution.get(resultName.getName());
if (atomUriNode == null || !atomUriNode.isURIResource()) {
continue;
}
String foundAtomURI = atomUriNode.asResource().getURI();
double score = 1.0;
if (querySolution.contains(scoreName.getName())) {
RDFNode scoreNode = querySolution.get(scoreName.getName());
if (scoreNode != null && scoreNode.isLiteral()) {
try {
score = scoreNode.asLiteral().getDouble();
} catch (NumberFormatException e) {
// if the score is not interpretable as double, ignore it
}
}
}
foundUris.add(new ScoredAtomUri(foundAtomURI, score));
}
} catch (Exception e) {
log.info("caught exception during sparql-based matching (more info on loglevel 'debug'): {} ", e.getMessage());
if (log.isDebugEnabled()) {
e.printStackTrace();
}
return Stream.empty();
}
// load data in parallel
return foundUris.parallelStream().map(foundAtomUri -> {
try {
// download the linked data and return a new AtomModelWrapper
Dataset ds = linkedDataSource.getDataForPublicResource(URI.create(foundAtomUri.uri));
// make sure we don't accidentally use empty or faulty results
if (!AtomModelWrapper.isAAtom(ds)) {
return null;
}
return new ScoredAtom(new AtomModelWrapper(ds), foundAtomUri.score);
} catch (Exception e) {
log.info("caught exception trying to load atom URI {} : {} (more on loglevel 'debug')", foundAtomUri, e.getMessage());
if (log.isDebugEnabled()) {
e.printStackTrace();
}
return null;
}
}).filter(foundAtom -> foundAtom != null);
}
use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class SparqlMatcherActor method processActiveAtomEvent.
/**
* Produces hints for the atom. The score is calculated as a function of scores
* provided by the embedded sparql queries: the range of those scores is
* projected on a range of 0-1. For inverse matches, the score is always 100%,
* because there is only one possible match - the original atom. Note: this
* could be improved by remembering reported match scores in the matcher and
* using historic scores for normalization, but that's a lot more work.
*/
protected void processActiveAtomEvent(AtomEvent atomEvent) throws IOException {
AtomModelWrapper atom = new AtomModelWrapper(atomEvent.deserializeAtomDataset());
log.debug("starting sparql-based matching for atom {}, cause: {}", atom.getAtomUri(), atomEvent.getCause());
List<ScoredAtom> matches = queryAtom(atom);
log.debug("found {} match candidates", matches.size());
// produce hints after post-filtering the matches we found:
Collection<HintEvent> hintEvents = produceHints(atom, // we
matches.stream().filter(foundAtom -> foundAtom.atom.getAtomState() == AtomState.ACTIVE).filter(foundAtom -> postFilter(atom, foundAtom.atom)).collect(Collectors.toList()), atomEvent.getCause());
publishHintEvents(hintEvents, atom.getAtomUri());
log.debug("finished sparql-based matching for atom {}", atom.getAtomUri());
}
use of won.protocol.util.AtomModelWrapper 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.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class ExecuteCreateAtomCommandAction method createWonMessage.
private WonMessage createWonMessage(URI atomURI, Dataset atomDataset) throws WonMessageBuilderException {
RdfUtils.replaceBaseURI(atomDataset, atomURI.toString(), true);
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
return WonMessageBuilder.createAtom().atom(atomURI).content().dataset(atomModelWrapper.copyDatasetWithoutSysinfo()).direction().fromOwner().build();
}
use of won.protocol.util.AtomModelWrapper in project webofneeds by researchstudio-sat.
the class ExecuteCreateAtomCommandAction method createWonMessage.
/**
* @param atomURI
* @param atomDataset
* @param usedForTesting
* @param doNotMatch
* @deprecated boolean usedForTesting and doNotMatch should not be used but
* added directly to the atomDataset, use
* {@link ExecuteCreateAtomCommandAction#createWonMessage(URI, Dataset)} instead
* @throws WonMessageBuilderException
*/
@Deprecated
private WonMessage createWonMessage(URI atomURI, Dataset atomDataset, final boolean usedForTesting, final boolean doNotMatch) throws WonMessageBuilderException {
RdfUtils.replaceBaseURI(atomDataset, atomURI.toString(), true);
AtomModelWrapper atomModelWrapper = new AtomModelWrapper(atomDataset);
if (doNotMatch) {
atomModelWrapper.addFlag(WONMATCH.NoHintForMe);
atomModelWrapper.addFlag(WONMATCH.NoHintForCounterpart);
}
if (usedForTesting) {
atomModelWrapper.addFlag(WONMATCH.UsedForTesting);
}
return WonMessageBuilder.createAtom().atom(atomURI).content().dataset(atomModelWrapper.copyDatasetWithoutSysinfo()).direction().fromOwner().build();
}
Aggregations