UPdate Data property Value based on SPARQL DELETE and INSERT - sparql

I have created an ontology, and I want to update some data property in it. I read SPARQL Update in enter link description here and I found that with SPARQL Update (Insert/Update) I can update RDF triple based on Pellet reasoner. but I'm in doubt that it is useful fo data property.
For example I have a person class with datapropery has-age "30" and has-age-category="child"
I create this SPARQL query and I don't have result.
"DELETE ?person nn:has-category-age 'child'\n"
"INSERT ?person nn:has-category-age 'adult'\n"
"WHERE {\n"
"?person rdf:type nn:person.\n"
"?person nn:has-age ?age.\n"
"?person nn:has-category-age ?category.\n"
"FILTER ((has-category-age='child') && (?has-age='30'))\n"
"} ";
Thanks a lot
my ontology is:
<?xml version="1.0"?>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-class -->
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-class">
<rdfs:domain rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Classroom"/>
</owl:ObjectProperty>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-time -->
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-time">
<rdfs:domain rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Courses"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Time"/>
</owl:ObjectProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-age -->
<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-age">
<rdfs:domain rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/>
</owl:DatatypeProperty>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-category-age -->
<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#has-category-age">
<rdfs:domain rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
</owl:DatatypeProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Classroom -->
<owl:Class rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Classroom"/>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Courses -->
<owl:Class rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Courses"/>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person -->
<owl:Class rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Time -->
<owl:Class rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Time"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Jihed -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Jihed">
<rdf:type rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<has-age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">30</has-age>
<has-category-age rdf:datatype="http://www.w3.org/2001/XMLSchema#string">child</has-category-age>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Joseph -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Joseph">
<rdf:type rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<has-age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">10</has-age>
<has-category-age rdf:datatype="http://www.w3.org/2001/XMLSchema#string">child</has-category-age>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Ralph -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Ralph">
<rdf:type rdf:resource="http://www.semanticweb.org/hp/ontologies/2017/9/untitled-ontology-563#Person"/>
<has-age rdf:datatype="http://www.w3.org/2001/XMLSchema#integer">25</has-age>
<has-category-age rdf:datatype="http://www.w3.org/2001/XMLSchema#string">adult</has-category-age>
</owl:NamedIndividual>

Formatting of your question always helps other to read the question.
Which triple store /SPARQL engine do you use?
Without seeing the data, how can we check whether the WHERE part is correct and matches some data?
Debugging of SPARQL Update queries is obviously possible by first checking whether the WHERE part matches any result - this can indeed be done by using a SELECT query.
From your SPARQL query I can see obvious errors in the WHERE part:
WHERE {
?person rdf:type nn:person .
?person nn:has-age ?ag .
?person nn:has-category-age ?categry .
FILTER ((has-category-age='child') && (?has-age='30'))
}
Triple pattern ?person nn:has-category-age ?categry . and the FILTER is (has-category-age='child'). Why? You have to filter based on the variable value, not using the predicate again
Triple pattern ?person nn:has-age ?ag . and the FILTER is (?has-age='30'). The same problem as before...in addition, if the datatype of the age is xsd:integer, you can't use '30', because inside quotes it would be a string. Either use 30 or "30"^^xsd:integer
Please please check your variable names next time, it's horrible to see typos there. ?ag -> ?age and ?categry -> ?category
Fixed WHERE part (I'm assuming the age is an integer value):
WHERE {
?person rdf:type nn:person .
?person nn:has-age ?age .
?person nn:has-category-age ?category .
FILTER ((?category='child') && (?age=30))
}

Related

get Literal using the object property Sparql Jena

I have two sparql queries:
public static String query2
= "PREFIX diag: <file:/D:/onto/owl_ontologies/diagnostic1.owl#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX owl:<http://www.w3.org/2002/07/owl#>"
+ "SELECT ?disease ?symptom"
+ "WHERE { ?disease diag:hasSymptom ?symptom}";
String moreSymptomsQuery
= "PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#> "
+ "SELECT ?symptom"
+ "WHERE {\"hyperglycemia\" diag:hasSymptom ?symptom}";
and this is a part of my OWL file
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:diag="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#">
<owl:Ontology rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
<owl:ObjectProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hasSymptom">
<rdfs:range rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<!--this is an individual "desease" hasSymptom "Symptom" -->
<diag:Desease rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia">
<diag:hasSymptom>hypergammaglobulinemia</diag:hasSymptom>
<diag:DesId>DES:000004</diag:DesId>
<diag:DesLabel>hyperglycemia</diag:DesLabel>
</diag:Desease>
and this is my code in Jena:
public void SelectQuesry() {
Query query = QueryFactory.create(queryName);
QueryExecution executeQuery = QueryExecutionFactory.create(query, modelDiag);
org.apache.jena.query.ResultSet res = executeQuery.execSelect();
while (res.hasNext()) {
QuerySolution qs = res.nextSolution();
Literal symp = qs.getLiteral("symptom");
System.out.println(symp.toString());
}
}
the first query gives the results, and no result by the seconde!!
I want to get the symptoms of each disease... this is important ... thanks for help.
A literal is never the subject of an RDF triple which in fact means that the triple pattern
"hyperglycemia" diag:hasSymptom ?symptom
in your second query doesn't match any data.
You have to use the URI of the RDF resource, i.e.http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia, as the subject of the triple pattern:
PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#>
SELECT ?symptom
WHERE {
diag:hyperglycemia diag:hasSymptom ?symptom
}
As a comment (I think I already told you last time): have a look at your data by using N-Triples syntax instead of RDF/XML. This directly reflects the patterns in the SPARQL query.
Moreover, in your data everything is a string literal except the diseases. Not sure why, but if you're really modeling an ontology then I would also use RDF resources for the symptoms - at least when you want to make some additional statements about the symptom.

How to display specific individual from ontology using sparql query?

Actually I want to display 3 individuals from the same type of class and also their datatype. I have try some code but still it doesn't work. Where it could be wrong?
Here,the code in my jsp file
<%
model=FileManager.get().loadModel("data.rdf");
out.print("<tr><th scope='row'><b>Sarapan Pagi</b></th>");
out.print("<th scope='row'>");
queryString=
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax=ns#>"+
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" +
"prefix owl: <http://www.w3.org/2002/07/owl#>" +
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>" +
"prefix ab: <http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#>" +
"SELECT ?x " +
"WHERE{"+
" ?x rdf:type ab:KumpBijirin." +
" FILTER (? x= :BiskutKrimKraker)." +
"}";
query=QueryFactory.create(queryString);
qexec=QueryExecutionFactory.create(query,model);
try{
ResultSet results=qexec.execSelect();
while (results.hasNext()){
QuerySolution soln=results.nextSolution();
Literal name=soln.getLiteral("x");
out.print(name);
}
}finally{
qexec.close();
}out.print("</th></tr>"); %>
This is some of my data.rdf file.
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#AcarTimun">
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanMalam"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanTengahHari"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#KumpSayur"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#MenuHidanganSampingan"/>
<hasKalori rdf:datatype="http://www.w3.org/2001/XMLSchema#float">33.3</hasKalori>
<hasKarbohidrat rdf:datatype="http://www.w3.org/2001/XMLSchema#float">7.7</hasKarbohidrat>
<hasLemak rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.2</hasLemak>
<hasProtein rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.7</hasProtein>
<hasSaizHidangan rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1hidangan</hasSaizHidangan>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#AirKosong -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#AirKosong">
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanMalam"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanTengahHari"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMinumMalam"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMinumPagi"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#MenuMinuman"/>
<hasKalori rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.0</hasKalori>
<hasKarbohidrat rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.0</hasKarbohidrat>
<hasLemak rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.0</hasLemak>
<hasProtein rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.0</hasProtein>
<hasSaizHidangan rdf:datatype="http://www.w3.org/2001/XMLSchema#float">1gelas</hasSaizHidangan>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#Anggur -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#Anggur">
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanMalam"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanTengahHari"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#KumpBuah"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#MenuPencuciMulut"/>
<hasKalori rdf:datatype="http://www.w3.org/2001/XMLSchema#float">63.2</hasKalori>
<hasKarbohidrat rdf:datatype="http://www.w3.org/2001/XMLSchema#float">15.0</hasKarbohidrat>
<hasLemak rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.0</hasLemak>
<hasProtein rdf:datatype="http://www.w3.org/2001/XMLSchema#float">0.8</hasProtein>
<hasSaizHidangan rdf:datatype="http://www.w3.org/2001/XMLSchema#int">8biji</hasSaizHidangan>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#AsamLaksaPenang -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#AsamLaksaPenang">
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanMalam"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#JenisMakanTengahHari"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#KumpBijirin"/>
<rdf:type rdf:resource="http://www.semanticweb.org/user/ontologies/2016/3/untitled-ontology-23#MenuHidanganUtama"/>
<hasKalori rdf:datatype="http://www.w3.org/2001/XMLSchema#float">432.0</hasKalori>
<hasKarbohidrat rdf:datatype="http://www.w3.org/2001/XMLSchema#float">53.0</hasKarbohidrat>
<hasLemak rdf:datatype="http://www.w3.org/2001/XMLSchema#float">2.0</hasLemak>
<hasProtein rdf:datatype="http://www.w3.org/2001/XMLSchema#float">21.1</hasProtein>
<hasSaizHidangan rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1mangkuk</hasSaizHidangan>
</owl:NamedIndividual>

How to list all elements of the selected enumerations with the use of SPARQL?

The following is a subquestion to my previous question: available here.
How to modify the following SPARQL query:
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?dt ?element ?elementType {
?dt a rdfs:Datatype ;
owl:oneOf/rdf:rest*/rdf:first ?element .
bind(datatype(?element) as ?elementType)
}
in order to to get a result of only A and C? I would like to obtain { "a1" "a2" "c1" "c2" }. The above query returns all enumeration values from the ontology, I mean: { "a1" "a2" "b1" "b2" "c1" "c2" }
We are given the ontology (A and B are equivalent but presented in different style syntax):
Variant A) in the functional style syntax:
Prefix(ont:=<http://www/ont.owl#>)
Prefix(owl:=<http://www.w3.org/2002/07/owl#>)
Prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
Prefix(xsd:=<http://www.w3.org/2001/XMLSchema#>)
Prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>)
Ontology(<http://www/ont.owl>
DatatypeDefinition( ont:A DataOneOf( "a1" "a2" ) )
DatatypeDefinition( ont:B DataOneOf( "b1" "b2" ) )
DatatypeDefinition( ont:C DataOneOf( "c1" "c2" ) )
)
Variant B) in the RDF/XML style syntax:
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY ont "http://www/ont.owl#" >
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY xml "http://www.w3.org/XML/1998/namespace" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www/ont.owl#"
xml:base="http://www/ont.owl"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:ont="http://www/ont.owl#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace">
<owl:Ontology rdf:about="http://www/ont.owl"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Datatypes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www/ont.owl#A -->
<rdfs:Datatype rdf:about="&ont;A">
<owl:equivalentClass>
<rdfs:Datatype>
<owl:oneOf>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>a1</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>a2</rdf:first>
<rdf:rest rdf:resource="&rdf;nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</owl:oneOf>
</rdfs:Datatype>
</owl:equivalentClass>
</rdfs:Datatype>
<!-- http://www/ont.owl#B -->
<rdfs:Datatype rdf:about="&ont;B">
<owl:equivalentClass>
<rdfs:Datatype>
<owl:oneOf>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>b1</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>b2</rdf:first>
<rdf:rest rdf:resource="&rdf;nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</owl:oneOf>
</rdfs:Datatype>
</owl:equivalentClass>
</rdfs:Datatype>
<!-- http://www/ont.owl#C -->
<rdfs:Datatype rdf:about="&ont;C">
<owl:equivalentClass>
<rdfs:Datatype>
<owl:oneOf>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>c1</rdf:first>
<rdf:rest>
<rdf:Description>
<rdf:type rdf:resource="&rdf;List"/>
<rdf:first>c2</rdf:first>
<rdf:rest rdf:resource="&rdf;nil"/>
</rdf:Description>
</rdf:rest>
</rdf:Description>
</owl:oneOf>
</rdfs:Datatype>
</owl:equivalentClass>
</rdfs:Datatype>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->
As I said in the comments on the previous answer:
#Annabelle I was basing the retrieval methods on the ontology that I provided. There are certainly other ways to select the data types if they're identified by IRIs. In your case, it looks like it would be values ?dt {:A :B } if you only want ?dt to be A or B.
and
In this case, note that the axiom is encoded by saying that :A is owl:equivalentClass to the datatype class expression. That's the extra link you need between the class IRI and the expression.
That gives us:
select ?dt ?element ?elementType {
values ?dt { ont:A ont:B }
?dt owl:equivalentClass/a rdfs:Datatype ;
owl:oneOf/rdf:rest*/rdf:first ?element .
bind(datatype(?element) as ?elementType)
}
This really isn't much different from the previous answer. You just need to add on the specific values that you're looking for, and then add the equivalentClass link.

Executing a SPARQL query

I have created a small ontology. It has only one class called methods and a datatype property action_nb (integer).
Then I created two individuals :
AHP => action_nb = 20
electre => action_nb = 50
I want to execute a simple query . Select method where nb_action < 40 and the results should give me EL1.
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<!ENTITY untitled-ontology-77 "http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#"
xml:base="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:untitled-ontology-77="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#action_nb -->
<owl:DatatypeProperty rdf:about="&untitled-ontology-77;action_nb">
<rdfs:domain rdf:resource="&untitled-ontology-77;methods"/>
<rdfs:range rdf:resource="&xsd;integer"/>
</owl:DatatypeProperty>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#methods -->
<owl:Class rdf:about="&untitled-ontology-77;methods"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#ahp -->
<owl:NamedIndividual rdf:about="&untitled-ontology-77;ahp">
<rdf:type rdf:resource="&untitled-ontology-77;methods"/>
<action_nb rdf:datatype="&xsd;integer">20</action_nb>
</owl:NamedIndividual>
<!-- http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#electre -->
<owl:NamedIndividual rdf:about="&untitled-ontology-77;electre">
<rdf:type rdf:resource="&untitled-ontology-77;methods"/>
<action_nb rdf:datatype="&xsd;integer">50</action_nb>
</owl:NamedIndividual>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->
PREFIX ex: <http://example.org/so#>
SELECT ?methods
WHERE {
?inst rdf:type ex:methods .
?inst ex:action_nb ?value .
FILTER (?value < 40)
}
this is the code that I have been manipulating: but it gave me only an empty row when filtering with 40 & 2 rows when using 60
The key to understanding how to approach this is to understand how RDF triples are defined. You've stated that action_nb is the property whose values are integers that you want to compare. Therefore create your query using that property:
PREFIX untitled-ontology-77: <http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#>
SELECT ?inst
WHERE {
?inst untitled-ontology-77:action_nb ?value .
FILTER (?value < 40)
}
...where the FILTER clause allows only values of action_nb that are less than 40.
If the property is used in other classes, and you want to make sure that only members of methods are found, then you will also need to query for instances of that class:
PREFIX untitled-ontology-77: <http://www.semanticweb.org/diabolico/ontologies/2016/3/untitled-ontology-77#>
SELECT ?inst
WHERE {
?inst rdf:type untitled-ontology-77:methods .
?inst untitled-ontology-77:action_nb ?value .
FILTER (?value < 40)
}
I doubt you'll need DISTINCT in this case, and be aware that finding all distinct values is performed after the query, but is applied pairwise to all SELECTED values. I.e. it may not perform well with large result sets. Therefore, use DISTINCT only when necessary.

Issues with Jena OWL reasoner

I am new to Jena but now encountered with some problems. I have a superclass Query Processing and its four subclasses:
Query Processing
— Query intent
— Query reformulation
— Query representation
— Query suggestion
When I use:
PREFIX Article: <http://www.semanticweb.org/aero/ontologies/computer-science#>
SELECT ?paper ?category
WHERE
{
?paper Article:inQueryIntent ?category .
}
Or with any the other 3 subclasses as the predicate like inQueryReformulation I can get the correct answer.
However when I used ?article Article:inQueryProcessing ?category which should return all the individuals related to the sub-classes QueryProcessing I got nothing. inQueryIntent (and the other 3) and inQueryProcessing are the object properties.
Here is my code:
Dataset dataset = TDBFactory.createDataset(directory);
Model model = dataset.getNamedModel(modelName);
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, model);
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
InfModel infModel = ModelFactory.createInfModel(reasoner, ontModel);
Query query = QueryFactory.create(queryString);
QueryExecution queryExe = QueryExecutionFactory.create(query, infModel);
here is the ontology snippet :
object properties:
<!-- http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#inQueryIntent -->
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#inQueryIntent">
<rdf:type rdf:resource="&owl;FunctionalProperty"/>
<rdfs:range rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Query_intent"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#article"/>
<rdfs:subPropertyOf rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#inInformationRetrievalQueryProcessing"/>
</owl:ObjectProperty>
class:
<owl:Class rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Query_intent">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Information_retrieval_query_processing"/>
</owl:Class>
individual:
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Classifying_Web_Queries_by_Topic_and_User_Intent">
<rdf:type rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#article"/>
<hasAuthor rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Bernard_J._Jansen"/>
<inQueryIntent rdf:resource="http://www.semanticweb.org/aero/ontologies/2013/1/computer-science#Query_intent"/>
</owl:NamedIndividual>
I just build a domain ontology in ComputerScience, all the classes are concepts (like query processing is a superclass and query intent/reformulation/… are subclasses) in hierarchy and, all the papers and their corresponding authors are the individuals