I am trying to develop a decision support system for soybean disease analysis using protege, fuseki server and php. I want to take input from a user and a return a disease name based on the matching of the literal. The query I am trying is :
$sparql = "PREFIX : <http://localhost/soy_test1#>
select ?Diaporthe_stem_canker where {?Diaporthe_stem_canker :hasTOC ' . TOC . ' . ' . TOC .' :hasTOC 'July'}";
It always returns Diaporthe_stem_canker no matter what the input is. I am trying to find what's the error in the sparql query. Any help would be appreciated.
Thanks.
Edit
The ontology I have is as follows:
A plant soybean is described by some plant descriptors. Plant descriptor has several types with some attribute values.
Example:
:PlantDescriptor :hasType "EnvironmentalDescriptors" .
:PlantDescriptor :hasAttribute :TOC .
:PlantDescriptor :hasTOC "July" .
:TOC rdfs:subClassOf :Attribute .
where PlantDescriptor and Attribute are the concepts of the ontology.
I am trying to take an input from the user for the attribute "TOC", which I want to match whether it's "July" or not; if it matches with July then it will return the disease name Diaporthe-stem-canker.
The string part is for matching the user input text with the value "July". That's why I have used it as a php variable inside SPARQL Query.
The query that I used in my Query engine is like this:
PREFIX : <http://localhost/soy_test1#>
select distinct ?P ?X ?Time
where {?PlantDescriptor :hasType ?P .
?PlantDescriptor :hasAttribute ?X.
?X :hasTOC "July" .
?X :hasTOC ?Time .}
which returns the value "July" under ?Time, the url for Attribute concept and the type of the PlantDescriptor for the corresponding Attribute.
Hope I have made my point more clear.
Thanks in advance.
The example isn't entirely clear because you say you are searching for Diaporthe-stem-canker, but that's not in your sample data. Assuming Diaporthe-stem-canker is a PlantDescriptor, the following query could get you what you need:
SELECT ?disease
WHERE {
?disease :hasTOC "July" .
}
And in your case the "July" string is inserted into the string, so you may have something like the following if you were using PHP (no idea what your syntax is(:
$sparql = "PREFIX : <http://localhost/soy_test1#>
select ?disease where {?disease :hasTOC " . $date . " . }"
...where $date is bound to "July", etc.
Related
How to List the laureate awards (given by their label) for which the description of the contribution (given by nobel:motivation) contains the word "human" together with the word "peace" (i.e., both words must be there).
I have use the bds:search namespace from the the full-text search feature of Blazegraph.
After visiting this link i have composed this query
Free text search in sparql when you have multiword and scaping character
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
FILTER ( bds:search ( ?description, '"human" AND "peace"' ) )
}
This query is returning me the following error on execution shown in image.
Error Image
How to correct this query and get the desired result?
You may take a look at the specification of this dataset or download an RDF dump of the dataset
Use bds:search to search for "human" category.Then apply filter and contain function to "peace".
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bds: <http://www.bigdata.com/rdf/search#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
PREFIX bif: <http://www.openlinksw.com/schemas/bif#>
SELECT ?awards ?description
WHERE {
?entity rdfs:label ?awards .
?entity nobel:motivation ?description .
?description bds:search "human" .
FILTER (CONTAINS(?description, "peace"))
}
I have some RDF data structured like this:
[ pref:ip_address "127.0.0.1" ;
pref:time "1459630844.482" ;
pref:url "https://google.com" ;
pref:user "johndoe"
] .
I need query that will return all results matching given IP, time frame (between from time and end time), url (even partial match) and user (also even partial match).
What I have now is simple query to get results based on single value, like this:
PREFIX pref: <http://something> SELECT DISTINCT * WHERE { ?u pref:user USER_VALUE . ?u ?p ?o . }
This returns all results with given user but only if given username is full match. Meaning it will return all results for johndoe if USER_VALUE is johndoe but not if it is john.
My knowledge of SPARQL is extremely limited and I appreciate any help. Thank you.
To do anything more than exact match, you need to use a FILTER and use operations like CONTAINS or REGEX.
Example:
{ ?u pref:user ?user .
?u ?p ?o .
FILTER( CONTAINS(?user, "john") )
}
There are a number of FILTER functions that may be useful including REGEX. See the spec for details.
I would like to get some places from DBPedia in certain area, and that would be easy if those points would had geometry property. However, all they have is georss:point. I have converted this to two doubles, but I cannot convert them to geo:geometry object that can be supplied to location filter.
The code I have thus far:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?name
?abstract
?ns
?ew
WHERE
{ ?m foaf:name ?name
. ?m georss:point ?coord
. ?m <http://dbpedia.org/ontology/abstract> ?abstract
. BIND( xsd:double(strbefore( ?coord, " " )) AS ?ns )
BIND( xsd:double(strafter( ?coord, " " )) AS ?ew )
BIND( geo:Point(?ew, ?ns) AS ?geo ) # <-- I have problem with this
FILTER (bif:st_intersects (?geo, bif:st_point(?geo), 10))
FILTER (LANG(?abstract) = "en")
}
If you know any way to convert those or other filtering method, please enlighten me. :)
There is not enough information here to give you a simple and clear answer.
However, I think you may be able to figure it out from the examples in the GEOSPARQL documentation for Virtuoso, the DBMS engine which hosts DBpedia. There's more here.
I'm using Jena TDB for store a dataset of triples from a file. I have a problem when I try to send a SPARQL query to TDB using filter. For example the following query works:
select ?ob where {
?ob rdfs:label "NameOfLabel"#language .
}
but this doesn't:
select ?ob where {
?ob rdfs:label ?pr .
filter( ?pr = "NameOfLabel" ) .
}
The purpose of this query is to find an ?ob from a "NameOfLabel" (ignoring the language). I've tried regex and str(?pr), and some other things, but these haven't worked. How can I do this?
Update (based on answer)
When I try using filter( str(?pr) = "NameOfLabel" ), I get an exception. Here's the stack trace:
Exception in thread "main" java.lang.IllegalArgumentException: getLow: Empty RecordBuffer
at com.hp.hpl.jena.tdb.base.buffer.RecordBuffer.getLow(RecordBuffer.java:59)
at com.hp.hpl.jena.tdb.base.recordbuffer.RecordRangeIterator.hasNext(RecordRangeIterator.java:112)
at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
at com.hp.hpl.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.hasNext(DatasetControlMRSW.java:119)
at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
at org.apache.jena.atlas.iterator.Iter$3.hasNext(Iter.java:200)
at org.apache.jena.atlas.iterator.Iter.hasNext(Iter.java:915)
at org.apache.jena.atlas.iterator.RepeatApplyIterator.hasNext(RepeatApplyIterator.java:59)
at com.hp.hpl.jena.tdb.solver.SolverLib$IterAbortable.hasNext(SolverLib.java:191)
at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:317)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterPlainWrapper.hasNextBinding(QueryIterPlainWrapper.java:54)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterConvert.hasNextBinding(QueryIterConvert.java:59)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIterDistinctReduced.hasNextBinding(QueryIterDistinctReduced.java:54)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorWrapper.hasNextBinding(QueryIteratorWrapper.java:40)
at com.hp.hpl.jena.sparql.engine.iterator.QueryIteratorBase.hasNext(QueryIteratorBase.java:112)
at com.hp.hpl.jena.sparql.engine.ResultSetStream.hasNext(ResultSetStream.java:75)
EDIT II:
Dataset dataset = TDBFactory.createDataset(DIRECTORY);
dataset.begin(ReadWrite.READ);
QueryExecution qExec = QueryExecutionFactory.create(query, dataset) ;
ResultSet risultati = qExec.execSelect();
while(risultati.hasNext()){
system.out.println(risultati.next());
}
The plain literal "NameOfLabel" is not the same as the literal with a language tag "NameOfLabel"#en. If
?ob rdfs:label "NameOfLabel"#en # (1)
works, then so should
?ob rdfs:label ?label .
filter ( ?label = "NameOfLabel"#en ) # (2)
If you want to compare the string content in a filter without comparing the language, just do:
?ob rdfs:label ?label .
filter ( str(?label) = "NameOfLabel" ) # (3)
Note that (2) is really not very good practice. It doesn't make much sense to filter on exact values, because you can just use (1) instead. (3) is OK, because you do actually need to use the str function. If you want to specify some exact values at run time, and have variables in the query, you also have the option of
values ?label { "NameOfObject"#en }
?ob rdfs:label ?label .
which has the advantage of being able to specify multiple values for ?label. If you're using Jena, you can also use a ParameterizedSparqlString and just have the pattern
?ob rdfs:label ?label .
but replace ?label when you have the value that you want. See my answer to get latitude and longitude of a place dbpedia for an example of ParameterizedSparqlStrings.
Hi am using AllegroGraph and Sparql query to retrieve the results. This is a sample data that reproduces my issue.
Consider below data where a person has first, middle and last names.
<http://mydomain.com/person1> <http://mydomain.com/firstName> "John"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://mydomain.com/middleName> "Paul"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://mydomain.com/lastName> "Jai"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>
<http://mydomain.com/person6> <http://mydomain.com/middleName> "Mannan"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person6> <http://mydomain.com/lastName> "Sathish"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person6> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>
Now I need to compute the persons name by combining the all 3 names. The names are optional and a person may not have any of the first, middle and last names.
Query I tried
select ?person ?name ?firstName ?middleName ?lastName where
{
?person rdf:type <http://mydomain.com/person>.
optional {?person <http://mydomain.com/firstName> ?firstName}.
optional {?person <http://mydomain.com/middleName> ?middleName}.
optional {?person <http://mydomain.com/lastName> ?lastName}.
bind (concat(str(?firstName),str(?middleName),str(?lastName)) as ?name).
}
But the result set does not contain the name for person6 (Mannan Sathish) since the first name is not present. Please let me know if I can ignore the firstName if it's not bound.
If a variable is not bound then str(...) will cause an error on evaluation and the whole BIND fails.
COALESCE can be used to give default values to expressions.
bind ( COALESCE(?firstName, "") As ?firstName1)
bind ( COALESCE(?middleName, "") As ?middleName1)
bind ( COALESCE(?lastName, "") As ?lastName1)
bind (concat(str(?firstName1),str(?middleName1),str(?lastName1)) as ?name