ONTOLOGY ENGINEERING Lab #6 – October 6, 2014
The FILTER Keyword Up to this point we’ve restricted the result set of a query by binding a variable to a resource using some object property. For example, the second statement of the following where clause “filters” the result set to roadways in Niagara County: SELECT ?route WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties. } LIMIT 100
The FILTER Keyword The FILTER keyword enables filtering on string and numeric values. The following SELECT query returns roadways that have “bridge” somewhere in the english label. SELECT ?route ?label WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route rdfs:label ?label. FILTER regex(?label, "bridge", "i"). FILTER (lang(?label)= "en"). } LIMIT 100
The FILTER Keyword The following SELECT query returns roadways that have lengths greater than 1000 miles. SELECT ?route ?length WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:lengthMi ?length. FILTER (?length > 1000) } LIMIT 100
The FILTER Keyword The following SELECT query returns public universities located in latitudes between 42 and 43 degrees and in longitudes between -79 and -77 degrees. SELECT ?route ?latitude ?longitude WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route geo:lat ?latitude. ?route geo:long ?longitude. FILTER (?latitude > 42 && ?latitude < 43). FILTER (?longitude > -79 && ?longitude < -77). } LIMIT 100
The FILTER Keyword The following SELECT query returns public companies that have revenues of more than 1 billion (US dollars) or equity of more than 100 billion (US dollars) SELECT ?company ?int_revenue ?int_equity WHERE { ?company dbpedia-owl:type dbpedia:Public_company. ?company dbpedia-owl:revenue ?revenue. ?company dbpedia-owl:equity ?equity. FILTER (datatype(?revenue) = ). BIND(xsd:integer(?revenue) as ?int_revenue). BIND(xsd:integer(?equity) as ?int_equity). FILTER (?int_revenue > || ?int_equity > ) } LIMIT 100
The Filter Keyword The following SELECT query uses the FILTER keyword, logical operator ! and the bound function to return roadways that do not have an associated name of their county SELECT ?route ?county WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. OPTIONAL {?route dbpprop:counties ?county }. FILTER(!bound(?county)). } LIMIT 100
The UNION Keyword Allows you to join multiple queries and return all of the result sets The following query returns roadways located in Niagara or Orleans counties: SELECT ?route WHERE { { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties. } UNION { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties. } 8
The UNION Keyword The part of a union query that is common can be written just once: SELECT ?route WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. {?route dbpprop:counties.} UNION {?route dbpprop:counties.} } 9
GROUP BY Keyword Allows grouping of data together into sets on which aggregate functions can be performed The following query returns the total length roadways by US state: SELECT ?state (SUM(?length) AS ?totalroadways) WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties ?county. ?county dbpedia-owl:state ?state. ?route dbpprop:lengthMi ?length. } GROUP BY ?state ORDER BY DESC(?totalroadways) 10
GROUP BY Keyword Other functions available for use include AVG(), MIN(), MAX(), and COUNT() The following query returns the average length of roadways by US state: SELECT ?state (AVG(?length) AS ?avgroadways) WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties ?county. ?county dbpedia-owl:state ?state. ?route dbpprop:lengthMi ?length. } GROUP BY ?state ORDER BY DESC(?avgroadways) 11
HAVING Keyword Works for aggregate values in the same way as the FILTER keyword works for individual values The following query returns states having more than 70,000 miles in roadways: SELECT ?state (SUM(?length) AS ?totalroadways) WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties ?county. ?county dbpedia-owl:state ?state. ?route dbpprop:lengthMi ?length. } GROUP BY ?state HAVING (SUM(?length) > 70000) ORDER BY DESC(?totalroadways) 12
Mathematical Functions Allow individual values to be added, subtracted, multiplied and divided to return calculated values The following query returns the area per mile ratio for states in the United States SELECT ?state (?area /(SUM(?length) as ?totallength) as ?areapermile) WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties ?county. ?county dbpedia-owl:state ?state. ?route dbpprop:lengthMi ?length. ?state dbpedia-owl:areaTotal ?area. } ORDER BY DESC(?areapermile) 13
Building a Top Ten List of Area to Length Ratios SELECT ?state (?area /(SUM(?length) as ?totallength) as ?areapermile) WHERE { ?route rdf:type dbpedia-owl:RouteOfTransportation. ?route dbpprop:counties ?county. ?county dbpedia-owl:state ?state. ?route dbpprop:lengthMi ?length. ?state dbpedia-owl:areaTotal ?area. } ORDER BY DESC(?areapermile) LIMIT 10 14
Exercises Find the number of roadways in counties of New York State Find the roadways that have a mention of “Buffalo” somewhere in the english version of their abstract Find roadways that are located in (dbpedia- owl:location) either Toronto or Edmonton Find roadways that are located in counties that have a population (dbpedia-owl:populationTotal) greater than 1.5 million