How to get the connected graph between 2 resources in RDF Graph [duplicate] - sparql

If this is the RDF graph , given the resource A , I need to construct all the triples connected to A till the end . here i have to get the graph include B,C,D,E
After that, suppose I've got this graph and want to go only from the starting point (:A) and get the subgraph produced by following the paths that end with an edge on property :d. For instance, if A1 is given as the starting point, and d as the property, we'd construct:
:A1 :a :B1,
:B1 :b :S1,
:B1 :b :S2,
:S1 :d :D1,
:S2 :d :D2,

The first case
To get the whole connected graph, you need to use a wildcard property path to follow most of the path, and then grab the last link with an actual variable. I usually use the empty relative path in constructing wildcards, so as to use <>|!<> as the wildcard, but since you mentioned that your endpoint doesn't like it, you can use any absolute IRI that you like, too. E.g.,
prefix x: <urn:ex:>
construct { ?s ?p ?o }
where { :A (x:|!x:)* ?s . ?s ?p ?o . }
This works because every property is either x: or not, so x:|!x: matches every property, and then (x:|!x:)* is an arbitrary length path, including paths of length zero, which means that ?s will be bound to everything reachable from :a, including :a itself. Then you're grabbing the triples where ?s is the subject. When you construct the graph of all those triples, you get the subgraph that you're looking for.
Here's an example based on the graph you showed. I used different properties for different edges to show that it works, but this will work if they're all the same, too.
#prefix : <urn:ex:> .
:A :p :B, :C .
:B :q :D .
:C :r :E .
:F :s :G .
:G :t :H .
prefix x: <urn:ex:>
prefix : <urn:ex:>
construct {
?s ?p ?o
}
where {
:A (x:|!x:)* ?s .
?s ?p ?o .
}
Since this is a construct query, the result is a graph, not a "table". It contains the triples we'd expect:
#prefix : <urn:ex:> .
:C :r :E .
:B :q :D .
:A :p :B , :C .
The second case
If you want to ensure that the paths end in a particular kind of edge, you can do that too. If you only want the paths from A1 to those ending with edges on d, you can do:
prefix x: <urn:ex:> #-- arbitrary, used for the property path.
prefix : <...> #-- whatever you need for your data.
construct {
?s1 ?p ?o1 . #-- internal edge in the path
?s2 :d ?o2 . #-- final edge in the path
}
where {
:A (x:|!x:)* ?s1 . #-- start at :A and go any length into the path
?s1 ?p ?o1 . #-- get the triple from within the path, but make
?o1 (x:|!x:)* ?s2 . #-- sure that from ?o1 you can get to to some other
?s2 :d ?o2 . #-- ?s2 that's related to an ?o2 by property :d .
}

The most important part of an RDF graph is the properties. Since your diagram does not define the properties, the question is rather ambiguous, but comes down to a couple of scenarios.
If the property is the same in the graph, then a transitive property path can be used:
CONSTRUCT {:A :prop ?rsc }
WHERE {
:A :prop* ?rsc .
}
If there are multiple types pf properties in the graph, it is more complicated to get the transitive closure. For example the following will get all properties in the example graph:
CONSTRUCT {
:A ?p ?rsc1 .
:A ?p1 ?rsc2 .
}
WHERE {
:A ?p ?rsc1 .
OPTIONAL {?rsc1 ?p1 ?rsc2 .}
}
Note this goes two levels deep. For arbitrary levels, it may be best to call the following query until no new triples are created:
CONSTRUCT {
?rsc ?p ?o .
}
WHERE {
?rsc ?p ?o .
}
...where rsc is bound to :A initially and to the values for ?o for subsequent iterations.

It's not possible to get the whole connected graph with one CONSTRUCT query. You'll need to run multiple queries. Even with multiple queries it gets a bit tricky when Blank Nodes are involved as you will have to gradually expand their context and return this context in every subsequent query.
For an example of some code that does exactly this, see: https://github.com/apache/clerezza-rdf-core/tree/master/impl.sparql/src/main/java/org/apache/clerezza/commons/rdf/impl/sparql.

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.

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

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

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.