如何使用sparql从cidoc crm rdf文件返回URL / author的详细信息

时间:2014-08-19 03:58:58

标签: java eclipse rdf sparql jena

感谢您的回答,但不幸的是我没有在控制台中得到任何结果,在这里我附上我的代码,请指导我在哪里犯了错误。

 FileManager.get().addLocatorClassLoader(test.class.getClassLoader());
          Model model=FileManager.get().loadModel("H:/EclipseWorkplace/MuseumDatabaseRecommendation/src/data3.rdf");
String spr="prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+ 
        "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
        "prefix crm:  <urn:x-stackoverflow:example#>\n"+
        "\n"+
        "SELECT * WHERE{\n"+
        "   [ crm:E21_Person/rdfs:label ?creator\n"+
        "   ; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]\n"+
        "                               ; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]\n"+
        "                               ]\n"+
        "   ; crm:P3_has_note [ a crm:P102_has_title\n"+
        "                     ; rdfs:label ?title\n"+
        "                     ]\n"+
        "   ]\n"+
        "  FILTER( ?creator = \"Brett WHITELEY\" ).\n"+
        "}";

Query query = QueryFactory.create(spr); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.create(  query,model );
//QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
ResultSet results = qExe.execSelect();
ResultSetFormatter.out(System.out, results, query);

我当前的输出是

-----------------------------------------
| creator | material | timespan | title |
=========================================
-----------------------------------------

感谢您的时间。

1 个答案:

答案 0 :(得分:4)

输入数据

为了便于阅读,我将您的模型重新构建为TURTLE语法,供其他人阅读。

@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix crm:   <http://www.cidoc-crm.org/cidoc-crm/> .

<http://phdprototype.tk/collectionimage   /4D0BFF17-5810-4644-A550-D35EE090D4A8.png>
        a                          "Painting" ;
        rdfs:label                 "Brett WHITELEY" ;
        crm:E21_Person             [ a           <E39_Actor> ;
                                     rdfs:label  "Brett WHITELEY"
                                   ] ;
        crm:E62_String             "Painting\n" ;
        crm:P108i_was_produced_by  [ a                     crm:E12_Production ;
                                     crm:P126_employed     [ a           crm:E57_Material ;
                                                             rdfs:label  "Oil"
                                                           ] ;
                                     crm:P4_has_time-span  [ a                            crm:E52_Time-Span ;
                                                             crm:P82_at_some_time_within  "\n      1976\n    "
                                                           ]
                                   ] ;
        crm:P3_has_note            [ a           crm:P102_has_title ;
                                     rdfs:label  "Interior with Time Past"
                                   ] ;
        crm:P7_took_place_at       [ a                          crm:E53_Place ;
                                     crm:E44_Place_Appellation  "    \n    5D\n    "
                                   ] ;
        crm:P91_has_unit           [ a           crm:E58_Measurement_Unit ;
                                     rdfs:label  "182.0 h * 200.0 w cm"
                                   ] .

此数据存在一些问题。例如,您的绘画有rdf:type普通文字"painting",这非常糟糕。这由a "painting"属性 - 对象对表示。我们可以对此数据执行查询,但您可能希望重新构建本体,以便它正确使用rdf:type这样的术语。

<强>查询

对于提供的示例数据,以下查询将提取您要查找的内容:

prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix crm:  <http://www.cidoc-crm.org/cidoc-crm/>

SELECT * WHERE{
   [ crm:E21_Person/rdfs:label ?creator
   ; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]
                               ; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]
                               ]
   ; crm:P3_has_note [ a crm:P102_has_title
                     ; rdfs:label ?title
                     ]
   ]
  FILTER( ?creator = "Brett WHITELEY" ).
}

将导致:

----------------------------------------------------------------------------------
| creator          | material | timespan             | title                     |
==================================================================================
| "Brett WHITELEY" | "Oil"    | "\n      1976\n    " | "Interior with Time Past" |
----------------------------------------------------------------------------------

请注意,这是根据您的特定样本数据量身定制的。您需要调整此查询以匹配实际允许的结构。对于(假设的)示例,TimeSpan实例有时可能具有crm:P82_at_some_time_span属性,或者可能有一些crm:exampleProperty将其与另一个值相关联。在这种情况下,您需要修改查询以使用属性路径来匹配它。

示例代码

public static final String queryString = "prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"+ 
        "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"+
        "prefix crm:  <http://www.cidoc-crm.org/cidoc-crm/>\n"+
        "\n"+
        "SELECT * WHERE{\n"+
        "   [ crm:E21_Person/rdfs:label ?creator\n"+
        "   ; crm:P108i_was_produced_by [ crm:P126_employed [ rdfs:label ?material ]\n"+
        "                               ; crm:P4_has_time-span [ crm:P82_at_some_time_within ?timespan ]\n"+
        "                               ]\n"+
        "   ; crm:P3_has_note [ a crm:P102_has_title\n"+
        "                     ; rdfs:label ?title\n"+
        "                     ]\n"+
        "   ]\n"+
        "  FILTER( ?creator = \"Brett WHITELEY\" ).\n"+
        "}";
public static final Query query = QueryFactory.create(queryString);

@Test
public void test() throws Exception {

    final Model model = ModelFactory.createDefaultModel();
    try( final InputStream in = this.getClass().getResourceAsStream("/so.rdf") ) {
        model.read(in, null, "RDF/XML");
    }
    model.write(System.out, "TTL");
    System.out.println("=================================================================");
    System.out.println(queryString);
    System.out.println("=================================================================");
    try( final QueryExecution exec = QueryExecutionFactory.create(query, model) ) {
        ResultSetFormatter.out(System.out, exec.execSelect(), query);
    }

}