SPARQL how to select number of all classes and instance without distinct? - sparql

I have problem to select data by using sparql query.
I made .owl file that structure is like as below:
Superclass
- subclassA - instance1
- subclassB
- subclass1 - instance1
- subclass2
-subclass1 - instance1
- subclassC
- subclass1 - instance1
- subclass2 - instance1
then i want to make result like:
+------------+-----------+-----------+-----------+-----------+
| Class | sub1 | sub2 | sub3 | instance |
+------------+-----------+-----------+-----------+-----------+
| Superclass | subclassA | | | instance1 |
| Superclass | subclassB | subclass1 | | instance1 |
| Superclass | subclassB | subclass2 | subclass1 | instance1 |
| Superclass | subclassC | | | instance1 |
| Superclass | subclassC | | | instance2 |
+------------+-----------+-----------+-----------+-----------+\
I want to show the results, including instance value, from Class to leafclass.
so, I tried to make query like
SELECT ?Class ?sub1 ?sub2 ?sub3 ?instance
WHERE{
?instance rdf:type ?Class.
?instance tdf:type ?sub1.
?sub1 rdfs:subClassOf ?Class.
}
but, I have no idea. How do i fix it?

Related

Column result unexpectedly returning nothing

Have a SPARQL query that returns nothing for Airport_Name when I believe it should.
PREFIX nas: <https://data.nasa.gov/ontologies/atmonto/NAS#>
PREFIX gen: <https://data.nasa.gov/ontologies/atmonto/general#>
SELECT *
WHERE{
{
{SELECT ?Airport_Name{
?Airport rdf:type nas:Airport ;
nas:airportName ?Airport_Name .
}
}
}UNION{
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
}
?Location rdf:type gen:PointLocation;
gen:longitude ?X ;
gen:latitude ?Y .
FILTER (?X > -80 && ?X < -64 && ?Y > 18 && ?Y < 32)
}
Expected result should return Location, X, Y, and Airport_Name
When I try to return Airport_Name at the bottom, the Location columns then appear blank, so im not sure what has been done wrongly. If anyone could point me in the right direction, that would be much appreciated.
Your query has several issues, the correct query would be something like below.
Query
SELECT *
WHERE
{ ?airport rdf:type/rdfs:subClassOf* nas:Airport ;
nas:airportName ?Airport_Name ;
nas:airportLocation ?location .
?location gen:longitude ?X ;
gen:latitude ?Y
FILTER ( ?X > "-80"^^xsd:float && ?X < "-64"^^xsd:float && ?Y > "18"^^xsd:float && ?Y < "32"^^xsd:float )
}
Result (sample with LIMIT 10)
--------------------------------------------------------------------------------------------------------------------------
| airport | Airport_Name | location | X | Y |
==========================================================================================================================
| nas:MYEMairport | "Governors Harbour" | nas:MYEMcoordinates | "-76.330178"^^xsd:float | "25.283586"^^xsd:float |
| nas:MDPCairport | "Punta Cana Intl" | nas:MDPCcoordinates | "-68.366186"^^xsd:float | "18.570781"^^xsd:float |
| nas:MUGTairport | "Mariana Grajales" | nas:MUGTcoordinates | "-75.158333"^^xsd:float | "20.085278"^^xsd:float |
| nas:MDSTairport | "Cibao Intl" | nas:MDSTcoordinates | "-70.604689"^^xsd:float | "19.406092"^^xsd:float |
| nas:MYLSairport | "Stella Maris" | nas:MYLScoordinates | "-75.268778"^^xsd:float | "23.583047"^^xsd:float |
| nas:MBNCairport | "North Caicos" | nas:MBNCcoordinates | "-71.939658"^^xsd:float | "21.917486"^^xsd:float |
| nas:MUMOairport | "Orestes Acosta" | nas:MUMOcoordinates | "-74.922222"^^xsd:float | "20.653889"^^xsd:float |
| nas:MYRPairport | "New Port Nelson" | nas:MYRPcoordinates | "-74.836186"^^xsd:float | "23.684378"^^xsd:float |
| nas:MYEGairport | "George Town" | nas:MYEGcoordinates | "-75.781670"^^xsd:float | "23.466667"^^xsd:float |
| nas:MUBYairport | "Carlos Manuel De Cespedes" | nas:MUBYcoordinates | "-76.621389"^^xsd:float | "20.396389"^^xsd:float |
--------------------------------------------------------------------------------------------------------------------------
Note, the query uses the property path rdf:type/rdfs:subClassOf* which only works if you've also loaded the ontology (NAS.ttl). The property path is necessary because the instance data (airportInst.ttl) uses subclasses of nas:Airport like nas:InternationAirport or nas:CanadianAirport.

Get all Countries Name from DBpedia using SPARQL

I want list of countries name from DBpedia.
I am using http://dbpedia.org/snorql/ to execute my query, but till now I have not found all countries name which are available in DBpedia.
For Example : dbr:United_Kingdom, dbr:India, dbr:United_States, etc.
Is the problem
You don't know how to write SPARQL in general? (That's OK, it's hard to get started.)
You don' know about the classes and predicates in DBpedia? (That's OK too. I have to check each time.)
Something else?
This gets all UN member nations. Getting "all countries" is probably just a matter of finding the right class in their ontology.
select distinct ?s
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> }
I happen to know that New York City is the largest city in the United States, and that DBpedia has a largestCity predicate and a New_York_City instance. So I wrote a query that should only get the United States as the subject, and then asked for all connected predicates and objects. You should look in that for an object that meets your exceptions for defining "all countries." If you don't find one, you may have to union a few other triple patterns into one query.
I have also filtered out objects that contain either of two terms that be relevant for you: "country" or "nation"
select distinct *
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> ;
dbo:largestCity dbr:New_York_City ;
?p ?o
filter(isURI(?o))
filter((regex(lcase(str(?o)), "country")) || (regex(lcase(str(?o)), "nation")))
}
Gives the foloowing, which should help you write a followup question that isn't specific to the United States.
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+
| s | p | o |
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://www.ifs.du.edu/ifs/frm_CountryProfile.aspx?Country=US |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://nationalatlas.gov/ |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/class/yago/Country108544813 |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G7_nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://schema.org/Country |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2000/01/rdf-schema#seeAlso | http://dbpedia.org/resource/Anti-miscegenation_laws |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:Member_states_of_the_United_Nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2002/07/owl#sameAs | http://transparency.270a.info/classification/country/US |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/ontology/Country |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G8_nations |
| http://dbpedia.org/resource/United_States | http://www.w3.org/2002/07/owl#sameAs | http://linked-web-apis.fit.cvut.cz/resource/united_states_of_america_country |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://www.nationalcenter.org/HistoricalDocuments.html |
| http://dbpedia.org/resource/United_States | http://dbpedia.org/ontology/wikiPageExternalLink | http://news.bbc.co.uk/2/hi/americas/country_profiles/1217752.stm |
| http://dbpedia.org/resource/United_States | http://www.w3.org/1999/02/22-rdf-syntax-ns#type | http://umbel.org/umbel/rc/Country |
| http://dbpedia.org/resource/United_States | http://purl.org/dc/terms/subject | http://dbpedia.org/resource/Category:G20_nations |
+--------------------------------------------+---------------------------------------------------+------------------------------------------------------------------------------+

Selecting Food Calories from SPARQL

I've started looking into SPARQL and I admit I find it very obscure and difficult.
I need to output the calories for all the foods.
I don't really understand the difference between the wd: wdt: p: and ps:
Similarly I am unclear on when to use P000 or Q000 ?
The best I got so far is:
SELECT ?food ?calories ?foodLabel ?caloriesLabel WHERE {
?food wdt:P31 wd:Q2095.
?food wdt:P31 ?calories.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Which gives : Results
Any suggestion would be welcome
If you are willing to download a 1 GB RDF file and publish it at your own triplestore, this looks promising:
https://datahub.io/dataset/open-food-facts
Specifically, http://en.openfoodfacts.org/data/en.openfoodfacts.org.products.rdf (If English is your preferred languague.)
I say download and publish as Googling suggests these data aren't already exposed at any public endpoint.
You will likely have to use different classes and predicates, compared to WikiData.
Hey, this is pretty neat!
SELECT *
WHERE
{ GRAPH <http://openfoodfacts.org>
{ ?s a <http://data.lirmm.fr/ontologies/food#FoodProduct> ;
<http://data.lirmm.fr/ontologies/food#name> ?fn ;
<http://data.lirmm.fr/ontologies/food#energyPer100g> ?energy
}
}
LIMIT 9
gives
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+
| s | fn | energy |
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+
| http://world-en.openfoodfacts.org/product/9400547030446/heinz-big-n-chunky-chicken-corn | "Heinz Big'n Chunky Chicken & Corn" | "245" |
| http://world-en.openfoodfacts.org/product/9400550004847/sour-patch-kids-pascall | "Sour Patch Kids" | "1440" |
| http://world-en.openfoodfacts.org/product/9400550602487/brunch-mixed-berry-bar-cadbury | "Brunch Mixed Berry Bar" | "1810" |
| http://world-en.openfoodfacts.org/product/9400550646276/pascall-family-pack-sweets-fruit-bursts | "Pascall Family Pack Sweets Fruit Bursts" | "1427" |
| http://world-en.openfoodfacts.org/product/9400553011477/choc-thins-griffin-s | "Choc Thins" | "2010" |
| http://world-en.openfoodfacts.org/product/9400553438786/gingernuts-griffin-s | "Gingernuts" | "1810" |
| http://world-en.openfoodfacts.org/product/9400563448614/nice-natural-rosted-nut-bar-chocolate | "Nice & Natural Rosted Nut Bar - Chocolate" | "2060" |
| http://world-en.openfoodfacts.org/product/9400563740589/nut-bar-caramel-cashew-flavour-nice-natural | "Nut Bar - Caramel Cashew Flavour" | "1960" |
| http://world-en.openfoodfacts.org/product/9400563741784/superfruits-cranberry-blueberry-nice-natural | "Superfruits - Cranberry & Blueberry" | "1520" |
+------------------------------------------------------------------------------------------------------+---------------------------------------------+--------+

Duplicate output results with SPARQL [duplicate]

This question already has an answer here:
Aggregating results from SPARQL query
(1 answer)
Closed 9 years ago.
I'm new to the semantic web concept, and I have a diploma work based on the semantic web. In my OWL ontology, I have defined the individuals in he following manner:
<Announce rdf:ID="Ann1" ...>
<...>
<...>
<requiredTechnologies>
<Technology rdfs:resource="tech1"/>
</requiredTechnologies>
<requiredTechnologies>
<Technology rdfs:resource="tech2"/>
</requiredTechnologies>
</Announce>
...
Basically, there are some properties that appear only once for a given individual, but the property "required technologies" can be used multiple times in one record (for one individual). So, when I run a SPARQL query, selecting all the data (I use Jena), I get this output:
=====================================
| "Ann1" | ... | ... | "tech1" |
| "Ann1" | ... | ... | "tech2" |
| "Ann2" | ... | ... | "tech3" |
| "Ann3" | ... | ... | "tech4" |
| "Ann3" | ... | ... | "tech5" |
| "Ann3" | ... | ... | "tech6" |
| ... | ... | ... | ... |
=====================================
The records where multiple "requiredTechnologies" attribute is present appear more then once. My question is how to get all the technologies that belong to a given individual in one line (something like this):
===================================================
| "Ann1" | ... | ... | "tech1, tech2" |
| "Ann2" | ... | ... | "tech2" |
| "Ann3" | ... | ... | "tech4, tech5, tech6" |
| ... | ... | ... | ... |
===================================================
Is there a way in SPARQL to get the multiple attribute in a list? Or any other workaround?
Yes, use the GROUP BY clause in conjunction with the GROUP_CONCAT aggregate.
Here's a generic example, you should easily be able to adapt to your data model:
SELECT ?s (GROUP_CONCAT(?value ; SEPARATOR = ",") AS ?values)
WHERE
{
?s <http://somePredicate> ?value .
}
GROUP BY ?s

OOP inheritance issue

I have two classes, and the methods in them are shown below;
|----AVL----| |-----RB------|
| | | |
| | | |
| - insert | | -balance |
| | | |
| - balance | | |
| | | |
|-----------| |-------------|
inside "insert" method of AVL, it calls "balance".
RB inherits AVL, so I can use insert method of AVL. Now when I call RB::insert(), it calls AVL::insert() & then AVL::balance(), but I want it to call RB::balance() from AVL::insert(), when a RB object calls "insert".
This is a classic case for virtual methods: make AVL.balance virtual and override it in RB. The correct implementation will then be called depending on what type of object calls balance -- it doesn't matter that the code that calls balance will be written as part of AVL.