SPARQL的正则表达式

swa*_*il7 1 java rdf sparql apache-jena

我从dbpedia下载了dbpedia_quotationsbook.zip,其中包含dbpedia_quotationsbook.nt triplestore.

在这个三元店

subject是authorname
谓词是"sameas"
对象是authorcode

我使用JENA尝试了这个查询三元组,简单的查询正在运行.

现在我想要所有的authorcode,其authorname与给定的字符串部分匹配.所以我尝试了以下查询

select ?code
where
{

FILTER regex(?name, "^Rob")  <http://www.w3.org/2002/07/owl#sameAs> ?code.

}
Run Code Online (Sandbox Code Playgroud)

上面的查询应返回其authorname包含"Rob"的所有authorcodes

我得到以下异常

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "." ". "" at line 5, column 74.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <BLANK_NODE_LABEL> ...
    <VAR1> ...
    <VAR2> ...
    "true" ...
    "false" ...
    <INTEGER> ...
    <DECIMAL> ...
    <DOUBLE> ...
    <INTEGER_POSITIVE> ...
    <DECIMAL_POSITIVE> ...
    <DOUBLE_POSITIVE> ...
    <INTEGER_NEGATIVE> ...
    <DECIMAL_NEGATIVE> ...
    <DOUBLE_NEGATIVE> ...
    <STRING_LITERAL1> ...
    <STRING_LITERAL2> ...
    <STRING_LITERAL_LONG1> ...
    <STRING_LITERAL_LONG2> ...
    "(" ...
    <NIL> ...
    "[" ...
    <ANON> ...

    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
    at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
    at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:34)
    at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:148)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:80)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:53)
    at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:41)
    at rdfcreate.NewClass.query(NewClass.java:55)
    at rdfcreate.NewClass.main(NewClass.java:97)
Run Code Online (Sandbox Code Playgroud)

耶拿密码

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.util.FileManager;

/**
 *
 * @author swapnil
 */
public class NewClass {
    String read()
        {
               final String tdbDirectory = "C:\\TDBLoadGeoCoordinatesAndLabels"; 


    String dataPath = "F:\\Swapnil Drive\\Project Stuff\\Project data 2015 16\\Freelancer\\SPARQL\\dbpedia_quotationsbook.nt";


     Model tdbModel = TDBFactory.createModel(tdbDirectory);
             /*Incrementally read data to the Model, once per run , RAM > 6 GB*/

             FileManager.get().readModel( tdbModel, dataPath, "N-TRIPLES");

             tdbModel.close();

             return tdbDirectory;
        }

       void query(String tdbDirectory, String query1)
       {


    Dataset dataset = TDBFactory.createDataset(tdbDirectory);
              Model tdb = dataset.getDefaultModel();
              Query query = QueryFactory.create(query1);
              QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
              /*Execute the Query*/
                ResultSet results = qexec.execSelect();
                System.out.println(results.getRowNumber());
                while (results.hasNext()) {
                      // Do something important
                    QuerySolution qs =   results.nextSolution();
              qs.toString();
                    System.out.println("sol "+qs);
                }
                   qexec.close();
               tdb.close() ;
       }

     public static void main(String[] args) {

            NewClass nc = new NewClass();
         String tdbd=    nc.read();
          nc.query(tdbd, "select ?code\n" +
    "WHERE\n" +
    "{\n" +
    "<http://dbpedia.org/resource/Robert_H._Schuller> <http://www.w3.org/2002/07/owl#sameAs> ?code.\n" +
    "}");


       }

}

}
Run Code Online (Sandbox Code Playgroud)

结果

sol(?code = http://quotationsbook.com/author/6523)

上面的查询给出了给定作者的代码.

请帮帮我

Tom*_*icz 5

你不能混合模式和过滤器.您必须先?name使用三重模式绑定(即选择),然后过滤结果.Jena基本上抱怨,因为您的SPARQL语法无效.

现在,您可以运行下面的查询,但您的数据仅包含dbpedia URI和quotationsbook URI之间的映射.

PREFIX owl: <http://www.w3.org/2002/07/owl#>    
select ?code
where
{
   ?author <name> ?name .
   ?author owl:sameAs ?code .

   FILTER regex(?name, "^Rob")
}
Run Code Online (Sandbox Code Playgroud)

以上的意思

  1. 获取作者的姓名
  2. 获取作者代码
  3. 仅包括名称与正则表达式匹配的作者
  4. 选择他们的代码

同样,这只适用于本地可用的数据.问题是你没有实际的名字.当然,您可以将查询更改为正则表达式整个dbpedia标识符,但这并不完美.

FILTER regex(?author, "Rob")
Run Code Online (Sandbox Code Playgroud)

您可以做什么,因为dbpedia资源是可解除引用的,将GRAPH模式中的名称三元模式包装起来

PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

select ?author ?code
where
{
   GRAPH  <file://path/to/dbpedia_quotationsbook.nt>
   {
      ?author owl:sameAs ?code .
   }

   GRAPH ?author
   {
      ?author <http://www.w3.org/2000/01/rdf-schema#label> ?name .
      FILTER regex(?name, "^Rob")
   }
}
Run Code Online (Sandbox Code Playgroud)

这是正在发生的事情

  1. 从导入文件中获取?authors和?codes(SPARQL GUI导入到图形中)
  2. 治疗?author为图名,以便它可以从网上下载
  3. 获取?author?name小号
  4. 过滤器?nameRob开头

根据您的SPARQL处理器(我使用dotNetRDF工具包中的SPARQL GUI),有两个重要的部分可以使其工作.

这是我得到的结果的屏幕截图.请注意dbpedia请求的突出显示设置和Fiddler日志.

SPARQL GUI中的联合查询

最重要的是我刚刚给你一个联邦SPARQL查询的例子.