Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用strstarts过滤器在Java代码中编写查询SPARQL_Java_Sparql_Jena_Semantic Web_Dbpedia - Fatal编程技术网

使用strstarts过滤器在Java代码中编写查询SPARQL

使用strstarts过滤器在Java代码中编写查询SPARQL,java,sparql,jena,semantic-web,dbpedia,Java,Sparql,Jena,Semantic Web,Dbpedia,我想使用Jena用Java编写这个SPARQL查询: 前缀dbpediaont: 前缀dbpedia: 前缀rdf: 选择?资源在哪里{ dbpedia:Fred_Guy rdf:type?资源 过滤器strstarts(str(?资源),”http://dbpedia.org/ontology") } 我正在使用以下代码: public QueryExecution query(){ String stringa = "http://dbpedia.org/resource

我想使用Jena用Java编写这个SPARQL查询:

前缀dbpediaont:
前缀dbpedia:
前缀rdf:
选择?资源在哪里{
dbpedia:Fred_Guy rdf:type?资源
过滤器strstarts(str(?资源),”http://dbpedia.org/ontology")
}
我正在使用以下代码:

public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), http://dbpedia.org/ontology)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }
public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), dbpediaont:)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }
strstarts筛选器中的“”上出现错误,因为它必须介于“”之间,对吗?如何用Java编写此代码?如果在“”之间写入“”,则代码中的第一个“将被视为查询的结束”


我做错了什么?

我已使用以下代码解决了问题:

public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), http://dbpedia.org/ontology)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }
public QueryExecution query(){

        String stringa = "http://dbpedia.org/resource/Fred_Guy";

        ParameterizedSparqlString qs = new ParameterizedSparqlString( "" +
                "prefix dbpediaont: <http://dbpedia.org/ontology/>\n" +
                "prefix dbpedia: <http://dbpedia.org/resource/>\n" +
                "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "\n" +  
                "select ?resource where {\n" +
                "?mat rdf:type ?resource\n" +
                "filter strstarts(str(?resource), dbpediaont:)\n" +
                "}" );


        Resource risorsa = ResourceFactory.createResource(stringa);
        qs.setParam( "mat", risorsa );

        //System.out.println( qs );

        QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery() );


        ResultSet results = ResultSetFactory.copyResults( exec.execSelect() );

        while ( results.hasNext() ) {

            System.out.println( results.next().get( "resource" ));
        }

        // A simpler way of printing the results.
        ResultSetFormatter.out( results );

        return exec;

    }
此错误是由于代码需要编写

dbpediaont:

str()

因为它是IRI,而不是字符串:

filter strstarts(str(?resource), str(dbpediaont:))
关于后一个问题的报道如下:


我希望这个答案能帮助一些人。

这是一种方法。另一种方法是
String query=“…\”internal String\“…”
。另一件令人惊讶的事情是,您已经表明您熟悉参数化sparqlstrings,因此您可以完成
筛选strstarts(str(?.resource),?prefix)
setParam(“prefix”,“http://…”);
。如果这解决了您的问题,那么您怎么会有这个问题。如果
strstarts(str(?resource),dbpediaont:)
在这一个中不起作用,很难看出它在这一个中如何起作用。有了这个答案,上面的错误就解决了,链接“”是另一个问题。因为“StackOverflow”"建议我为新问题打开新帖子,然后我打开了一个新帖子。答案解决了这个问题,所以不值得投反对票。你是对的,从某种意义上说,上面的代码不会产生与问题中相同的错误。但它仍然会产生错误,因此如果有人发现问题并尝试,这是毫无帮助的使用此答案,特别是因为答案并不表明代码存在任何其他问题。此问题的可能重复与strstarts、jena、sparql或dbpedia无关。它只是在Java中的字符串中包含双引号,并且非常简单,如
string foo=“John says,\”只需用斜杠将其转义。\“”;