[英]using a sparql query as a building block in another query
我可以编写一个SPARQL查询以将单个三元组中找到的句子中的单词与单个句子联系起来:
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
但是我找不到在其他查询中使用此选择语句的方法,在该查询中找到句子?sent
并想插入此选择语句:
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
}
}
结果不是在第一部分中找到的句子,但是似乎嵌套查询中?sent
的语句不受外部查询的限制。 我看不到如何正确嵌套。 谢谢你的帮助!
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.