SPARQL all predicate-object pairs of subject and all its superclasses - oop

Imagine you do something crazy and store your object-oriented model as an RDF graph.
shows a simplified example of the inheritance hierarchy and the associated attributes.
In practice, you get such graph structure if you translate some UML class diagram into RDFS.
The question is: what SPARQL query can deliver all the predicate-object pairs necessary to instantiate a particular resource of "Class C". In other words: how do you get all the predicate-object pairs along the whole inheritance chain (only single inheritance).

Given this diagram, the predicate-object pairs of all members of the class:ClassC is simply:
SELECT ?inst ?p ?o
WHERE {
?inst a :ClassC .
Inst ?p ?o .
Keep in mind that there is not property inheritance in RDF/RDFS. If you want to find all of property/values pairs for ClassA with entailments for subclasses then useL
SELECT ?inst ?p ?o
WHERE {
?cls rdfs:subClassOf* :ClassA .
?inst a ?cls .
?inst ?p ?o
}
In this respect, RDFS works a bit backwards of one's expectations of OO inheritance.

With the info from #scotthenninger the following query did the job:
SELECT ?p ?o
WHERE {
:ClassC rdfs:subClassOf* ?anySuperClass .
?anySuperClass ?p ?o .
}
edit:
Similar query gets all the self-defined properties and their range along the inheritance chain:
SELECT ?prop ?obj
WHERE {
:ClassC rdfs:subClassOf* ?anySuperClass .
?prop rdfs:domain ?anySuperClass .
?prop rdfs:range ?obj .
}
End results combined:
foo:ID xsd:string
foo:name xsd:string
rdfs:comment xsd:string
foo:similarTo :ClassD

Related

Conditional insertion of a new concept class to an ontology model

I am in a learning phase of SPARQL and ontology building. I have a model and I would like to add a new concept class to multiple concepts in a model using regex/filter.
I have following concepts:
A647674
A878678
RR36868
DD36868
The expected output is :
A647674
A878678
RR36868 rdf:type http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule
DD36868 rdf:type http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule
I am using below SPARQL query to do this.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
INSERT {
?s rdf:type 'http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule' .
}
WHERE
{
{?s ?p ?o .
filter regex(str(?s), "http://ontology.aaaaaaa.com/drugs/aaaaaaa#RR-").
}
union
{?s ?p ?o .
filter regex(str(?s), "http://ontology.aaaaaaa.com/drugs/aaaaaaa#DD").
}
};
#LIMIT 100
I am getting below error using above query.
OmServerGenericException[message="http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule",responseCode=500]
Caused by: org.apache.jena.rdf.model.ResourceRequiredException: "http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule"
Any help is highly appreciated
You are providing a string value:
?s rdf:type 'http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule' .
You need to provide a URI value:
?s rdf:type <http://schemas.aaaaaaa.com/ontologies/drug#SmallMolecule> .
Or you could define a prefix (PREFIX drug: <http://schemas.aaaaaaa.com/ontologies/drug#>) to use it like this:
?s rdf:type drug:SmallMolecule .
(The suggestions given in my answer to your previous question apply here, too: you could use STRSTARTS instead of REGEX, and one FILTER with || instead of UNION.)

Listing Properties and Values of an Individual on Dbpedia

How can I list properties with their values for any given DBpedia class? I'm new to this and have looked at several other questions on this but I haven't found exactly what I'm looking for.
What I'm trying to do is providing some relevant additional information to topics of conversation I have got from text mining.
Say for example the topic of conversation in a certain community is iPhones. I would like to use this word to query the DBpedia page for this word, IPhone, to get an output such as:
Type: Smartphone
Operating System: IOS
Manufacturer: Foxconn
EDIT:
Using the query from AKSW I can print the p (property?) and o (object?), although I'm still not getting the output I want. Instead of getting something like:
weight: 133.0
I get
http://dbpedia.org/property/weight:133.0
Is there a way to just get the name of the property instead of the DBpedia link?
My Code
Classes do not "have" properties with values. Instances (resp. resources or individuals) do have a relationship via a property to some value which can be an individual itself or a literal (or some anonymous instance aka blank node). And instances belong to a class. e.g. Berlin belongs to the class City
What you want is to get all outgoing values of a given resource in DBpedia:
SELECT * WHERE { <http://dbpedia.org/resource/IPhone> ?p ?o }
Alternatively, you can use SPARQL DESCRIBE, which return the data in forms of an RDF graph resp. a set of RDF triples:
DESCRIBE <http://dbpedia.org/resource/IPhone>
This might also return incoming information because it's not really specified in the W3C recommendation what has to be returned.
As stated by AKSW properties often link to other classes rather than values. If you want all properties and their values, including other classes the the below gives you the label and filters by language (put the language code you need where have put "en").
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
If you don't want any properties that link to other classes, then you only want datatype properties so this code could help:
SELECT DISTINCT ?label ?o
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?p a owl:DatatypeProperty .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Obviously this gives you far less information and functionality, but it might just be what you're after?
Edit: In reply to your comment, it is also possible to get the labels for the values, using the same technique:
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?o <http://www.w3.org/2000/01/rdf-schema#label> ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
Note that http://www.w3.org/2000/01/rdf-schema#label is often shortened to rdfs:label by defining prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
So you could also do:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?label ?oLabel
WHERE {
<http://dbpedia.org/resource/IPhone> ?p ?o.
?p rdfs:label ?label .
?o rdfs:label ?oLabel
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "en"))
}
and get exactly the same result but possibly easier to read.

Get relationships between direct and indirect instances a class?

I have a class named WeatherPatterns, it has many subclasses and the subclasses have named Individuals/ Instances.
I want a sparql query to retrieve all instances of WeatherPatterns that have relation with each other. For example, the output triples should be listed like the following.
Cloud produces Rain
Rain causes Flood
How to make this query? thanks in advance
Without your data, we can't make an exact query, but if you're looking for relationships between things that are direct or indirect instances of WeatherPatterns, that's not too hard:
select ?s ?p ?o {
#-- Make sure that ?s and ?o are
#-- instance of WeatherPatterns
#-- or its subclasses.
?s a/rdfs:subClassOf* :WeatherPatterns .
?o a/rdfs:subClassOf* :WeatherPatterns .
#-- Find relationships between
#-- ?s and ?o.
?s ?p ?o .
}
The property path ?s a/rdfs:subClassOf* :WeatherPatterns uses a as the standard abbreviation for rdf:type, and matches when ?s belongs to a class that is related by a chain of rdfs:subClassOf properties to :WeatherPatterns. (The chain can be of length zero, which means that ?s is a direct instance of :WeatherPatterns.) If you want to combine the two checks into one, you can also do:
:WeatherPatterns ^(a/rdfs:subClassOf*) ?s, ?o .

Custom SPARQL Construct with enumeration

Is it possible to execute SPARQL construct while adding information outside the scope of query? e.g., I want to execute SPARQL construct while defining enumeration information like this:
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
construct {
?s a skos:Concept
?s ex:index <enumeration starting from 1 -- this is just a sample>
}
where {
?s a skos:Concept
}
is it possible to do something like that with pure SPARQL? what are the alternatives?
* Additional Information *
Probably I am not explained my problem clearly, so basically I want to achieve the following (assuming that ex:index is a valid datatypeProperty):
== Initial RDF triples ==
#prefix skos:<http://www.w3.org/2004/02/skos/core#>
#prefix ex: <http://example.org/> .
ex:abc rdf:type skos:Concept .
ex:def rdf:type skos:Concept .
...
ex:endOfSample rdf:type skos:Concept .
== RDF triples after SPARQL Update execution ==
#prefix skos:<http://www.w3.org/2004/02/skos/core#>
#prefix ex: <http://example.org/> .
ex:abc rdf:type skos:Concept ;
ex:index 1 .
ex:def rdf:type skos:Concept ;
ex:index 2 .
...
ex:endOfSample rdf:type skos:Concept ;
ex:index <endOfSampleNumber> .
You can construct any valid RDF value in a CONSTRUCT. However the query will fail if any of the variables in the CONSTRUCT graph pattern is unbound after executing the WHERE graph. I.e. there can be no binding for ?p in your query and the CONSTRUCT will never execute.
This is an example that should get you started:
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX ex:<http://example.org/construct#>
construct {
ex:someProp a owl:ObjectProperty .
?s ex:someProp (1 2 3)
}
where {
?s a skos:Concept
}
This will result in the construction of seven triples for the property value and the list structure.
The ex:someProp is added because there isn't a good object property in SKOS for ad-hoc lists. It would be best to define the property with some semantic meaning. Also note that while the {ex:someProp a owl:ObjectProperty} triple will be asserted for each match of {?s a skos:Concept}, it is the same triple, hence there will be only one in the end. The price is efficiency, so asserting the property outside of this query would be a better choice - it is included in the above query for the sake of example completeness.

How to query an RDF individual for its data properties?

I have an ontology where arc_cfp is an individual of class Arc. I would like to know how could I get all the data properties of the individual, given that I have the individual's URI?
Basically, I am doing this:
SELECT ?idRef ?name ?src ?dst ?perf
WHERE
{
?x rdf:type http://www.semanticweb.org/ontologies/2012/1/graph.owl#arc_cfp .
?x graph:idRef_arc ?idRef .
?x graph:name_arc ?name .
?x graph:hasSource ?src .
?x graph:hasDestination ?dst .
?x graph:hasPerformatif ?perf .
}
I am pretty sure, using rdf:type is the problem. But, I have no idea what I need to use.
Thanks.
~Codera
Assuming you want a purely exploratory query of the form "give me all the triples about a subject" it should look the following:
SELECT *
WHERE
{
<http://example.org/SomeThing> ?p ?o
}
This will give you all predicate object pairs associated with the constant URI you pass in. If you are interesting in incoming as well as outgoing properties you could do the following instead:
SELECT *
WHERE
{
{ <http://example.org/SomeThing> ?p ?o }
UNION
{ ?s ?p <http://example.org/SomeThing> }
}
You can also use a DESCRIBE query to grab all the RDF data about a Resource.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
DESCRIBE ?x
WHERE
{
?x rdf:type http://www.semanticweb.org/ontologies/2012/1/graph.owl#arc_cfp .
}
P.S. Don't forget to put prefixes in your queries.