使用CIDOC-CRM本体查询有关SPARQL的RDF文件

时间:2018-01-24 16:24:58

标签: python-2.7 sparql rdflib rdf-xml

我是Apache Spark,SPARQL的初学者,我试图使用CIDOC-CRM本体对RDF文件进行一些查询,我执行此SPARQL查询但它永远不会转向结果

from rdflib import Graph
g = Graph()
g.parse("data.rdf")

res = g.query(
   """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      PREFIX gml: <http://www.opengis.net/gml>
      PREFIX archaeo: <http://www.ics.forth.gr/isl/CRMextCRMarchaeo.rdfs/>
      PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm>
      PREFIX sci: <http://www.ics.forth.gr/isl/CRMext/CRMsci.rdfs>
      PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
      PREFIX crmdig: <http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>


      SELECT *
      WHERE { [ archaeo:A1_Excavation_Process_Unit [ crm:P01i_is_domain_of 
              [ crm:PC14_carried_out_by [ crm:P02_has_range
              [ crm:E21_Person [ crm:P1_is_identified_by 
              [ crm:E82_Actor_Appellation 
              [ rdfs:label ?label ] ] ] ] ] ] ] ] }
      LIMIT 20 """ )

来自data.rdf的样本如下

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:gml="http://www.opengis.net/gml"
xmlns:archaeo="http://www.ics.forth.gr/isl/CRMext/CRMarchaeo.rdfs/"
xmlns:crm="http://www.cidoc-crm.org/cidoc-crm/"
xmlns:sci="http://www.ics.forth.gr/isl/CRMext/CRMsci.rdfs/"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
xmlns:crmdig="http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
      <archaeo:A1_Excavation_Process_Unit 
      rdf:about="http://archaeositarproject.it  
      /st_information_source_#4104">
      <crm:P1_is_identified_by>
      <crm:E41_Appellation>
      <rdfs:label>ninfeo e biblioteca di via Lanza</rdfs:label>
      </crm:E41_Appellation>
      </crm:P1_is_identified_by>
      <crm:P3_has_note xml:lang="ita">Breve nota sul rinvenimento di alcune strutture sulla carreggiata di via dello Statuto, fatta da R. A. Lanciani al momento della scoperta, sul Bollettino Comunale</crm:P3_has_note>
      <crm:P01i_is_domain_of>
      <crm:PC14_carried_out_by>
      <crm:P02_has_range>
        <crm:E40_Legal_Body rdf:about="http://archaeositarproject.it/st_person_#382_106164915">
          <crm:P1_is_identified_by>
            <crm:E82_Actor_Appellation>
              <rdfs:label>SSBAR</rdfs:label>
            </crm:E82_Actor_Appellation>
          </crm:P1_is_identified_by>
        </crm:E40_Legal_Body>
      </crm:P02_has_range>
      <crm:P14.1_in_the_role_of>
        <crm:E55_Type>
          <rdfs:label>owner</rdfs:label>
        </crm:E55_Type>
      </crm:P14.1_in_the_role_of>
      </crm:PC14_carried_out_by>
      </crm:P01i_is_domain_of>
      <crm:P4_has_time-span>
      <crm:E52_Time-Span>
      <crm:P82b_end_of_the_end rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"
      >1884-12-30T23:00:00Z</crm:P82b_end_of_the_end>
      <crm:P81b_begin_of_the_end rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"
      >1884-12-30T23:00:00Z</crm:P81b_begin_of_the_end>
      <crm:P81a_end_of_the_begin rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"
      >1883-12-31T23:00:00Z</crm:P81a_end_of_the_begin>
      <crm:P82a_begin_of_the_begin rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"
      >1883-12-31T23:00:00Z</crm:P82a_begin_of_the_begin>
      </crm:E52_Time-Span>
      </crm:P4_has_time-span>
      <crm:P2_has_type>
      <crm:E55_Type rdf:about="http://archaeositarproject.it/st_acquisition_methodology_#2">
      <skos:broadMatch>
        <skos:Concept rdf:about="http://vocab.getty.edu/aat/300053702"/>
      </skos:broadMatch>
      <rdfs:label xml:lang="eng">tranch or sample excavation</rdfs:label>
      <rdfs:label xml:lang="ita">Saggio di scavo</rdfs:label>
      </crm:E55_Type>
      </crm:P2_has_type>
      <crm:P70i_is_documented_in>
      <crm:E31_Document rdf:about="http://archaeositarproject.it/st_information_source_document_#33319">
      <crm:P104_is_subject_to>
        <crm:E30_Right>
          <crm:P1_is_identified_by>
            <crm:E41_Appellation rdf:about="http://archaeositarproject.it/st_license_#1">
              <rdfs:label>Licenza da definire</rdfs:label>
            </crm:E41_Appellation>
          </crm:P1_is_identified_by>
        </crm:E30_Right>
      </crm:P104_is_subject_to>
      <crm:P2_has_type>
        <crm:E55_Type rdf:about="http://archaeositarproject.it/st_information_source_document_type_#4">
          <skos:broadMatch>
            <skos:Concept rdf:about="http://vocab.getty.edu/aat/300191086"/>
          </skos:broadMatch>
          <skos:broadMatch>
            <skos:Concept rdf:about="http://vocab.getty.edu/aat/300034017"/>
          </skos:broadMatch>
          <rdfs:label xml:lang="eng">accompanying drawing</rdfs:label>
          <rdfs:label xml:lang="ita">Elaborati descrittivi</rdfs:label>
        </crm:E55_Type>
      </crm:P2_has_type>
      <crm:P3_has_note>estratto del test</crm:P3_has_note>
      <crm:P102_has_title>
        <crm:E35_Title>
          <rdfs:label xml:lang="ita">relazione</rdfs:label>
        </crm:E35_Title>
      </crm:P102_has_title>
      </crm:E31_Document>
      </crm:P70i_is_documented_in>
      <crm:P48_has_preferred_identifier>
      <crm:E42_Identifier>
      <crm:P2_has_type>
        <crm:E55_Type>
          <rdfs:label>internal code</rdfs:label>
        </crm:E55_Type>
      </crm:P2_has_type>
      <rdfs:label>5518</rdfs:label>
      </crm:E42_Identifier>
      </crm:P48_has_preferred_identifier>
      <crm:P1_is_identified_by>
      <crm:E42_Identifier>
      <crm:P2_has_type>
        <crm:E55_Type>
          <rdfs:label>internal identifier</rdfs:label>
        </crm:E55_Type>
      </crm:P2_has_type>
      <rdfs:label>4104_2</rdfs:label>
      </crm:E42_Identifier>
      </crm:P1_is_identified_by>
      <crm:P7_took_place_at>
      <crm:E53_Place>
      <crm:P1_is_identified_by>
        <crm:E44_Place_Appellation>
          <rdfs:label>Rione XV Esquilino</rdfs:label>
        </crm:E44_Place_Appellation>
      </crm:P1_is_identified_by>
      </crm:E53_Place>
      </crm:P7_took_place_at>
      <crm:P01i_is_domain_of>
      <crm:PC14_carried_out_by>
      <crm:P02_has_range>
        <crm:E21_Person rdf:about="http://archaeositarproject.it/st_person_#12_1788137750">
          <crm:P1_is_identified_by>
            <crm:E82_Actor_Appellation>
              <rdfs:label>Mirella Serlorenzi</rdfs:label>
            </crm:E82_Actor_Appellation>
          </crm:P1_is_identified_by>
        </crm:E21_Person>
      </crm:P02_has_range>
      <crm:P14.1_in_the_role_of>
        <crm:E55_Type>
          <rdfs:label>liable officier</rdfs:label>
        </crm:E55_Type>
      </crm:P14.1_in_the_role_of>
      </crm:PC14_carried_out_by>
      </crm:P01i_is_domain_of>
      </archaeo:A1_Excavation_Process_Unit>
      </rdf:RDF>

例如,我想获得标签&#34; Mirella Serlorenzi&#34;以及所有其他人,尊重查询中的条件

   <crm:P02_has_range>
    <crm:E21_Person rdf:about="http://archaeositarproject.it/st_person_#12_1788137750">
      <crm:P1_is_identified_by>
        <crm:E82_Actor_Appellation>
          <rdfs:label>Mirella Serlorenzi</rdfs:label>
        </crm:E82_Actor_Appellation>
      </crm:P1_is_identified_by>
    </crm:E21_Person>
  </crm:P02_has_range>

我希望你有一些帮助,或者有些书要看看,提前谢谢

1 个答案:

答案 0 :(得分:0)

无论如何,谢谢你们,我找到了解决问题的方法,

这是我想要的查询示例:

g.parse("pyrdfLib/sitar_rdf.rdf")

g.bind(a,b):它用于绑定PREFIX:

g.bind("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
g.bind("gml","http://www.opengis.net/gml")
g.bind("archaeo","http://www.ics.forth.gr/isl/CRMext/CRMarchaeo.rdfs/")
g.bind("crm","http://www.cidoc-crm.org/cidoc-crm/")
g.bind("sci","http://www.ics.forth.gr/isl/CRMext/CRMsci.rdfs/")
g.bind("skos","http://www.w3.org/2004/02/skos/core#")
g.bind("crmdig","http://www.ics.forth.gr/isl/CRMext/CRMdig.rdfs/")
g.bind("rdfs","http://www.w3.org/2000/01/rdf-schema#")


res = g.query("""
SELECT ?arc1 ?mMade
WHERE {
    ?s rdf:type sci:S22_Segment_of_Matter .
    ?s crm:P140i_was_attributed_by ?arc1 .
    ?arc1 rdf:type archaeo:A1_Excavation_Process_Unit .
    ?s sci:O22_partly_or_completely_contains ?archaeo .
    ?archaeo rdf:type archaeo:A8_Stratigraphic_Unit .
    ?archaeo archaeo:AP15_is_or_contains_remains_of ?mMade .

} """ )