sparql - how to query through multiple entities? - sparql

I have a rdf-schema and the triples in the turtle syntax (extract):
#prefix dbr: <http://dbpedia.org/resource/> .
#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#> .
#own, TODO change it
#prefix japany: <http://vschuberth.bplaced.net/rdfschema.ttl#> .
japany:import a rdfs:Class.
japany:food a rdfs:Class.
japany:radioactiveIngredients a rdfs:Class.
japany:imports a rdf:Property;
rdfs:range dbr:PopulatedPlace;
rdfs:domain japany:import.
japany:exports a rdf:Property;
rdfs:range dbr:PopulatedPlace;
rdfs:domain japany:import.
japany:importQuantity a rdf:Property;
rdfs:range xsd:integer;
rdfs:domain japany:import.
japany:importValue a rdf:Property;
rdfs:range xsd:integer;
rdfs:domain japany:import.
japany:year a rdf:Property;
rdfs:range xsd:gYear;
rdfs:domain japany:import.
japany:isPartOf a rdf:Property;
rdfs:range japany:import;
rdfs:domain japany:food.
japany:nameOfIng a rdf:Property;
rdfs:range rdf:Class;
rdfs:domain japany:radioactiveIngredients.
japany:amount a rdf:Property;
rdfs:range japany:bqkg;
rdfs:domain japany:radioactiveIngredients.
japany:nameOfFood a rdf:Property;
rdfs:range rdf:Class;
rdfs:domain japany:food.
japany:dateOfSampling a rdf:Property;
rdfs:range xsd:date;
rdfs:domain japany:radioactiveIngredients.
japany:isIn a rdf:Property;
rdfs:range xsd:food;
rdfs:domain japany:radioactiveIngredients.
<#Import_of_Olivenöl>
japany:imports dbr:Germany;
japany:exports dbr:Japan;
japany:importQuantity 0;
japany:importValue 36;
japany:year "2011"^^xsd:gYear.
<#Olivenöl_Class>
japany:isPartOf <#Import_of_Olivenöl>.
<#Iodine131_in_Olivenöl>
japany:nameOfIng "Iodine131";
japany:amount "< 2,3";
japany:nameOfFood "Olivenöl";
japany:dateOfSampling "15.04.2011"^^xsd:date;
japany:isIn <#Import_of_Olivenöl>.
<#Cesium134_in_Olivenöl>
japany:nameOfIng "Caesium134";
japany:amount "< 1,54";
japany:nameOfFood "Olivenöl";
japany:dateOfSampling "15.04.2011"^^xsd:date;
japany:isIn <#Import_of_Olivenöl>.
<#Cesium137_in_Olivenöl>
japany:nameOfIng "Caesium137";
japany:amount "< 1,95";
japany:nameOfFood "Olivenöl";
japany:dateOfSampling "15.04.2011"^^xsd:date;
japany:isIn <#Import_of_Olivenöl>.
i'm confused how to query through all those entities. they are related to each other. for example: i want to write a query/ queries to get the importValue and importQuantity of each food per year and also list the containing amount of each radioactiveIngredient.
this query will give me incorrect data:
PREFIX japany: <http://vschuberth.bplaced.net/rdfschema.ttl#>
SELECT ?importValue ?importQuantity ?name ?year
WHERE {
?trade japany:importValue ?importValue;
japany:year ?year;
japany:importQuantity ?importQuantity.
?food japany:nameOfFood ?name.
}
ORDER BY ?importQuantity
can anybody help?

A problem is ?food and japany:import are not linked in your query.
Here is a working query :
PREFIX japany: <http://vschuberth.bplaced.net/rdfschema.ttl#>
SELECT ?importValue ?importQuantity ?name ?year
WHERE {
?food japany:isPartOf ?import .
?import japany:importValue ?importValue ;
japany:importQuantity ?importQuantity ;
japany:year ?year .
?food japany:nameOfFood ?name.
}
ORDER BY ?importQuantity
But this is creepy :
japany:nameOfFood a rdf:Property;
rdfs:range rdf:Class.
Why don't you create it as a DatatypeProperty and therefore store a Literal?

Related

How do I deduce all of a person's descendants?

Here is my example.owl:
#prefix : <#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#base <http://ex.org> .
<http://ex.org> rdf:type owl:Ontology .
:hasChild rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf :hasDescendant .
:hasDaughter rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf :hasChild ;
owl:propertyDisjointWith :hasSon .
:hasDescendant rdf:type owl:ObjectProperty .
:hasSon rdf:type owl:ObjectProperty ;
rdfs:subPropertyOf :hasChild .
:Person rdf:type owl:Class .
:Bob rdf:type owl:NamedIndividual , :Person ;
:hasDaughter :Mary ;
:hasSon :John .
:LittleBoy rdf:type owl:NamedIndividual , :Person .
:LittleGirl rdf:type owl:NamedIndividual , :Person .
:John rdf:type owl:NamedIndividual , :Person ;
:hasSon :LittleBoy .
:Mary rdf:type owl:NamedIndividual , :Person ;
:hasDaughter :LittleGirl .
I want to get all of a Bob's descendants or Bob's child by a Sparql query,
How do I write this SPARQL statement?
Thanks for help.

How can I have my SPARQL query results in Turtle format in Apache Jena with commandline interface?

I have a command
./bin/arq --data ./bin/dbpedia_2015-10.nt --query ./bin/166.rq
which works perfect and I can see my result in my command line interface. I would like to use jena RIOT to have my result in the file.ttl . But as I am not familiar with commandline and linux I don't know how to do it. is there any suggestion.
I dont want to use the dbpedia datasets niether I just need the results from my sparql query.
this is my code `
select DISTINCT ?instance ?domain ?range ?subClassOf #
where {
?instance rdf:type ?type;
rdfs:domain ?domain;
rdfs:range ?range;
rdfs:subClassOf* ?subClassOf.
}
`
' but I have the error when change it to construct '
construct { DISTINCT ?instance ?domain ?range ?subClassOf.}
where {
?instance rdf:type ?type;
rdfs:domain ?domain;
rdfs:range ?range;
rdfs:subClassOf* ?subClassOf.
}
It will be in Turtle syntax once you use a CONSTRUCT or DESCRIBE query.
As you say your aim is to produce Turtle output and Turtle is an RDF serialisation format you need to CONSTRUCT RDF triples. e.g.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT { ?instance rdf:type ?type;
rdfs:domain ?domain ;
rdfs:range ?range;
rdfs:subClassOf ?subClassOf .
}
where {
?instance rdf:type ?type;
rdfs:domain ?domain;
rdfs:range ?range;
rdfs:subClassOf* ?subClassOf.
}

sparql query: get several values from a List

My query returns result dependent on which one of two sparql OPTIONS clauses come first for a List of Images. Although using Jena ARQ is not an option at this point, and I'd like to solve this with a pure SPARQL query, still I'd like to know how it could be solved with Jena as well.
My data presentation is attached below, the data may contain a list of images. My image representation is also below. I'm attaching my query as well.
The sprql query has two variables urlX, and urlY declared in the 2 OPTIONS blocks, if a List of images exists. Depending on which of the OPTIONS comes first, I get the value for that one variable, while the other one doesn't get reached. It seems the issue has to do with using OPTIONS clause. I'm not sure what else I can try instead, I'm far from being an expert on sparql queries. I want the query to do the following: if a collection of images is present, I want to see if both image sizes (dc:conformsTo) are present and get both urlX and urlY values, or get the ones that exist. Much appreciate your time.
My data representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://lews.com/content/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331340 lews:name "the Human Good Luck Charm is Back"^^xsd:token ;
dc:created "2014-10-20T17:14:55.357-07:00"^^xsd:dateTime ;
dc:identifier "26331340"^^xsd:int ;
dc:modified "2016-08-04T13:43:00.897-07:00"^^xsd:dateTime ;
dc:title "the Human Good Luck Charm is Back" ;
dc:hasPart <http://lews.com/content/26331340#Images> ;
dc:abstract "As the World Series gets underway..." ;
dc:description "The super fan who rooted for the Royals is back to boost morale." ;
dc:subject "hoping for a World Series victory".
<http://lews.com/content/26331340#Images> dc:identifier "Images"^^xsd:token ;
rdf:first lews:26331375 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 ;
rdf:li <http://lews.com/content/26331340#Images> , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 , _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 ;
a rdf:List .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331376 rdf:first lews:26331376 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331377 rdf:first lews:26331377 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331378 rdf:first lews:26331378 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331379 rdf:first lews:26331379 ;
rdf:rest _:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 .
_:genid-15e530b0195547d9ac3f8e5e6785a747-x26331340Images26331380 rdf:first lews:26331380 ;
rdf:rest rdf:nil .
My image representation:
#prefix dc: <http://purl.org/dc/terms/> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix lews: <http://abcnews.com/content/> .
#prefix mrss: <http://search.yahoo.com/mrss/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
lews:26331376 lews:name "141020_wn_donvan0_704x396.jpg"^^xsd:token ;
lews:section "wnt"^^xsd:token ;
lews:type "Image"^^xsd:token ;
dc:conformsTo "704x396"^^xsd:token ;
dc:created "2014-10-20T17:15:09.637-07:00"^^xsd:dateTime ;
dc:hasFormat <http://lews.go.com/images/WNT/141020_wn_donvan0_704x396.jpg> ;
dc:identifier "26331376"^^xsd:int ;
dc:isPartOf <http://lews.go.com/WNT> ;
dc:modified "2014-10-20T17:15:09.947-07:00"^^xsd:dateTime ;
dc:type "StillImage"^^xsd:token ;
mrss:height "396"^^xsd:int ;
mrss:width "704"^^xsd:int ;
xsd:date "2014-10-20"^^xsd:date ;
xsd:gMonthDay "--10-20"^^xsd:gMonthDay ;
xsd:gYear "2014"^^xsd:gYear ;
xsd:gYearMonth "2014-10"^^xsd:gYearMonth .
My query:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName
?subject dc:identifier ?id.
OPTIONAL {?subject dc:title ?title.}
OPTIONAL {?subject dc:description ?description.}
OPTIONAL {?subject dc:isPartOf ?section.}
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL{
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
LIMIT ${limit}
If I understood correctly what it is you want, you just need to group the optionals differently:
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX mrss: <http://search.yahoo.com/mrss/>
PREFIX search: <http://www.openrdf.org/contrib/lucenesail#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX lews: <http://abcnews.com/content/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?title ?description ?urlX ?urlY ?section ?imgName {
?subject dc:identifier ?id.
OPTIONAL { ?subject dc:title ?title. }
OPTIONAL { ?subject dc:description ?description. }
OPTIONAL { ?subject dc:isPartOf ?section. }
OPTIONAL {
?subject dc:hasPart ?imageCol.
?imageCol dc:identifier "Images"^^xsd:token.
OPTIONAL {
?imageCol rdf:li ?bnode.
?bnode rdf:first ?image.
?image lews:name ?imgName;
# Here we optionally bind ?urlX and/or ?urlY
OPTIONAL {
?image dc:conformsTo "4x3";
dc:hasFormat ?urlX.
}
OPTIONAL {
?image dc:conformsTo "16x9";
dc:hasFormat ?urlY.
}
}
}
}

owl: property domain restriction on property value

I am studying the inference in OWL, currently the restriction in domain definition:
#prefix : <http://www.test.org/2015/4/ontology#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#base <http://www.test.org/2015/4/ontology> .
<http://www.test.org/2015/4/ontology> rdf:type owl:Ontology .
:Class1 rdf:type owl:Class .
:Prop1 rdf:type owl:DatatypeProperty ;
rdfs:domain [ rdf:type owl:Class ;
owl:intersectionOf ( :Class1
[ rdf:type owl:Restriction ;
owl:onProperty :Prop1 ;
owl:hasValue "class1"
]
)
] .
:Ind1 rdf:type owl:NamedIndividual ;
:Prop1 "p" .
I've expected that the reasoner (Pellet) infers
:Ind1 rdf:type :Class1
only if there is
:Ind1 :Prop1 "class1"
but it seems to ignore the restriction in the domain definition.
Is it correct to define restrictions in damain definitions? The reasoner (Pellet) does not forbid me to do that.

Fuseki 1.0.1 SPARQL Update returns 404

I'm trying to learn to update data in Fuseki, but when I try I get a 404 error. I am clearly not doing something right. Perhaps it is my INSERT command? I've tried a ton of them though.
I am using the web based SPARQL interface at /sparql.tpl.
I can get SPARQL Queries to work on that same page just fine. But the second form, labeled SPARQL Updates is what I'm using for my update, and that gives me errors:
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX booklet: <http://www.semanticweb.org/cstepnitz/ontologies/booklet>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ontology: <http://dbpedia.org/ontology/>
INSERT DATA
{ <http://dbpedia.org/resource/Johnny_Got_His_Gun>
rdf:type ontology:Book
}
This is the error I get when the form posts to the url localhost:3030/bookfinder/update:
Error 404: Not Found
Fuseki - version 1.0.1 (Build date: 2014-01-18T19:01:20+0000)
This is my TTL file.
#prefix : <http://www.semanticweb.org/cstepnitz/ontologies/bookreader#> .
#prefix bibo: <http://purl.org/ontology/bibo/> .
#prefix booklet: <http://www.semanticweb.org/cstepnitz/ontologies/booklet> .
#prefix dbp: <http://dbpedia.org/ontology/> .
#prefix dbpedia: <http://dbpedia.org/ontology/> .
#prefix dbpedia-owl: <http://dbpedia.org/ontology/> .
#prefix NS5: <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#> .
#prefix rdms: <http://en.wikipedia.org/wiki/Comparison_of_relational_database_management_systems> .
#prefix owlapi: <http://www.semanticweb.org/owlapi#> .
#prefix schema: <http://schema.org/> .
#base <http://www.semanticweb.org/cstepnitz/ontologies/bookreader> .
<http://www.semanticweb.org/cstepnitz/ontologies/bookreader> NS5:type owl:Ontology ;
owl:imports dbp: .
booklet:readbook NS5:type owl:ObjectProperty ;
rdfs:range dbpedia-owl:Book ;
rdfs:domain booklet:Reading ;
rdfs:subPropertyOf owl:topObjectProperty .
booklet:reading NS5:type owl:ObjectProperty ;
rdfs:domain booklet:Bookreader ;
rdfs:range booklet:Reading .
:OWLObjectProperty_18e83c77_09cb_4d9b_90c6_93b2a5095d78 NS5:type owl:ObjectProperty ;
rdfs:label "recommendedBooks"#en ;
rdfs:range dbpedia-owl:Book ;
rdfs:domain booklet:Bookreader .
:OWLObjectProperty_86497b97_bef7_4b9e_80bc_6080ce0cbfe3 NS5:type owl:ObjectProperty ;
rdfs:label "bookrating"#en ;
rdfs:range booklet:Bookrating ;
rdfs:domain booklet:Reading ;
rdfs:subPropertyOf owl:topObjectProperty .
dbpedia-owl:Book NS5:type owl:Class .
schema:Person NS5:type owl:Class .
booklet:Bookrating NS5:type owl:Class .
booklet:Bookreader NS5:type owl:Class ;
rdfs:subClassOf schema:Person .
booklet:Reading NS5:type owl:Class .
:OWLClass_38c67bca_82ba_44b7_85dd_31d0c2883702 NS5:type owl:Class ;
rdfs:label "Poor"#en ;
rdfs:subClassOf booklet:Bookrating .
:OWLClass_5630d470_6dda_4cd5_9596_ddf0eab29cde NS5:type owl:Class ;
rdfs:label "Best"#en ;
rdfs:subClassOf booklet:Bookrating .
:OWLClass_694d8a9e_5fba_45a2_81b3_f47f1a21af4d NS5:type owl:Class ;
rdfs:label "Average"#en ;
rdfs:subClassOf booklet:Bookrating .
:OWLClass_dfb73eb5_8a7c_4628_a423_72056e7ee81f NS5:type owl:Class ;
rdfs:label "Good"#en ;
rdfs:subClassOf booklet:Bookrating .
:OWLClass_f3923184_fbb7_48ec_954e_49e233454099 NS5:type owl:Class ;
rdfs:label "Awful"#en ;
rdfs:subClassOf booklet:Bookrating .
Any guesses?
As explained in the comments on my question, the problem was the "--update" flag needed to be added at startup. See http://jena.apache.org/documentation/serving_data/