DBpedia query giving me error when using jena - sparql

When i run this query on dbpedia.org
SELECT DISTINCT ?resource ?label ?location
WHERE
{
<http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo .
?resource geo:geometry ?location ;
rdfs:label ?label .
FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) .
FILTER( lang( ?label ) = "en" )
}
it is producing results fine but same query gives me error when using java jena api.
String s2 = "SELECT DISTINCT ?resource ?label ?location \r\n" +
"WHERE \r\n" +
" { \r\n" +
" <http://dbpedia.org/resource/New_York_City> geo:geometry ?sourcegeo . \r\n" +
" ?resource geo:geometry ?location ; \r\n" +
" rdfs:label ?label . \r\n" +
" FILTER( bif:st_intersects( ?location, ?sourcegeo, 20 ) ) . \r\n" +
" FILTER( lang( ?label ) = \"en\" ) \r\n" +
" }\r\n" +
"";
Query query = QueryFactory.create(s2); //s2 = the query above
QueryExecution qExe = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query );
ResultSet results = qExe.execSelect();
ResultSetFormatter.out(System.out, results, query) ;
it gives me the following errors
Exception in thread "main" org.apache.jena.query.QueryParseException: Line 4, column 49: Unresolved prefixed name: geo:geometry
at org.apache.jena.sparql.lang.ParserBase.throwParseException(ParserBase.java:521)
at org.apache.jena.sparql.lang.ParserBase.resolvePName(ParserBase.java:286)
at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.PrefixedName(SPARQLParser11.java:4857)
at org.apache.jena.sparql.lang.sparql_11.SPARQLParser11.iri(SPARQLParser11.java:4841) and it goes on

DBPedia has a number of Predefined Namespace Prefixes, which includes
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>,
which Jena does not have. Hence you have to specify it for use Jena before your SELECT. I.e.
String s2 = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>\r\n" +
"SELECT DISTINCT ?resource ?label ?location \r\n" +
... etc

Related

Best way to filter DBpedia results and return a specific results using SPARQL

I have a little problem...
From a list on wikipedia Page Ids, i want to return:
pageid
Abstract ( English)
Thumbnail
type
Only for company OR university page
I have this simple SPARQL query:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select ?pageid ?abstract ?thumbnail ?company ?type
where {
?resource dbpedia-owl:wikiPageID ?pageid;
dbpedia-owl:abstract ?abstract
. OPTIONAL {
?resource dbpedia-owl:thumbnail ?thumbnail
}
. OPTIONAL {
?resource dbpedia-owl:type ?type
}
. OPTIONAL {
?resource ?company dbpedia-owl:Company
}
FILTER(
?pageid = 14617 || ?pageid = 26989 || ?pageid = 31776 ||
?pageid = 256913 || ?pageid = 342924 || ?pageid = 1785141 ||
?pageid = 3057187 || ?pageid = 7529378 || ?pageid = 18978754
)
FILTER(langMatches(lang(?abstract),"en"))
}
I have this result: SPARQL Result
I managed to return the data that I wanted except that I cannot filter only that companies and universities.
is there any idea on how to remove pages that are not companies or universities in the dbpedia query results?
#AKSW has provided a query that does what you want, but I think it would be better with some more detailed explanation of why. So, here is that query, with a bunch of whitespace and some inline comments to help clarify --
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?pageid ?abstract ?thumbnail ?type
WHERE
{
# the next couple lines limit rdf:type values
VALUES ?type
{ dbo:Company dbo:University }
# the next few lines limit dbo:wikiPageID values
VALUES ?pageid
{ 14617 26989 31776
256913 342924 1785141
3057187 7529378 18978754 }
# the next few lines get values for the 3 predicates
# you required for each subject
?resource dbo:wikiPageID ?pageid ;
rdf:type ?type ;
dbo:abstract ?abstract .
# the next line gets thumbnails, if they exist
OPTIONAL { ?resource dbo:thumbnail ?thumbnail }
# the next line limits the solutions you receive to those
# with an abstract langtagged as "en"
FILTER ( langMatches ( lang ( ?abstract ), "en" ) )
}

transaction time out in sparql query

select distinct ?label ?resource count(distinct ?type) as ?score
where
{
values ?type
{ <http://dbpedia.org/class/yago/Abstraction100002137>
<http://dbpedia.org/class/yago/Company108058098>
<http://dbpedia.org/class/yago/ElectronicsCompany108003035>
<http://dbpedia.org/class/yago/Group100031264>
<http://dbpedia.org/class/yago/Institution108053576>
<http://dbpedia.org/class/yago/Organization108008335>
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)
limit 10
I am trying to run this query but it shows transaction timeout, I cut out many of these links from this query, but still it shows same issue

Problem when running query with MINUS filter in jena

I want to add the MINUS filter in a query but when I add it the query simply does not execute, but when I remove the MINUS filter the query executes normally.
This is the code when I use MINUS:
Query sparql = QueryFactory.create("PREFIX sosa: <http://www.w3.org/ns/sosa/>\r\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"SELECT ?s ?c ?p ?o FROM <http://147.27.60.65/sensorOntology> WHERE {?s rdf:type owl:NamedIndividual.\r\n" +
"?p rdf:type owl:ObjectProperty.\r\n" +
"?c rdf:type owl:Class.\r\n" +
"?s rdf:type ?c.\r\n" +
"?s ?p ?o\r\n" +
"MINUS {?s rdf:type owl:Thing}\r\n" +
"}");
QueryExecution vqe = QueryExecutionFactory.create(sparql, inf);
This is the code that executes correctly when I do not use MINUS:
Query sparql = QueryFactory.create("PREFIX sosa: <http://www.w3.org/ns/sosa/>\r\n" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#>\r\n" +
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"SELECT ?s ?c ?p ?o FROM <http://147.27.60.65/sensorOntology> WHERE {?s rdf:type owl:NamedIndividual.\r\n" +
"?p rdf:type owl:ObjectProperty.\r\n" +
"?c rdf:type owl:Class.\r\n" +
"?s rdf:type ?c.\r\n" +
"?s ?p ?o}");
QueryExecution vqe = QueryExecutionFactory.create(sparql, inf);
Is there something wrong with the syntax? Is it perhaps that I query a InfModel type ?

Encountered " "<" "< "" at line 1, column 15. Was expecting: <IRIref>

Trying to use the query in the endpoint. The query was created in SPARQL. The error coming like
Encountered " "<" "< "" at line 1, column 15.
Was expecting:
<IRIref> ...
Query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition}
The url
http://localhost:3030/skosmos/query?query=
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?Class ?Title ?Definition
WHERE {
?Value rdfs:label ?Class
FILTER regex(?Class, "Motion") .
?def rdfs:domain ?Value .
?def rdfs:label ?Title .
?def rdfs:comment ?Definition
}
I took your original query, URL-encoded it with one of many services and tools you might use, randomly selected from a web search, and appended it to the start of what you had as "the URL", http://localhost:3030/skosmos/query?query= ... and you verified that this worked --
http://localhost:3030/skosmos/query?query=PREFIX+rdfs%3A+%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0D%0A%0D%0ASELECT+%3FClass+%3FTitle+%3FDefinition%0D%0AWHERE+%7B%0D%0A%3FValue+rdfs%3Alabel+%3FClass%0D%0AFILTER+regex%28%3FClass%2C+%22Motion%22%29+.%0D%0A%3Fdef+rdfs%3Adomain+%3FValue+.%0D%0A%3Fdef+rdfs%3Alabel+%3FTitle+.%0D%0A%3Fdef+rdfs%3Acomment+%3FDefinition%7D

SPARQL query with FILTER returning correct values for its variables BUT only the given value for the variable being filtered

I'm using Jena to make a SPARQL query to search for all a document's properties by its subject. But the documents can have more than one subject, and when I do the search, it doesn't return me all the documents' properties, including all the documents' subjects, but even if it has 3 subjects (for example) it returns me all the documents properties + only the subject I set at FILTER.
I'd like to have as a return all the properties from the found document + all the subjects (that belong to the found document) and not only the one at FILTER.
Query (this.subject is a variable that has its value set in a JSF page):
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX dc: <http://purl.org/dc/elements/1.1/> " +
"PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?document ?subject" +
" ?title ?description ?language WHERE { " +
"?document dc:title ?title." +
"?document dc:subject ?subject." +
"?document dc:description ?description." +
"?document dc:language ?language." +
"FILTER ( regex(?subject, replace( \"" + this.subject + "\", ' ', '|' ), 'i' )). }";
Thank you!
You likely want to use a sub-query to restrict to the documents matching the FILTER and then select the rest of the stuff you are actually interesting in e.g.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc : <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?document ?subject ?title ?description ?language
WHERE
{
{
SELECT ?document
WHERE
{
?document dc:subject ?subject .
FILTER(REGEX(?subject, REPLACE("search term", " ", "|"), "i"))
}
}
?document dc:title ?title ;
dc:description ?description ;
dc:subject ?subject ;
dc:language ?language .
}
Note that this is still going to give you a row for each document-subject combination so if you have a document with 3 subjects you'll get three rows for that document. If you want to combine documents into a single row then you can use GROUP BY and then a GROUP_CONCAT aggregate, there are other questions already on Stack Overflow that detail how to do that.
Notes
Also note that using simple string concatenation to inject constants into your query is ill advised, take a look at Jena's ParameterizedSparqlString for a more user friendly and SPARQL injection proof API for building queries.