Skip to content

Instantly share code, notes, and snippets.

@andrewstellman
Last active June 11, 2019 15:21
Show Gist options
  • Save andrewstellman/21192dadab0f95daac48b57bf5686d60 to your computer and use it in GitHub Desktop.
Save andrewstellman/21192dadab0f95daac48b57bf5686d60 to your computer and use it in GitHub Desktop.
Quick RDF example of an ontology, data, and queries

Quick RDF example

Quick example of RDF that shows structured data. Example of triples using this ontology:

  :WallyWorld a :Company ;
    rdfs:label "Wally World Amusements" .

  :Bill a :Employee ;
    :employedBy :WallyWorld ;
    :hasEmployeeRecord [
      :fullName "Bill Lund" ;
      :employeeType :PartTimeEmployee ;
      :employeeRating "27.2"^^xsd:decimal ;
      :yearsWorked "6"^^xsd:int ;
  ] .

Those triples assert the following facts:

Bill is employed by Wally World Amusements. Bill has an employee record with the following information:

  • His full name is "Bill Lund"
  • He's a part time employee
  • His employee rating is 27.2
  • He's worked at Wally World for 6 years

Files

Get up and running quickly

If you want to get up and running quickly:

  1. download Blazegraph (https://www.blazegraph.com/download/)
  2. run it (java -server -Xmx4g -jar blazegraph.jar)
  3. open the local server web page, go to the update page
  4. upload both of the files (choose type RDF Data and format TriG)
  5. go to the query page, paste in one of the queries and run it
PREFIX : <http://www.stellman-greene.com/employee_example/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?company ?employeeType (AVG(?yearsWorked) as ?avg) {
?employee :employedBy ?company .
?company rdfs:label ?companyName .
?employee :hasEmployeeRecord ?employeeRecord .
?employeeRecord :employeeType ?employeeType .
?employeeRecord :yearsWorked ?yearsWorked .
}
GROUP BY ?company ?employeeType
@prefix : <http://www.stellman-greene.com/employee_example/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:EmployeeData {
:EmployeeData rdf:comment "Data for a few employees" .
:BigCo a :Company ;
rdfs:label "Big Co, Inc." .
:WallyWorld a :Company ;
rdfs:label "Wally World Amusements" .
:Joe a :Employee ;
:employedBy :BigCo ;
:hasEmployeeRecord [
:fullName "Joe Josephson" ;
:employeeType :FullTimeEmployee ;
:employeeRating "72.37"^^xsd:decimal ;
:yearsWorked "4"^^xsd:int ;
] .
:Barb a :Employee ;
:employedBy :BigCo ;
:hasEmployeeRecord [
:fullName "Barbara O'Mally" ;
:employeeType :FullTimeEmployee ;
:employeeRating "98.11"^^xsd:decimal ;
:yearsWorked "9"^^xsd:int ;
] .
:Ernie a :Employee ;
:employedBy :BigCo ;
:hasEmployeeRecord [
:fullName "Ernie MacPherson" ;
:employeeType :PartTimeEmployee ;
:employeeRating "37.2"^^xsd:decimal ;
:yearsWorked "2"^^xsd:int ;
] .
:Martha a :Employee ;
:employedBy :BigCo ;
:hasEmployeeRecord [
:fullName "Martha Wayne" ;
:employeeType :PartTimeEmployee ;
:employeeRating "11.11"^^xsd:decimal ;
:yearsWorked "17"^^xsd:int ;
] .
:Zoe a :Employee ;
:employedBy :WallyWorld ;
:hasEmployeeRecord [
:fullName "Zoe Danielson" ;
:employeeType :FullTimeEmployee ;
:employeeRating "42.11"^^xsd:decimal ;
:yearsWorked "11"^^xsd:int ;
] .
:Edith a :Employee ;
:employedBy :WallyWorld ;
:hasEmployeeRecord [
:fullName "Edith Smith" ;
:employeeType :PartTimeEmployee ;
:employeeRating "77.17"^^xsd:decimal ;
:yearsWorked "11"^^xsd:int ;
] .
:Samantha a :Employee ;
:employedBy :WallyWorld ;
:hasEmployeeRecord [
:fullName "Samantha Sally" ;
:employeeType :FullTimeEmployee ;
:employeeRating "55.2"^^xsd:decimal ;
:yearsWorked "6"^^xsd:int ;
] .
:Bill a :Employee ;
:employedBy :WallyWorld ;
:hasEmployeeRecord [
:fullName "Bill Lund" ;
:employeeType :PartTimeEmployee ;
:employeeRating "27.2"^^xsd:decimal ;
:yearsWorked "6"^^xsd:int ;
] .
}
@prefix : <http://www.stellman-greene.com/employee_example/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:EmployeeOntology {
:EmployeeOntology rdfs:comment "A quick ontology for modeling employees at a company" .
:Company a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:label "A company" .
:Employee a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:label "An employee" .
:EmployeeType a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:label "Superclass of employee type" .
:FullTimeEmployee a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:subClassOf :EmployeeType ;
rdfs:label "Full-time employee" .
:PartTimeEmployee a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:subClassOf :EmployeeType ;
rdfs:label "Part-time employee" .
:EmployeeRecord a <http://www.w3.org/2002/07/owl#Class> ;
rdfs:label "An employee record" .
:employedBy a <http://www.w3.org/2002/07/owl#ObjectProperty> ;
rdfs:domain :Employee ;
rdfs:range :Company ;
rdfs:label "is employed by" .
:hasEmployeeRecord a <http://www.w3.org/2002/07/owl#ObjectProperty> ;
rdfs:domain :Employee ;
rdfs:range :EmployeeRecord ;
rdfs:label "has employee record" .
:fullName a <http://www.w3.org/2002/07/owl#DatatypeProperty> ;
rdfs:domain :EmployeeRecord ;
rdfs:range xsd:string ;
rdfs:label "full name for the employee" .
:employeeType a <http://www.w3.org/2002/07/owl#ObjectProperty> ;
rdfs:domain :EmployeeRecord ;
rdfs:range :EmployeeType ;
rdfs:label "is employed by" .
:employeeRating a <http://www.w3.org/2002/07/owl#DatatypeProperty> ;
rdfs:domain :EmployeeRecord ;
rdfs:range xsd:decimal ;
rdfs:label "rating for the employee" .
:yearsWorked a <http://www.w3.org/2002/07/owl#DatatypeProperty> ;
rdfs:domain :EmployeeRecord ;
rdfs:range xsd:int ;
rdfs:label "years that the employee has worked at the company" .
}
PREFIX : <http://www.stellman-greene.com/employee_example/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?company (COUNT(?yearsWorked) as ?count) (MAX(?yearsWorked) as ?max) (MIN(?yearsWorked) as ?min) (AVG(?yearsWorked) as ?avg) {
?employee :employedBy ?company .
?company rdfs:label ?companyName .
?employee :hasEmployeeRecord ?employeeRecord .
?employeeRecord :employeeType :FullTimeEmployee .
?employeeRecord :yearsWorked ?yearsWorked .
}
GROUP BY ?company ?employeeType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment