Data
<ns0:Address rdf:datatype="http://www.w3.org/2001/XMLSchema#string">s block, kotahena</ns0:Address>
SPARQL Query:
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX ns0: <http://www.Jobsurveillance.com/CV#> " +
"SELECT (str(?object) as ?label) WHERE { " +
" ?subject rdf:Full_Name ?object ." +
"}"
Error during Run
Exception error occurs when running the program.
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered "(" at line 1, column 212.
Was expecting one of:
<VAR1> ...
<VAR2> ...
"distinct" ...
"reduced" ...
"*" ...
Related
I am a beginner in SPARQL. I am using the pizza ontology and I am trying to write a SPARQL query to get the number of pizzas of a particular topping.
If I have this query:
prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
select ?pizza
where {?pizza a owl:Class ;
rdfs:subClassOf ?restriction.
?restriction owl:onProperty pizza:hasTopping ;
owl:someValuesFrom pizza:PeperoniSausageTopping
}
I have the following result (using Jena):
------------------------------------------------------------------
| pizza |
==================================================================
| <http://www.co-ode.org/ontologies/pizza/pizza.owl#FourSeasons> |
| <http://www.co-ode.org/ontologies/pizza/pizza.owl#American> |
| <http://www.co-ode.org/ontologies/pizza/pizza.owl#AmericanHot> |
------------------------------------------------------------------
Now I wanted to return the number of pizzas for the query (here 3), and not the list of pizzas, so I wrote this query:
prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
select (sum(?pizza))
where {?pizza a owl:Class ;
rdfs:subClassOf ?restriction.
?restriction owl:onProperty pizza:hasTopping ;
owl:someValuesFrom pizza:PeperoniSausageTopping
}
But I have an exception, the query is incorrect, what did I do wrong?
The Exception I have with Jena is:
org.apache.jena.query.QueryParseException: Encountered " ")" ") "" at line 5, column 20.
Was expecting one of:
"not" ...
"as" ...
"in" ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
"=" ...
"!=" ...
">" ...
"<" ...
"<=" ...
">=" ...
"||" ...
"&&" ...
"+" ...
"-" ...
"*" ...
"/" ...
I want to get the Job ID passed as parameter and check the id with the id in the owl file and then, match the job skill of that job id to the skill of the candidates (candidates also contain skills). I am getting the url parameter and storing in a variable.
String jobID = request.getParameter("JobID");
String query =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX ns0: <http://www.Jobsurveillance.com/CV#> " +
"SELECT (str(?object) as ?label) " +
"(str(?object2) as ?label2) " +
"(str (?phoneNumobj) as ?label3) " +
"(str (?degreeobj) as ?label4) " +
"(str (?jobcatObj) as ?label5) " +
"(str (?candidateSkill) as ?label8) " +
"(str (?jobAdID) as ?label6) " +
"(str (?jobSkill) as ?label7) WHERE { " +
" ?person ns0:Full_Name ?object ." +
" ?person ns0:Email_Id ?object2 ." +
" ?person ns0:Phone_Number ?phoneNumobj ." +
" ?person ns0:Degree ?degreeobj ." +
" ?person ns0:Job_Category ?jobcatObj ." +
" ?person ns0:Skills ?candidateSkill ." +
" ?job ns0:JobID ?jobAdID ." +
" ?job ns0:JobSkills ?jobSkill ." +
" filter (regex(?jobAdID, \"" + jobID + "\") ." +
" filter (?jobSkill = ?candidateSkill)) ." +
"}";
When I run i get an error like below:
com.hp.hpl.jena.query.QueryParseException: Encountered " "." ". "" at line 1,
column 756.
Was expecting one of:
"not" ...
"in" ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
")" ...
"=" ...
"!=" ...
">" ...
"<" ...
"<=" ...
">=" ...
"||" ...
"&&" ...
"+" ...
"-" ...
"*" ...
"/" ...
My Dataset is as follows
<owl:NamedIndividual rdf:about="http://www.Jobsurveillance.com/CV#Candidate6">
<rdf:type>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.Jobsurveillance.com/CV#includes"/>
<owl:someValuesFrom rdf:resource="http://www.Jobsurveillance.com/CV#CV"/>
</owl:Restriction>
</rdf:type>
<ns0:Address rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Colombo 15</ns0:Address>
<ns0:Company rdf:datatype="http://www.w3.org/2001/XMLSchema#string">TechOrin</ns0:Company>
<ns0:Degree rdf:datatype="http://www.w3.org/2001/XMLSchema#string">B.Sc (Hons), Information Technology</ns0:Degree>
<ns0:Email_Id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">yashmitha#gmail.com</ns0:Email_Id>
<ns0:Skills rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Java</ns0:Skills>
<ns0:Job_Category rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Marketing</ns0:Job_Category>
<ns0:Full_Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Yashmitha</ns0:Full_Name>
<ns0:Phone_Number rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">0723679017</ns0:Phone_Number>
</owl:NamedIndividual>
This is my SPARQL query. Am I implementing the filter in the correct way. Could anyone pl help me with this. Thanks!
ISSUE SOLVED. The query below helped me get the exact output which I needed
String query =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX ns0: <http://www.Jobsurveillance.com/CV#> " +
"SELECT (str(?object) as ?label) " +
"(str(?object2) as ?label2) " +
"(str (?phoneNumobj) as ?label3) " +
"(str (?degreeobj) as ?label4) " +
"(str (?jobcatObj) as ?label5) " +
"(str (?candidateSkill) as ?label8) " +
"(str (?jobSkill) as ?label7) WHERE { " +
" ?person ns0:Full_Name ?object ." +
" ?person ns0:Email_Id ?object2 ." +
" ?person ns0:Phone_Number ?phoneNumobj ." +
" ?person ns0:Degree ?degreeobj ." +
" ?person ns0:Job_Category ?jobcatObj ." +
" ?person ns0:Skills ?candidateSkill ." +
" ?job ns0:JobID \"" + jobID + "\"." +
" ?job ns0:JobSkills ?jobSkill ." +
" filter (?jobSkill = ?candidateSkill)" +
"}";
In the above code the below line which was
filter (?jobSkill = ?candidateSkill))
is corrected as
filter (?jobSkill = ?candidateSkill)
Below SPARQL execdescribe query giving me only two prefixes as output while running with Jena query but when I run this query on virtuoso SPARQL endpoint it's giving perfect output.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX db: <http://dbpedia.org/ontology/>
PREFIX prop: <http://dbpedia.org/property/>
DESCRIBE ?movie ?author ?genre
WHERE {
?movie rdf:type db:Film ;
prop:author ?author ;
prop:genre ?genre .
}
LIMIT 2
OFFSET 0
When I run with Jena I am getting only two line output like this,
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
Below is my code which I am using but with some query its working fine,
String queryString = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" +
"PREFIX db: <http://dbpedia.org/ontology/> " +
"PREFIX prop: <http://dbpedia.org/property/>" +
"DESCRIBE ?movie ?author ?genre" +
"WHERE { " +
"?movie rdf:type db:Film ;" +
"prop:author ?author ;" +
"prop:genre ?genre ." +
"}" +
"LIMIT 2" +
"OFFSET 0";
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:8890/sparql", queryString);
Model results = qexec.execDescribe();
results.write(System.out,"TTL");
Its perfectly giving me output on virtuoso SPARQL end point.
below is screen shot,
As suggested in comments by #AKSW, There is a space issue in the query. So after edited my query, it will perfectly work fine.
I'm tring to find informations about a place (ex : New York http://dbpedia.org/page/New_York). I' try to get informations suchs as nearestCity , museum)...
My request returns nothing...
String name = "New_York";
String s4 = "PREFIX g: <http://www.w3.org/2003/01/geo/wgs84_pos#>\n" +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
"PREFIX onto: <http://dbpedia.org/ontology/>\n" +
"PREFIX dbpedia: <http://dbpedia.org/resource/>PREFIX dcterms: <http://purl.org/dc/terms/>\n"+
"PREFIX dp: <http://dbpedia.org/resource/>\n"+
"PREFIX dbprop: <http://dbpedia.org/property/>\n" +
"PREFIX grs: <http://www.georss.org/georss/>\n"+
"PREFIX dbpedia-oll: <http://dbpedia.org/ontology/> \n"+
"PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>PREFIX category: <http://dbpedia.org/resource/Category:>\n"+
"\n" +
"SELECT ?nearestCity \n" +
"WHERE\n" +
"{ OPTIONAL{onto:"+name+" grs:nearestCity ?nearestCity .}\n"+
" }\n" +
"LIMIT 300\n" +
"";
There are a couple problems with the query you are doing:
1) Some namespaces are wrong: the resource New_York has the URI: http://dbpedia.org/resource/New_York. In your query that would translate to "dbpedia:"+name instead of "onto:"+name.
2) The properties that you are looking for point to the resource New_York, not the other way around. For example, for the nearest city one, this is the query that works (note the namespaces again for the property):
SELECT ?nearestCity where {
?nearestCity <http://dbpedia.org/property/nearestCity> <http://dbpedia.org/resource/New_York>.
}LIMIT 300
(See results)
And for museum:
SELECT ?m where {
?m <http://dbpedia.org/ontology/museum> <http://dbpedia.org/resource/New_York>.
}LIMIT 300
(See results)
I am trying to fetch the names of universities using SPARQL:
The rdf data is the following (and a whole lot more, but that is irrelevant).
<Organization rdf:about="http://data.semanticweb.org/organization/the-university-of-queensland">
<rdfs:label>The University of Queensland</rdfs:label>
<homepage rdf:resource="http://www.uq.edu.au/"/>
<member rdf:resource="http://data.semanticweb.org/person/jane-hunter"/>
<member rdf:resource="http://data.semanticweb.org/person/kwok-cheung"/>
<member rdf:resource="http://data.semanticweb.org/person/robert-m-colomb"/>
<name>The University of Queensland</name>
</Organization>
I have written a java program which queries the data.
My string to query the data is the following:
queryString += "PREFIX swrc: <http://swrc.ontoware.org/ontology#> \n";
queryString += "PREFIX dc: <http://purl.org/dc/elements/1.1/> \n";
queryString += "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n";
queryString += "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \n";
queryString += "PREFIX ical: <http://www.w3.org/2002/12/cal/ical#> \n";
queryString += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n";
queryString += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n";
queryString += "PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#> \n";
queryString += "PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#> \n";
queryString += "SELECT ?name WHERE {\n";
queryString += "?university rdfs:label ?aff . \n ?university foaf:name ?name FILTER(str(?aff)='uni') }";
Unfortunately, this is not correct, as no result is returned:
<?xml version='1.0' encoding='UTF-8'?>
<sparql xmlns='w3.org/2005/sparql-results#'>
<head>
<variable name='name'/>
</head>
<results>
</results>
</sparql>
Can anyone point me in the right direction?
P.S. If possible I'd like to only fetch 10 university names.
I understand that you're assuming all universities have the string "uni" in their names.
Notice that you're checking for equality of a value with the string "uni". There really is no such instance in your data set.
Replacing FILTER(str(?aff)='uni') with FILTER regex(?aff, "uni", "i") will allow you to match the values that contain the string "uni" instead. It's a regular expression filter that takes three arguments.
a variable
a regular expression compliant with the syntax described here
a set of optional flags, in this case, I used one flag, "i". It means the match must be case-insensitive.
In order to limit the number of results, you can just append the query with the LIMIT keyword.
The resulting query should be:
PREFIX swrc: <http://swrc.ontoware.org/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX ical: <http://www.w3.org/2002/12/cal/ical#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#>
SELECT ?name WHERE {
?university rdfs:label ?aff .
?university foaf:name ?name
FILTER regex(?aff, "uni", "i")
} LIMIT 10;
In your RDF snippet does not have the "foaf:" namespace. So, maybe you should put in your RDF. Unless of course it is the default namespace in your RDF document.