As we write SPARQL in Virtuoso, it is easy to escape a space within an URI by coding the space with %20. However, when I run the same query on Virtuoso isql service, the escape fails. Here is my query:
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdno: <http://www.wikidata.org/prop/novalue/>
PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>
#Items with specific picture
#Note that URL of the filename must be URL encoded.
#added before 2016-10
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P18 <http://commons.wikimedia.org/wiki/Special:FilePath/Flag%20of%20the%20Netherlands.svg>
OPTIONAL{?item rdfs:label ?en . FILTER(LANG(?en) = "en")}
BIND(str(COALESCE(?en,strafter(str(?item),"http://www.wikidata.org/entity/"))) AS ?itemLabel)
}
LIMIT 100
The error says:
*** Error 37000: [Virtuoso Driver][Virtuoso Server]SQ074: Line 22: SP030: SPARQL compiler, line 18: syntax error at '<' before 'http:' at
line 0 of Top-Level:
SPARQL PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdno: <http://www.wikidata.org/prop/novalue/>
PREFIX schema: <http://schema.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>
#Items with specific picture
#Note that URL of the filename must be URL encoded.
#added before 2016-10
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P18 <http://commons.wikimedia.org/wiki/Special:FilePath/Flag of the Netherlands.svg>
OPTIONAL{?item rdfs:label ?en . FILTER(LANG(?en) = "en")}
BIND(str(COALESCE(?en,strafter(str(?item),"http://www.wikidata.org/entity/"))) AS ?itemLabel)
}
LIMIT 100
Isql is not being understood with the %20 because it prints the resource (URI) with white space.
Related
Hello I am trying to find the total number of municipalities a region has along with the name of each region and the total number of municipalities a regional unit has along with the name of the regional unit. A region consists of regional units and a regional unit consists of municipalities. Below is my query that unfortunately returns wrong results. What I am basically trying to do is group by region and get the name and the total municipalities of each region and group by regional unit and take the name and the total municipalities of each unit. Any suggestions to the right direction would be appreciated. Cheers!:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema
PREFIX strdf: <http://strdf.di.uoa.gr/ontology
PREFIX gag: <http://geo.linkedopendata.gr/gag/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?region ?municipality_region ?unit ?municipality_unit
WHERE
{
{ SELECT ?region (COUNT(?municipality) AS ?municipality_region)
{?m rdf:type gag:Δήμος .
?m gag:έχει_επίσημο_όνομα ?municipality .
?m gag:ανήκει_σε ?reg_un .
?reg_un gag:ανήκει_σε ?reg .
?reg gag:έχει_επίσημο_όνομα ?region .
}GROUP BY ?region}
{ SELECT ?unit (COUNT(?municipality_un) AS ?municipality_unit)
{ ?m rdf:type gag:Δήμος .
?m gag:έχει_επίσημο_όνομα ?municipality_un .
?m gag:ανήκει_σε ?reg_un .
?reg_un gag:έχει_επίσημο_όνομα ?unit .
} GROUP BY ?unit}
};
Below I am giving a mapping of properties in english:
Δήμος = municipality
έχει_επίσημο_όνομα = has name
ανήκει_σε = belongs to
And here is the ontology I am working with:
link
I have an "EquivalentTo" definition in Protege of a class EquivClass as (hasObjProp some ClassA) and (has_data_prop exactly 1 rdfs:Literal)
Is there a form of SPARQL query for GraphDB 9.4 to get the "direct" answer to a select query of an equivalent class without having to collect and traverse all the constituent blank nodes explicitly? Basically, I'm looking for a short cut. I'm not looking to get instances of the equivalent class, just the class definition itself in one go. I've tried to search for answers, but I'm not really clear on what possibly related answers are saying.
I'd like to get something akin to
(hasObjProp some ClassA) and (has_data_prop exactly 1 rdfs:Literal)
as an answer to the SELECT query on EquivClass. If the answer is "not possible", that's enough. I can write the blank node traversal with the necessary properties myself.
Thanks!!
Files are -
Ontology imported into GraphDB: tester.owl - https://pastebin.com/92K7dKRZ
SELECT of all triples from GraphDB *excluding* inferred triples: tester-graphdb-sparql-select-all-excl-inferred.tsv - https://pastebin.com/fYdG37v5
SELECT of all triples from GraphDB *including* inferred triples: tester-graphdb-sparql-select-all-incl-inferred.tsv - https://pastebin.com/vvqPH1FZ
Added sample query in response to #UninformedUser. I use "select *" for example, but really I'm interested in the "end results", ie, ?fp, ?fo, ?rop, ?roo. Essentially, I'm looking for something simpler and more succinct than what I have below.The example I posted only has a single intersection ("and" clause). In my real world set, there are multiple equiv classes with different numbers of "and" clauses.
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <http://www.semanticweb.org/ontologies/2020/9/tester#>
select * where {
:EquivClass owl:equivalentClass ?bneq .
?bneq ?p ?bnhead .
?bnhead rdf:first ?first .
?first ?fp ?fo .
?bn3 rdf:rest ?rest .
?rest ?rp ?ro .
?ro ?rop ?roo .
filter(?bn3 != owl:Class && ?ro!=rdf:nil)
}
You can unroll the list using a property path:
prefix : <http://www.semanticweb.org/ontologies/2020/9/tester#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select * {
:EquivClass owl:equivalentClass/owl:intersectionOf/rdf:rest*/rdf:first ?restr.
?restr a owl:Restriction .
optional {?restr owl:onProperty ?prop}
optional {?restr owl:cardinality ?cardinality}
optional {?restr owl:someValuesFrom ?class}
}
This returns:
| | restr | prop | cardinality | class |
| 1 | _:node3 | :hasObjProp | | :ClassA |
| 2 | _:node5 | :has_data_prop | "1" ^^xsd:nonNegativeInteger | |
My PostgreSQL data has "id" column.
This column has each item's ID
Example.
PLPL-WIT-2009-00537
DE14531/11-1
NLRTD-2014-0603
some of them has "/" in ID (ex.DE14531/11-1 contains "/")
I want to replace all "/" to "--"
So I tried this
UPDATE table_name SET id=REPLACE(id,"/","--");
but an error occurred.
ERROR: column "/" does not exist
What should I do?
Replace double quote to single quote as below:
UPDATE table_name SET id=REPLACE(id, '/', '--');
I'm creating an Xtext plugin and for some reason, the following line incorrectly matches the StringStatement rule when it should match the UnstringStatement rule:
UNSTRING test2 DELIMITED BY " " INTO test2 END-UNSTRING
Here is my grammar:
Program:
(elements+=Elemental)*
(s+=Statement)*
;
Variable_Name:
varName=ID ("-" ID)*
;
Variable_Reference:
varRef=ID ("-" ID)*
;
Elemental:
'VAR' var=Variable_Name
;
Statement:
(us=UnstringStatement|s=StringStatement)
;
StringParam:
Variable_Reference | STRING
;
StringStatement:
'STRING' in=StringParam 'DELIMITED BY SIZE INTO' out=Variable_Reference 'END-STRING'
;
UnstringStatement:
'UNSTRING' in=StringParam 'DELIMITED BY' string2=STRING 'INTO' (outs+=Variable_Reference)* 'END-UNSTRING'
;
When I run the project as an Eclipse Application, the 'UNSTRING' token is highlighted (correctly), but the rest of the line has the error "Mismatched character '"' expecting 'S'." The 'S' that the error refers too, is from 'SIZE'.
Any idea why the two rules overlap like this?
EDIT, forgot the STRING rule:
terminal STRING :
'"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"' |
"'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'"
;
EDIT 2:
After stepping through some of the Lexer code, I discovered that the token "DELIMITED BY" is incorrectly matched to "DELIMITED BY SIZE INTO", which then fails.
EDIT 3 FIXED:
I fixed this, but have no idea why it works. I just added a terminal DELIMITED_BY:
terminal DELIMITED_BY: 'DELIMITED BY'
StringStatement:
'STRING' in=StringParam DELIMITED_BY 'SIZE INTO' out=Variable_Reference 'END-STRING'
;
UnstringStatement:
'UNSTRING' in=StringParam DELIMITED_BY string2=STRING 'INTO' (outs+=Variable_Reference)* 'END-UNSTRING'
;
The STRING Token looks too greedy. In ANTLR the expression should be
terminal STRING :
'"' ( '\\' . | !('\\'|'"') )*? '"' |
"'" ( '\\' . | !('\\'|"'") )*? "'"
;
The following code queries DBpedia for places within a bounded geographic area and returns the name, lat, and long of the place. I'd also like the query to return the category of the place--e.g., park, restaurant, museum, etc.
The following code works fine.
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setQuery("""
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX category: <http://dbpedia.org/resource/Category:>
SELECT * WHERE {
?s a dbo:Place .
?s geo:lat ?lat .
?s geo:long ?long .
I tried to add the following code to get categories for places, but this doesn't work:
?s category:cat ?cat .
What should I add/change? Thanks.
You can get the category of a place (assuming you mean the type) by finding the type (rdfs:type) or the subject (dcterms:subject) of a resource. In DBPedia the first relates to the DBPedia and Yago ontologies and the second is a SKOS hierarchy in DBPedia. Here is an example query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT * WHERE {
?s a dbo:Place .
?s geo:lat ?lat .
?s geo:long ?long .
?s a ?type .
?s dcterms:subject ?sub
}
Note that you will get multiple types and subjects for each place.