Search in sources :

Example 1 with OpUnion

use of org.apache.jena.sparql.algebra.op.OpUnion in project webofneeds by researchstudio-sat.

the class SparqlMatcherActor method defaultQuery.

private Optional<Op> defaultQuery(AtomModelWrapper atom) {
    Model model = atom.getAtomModel();
    String atomURI = atom.getAtomUri();
    ArrayList<Op> queries = new ArrayList<>(3);
    Statement seeks = model.getProperty(model.createResource(atomURI), model.createProperty("https://w3id.org/won/matching#seeks"));
    if (seeks != null) {
        Op seeksQuery = createAtomQuery(model, seeks);
        if (seeksQuery != null) {
            queries.add(seeksQuery);
        }
    }
    Statement search = model.getProperty(model.createResource(atomURI), model.createProperty("https://w3id.org/won/matching#searchString"));
    if (search != null) {
        String searchString = search.getString();
        queries.add(SparqlMatcherUtils.createSearchQuery(searchString, resultName, 2, true, true));
    }
    return queries.stream().reduce((left, right) -> new OpUnion(left, right)).map((union) -> new OpDistinct(new OpProject(union, Arrays.asList(new Var[] { resultName }))));
}
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) Op(org.apache.jena.sparql.algebra.Op) OpUnion(org.apache.jena.sparql.algebra.op.OpUnion) OpProject(org.apache.jena.sparql.algebra.op.OpProject) Var(org.apache.jena.sparql.core.Var) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct)

Example 2 with OpUnion

use of org.apache.jena.sparql.algebra.op.OpUnion in project webofneeds by researchstudio-sat.

the class SparqlMatcherUtils method createSearchQuery.

public static Op createSearchQuery(String searchString, Var resultName, int hops, boolean disjunctive, boolean tokenize) {
    Var textSearchTarget = Var.alloc("textSearchTarget");
    Optional<Op> union = IntStream.range(1, hops + 1).mapToObj(hopCount -> makePathBGPPattern(resultName, textSearchTarget, hopCount, op -> {
        Expr filterExpression = Arrays.stream(tokenize ? searchString.toLowerCase().split(" ") : new String[] { searchString.toLowerCase() }).<Expr>map(searchPart -> new E_StrContains(new E_StrLowerCase(new ExprVar(textSearchTarget)), new NodeValueString(searchPart))).reduce((left, right) -> disjunctive ? new E_LogicalOr(left, right) : new E_LogicalAnd(left, right)).orElse(new NodeValueBoolean(true));
        return OpFilter.filterBy(new ExprList(filterExpression), op);
    })).reduce((op1, op2) -> new OpUnion(op1, op2));
    Op maintriple = new OpTriple(new Triple(resultName, RDF.type.asNode(), WON.Atom.asNode()));
    Op mainOp = union.isPresent() ? OpJoin.create(maintriple, union.get()) : maintriple;
    return mainOp;
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) Transformer(org.apache.jena.sparql.algebra.Transformer) RDF(org.apache.jena.vocabulary.RDF) ResourceImpl(org.apache.jena.rdf.model.impl.ResourceImpl) OpModifier(org.apache.jena.sparql.algebra.op.OpModifier) OpVisitorBase(org.apache.jena.sparql.algebra.OpVisitorBase) BasicPattern(org.apache.jena.sparql.core.BasicPattern) ExprList(org.apache.jena.sparql.expr.ExprList) Random(java.util.Random) OpGraph(org.apache.jena.sparql.algebra.op.OpGraph) OpJoin(org.apache.jena.sparql.algebra.op.OpJoin) OpProject(org.apache.jena.sparql.algebra.op.OpProject) Walker(org.apache.jena.sparql.algebra.walker.Walker) OpUnion(org.apache.jena.sparql.algebra.op.OpUnion) Function(java.util.function.Function) OpDistinct(org.apache.jena.sparql.algebra.op.OpDistinct) OpService(org.apache.jena.sparql.algebra.op.OpService) OpConditional(org.apache.jena.sparql.algebra.op.OpConditional) E_LogicalOr(org.apache.jena.sparql.expr.E_LogicalOr) OpList(org.apache.jena.sparql.algebra.op.OpList) Op(org.apache.jena.sparql.algebra.Op) TransformCopy(org.apache.jena.sparql.algebra.TransformCopy) E_StrLowerCase(org.apache.jena.sparql.expr.E_StrLowerCase) ExprVar(org.apache.jena.sparql.expr.ExprVar) WONMATCH(won.protocol.vocabulary.WONMATCH) WON(won.protocol.vocabulary.WON) OpLeftJoin(org.apache.jena.sparql.algebra.op.OpLeftJoin) Op2(org.apache.jena.sparql.algebra.op.Op2) NodeValueString(org.apache.jena.sparql.expr.nodevalue.NodeValueString) Triple(org.apache.jena.graph.Triple) Expr(org.apache.jena.sparql.expr.Expr) E_NotExists(org.apache.jena.sparql.expr.E_NotExists) OpVisitorByTypeBase(org.apache.jena.sparql.algebra.OpVisitorByTypeBase) OpSlice(org.apache.jena.sparql.algebra.op.OpSlice) Var(org.apache.jena.sparql.core.Var) OpBGP(org.apache.jena.sparql.algebra.op.OpBGP) E_LogicalAnd(org.apache.jena.sparql.expr.E_LogicalAnd) NodeValueBoolean(org.apache.jena.sparql.expr.nodevalue.NodeValueBoolean) OpTriple(org.apache.jena.sparql.algebra.op.OpTriple) OpMinus(org.apache.jena.sparql.algebra.op.OpMinus) Optional(java.util.Optional) OpOrder(org.apache.jena.sparql.algebra.op.OpOrder) E_StrContains(org.apache.jena.sparql.expr.E_StrContains) OpFilter(org.apache.jena.sparql.algebra.op.OpFilter) OpReduced(org.apache.jena.sparql.algebra.op.OpReduced) ExprVar(org.apache.jena.sparql.expr.ExprVar) Op(org.apache.jena.sparql.algebra.Op) OpUnion(org.apache.jena.sparql.algebra.op.OpUnion) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) NodeValueString(org.apache.jena.sparql.expr.nodevalue.NodeValueString) Triple(org.apache.jena.graph.Triple) OpTriple(org.apache.jena.sparql.algebra.op.OpTriple) E_LogicalAnd(org.apache.jena.sparql.expr.E_LogicalAnd) NodeValueBoolean(org.apache.jena.sparql.expr.nodevalue.NodeValueBoolean) Expr(org.apache.jena.sparql.expr.Expr) ExprList(org.apache.jena.sparql.expr.ExprList) E_StrLowerCase(org.apache.jena.sparql.expr.E_StrLowerCase) E_LogicalOr(org.apache.jena.sparql.expr.E_LogicalOr) OpTriple(org.apache.jena.sparql.algebra.op.OpTriple) E_StrContains(org.apache.jena.sparql.expr.E_StrContains)

Aggregations

Triple (org.apache.jena.graph.Triple)2 ResourceImpl (org.apache.jena.rdf.model.impl.ResourceImpl)2 Op (org.apache.jena.sparql.algebra.Op)2 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)2 OpDistinct (org.apache.jena.sparql.algebra.op.OpDistinct)2 OpProject (org.apache.jena.sparql.algebra.op.OpProject)2 OpUnion (org.apache.jena.sparql.algebra.op.OpUnion)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 Var (org.apache.jena.sparql.core.Var)2 WONMATCH (won.protocol.vocabulary.WONMATCH)2 ActorRef (akka.actor.ActorRef)1 OneForOneStrategy (akka.actor.OneForOneStrategy)1 SupervisorStrategy (akka.actor.SupervisorStrategy)1 UntypedActor (akka.actor.UntypedActor)1 DistributedPubSub (akka.cluster.pubsub.DistributedPubSub)1 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 Logging (akka.event.Logging)1 LoggingAdapter (akka.event.LoggingAdapter)1 Function (akka.japi.Function)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1