这是我的查询
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix no: <http://www.newontology.org/no#>
prefix rs: <http://semanticrecommender.com/rs#>
prefix mo: <http://music.org/musicontology/mo#>
prefix : <http://www.MusicSemanticOntology.com/mso#>
select ?item (SUM(?similarity * ?importance * ?levelImportance * ?ratingValue) as ?summedSimilarity)
(group_concat(distinct ?x) as ?commingFromLikingThisInstance)
(group_concat(?becauseOf ; separator = " ,and ") as ?reason)
where
{
values ?user { :ania }
#the variable ?x is bound to the items the user :ania has liked.
?user rs:hasRated ?ratings.
?ratings a rs:Likes.
?ratings rs:about ?x.
?ratings rs:ratesBy ?ratingValue.
?ratings rs:createdOn ?ratingDate.
#level 0 class similarities
{
#extract all the items that are from the same class (type) as the liked items.
#I assumed the being from the same class accounts for 50% of the similarities.
#This value can be changed according to the test or the application domain.
values ?classImportance {0.5} #class level
?x a ?class .
?item a ?class .
?class rs:hasSimilarityValue ?similarity .
bind (?classImportance as ?importance)
bind( 4/7 as ?levelImportance)
bind (concat("it shares the same class, which is ", str(?class), " with ", str(?x)) as ?becauseOf)
}
}
group by ?item
order by desc(?summedSimilarity)
如果我将它放在fuseki sparql接口中,它会工作,但是如果我把它放在一个文件中并从jena调用该文件,我会得到以下例外:
EVERE: Servlet.service() for servlet [com.semanticrecommender.web.Main] in context with path [/SemanticRecommender] threw exception
HttpException: 400
at org.apache.jena.sparql.engine.http.HttpQuery.rewrap(HttpQuery.java:411)
at org.apache.jena.sparql.engine.http.HttpQuery.execPost(HttpQuery.java:399)
at org.apache.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:291)
at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execResultSetInner(QueryEngineHTTP.java:359)
at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:351)
虽然我打印了jena从文件加载并将其复制到fuseki的查询,但它在fuseki上完美运行
这是我加载查询的方式(但我确定这与实际问题无关)
InputStream testIn = getClass().getResourceAsStream("/recommend.rq");
String queryTemplate = IOUtils.toString(testIn);
System.out.println(queryTemplate);
QueryExecution x = QueryExecutionFactory.sparqlService(
"http://localhost:3030/rs/query", queryTemplate);
ResultSet results = x.execSelect();
ResultSetFormatter.out(System.out, results);
此代码工作
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix no: <http://www.newontology.org/no#>
prefix rs: <http://semanticrecommender.com/rs#>
prefix mo: <http://music.org/musicontology/mo#>
prefix : <http://www.MusicSemanticOntology.com/mso#>
select *
where
{
values ?user { :ania }
#the variable ?x is bound to the items the user :ania has liked.
?user rs:hasRated ?ratings.
?ratings a rs:Likes.
?ratings rs:about ?x.
?ratings rs:ratesBy ?ratingValue.
?ratings rs:createdOn ?ratingDate.
#level 0 class similarities
{
#extract all the items that are from the same class (type) as the liked items.
#I assumed the being from the same class accounts for 50% of the similarities.
#This value can be changed according to the test or the application domain.
values ?classImportance {0.5} #class level
?x a ?class .
?item a ?class .
?class rs:hasSimilarityValue ?similarity .
bind (?classImportance as ?importance)
bind( 4/7 as ?levelImportance)
bind (concat("it shares the same class, which is ", str(?class), " with ", str(?x)) as ?becauseOf)
}
}
除了group by之外,这两个查询是相同的,一个with group by只能在Fuseki界面上工作,而不是eclipse java,但另一个同时适用于
问题发生在这两行
(group_concat(distinct ?x) as ?commingFromLikingThisInstance)
(group_concat(?becauseOf ; separator = " ,and ") as ?reason)
当我删除它们时,一切正常,但是当我把它们放入时我得到了那个错误
日志是:
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Error 400: Parse error: [\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX : <http://www.MusicSemanticOntology.com/mso#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX rs: <http://semanticrecommender.com/rs#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "SELECT ?item (SUM(( ( ( ?similarity * ?importance ) * ?levelImportance ) * ?ratingValue )) AS ?summedSimilarity) (GROUP_CONCAT DISTINCT (?x) AS ?commingFromLikingThisInstance) (GROUP_CONCAT (?becauseOf ; separator=' ,and ') AS ?reason)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "WHERE[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " { VALUES ?user { :ania }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " ?user rs:hasRated ?ratings .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " ?ratings rdf:type rs:Likes ;[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " rs:about ?x ;[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " rs:ratesBy ?ratingValue[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " { VALUES ?classImportance { 0.5 }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " BIND(?classImportance AS ?importance)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " BIND(( 4 / 7 ) AS ?levelImportance)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " ?x rdf:type ?class .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " ?item rdf:type ?class .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " ?class rs:hasSimilarityValue ?similarity[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " BIND(concat("it shares the same class, which is ", str(?class), " with ", str(?x)) AS ?becauseOf)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "GROUP BY ?item[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "ORDER BY DESC(?summedSimilarity)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\r]Encountered " "distinct" "DISTINCT "" at line 6, column 129.[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Was expecting:[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " "(" ...[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << " [\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Fuseki - version 2.3.1 (Build date: 2015-12-08T09:24:07+0000)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager:274 - Connection [id: 0][route: {}->http://localhost:3030] can be kept alive indefinitely
2016-03-28 11:17:50 DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager:281 - Connection released: [id: 0][route: {}->http://localhost:3030][total kept alive: 1; route allocated: 1 of 5; total allocated: 1 of 10]
现在我发现了真正的问题,它是单词DISTINCT
,当我删除它时,一切正常,当我把它放回去时,它只是从fuseki接口工作而不是来自jena java :( help伙计们
答案 0 :(得分:2)
我认为如果可以的话,你可能希望Jena避免本地解析,并在这种情况下直接将查询发送到远程端点。 answer.semanticweb.com上的jena throws QueryParsingException on correct but non-standard SPARQL问题描述了这种方法。我们的想法是使用查询字符串创建 QueryEngineHTTP 。
至于为什么您收到此错误,我认为这可能是Jena结束时的错误。我有一些证据和一些假设。调查一下,并与sparql.org's query validator(由耶拿支持)玩,有一些奇怪的事情发生。如果输入查询
select (group_concat(distinct ?x) as ?y) (sum(distinct ?x) as ?z) {}
进入解析器后,格式化的解析查询显示为:
SELECT (GROUP_CONCAT DISTINCT (?x) AS ?y) (SUM(DISTINCT ?x) AS ?z) WHERE {}
不合法。 (注意关闭的不同位置 GROUP_CONCAT。另请注意,它出现在 group_concat 中,但与 sum 无关。)
当使用Jena将查询发送到远程端点时,如果Jena是第一个 解析输入查询,然后将重新格式化的查询发送到 远程端点,它可以解释调试日志消息和解析错误,但是 我不确定这是否是如何实施的。