gpt4 book ai didi

java - SPARQL 查询适用于 Fuseki 接口(interface),但适用于耶拿

转载 作者:行者123 更新时间:2023-11-30 03:00:29 25 4
gpt4 key购买 nike

这是我的查询

    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接口(interface)中它就可以工作,但是如果我把它放在一个文件中并从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 之外,这两个查询是相同的,带有 group by 的查询仅适用于 Fuseki 界面,不适用于 eclipse java,而另一个查询则适用于两者

更新3

问题出现在这两行

(group_concat(distinct ?x) as ?commingFromLikingThisInstance)
(group_concat(?becauseOf ; separator = " ,and ") as ?reason)

当我删除它们时,一切正常,但是当我放置它们时,我收到了错误

更新 4

日志是:

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]

更新5

现在我发现了真正的问题,它是单词DISTINCT,当我删除它时,一切正常,当我把它放回去时,它只能从fuseki界面工作,而不是从jena java工作:(请帮助大家

最佳答案

我认为您可能希望 Jena 避免本地解析(如果可以的话),在这种情况下,并将查询直接发送到远程端点。 jena throws QueryParsingException on correct but non-standard SPARQL 中描述了该方法。关于answers.semanticweb.com 的问题。这个想法是使用查询字符串创建一个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 {}

这是合法的。 (请注意,distinct 与GROUP_CONCAT。另请注意,它发生在 group_concat 中,但不会发生在 sum 中。)

当使用 Jena 将查询发送到远程端点时,如果 Jena 首先解析输入查询,然后将重新格式化的查询发送到远程端点,这将解释调试日志消息和解析错误,但是我不确定事情是否是这样实现的。

关于java - SPARQL 查询适用于 Fuseki 接口(interface),但适用于耶拿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36137483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com