Search in sources :

Example 21 with AtomModelWrapper

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);
}
Also used : SparqlMatcherConfig(won.matcher.sparql.config.SparqlMatcherConfig) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) java.util(java.util) ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) won.matcher.service.common.event(won.matcher.service.common.event) BasicPattern(org.apache.jena.sparql.core.BasicPattern) Autowired(org.springframework.beans.factory.annotation.Autowired) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpUnion(org.apache.jena.sparql.algebra.op.OpUnion) Logging(akka.event.Logging) Algebra(org.apache.jena.sparql.algebra.Algebra) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct) Scope(org.springframework.context.annotation.Scope) ActorRef(akka.actor.ActorRef) Op(org.apache.jena.sparql.algebra.Op) JsonLdError(com.github.jsonldjava.core.JsonLdError) DistributedPubSub(akka.cluster.pubsub.DistributedPubSub) UntypedActor(akka.actor.UntypedActor) StreamSupport(java.util.stream.StreamSupport) SupervisorStrategy(akka.actor.SupervisorStrategy) URI(java.net.URI) WONMATCH(won.protocol.vocabulary.WONMATCH) org.apache.jena.query(org.apache.jena.query) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) LoggingAdapter(akka.event.LoggingAdapter) Function(akka.japi.Function) BindingHashMap(org.apache.jena.sparql.engine.binding.BindingHashMap) Triple(org.apache.jena.graph.Triple) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) org.apache.jena.rdf.model(org.apache.jena.rdf.model) AtomState(won.protocol.model.AtomState) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Component(org.springframework.stereotype.Component) Duration(scala.concurrent.duration.Duration) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) OneForOneStrategy(akka.actor.OneForOneStrategy) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) Var(org.apache.jena.sparql.core.Var) BindingHashMap(org.apache.jena.sparql.engine.binding.BindingHashMap) IOException(java.io.IOException) ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 22 with AtomModelWrapper

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());
}
Also used : SparqlMatcherConfig(won.matcher.sparql.config.SparqlMatcherConfig) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) java.util(java.util) ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) won.matcher.service.common.event(won.matcher.service.common.event) BasicPattern(org.apache.jena.sparql.core.BasicPattern) Autowired(org.springframework.beans.factory.annotation.Autowired) OpProject(org.apache.jena.sparql.algebra.op.OpProject) OpUnion(org.apache.jena.sparql.algebra.op.OpUnion) Logging(akka.event.Logging) Algebra(org.apache.jena.sparql.algebra.Algebra) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct) Scope(org.springframework.context.annotation.Scope) ActorRef(akka.actor.ActorRef) Op(org.apache.jena.sparql.algebra.Op) JsonLdError(com.github.jsonldjava.core.JsonLdError) DistributedPubSub(akka.cluster.pubsub.DistributedPubSub) UntypedActor(akka.actor.UntypedActor) StreamSupport(java.util.stream.StreamSupport) SupervisorStrategy(akka.actor.SupervisorStrategy) URI(java.net.URI) WONMATCH(won.protocol.vocabulary.WONMATCH) org.apache.jena.query(org.apache.jena.query) OpAsQuery(org.apache.jena.sparql.algebra.OpAsQuery) LoggingAdapter(akka.event.LoggingAdapter) Function(akka.japi.Function) BindingHashMap(org.apache.jena.sparql.engine.binding.BindingHashMap) Triple(org.apache.jena.graph.Triple) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) org.apache.jena.rdf.model(org.apache.jena.rdf.model) AtomState(won.protocol.model.AtomState) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Component(org.springframework.stereotype.Component) Duration(scala.concurrent.duration.Duration) Stream(java.util.stream.Stream) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) OneForOneStrategy(akka.actor.OneForOneStrategy) AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 23 with AtomModelWrapper

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);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Dataset(org.apache.jena.query.Dataset) Model(org.apache.jena.rdf.model.Model) AtomModelWrapper(won.protocol.util.AtomModelWrapper) AtomProducer(won.bot.framework.component.atomproducer.AtomProducer) RoundRobinCompositeAtomProducer(won.bot.framework.component.atomproducer.impl.RoundRobinCompositeAtomProducer) AtomIndexer(won.matcher.solr.index.AtomIndexer)

Example 24 with AtomModelWrapper

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();
}
Also used : AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Example 25 with AtomModelWrapper

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();
}
Also used : AtomModelWrapper(won.protocol.util.AtomModelWrapper)

Aggregations

AtomModelWrapper (won.protocol.util.AtomModelWrapper)37 Dataset (org.apache.jena.query.Dataset)19 Resource (org.apache.jena.rdf.model.Resource)15 URI (java.net.URI)9 Model (org.apache.jena.rdf.model.Model)8 WonMessage (won.protocol.message.WonMessage)6 RdfUtils (won.protocol.util.RdfUtils)6 IOException (java.io.IOException)5 MethodHandles (java.lang.invoke.MethodHandles)5 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 GoalInstantiationProducer (won.utils.goals.GoalInstantiationProducer)5 GoalInstantiationResult (won.utils.goals.GoalInstantiationResult)5 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)4 java.util (java.util)4 Collectors (java.util.stream.Collectors)4 StringUtils (org.apache.commons.lang3.StringUtils)4 ActorRef (akka.actor.ActorRef)3