Search in sources :

Example 51 with ExprVar

use of org.apache.jena.sparql.expr.ExprVar 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

ExprVar (org.apache.jena.sparql.expr.ExprVar)51 Expr (org.apache.jena.sparql.expr.Expr)34 Test (org.junit.Test)17 ContractTest (org.xenei.junit.contract.ContractTest)15 Var (org.apache.jena.sparql.core.Var)12 SortCondition (org.apache.jena.query.SortCondition)11 Node (org.apache.jena.graph.Node)9 Op (org.apache.jena.sparql.algebra.Op)7 Triple (org.apache.jena.graph.Triple)6 Query (org.apache.jena.query.Query)6 E_LessThan (org.apache.jena.sparql.expr.E_LessThan)5 E_Random (org.apache.jena.sparql.expr.E_Random)5 NodeValueInteger (org.apache.jena.sparql.expr.nodevalue.NodeValueInteger)5 OpBGP (org.apache.jena.sparql.algebra.op.OpBGP)4 BasicPattern (org.apache.jena.sparql.core.BasicPattern)4 E_Multiply (org.apache.jena.sparql.expr.E_Multiply)4 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)4 ArrayList (java.util.ArrayList)3 E_Regex (org.apache.jena.sparql.expr.E_Regex)3 Random (java.util.Random)2