Conditional insertion of a new concept class to an ontology model - sparql

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.)

Related

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.

Returning properties from dbpedia Virtuoso

When I look at the HTML page: http://dbpedia.org/page/Bill_Nye I can see a lot of properties that are not returned in the following simple query from the Virtuoso page (http://pt.dbpedia.org/sparql):
prefix foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s ?p WHERE {
?e foaf:name "Bill Nye"#en .
?e ?s ?p.
}
No results return when I try to access one of the properties I can see on this page- say foaf:depiction:
prefix foaf: <http://xmlns.com/foaf/0.1/>
SELECT $depiction WHERE {
?s foaf:name "Bill Nye"#en.
?e foaf:depiction ?depiction
}
When I run them via the sparql endpoint at http://dbpedia.org/sparql, after encoding
SELECT ?s ?p WHERE { ?e foaf:name "Bill Nye"#en.?e ?s ?p. }
I get
http://dbpedia.org/sparql?query=SELECT%20%3Fs%20%3Fp%20WHERE%20%7B%20%3Fe%20foaf%3Aname%20%22Bill%20Nye%22#en.%3Fe%20%3Fs%20%3Fp.%20%7D&format=json
And a result of what looks like all the properties shown at http://dbpedia.org/page/Bill_Nye. I would love an explaniation of the difference, is it simply the Virtuoso interface or something more? I'm pretty fresh at this semantic web, so please be gentle.
Please note that you are sending the queries to two different Virtuoso installations:
pt.dbpedia.org/sparql : is the international chapter for Portuguese language (PT stands for DBpedia Portuguese)
dbpedia.org/sparql : is the main SPARQL endpoint for DBpedia, containing data from multiple languages, but in an English-centric way.
You will also have different experiences with es.dbpedia.org, it.dbpedia.org, el.dbpedia.org, etc.
I18n chapters do not load exactly the same data sets as the main DBpedia. Please see:
http://mappings.dbpedia.org/index.php/DBpedia_datasets
I believe the following query (ordered) will enable you to more easily identify the missing results --
prefix foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s ?p ?o WHERE
{
?s foaf:name "Bill Nye"#en .
?s ?p ?o.
}
order by 2
here you can easily identify that all the missing results have predicates of the form -
is ********** of
Basically, these are just eye candy in the human viewable Html view - and represent additional relationships wherever the object of some other subject is http://dbpedia.org/resource/Bill_Nye
That is -
<something else> ?p <http://dbpedia.org/resource/Bill_Nye>