===== Modeling with RDF and RDFS =====
* Last verification: **20220909**
* Tools required for this lab:
* Any RDF validator/converter that supports Turtle notation. I suggest one of the following three:
* **[[https://rdfshape.weso.es/|RDFShape]]** -- a lightweight online service for RDF validation, visualization and conversion
* **[[http://any23.org/|Apache Any23 (Anything to Triples)]]** -- online service with better error messages during file validation (but unfortunately it is often down)
* **Apache Any23** (offline) -- if both online services are down, this solution is a backup (see details below, in the info box) \\ \\
To launch Apache Any23 service on your own computer:
* Download **{{https://krzysztof.kutt.pl/didactics/semweb/apache-any23-service-1.1-server-embedded.tar.gz|Apache Any23 Service (Standalone server embedded) 1.1}}** and unpack the zip somewhere
* You can also download the latest version directly from [[https://any23.apache.org/download.html]]
* Start any23 service with the command: ./[PATH-to-unpacked-archive]/apache-any23-service-1.1-server-embedded/bin/any23server
* Note: use the ''any23server.bat'' file if you are using Windows
* Change ''"$REPO"'' into ''%REPO%'' twice (line #79 in ''any23server.bat'' file) if ''Error: Unable to access jarfile $REPO/jetty-runner-8.1.4.v20120524.jar.'' message appears
* After a few seconds, any23 service will be available at [[http://localhost:8080/apache-any23-service/]] :-)
==== Prepare yourself for the lab ====
* [[http://www.w3.org/TR/rdf11-primer/|RDF Primer]]: general ideas about RDF; we will use only Turtle syntax so you can simply omit sections 5.2-5.4
* [[http://www.w3.org/TR/rdf-schema/|RDF Schema 1.1]]
* [[https://web.archive.org/web/20170623182127/http://haystack.csail.mit.edu:80/blog/2008/11/06/a-quick-tutorial-on-the-tutrle-rdf-serialization/|A Quick Tutorial on the Turtle RDF Serialization]]
==== Lab instructions ====
=== 1. Modeling knowledge with RDF triples [30 minutes] ===
**RDF** is **a data model** for which several syntaxes have been developed (see Section 5 in [[http://www.w3.org/TR/rdf11-primer/|RDF Primer]]). \\
**RDF document** is an RDF graph (describing some objects) serialized into chosen representation/syntax.
During [[lab_intro#modeling_knowledge_with_rdf_graphs_30_minutes|the previous lab]], you created an RDF model/graph on a topic of your choice (The Bold and the Beautiful / The Game of Thrones / other). \\
During this lab, we will serialize it with the Turtle syntax (an RDF document will be created) and then extend it with some RDF Schema (RDFS) extras. \\
Let's start!
- Create a new document with a ''*.ttl'' extension.
- Create a document preamble:
* Define [[http://www.w3.org/TR/turtle/#prefixed-name|prefixes]] for namespaces used. You can start with a standard RDF namespace and one "your own" dedicated for this graph: @prefix rdf: .
@prefix bb: .
* You can also use a [[http://www.w3.org/TR/turtle/#relative-iri|base namespace]]: @base .
- Write down ~15 selected triples from your graph. Make sure you include both **resources** (with URIs) and **literals**.
* Use the [[http://www.w3.org/TR/turtle/|RDF Turtle]] for reference.
* Comments in Turtle begins with ''#'' symbol.
- Validate your document using the selected validation service:
* **[preffered]** If you use [[https://rdfshape.weso.es/|RDFShape]]:
* go to the [[https://rdfshape.weso.es/dataInfo|Data analysis]] (RDF -> Analysis & Visualization):
* Paste your document in **Text** tab
* Format: **Turtle**
* Inference: **NONE**
* Click "Analyze":
* If everything is OK, you will see **Well formed RDF** message and summary of the document; you can also see the visualization under **Visualizations** tab
* Most errors are flagged when you type in the editor. You can also see the report after clicking "Analyze": you should expand the **Error response from data/info** box to see the details
* If you use [[http://any23.org/|Apache any23]] service (online/offline):
* Go to the "Convert copy&pasted document" section:
* input format: **Turtle (text/turtle)**
* output format: **turtle**
* validation: **validate**
* report: **checked**
* annotate: **unchecked**
* Paste your code and click "Convert" button:
* If something is wrong, you can see details in report (in web browser), e.g.: \\ {{any23_some_problems_web.png?direct&600|}}
* If code was successfully validated then you get an XML with a lot of empty tags and your Turtle code inside '''' (indentation may be changed)
=== 2. RDF: Containers and Collections [15 minutes] ===
In the RDF there are two ways to describe a set or a sequence of objects:
* //[[http://www.w3.org/TR/rdf11-mt/#rdf-containers|Containters]]// (''rdf:Bag'', ''rdf:Seq'' and ''rdf:Alt''),
* //[[http://www.w3.org/TR/rdf11-mt/#rdf-collections|Collections]]// (''rdf:List'').
Important difference (see [[http://www.w3.org/TR/rdf11-mt/|RDF 1.1 Semantics]]):
> There is **no way** in RDF to assert that __a container__ contains **only a fixed number of members**. This is a reflection of the fact that it is always consistent to add a triple to a graph asserting a membership property of any container. And finally, there is no built-in assumption that an RDF container has only finitely many members. [...]
> Collections differ from containers [...] in having an explicit terminator, allowing applications to determine **the exact set** of items in __the collection__.
- Create __a container__ in your RDF document.
- Create also __a collection__.
* For hint and examples, see the [[http://www.w3.org/TR/turtle/#collections|recommendation]] (there is a compact notation for lists in Turtle!).
=== 3. RDF: Datatypes [15 minutes] ===
RDF allows the types of literals to be defined explicitely, making them easier to process later. The basic types are defined in the [[https://www.w3.org/TR/xmlschema11-2/|W3C XML Schema Definition Language (XSD) 1.1 Part 2: Datatypes]] recommendation.
- Familiarize yourself with the [[https://www.w3.org/TR/xmlschema11-2/#built-in-primitive-datatypes|primitive datatypes]] ([[https://www.w3.org/TR/xmlschema11-2/#built-in-datatypes|a hierarchy in Section 3]] may be useful).
- Add the XSD datatypes to chosen information in your RDF document
* Use at least 4 different datatypes!
* E.g.:
# ...
@prefix xsd: .
# ...
# ...
;
"1900-01-01"^^xsd:date;
"Chemistry"^^xsd:string;
# ...
=== 4. RDF Visualization [10 minutes] ===
We are in the middle, so let's visualize our RDF files.
- //Do you remember to use the validator regularly during the lab to verify the correctness of your RDF document?//
- Generate the visualization of your RDF document (NOTE: The file must not contain errors to be able to generate a visualization!):
* If you use [[https://rdfshape.weso.es/|RDFShape]]:
- Simply go to the [[https://rdfshape.weso.es/dataInfo|Data analysis]], click **Analyze** and then go to the **Visualizations**/**DOT** tab. You can download the image using dedicated button.
* If you use [[http://any23.org/|Apache any23]], you should follow the following procedure:
- Convert your file to RDF/XML (''rdfxml'') format
- Simply copy the code of the generated RDF/XML document
- Go to the [[http://www.w3.org/RDF/Validator/|RDF Validator]] page and simply paste generated code into ''Check by Direct Input'' field. Select ''Graph only'' from dropdown list and click ''Parse RDF''.
- Analyze the results:
- How are containers represented in the graph?
- How are collections represented in the graph?
- How are the datatypes presented?
- Does the visualization as a whole mirror your earlier hand-drawn graph?
=== 5. Semantic vocabularies: FOAF, Dublin Core and FHKB [30 minutes] ===
Semantic vocabularies are sets of predefined properties for describing some domains. Examples include:
* [[wp>FOAF_(ontology)|FOAF]] for describing people, their activities and relations (//seen in previous lab//)
* [[http://xmlns.com/foaf/spec/|FOAF Vocabulary Specification]]
* [[wp>Dublin_Core|Dublin Core]] for describing digital/physical "media" resources (books, movies, webpages, ...)
* [[https://www.dublincore.org/specifications/dublin-core/dcmi-terms/|DCMI Metadata Terms]] -- definitions of all terms. Prefixes: @prefix dc: . # Sect 3 in the document
@prefix dcterms: . # Sect 2 in the document
* [[wp>Semantically-Interlinked_Online_Communities|SIOC]] for describing online communities and social data (posts, forums, users)
* [[https://www.w3.org/2008/09/msnws/papers/sioc.html|SIOC: Content Exchange and Semantic Interoperability Between Social Networks]] -- introductory paper
* {{http://ceur-ws.org/Vol-1207/paper_11.pdf|FKHB}} for describing family relations (in genealogy)
* [[https://github.com/blokhin/genealogical-trees/blob/master/data/header.ttl|FKHB.ttl]] -- file with all the definitions
* [[https://blog.tilde.pro/semantic-web-technologies-on-an-example-of-family-trees-7518f3f835a9|Semantic Web Technologies on an Example of Family Trees]] -- a blog entry about the FKHB in use
- Take a look at all above-mentioned dictionaries. How they are structured? What concepts and relations are available?
- Modify your RDF document using the properties from these dictionaries, e.g.:
# ...
@prefix dc: .
@prefix dcterms: .
@prefix foaf: .
@prefix fhkb: .
# ...
# ...
fhkb:isDaughterOf ;
"1900-01-01"^^xsd:date;
"Chemistry"^^xsd:string;
foaf:name "Brooke Logan", "Brooke Forrester", "Brooke Chambers", "Brooke Jones", "Brooke Marone", "Brooke Spencer".
# ...
=== 6. RDF Schema: classes [15 minutes] ===
RDF Schema allows to organize objects into classes and define simple taxonomies.
- Define classes of items in your database.
* You may find [[https://www.w3.org/TR/rdf-schema/|RDF Schema 1.1]] recommendation useful.
* Consider reusing the classes from dictionaries mentioned before.
* Example:
@base .
@prefix rdf: .
@prefix rdfs: .
rdf:type rdfs:Class .
rdf:type rdfs:Class ;
rdfs:subClassOf .
foaf:Person rdfs:subClassOf .
- Add the ''rdf:type'' statements to your RDF file, e.g.:
a foaf:Person .
rdf:type .
* Note that ''a'' is an alias for ''rdf:type'' so two statements: rdf:type .
a .
are equivalent.
=== 7. RDF Schema: properties [15 minutes] ===
RDF Schema also provides a way to define Properties as well as their domains and ranges.
- In your file identify properties that are not defined in external dictionaries (e.g., Dublin Core), as in this code:
fhkb:isDaughterOf ;
"1900-01-01"^^xsd:date;
"Chemistry"^^xsd:string;
foaf:name "Brooke Logan", "Brooke Forrester", "Brooke Chambers", "Brooke Jones", "Brooke Marone", "Brooke Spencer".
'''' is not defined in external dictionary -- it is defined in our own namespace.
- Describe each such property using RDF Schema.
* You may find [[https://www.w3.org/TR/rdf-schema/|RDF Schema 1.1]] recommendation useful.
* Example:
@base .
@prefix rdf: .
@prefix rdfs: .
rdf:type rdfs:Property ;
rdfs:domain foaf:person ;
rdfs:range rdfs:Literal.
=== 8. RDF Schema: Non-modeling properties [10 minutes] ===
RDF Schema also provides some handful properties that are not used in inference process. These are:
* ''rdfs:label'' -- used by convention to provide a human-readable name that is displayed by semantic web agents,
* ''rdfs:seeAlso'' -- cross-referencing; provide links to supplementary information,
* ''rdfs:isDefinedBy'' -- subPropertyOf rdfs:seeAlso; provides a link to the primary source of information,
* ''rdfs:comment'' -- for everything you want :-) .
- Use each of them in your RDF file.
- **Check if your Turtle file passes validation!**
- Save the file in a safe place -- we will process it during the next lab!
==== Learn more! ====
{{section>.:lab_intro#learn_more&noheader&nofooter}}