简体   繁体   中英

using a sparql query as a building block in another query

I can write a SPARQL query to concat words in a sentence found in individual triples to a single sentence:

SELECT  ?sent   (sample (?sf1) as ?sf11) (group_concat(?wf)  as ?sf2)  
WHERE 
{   ?sent a nlp:Sentence .
    {select *
    { ?w rdfs:partOf ?sent . 
      ?w a nlp:Token .
      ?w nlp:wordForm ?wf . 
    }   order by ?w 
  }
}  group by ?sent   
  limit 20

but I cannot find a way to use this select statement in another query, where I find the sentence ?sent and would like to insert this select statement:

select * 
where  
{   ?tok a wn:Locomote. 
    ?tok nlp:lemma3 ?lem .
    ?tok rdfs:partOf ?sent .
    ?sent a nlp:Sentence .     
    ?werk ^rdfs:partOf ?sent . 
    {SELECT ?sent  (group_concat(?wf)  as ?sf2)  
    WHERE 
    {   ?sent a nlp:Sentence .
        {select *
        { ?w rdfs:partOf ?sent . 
          ?w a nlp:Token .
          ?w nlp:wordForm ?wf . 
        }   order by ?w 
      }
    }  group by ?sent  
  }
} 

The result is a not the sentence found in the first part, but it seems as ?sent in the nested query is not restricted by the outer query. I do not see how to nest properly. Thank you for help!

Example of custom aggregate in Jena:

package org.stackoverflow.jena.customaggregate;

import com.google.common.base.Joiner;
import org.apache.jena.atlas.logging.LogCtl;
import org.apache.jena.graph.Graph;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.sparql.engine.binding.Binding;
import org.apache.jena.sparql.expr.Expr;
import org.apache.jena.sparql.expr.ExprEvalException;
import org.apache.jena.sparql.expr.ExprList;
import org.apache.jena.sparql.expr.NodeValue;
import org.apache.jena.sparql.expr.aggregate.Accumulator;
import org.apache.jena.sparql.expr.aggregate.AccumulatorFactory;
import org.apache.jena.sparql.expr.aggregate.AggCustom;
import org.apache.jena.sparql.expr.aggregate.AggregateRegistry;
import org.apache.jena.sparql.function.FunctionEnv;
import org.apache.jena.sparql.graph.NodeConst;
import org.apache.jena.sparql.sse.SSE;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class AggGroupConcatSorted {
    static {
        LogCtl.setCmdLogging();
    }

    static AccumulatorFactory accumulatorFactory = (agg, distinct) -> new AccGroupConcatSorted(agg);

    static class AccGroupConcatSorted implements Accumulator {
        private AggCustom agg;

        private List<String> nodeStrList = new ArrayList<>();

        AccGroupConcatSorted(AggCustom agg) {
            this.agg = agg;
        }

        @Override
        public void accumulate(Binding binding, FunctionEnv functionEnv) {
            ExprList exprList = agg.getExprList();
            for (Expr expr : exprList) {
                try {
                    NodeValue nv = expr.eval(binding, functionEnv);
                    // Evaluation succeeded, add string to list
                    nodeStrList.add(nv.asString());
                } catch (ExprEvalException ex) {
                }
            }
        }

        @Override
        public NodeValue getValue() {
            // sort list
            Collections.sort(nodeStrList);
            // return single node which in fact is the concatenated string of the list elements
            return NodeValue.makeString(Joiner.on(" ").join(nodeStrList));
        }
    }

    public static void main(String[] args) {
        // Example aggregate that concatenates the node values in sorted order
        String aggUri = "http://example.org/group_concat_sorted" ;

        // register the custom aggregate - returns unbound for no rows
        AggregateRegistry.register(aggUri, accumulatorFactory, NodeConst.nodeMinusOne);

        // sample data
        Graph g = SSE.parseGraph("(graph (:s :p \"b\") (:s :p \"bc\") (:s :p \"abc\"))") ;
        String qs = "SELECT (<http://example.org/group_concat_sorted>(?o) AS ?x) {?s ?p ?o}" ;

        // query execution
        Query q = QueryFactory.create(qs) ;
        try ( QueryExecution qexec = QueryExecutionFactory.create(q, ModelFactory.createModelForGraph(g)) ) {
            ResultSet rs = qexec.execSelect() ;
            ResultSetFormatter.out(rs);
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM