i'm new dealing with Ontologies and finding problems to get my SPARQL Query working , trying to read value of specific Object property that has multiple Ranges Object Property Screenshot
trying this Query Return all Object Properties Execution Result , Protege Visualization
PREFIX ns: <http://www.semanticweb.org/pavilion/ontologies/2017/5/untitled-ontology-66#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT *
WHERE {
ns:star1086 ns:possesses ?z .
}
The Desired Result that i want to read only the desired Range value, Thanks in advance.
I think there is something wrong about your ontology.
Specifying muliple ranges to your predicate creates an intersection. Take the following statement:
?star ns:possesses ?something
Then ?something is a SpectralType and a StarTemperature and a StarCoordinates and a StarName at the same time, which is not what you want.
Instead, you should use unions. Using unions, you can state that the object of a ns:possesses statement can be either a SpectralType or a StarTemperature or a StarCoordinates or a StarName. Then, in your SPARQL query you can write the following to get only statements from a single type.
SELECT * WHERE {
ns:star1086 ns:possesses ?z .
?z a ns:SpectralType .
}
In Protégé, to write a union, open the class expression editor (by clicking on the "plus" next to "Ranges" for instance) and separate the different members with or :
SpectralType or StarTemperature or StarCoordinates or StarName
And click "OK" to create the new range.
Further considerations
Let's take a step a back and look at your ontology.
You should not use a single predicate to store all these information in the first place. Instead, I suggest you use different sub-predicates so that your graphs and queries hold more semantic value.
Furthermore, StarName and Temperature are literal values. You should not use classes for that. Use datatype properties instead.
Here is a Gist you can download and open in Protégé. It contains some sample data so you can try the following SPARQL queries.
PREFIX : <http://www.richarddegenne.com/ontology/astronomy#>
# Get all statements about :star1086
SELECT * WHERE {
:star1086 ?predicate ?object
}
# Get some statement about :star1086
SELECT * WHERE {
VALUES ?predicate {
:hasSpectralType :temperate
}
:star1086 ?predicate ?object
}
# Ask whether a given pattern is true
ASK WHERE {
:star1086 :hasSpectralType :yellowDwarf
}
# Filter stars based on their temperature
# Note: You might want to add more stars with different temperature
# if you want useful results.
SELECT ?star WHERE {
?star :temperature ?temperature
FILTER(?temperature > 5000)
}
Related
How are you?
I 'm taking my first steps with GraphDb and I've found a couple of behaviors that I don't quite understand. Let me build the case.
I have the following Ontology, which I have loaded into GraphDb.
enter image description here
Let's only consider the tree under Persona.
When I go to Visual Graph and type :Persona in the search bar, this is what returns:
enter image description here
As you can see all the nodes corresponding to the ontology are there, but also some other nodes like Class (in different colors), and Thing and Nothing. (Ignore Paul which is an instance I added)
What are these other nodes? and how can I prevent them from appearing in this view?
Now, when I query with SPARQL, and I run
select * where {
?s ?p ?o .
}
If I have "Include inferred data in results" Off, I get this
enter image description here
Which is fine. It's what I would expect.
But ... when I turn inference On , which I believe is one of the true powers of working with rdf, I get around 800 records with all sort of triples with definitions for owl and rdf generic objects.
In order to get only my stuff and keep the inference capability on, I filtered as follows
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX pe: <http://www.semanticlab.com/ontologias/EmpresasYPersonas#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select * where {
?s ?p ?o .
FILTER (REGEX(str(?s),"Persona*" )).
FILTER (!REGEX(str(?o),"owl*" )).
FILTER (!REGEX(str(?o),"rdf*" )).
}
order by ?s
Which returns
enter image description here
Here I see some inferencing, for example Paul classified as a Persona, which is not explicitly stated.
But on the other hand there are a bunch of new triples that I don't understand nor want, as they don't bring any value: for example the ones with owl:sameAs, owl:equivalentClass, or the ones stating that Persona is a subClassOf Persona (which in fact I believe is wrong).
Could you please explain why this is happening, and how to prevent this behavior?
I'm aware that I might be making some mistakes, so if you spot any, please let me know.
I'm trying to create a SPARQL query to return all the Land Registry House Price Index data for a region by searching for the name. I am stuck on one part of this, which I have set out below.
This code returns up the Region Name, based on the Region Identifier. This works and returns "Barnet".
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT
?region ?regionName
WHERE
{
<http://landregistry.data.gov.uk/id/region/barnet> rdfs:label ?regionName .
}
However, I would like to reverse this so I can get the Region Identifier by entering the Region Name. The below code result is "No Data in Table". I've spent a good few hours looking at tutorials and think it should be working based on what they say. Am I missing something obvious?
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT
?region
WHERE
{
?region rdfs:label "Barnet" .
}
As mentioned in the comments, if you click to view the data as plain text then you can see that there is also a language tag on the data. The tag needs to be included when searching, otherwise the match won't be made.
I am new to SPARQL and trying to fetch a movie adapted from specific book from dbpedia. This is what I have so far:
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
<http://dbpedia.org/page/2001:_A_Space_Odyssey> a ?type.
?type onto:basedOn ?book .
?book a onto:Book
}
I can't get any results. How can I do that?
When using any web resource, and in your case the property :basedOn, you need to make sure that you have declared the right prefix. If you are querying from the DBpedia SPARQL endpoint, then you can directly use dbo:basedOneven without declaring it, as it is among predefined. Alternatively, if you want to use your own, or if you are using another SPARQL client, make sure that whatever short name you choose for this property, you declare the prefix for http://dbpedia.org/ontology/.
Then, first, to get more result you may not restrict the type of the subject of this triple pattern, as there could be movies that actually not type as such. So, a query like this
select distinct *
{
?movie dbo:basedOn ?book .
?book a dbo:Book .
}
will give you lots of good results but not all. For example, the resource from your example will be missing. You can easily check test the available properties between these two resource with a query like this:
select ?p
{
{<http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey> }
UNION
{ <http://dbpedia.org/resource/2001:_A_Space_Odyssey> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)>}
}
You'll get only one result:
http://www.w3.org/2000/01/rdf-schema#seeAlso
(note that the URI is with 'resource', not with 'page')
Then you may search for any path between the two resource, using the method described here, or find a combination of other patterns that would increase the number of results.
In TopBraid Composer, I save all my SPARQL queries as Comments in separate instances. I want to calculate their value and I want that value in a separate instance. For example, my SPARQL queries is 2 * 3 and when I run it, the result is 6. Here my comment contain "2 * 3".
What I want is the result "6" to be displayed in a separate instance. I will use the value of that instance in some other calculation, say "6 (of this) * 4" = 24. Please let me know if there is any way to deal with it.
It will save us from writing and arranging so many inner queries. All we would need is write simple queries and connect them through this method and the final query will get us the result. Thank you so much.
SPARQL is pretty good about casting to basic XML schema types, so you can simply cast these to xs:integer - as an example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xs: <http://www.w3.org/2001/XMLSchema#>
SELECT ?product
WHERE {
<x> rdfs:comment ?c1 .
<y> rdfs:comment ?c2 .
BIND (xs:integer(?c1) * xs:integer(?c2) AS ?product)
}
I am trying to retrieve the value of the dbpedia-owl:influenced in this page e.g: Andy_Warhol
The query I write is:
PREFIX rsc : http://dbpedia.org/resource
PREFIX dbpedia-owl :http://dbpedia.org/ontology
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:infuenced ?o .
}
but it is EMPTY.
Strange is that when I have the same query for another property from the ontology type like "birthPlace", the sparql engine gives the result back:
SELECT ?o WHERE {
rsc:Andy_Warhol dbpedia-owl:birthplace ?o .
}
which is a link to another resource:
dbpedia.org/resource/Pittsburgh
I am just confused how to write this query?
besides several formal errors addressed in the answer of #Joshua, there is also the semantic problem that the properties you are looking for - in this case - seem to be found on the entities that were influenced.
this query might give you the desired results
PREFIX rsc: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT ?s WHERE {
?s dbpedia-owl:influencedBy rsc:Andy_Warhol .
}
run query
There are a few issues here. One is that the SPARQL, as presented, isn't correct. I edited to make the prefix syntax legal, but the prefixes were still wrong (they didn't end with a final slash). You don't want to be querying for http://dbpedia.org/resourceAndy_Warhol after all; you want to query for http://dbpedia.org/resource/Andy_Warhol. Some standard namespaces for DBpedia are listed on their SPARQL endpoint. Using those namespaces and the SPARQL endpoint, we can ask for all the triples that have http://dbpedia.org/resource/Andy_Warhol as the subject with this query:
SELECT * WHERE {
dbpedia:Andy_Warhol ?p ?o .
}
In the results produced there, you'll see the one using http://dbpedia.org/ontology/birthPlace (note the captial P in birthPlace), but you won't see any triples with the predicate http://dbpedia.org/ontology/infuenced, so it makes sense that your first query has no results. Do you have some reason to suppose that there should be some results?