长时间SELECT查询的SPARQLWrapper QueryBadFormed错误

时间:2018-10-13 17:49:49

标签: python rdflib graphdb sparqlwrapper

在SELECT查询中可以使用的查询大小是否受到限制?我有一个很长的SELECT查询(在下面发布),该查询不断抛出QueryBadFormed错误。我已经在sparql.org上验证了查询,并且在三元组商店中运行了查询。我正在使用GraphDB 8.6 SE。查询运行正常。

代码:

from SPARQLWrapper import SPARQLWrapper, SPARQLWrapper2, JSON,  CSV, TSV

# set endpoint and query
endpoint = r"http://localhost:7200/repositories/EDR"
query = get_dental_procedures_query()  # return query below

# get results from endpoint
sparql = SPARQLWrapper(endpoint)
setReturnFormat(JSON)  # I've also tried CSV and TSV
sparql.setQuery(query)
results = sparql.query().convert()

返回错误:

  

SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed.

以下是查询:

BASE    <http://purl.regenstrief.org/NDPBRN/dental-practice/>
PREFIX  mesial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Mesial_surface_enamel_of_tooth>
PREFIX  exception: <http://purl.obolibrary.org/obo/OHD_0000404>
PREFIX  ada_num: <http://purl.obolibrary.org/obo/OHD_0000065>
PREFIX  occurrence_date: <http://purl.obolibrary.org/obo/OHD_0000015>
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  resin_filling_proc: <http://purl.obolibrary.org/obo/OHD_0000042>
PREFIX  birth_date: <http://purl.obolibrary.org/obo/OHD_0000050>
PREFIX  restored_buccal: <http://purl.obolibrary.org/obo/OHD_0000222>
PREFIX  caries_finding: <http://purl.obolibrary.org/obo/OHD_0000024>
PREFIX  dental_finding: <http://purl.obolibrary.org/obo/OHD_0000010>
PREFIX  molar: <http://purl.obolibrary.org/obo/FMA_55638>
PREFIX  male_gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000007>
PREFIX  endodontically_restored_tooth: <http://purl.obolibrary.org/obo/0000236>
PREFIX  root_canal_treatment: <http://purl.obolibrary.org/obo/OHD_0000230>
PREFIX  has_part: <http://purl.obolibrary.org/obo/BFO_0000051>
PREFIX  gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000007>
PREFIX  part_of: <http://purl.obolibrary.org/obo/BFO_0000050>
PREFIX  inheres_in: <http://purl.obolibrary.org/obo/BFO_0000052>
PREFIX  missing_tooth_finding: <http://purl.obolibrary.org/obo/OHD_0000026>
PREFIX  pbrn_id: <http://purl.obolibrary.org/obo/OHD_0000273>
PREFIX  distal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Distal_surface_enamel_of_tooth>
PREFIX  has_output: <http://purl.obolibrary.org/obo/OBI_0000299>
PREFIX  occlusal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Occlusal_surface_enamel_of_tooth>
PREFIX  incisor: <http://purl.obolibrary.org/obo/FMA_12823>
PREFIX  graph: <http://purl.regenstrief.org/NDPBRN/dental-practice#>
PREFIX  patient_role: <http://purl.obolibrary.org/obo/OHD_0000190>
PREFIX  anterior_tooth: <http://purl.obolibrary.org/obo/OHD_0000307>
PREFIX  resin: <http://purl.obolibrary.org/obo/OHD_0000036>
PREFIX  restored_lingual: <http://purl.obolibrary.org/obo/OHD_0000226>
PREFIX  dental_proc: <http://purl.obolibrary.org/obo/OHD_0000002>
PREFIX  restored_surface: <http://purl.obolibrary.org/obo/OHD_0000208>
PREFIX  extracoronally_restored_tooth: <http://purl.obolibrary.org/obo/0000238>
PREFIX  lingual_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Lingual_surface_enamel_of_tooth>
PREFIX  dentition: <http://purl.obolibrary.org/obo/FMA_75152>
PREFIX  sesame: <http://www.openrdf.org/schema/sesame#>
PREFIX  lesion: <http://purl.obolibrary.org/obo/OHD_0000021>
PREFIX  labial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Labial_surface_enamel_of_tooth>
PREFIX  has_input: <http://purl.obolibrary.org/obo/OBI_0000293>
PREFIX  posterior_tooth: <http://purl.obolibrary.org/obo/OHD_0000308>
PREFIX  extraction_proc: <http://purl.obolibrary.org/obo/OHD_0000057>
PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  restored_occlusal: <http://purl.obolibrary.org/obo/OHD_0000228>
PREFIX  is_about: <http://purl.obolibrary.org/obo/IAO_0000136>
PREFIX  restored_labial: <http://purl.obolibrary.org/obo/OHD_0000225>
PREFIX  coronally_restored_tooth: <http://purl.obolibrary.org/obo/0000237>
PREFIX  patient: <http://purl.obolibrary.org/obo/OHD_0000012>
PREFIX  prop: <http://purl.regenstrief.org/NDPBRN/property/>
PREFIX  restoration_proc: <http://purl.obolibrary.org/obo/OHD_0000004>
PREFIX  last_visit_date: <http://purl.obolibrary.org/obo/OHD_0000219>
PREFIX  tooth: <http://purl.obolibrary.org/obo/FMA_12516>
PREFIX  intracoronally_restored_tooth: <http://purl.obolibrary.org/obo/0000239>
PREFIX  bearer_of: <http://purl.obolibrary.org/obo/BFO_0000053>
PREFIX  first_visit_date: <http://purl.obolibrary.org/obo/OHD_0000218>
PREFIX  surgically_modified_tooth: <http://purl.obolibrary.org/obo/0000231>
PREFIX  canine: <http://purl.obolibrary.org/obo/FMA_55636>
PREFIX  facial_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Facial_surface_enamel_of_tooth>
PREFIX  restored_distal: <http://purl.obolibrary.org/obo/OHD_0000223>
PREFIX  premolar: <http://purl.obolibrary.org/obo/FMA_55637>
PREFIX  restored_tooth: <http://purl.obolibrary.org/obo/OHD_0000189>
PREFIX  restored_facial: <http://purl.obolibrary.org/obo/OHD_0000235>
PREFIX  material: <http://purl.obolibrary.org/obo/OHD_0000000>
PREFIX  missing_tooth_num: <http://purl.obolibrary.org/obo/OHD_0000234>
PREFIX  buccal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Buccal_surface_enamel_of_tooth>
PREFIX  realizes: <http://purl.obolibrary.org/obo/BFO_0000055>
PREFIX  female_gender_role: <http://purl.obolibrary.org/obo/OMRSE_00000008>
PREFIX  restored_mesial: <http://purl.obolibrary.org/obo/OHD_0000227>
PREFIX  restored_incisal: <http://purl.obolibrary.org/obo/OHD_0000224>
PREFIX  visit: <http://purl.obolibrary.org/obo/OHD_0000009>
PREFIX  obo:  <http://purl.obolibrary.org/obo/>
PREFIX  incisal_surface: <http://purl.obolibrary.org/obo/FMA_no_fmaid_Incisal_surface_enamel_of_tooth>

SELECT DISTINCT  ?practice ?patient_id ?gender ?dob ?first_visit ?last_visit ?tooth_id ?tooth_num ?first_PCR ?first_RCT ?event_name ?ada_code ?event_date ?extract_date ?missing_date (if(bound(?surface_m), 1, 0) AS ?m) (if(bound(?surface_o), 1, 0) AS ?o) (if(bound(?surface_d), 1, 0) AS ?d) (if(bound(?surface_b), 1, 0) AS ?b) (if(bound(?surface_l), 1, 0) AS ?l) (if(bound(?surface_f), 1, 0) AS ?f) (if(bound(?surface_incisal), 1, 0) AS ?i)
WHERE
  { ?patient_i  a                   patient: ;
          birth_date:           ?dob ;
          pbrn_id:              ?pbrn_id
    OPTIONAL
      { ?gender_t  rdfs:subClassOf   male_gender_role: ;
                rdfs:label         ?gender_name .
        ?gender_i  sesame:directType  ?gender_t ;
                inheres_in:        ?patient_i
      }
  ?patient_i  first_visit_date:  ?first_visit ;
          last_visit_date:   ?last_visit .
  ?tooth_t  rdfs:subClassOf    tooth: ;
          ada_num:           ?ada_num .
  ?tooth_i  sesame:directType  ?tooth_t
  OPTIONAL
    { ?tooth_i  prop:first_PCR_date  ?first_PCR }
  OPTIONAL
    { ?tooth_i  prop:first_RCT_date  ?first_RCT }
  OPTIONAL
    { ?tooth_i  prop:extraction_date  ?extract_date }
  OPTIONAL
      { ?tooth_i  prop:missing_tooth_finding_date  ?missing_date }
  ?event_t  rdfs:subClassOf    dental_proc: ;
            rdfs:label         ?event_name .
  ?event_i  sesame:directType  ?event_t ;
            has_input:         ?patient_i ;
            has_output:        ?tooth_i ;
            occurrence_date:   ?event_date ;
            prop:ada_code      ?ada_code
  OPTIONAL
    { ?event_i  has_output:        ?surface_m .
      ?surface_m  sesame:directType  restored_mesial: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_o .
      ?surface_o  sesame:directType  restored_occlusal: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_d .
    ?surface_d  sesame:directType  restored_distal: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_b .
      ?surface_b  sesame:directType  restored_buccal: ;
              part_of:           ?tooth_i
    }
OPTIONAL
  { ?event_i  has_output:        ?surface_l .
    ?surface_l  sesame:directType  restored_lingual: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_f .
      ?surface_f  sesame:directType  restored_facial: ;
              part_of:           ?tooth_i
    }
  OPTIONAL
    { ?event_i  has_output:        ?surface_incisal .
      ?surface_incisal
              sesame:directType  restored_incisal: ;
              part_of:           ?tooth_i
    }
  BIND(strafter(str(?tooth_i), "tooth/") AS ?tooth_id)
  BIND(strafter(str(?patient_i), "patient/") AS ?patient_id)
  BIND(strbefore(str(?gender_name), " ") AS ?gender)
  BIND(strafter(str(?ada_num), "Tooth ") AS ?tooth_num)
  BIND(strafter(str(?pbrn_id), "NDPBRN practice ") AS ?practice)
}
limit 5

1 个答案:

答案 0 :(得分:2)

对长时间查询使用POST HTTP方法:

sparql.setMethod('POST')

更多信息:https://www.w3.org/TR/sparql11-protocol/#query-operation


对于您而言,限制似乎与urllib2有关。似乎上述方法也不适用于SPARQLWrapper2()