\documentclass{beamer}

\graphicspath{
    {resources/}
}

\usepackage{enumitem}
\usepackage{lmodern}  % for bold teletype font
\usepackage{amsmath}  % for \hookrightarrow
\usepackage{xcolor}   % for \textcolor
\usepackage{listings}
\usepackage{tipa}

\setlist[description]{style=nextline}

\lstset{
    basicstyle=\small\ttfamily,
    columns=fullflexible,
    breaklines=true,
    postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
    escapeinside={(*@}{@*)},
}


\makeatletter
\def\blfootnote{\gdef\@thefnmark{}\@footnotetext}
\makeatother

\usetheme{metropolis}

\title{SPARQL}
\author{Julian Richen}
\institute{UCF}
\date{November 29th, 2017}

\begin{document}

\begin{frame}
    \titlepage
\end{frame}

\begin{frame}{Table of contents}
    %\tableofcontents
    \tableofcontents[hideallsubsections]
\end{frame}

\section{What is SPARQL?}


    \subsection{SPARQL stands for \ldots}
        \begin{frame}{\subsecname}
            \centering SPARQL Protocol and RDF Query Language\\
            \small sparkle /ˈ'sp\"{a}rk(\textschwa)l/\\
            \tiny \textit{recursive acronym}
        \end{frame}

    \subsection{It's \ldots}
        \begin{frame}{\subsecname}
            \begin{columns}[T]
                \begin{column}{.75\textwidth}
                    \begin{block}{}
                        \begin{itemize}
                            \item an RDF Query Language
                            \item used to retrieve data stored in Resource Description Framework Format (RDF for short)
                            \item developed by the W3C
                            \item released in 2008 (version 1.0)
                            \item latest release was March, 2013 (version 1.1)
                        \end{itemize}
                    \end{block}
                \end{column}
                \begin{column}{.25\textwidth}
                    \begin{block}{}
                        \begin{figure}
                                \includegraphics[width=1\textwidth]{resources/W3C.png}
                        \end{figure}
                    \end{block}
                \end{column}
            \end{columns}
        \end{frame}
        
        \begin{frame}{But wait, there's more}
            \begin{itemize}
                \item SPARQL queries against data stored in key-value pairs
                \item Data in RDF is formatted as subject-predicate-object, similar to other semi-structured databases
                    \begin{itemize}
                        \item Think NoSQL or MongoDB (``document-oriented database'')
                    \end{itemize}
                \item The ``subject-predicate-object'' are defined by Ontologies at specified URIs
                \item Relational databases' concept of rows and columns don't really exists
                    \begin{itemize}
                        \item However, it can help to visualize the data like a relational database
                    \end{itemize}
            \end{itemize}
        \end{frame}
        
        \begin{frame}{But first}
            \begin{block}{}
                \centering ``We need to understand the database, before we can query it'' \\
                \flushright - some philosopher
            \end{block}
        \end{frame}

\section{RDF Format}
    \subsection{What's RDF}
         \begin{frame}{\subsecname}
            \begin{itemize}
                \item It's a Resource Description Framework
                \item Data is defined by web resources (URIs) with data serialization
                \item Stores key-value data in ``triples'' or ``RDF Stores''
                \item Can store data in three formats:
                    \begin{itemize}
                        \item RDF/XML
                        \item N-Triples
                        \item Turtle
                    \end{itemize}
            \end{itemize}
        \end{frame}
    
        \begin{frame}{Example}
            \begin{figure}
                \includegraphics[width=1\textwidth]{resources/graph.png}
            \end{figure}
        \end{frame}
        
        \begin{frame}{Visualized as a table in a relational database}
            \begin{center}
                \begin{tabular}{ | l | l | l | l | }
                    \hline
                    \multicolumn{4}{ | l | }{employee} \\
                    \hline
                    id & givenName & familyName & title \\ \hline
                    1 & John & Hitt & President \\ \hline
                    2 & John & Schell & Vice President \\
                    \hline
                \end{tabular}
            \end{center}
        \end{frame}
        
        \begin{frame}{Triple or RDF Store}
            \begin{center}
                \begin{tabular}{ | c c c | }
                    \hline
                    subject & predicate & object \\
                    \hline
                    entity identifier & attribute name & attribute value \\ \hline
                    \tiny{$<$http://www.ucf.edu/employee/1$>$} & \tiny{$<$http://xmlns.com/foaf/0.1/givenName$>$} & \tiny{"John"} . \\ \hline
                    \tiny{$<$/ucf/employee/1$>$} & \tiny{$<$/foaf/givenName$>$} & \tiny{"John"} \\ \hline
                    employee:1\footnote{Shorthand using prefix, explained later} & foaf:givenName & "John" . \\
                    \hline
                \end{tabular}
            \end{center}
        \end{frame}
    
\begin{frame}[fragile]{RDF/XML Format}
\begin{lstlisting}[language=XML]
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:foaf="http://xmlns.com/foaf/0.1/">
    <rdf:Description rdf:about="http://www.ucf.edu/employee/1">
        <foaf:givenName>John</foaf:givenName>
        <foaf:familyName>Hitt</foaf:familyName>
        <foaf:title>President</foaf:title>
    </rdf:Description>
    <rdf:Description rdf:about="http://www.ucf.edu/employee/2">
        <foaf:givenName>John</foaf:givenName>
        <foaf:familyName>Schell</foaf:familyName>
        <foaf:title>Vice President</foaf:title>
    </rdf:Description>
</rdf:RDF>
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{N-Triples}
\begin{lstlisting}[language=XML]
<http://www.ucf.edu/employee/1> <http://xmlns.com/foaf/0.1/givenName> "John" .
<http://www.ucf.edu/employee/1> <http://xmlns.com/foaf/0.1/familyName> "Hitt" .
<http://www.ucf.edu/employee/1> <http://xmlns.com/foaf/0.1/title> "President" .

<http://www.ucf.edu/employee/2> <http://xmlns.com/foaf/0.1/givenName> "John" .
<http://www.ucf.edu/employee/2> <http://xmlns.com/foaf/0.1/familyName> "Schell" .
<http://www.ucf.edu/employee/2> <http://xmlns.com/foaf/0.1/title> "Vice President" .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Tutrle Syntax}
\begin{lstlisting}[language=XML]
@prefix employee: <http://www.ucf.edu/employee/> .
@prefix foaf:     <http://xmlns.com/foaf/0.1/> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .
employee:1 foaf:title "President" .

employee:2 foaf:givenName "John" .
employee:2 foaf:familyName "Schell" .
employee:2 foaf:title "Vice President" .
\end{lstlisting}

\blfootnote{FOAF: Friend of a Friend}
\end{frame}
 
\begin{frame}[fragile]{Tutrle Syntax cont.}
\begin{lstlisting}[language=XML]
@prefix (*@\textcolor{red}{employee}@*): <http://www.ucf.edu/employee/> .
@prefix (*@\textcolor{blue}{foaf}@*): <http://xmlns.com/foaf/0.1/> .

(*@\textcolor{red}{employee}@*):1 (*@\textcolor{blue}{foaf}@*):givenName "John" .
(*@\textcolor{red}{employee}@*):1 (*@\textcolor{blue}{foaf}@*):familyName "Hitt" .
(*@\textcolor{red}{employee}@*):1 (*@\textcolor{blue}{foaf}@*):title "President" .

(*@\textcolor{red}{employee}@*):2 (*@\textcolor{blue}{foaf}@*):givenName "John" .
(*@\textcolor{red}{employee}@*):2 (*@\textcolor{blue}{foaf}@*):familyName "Schell" .
(*@\textcolor{red}{employee}@*):2 (*@\textcolor{blue}{foaf}@*):title "Vice President" .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Match the colors}  
\begin{lstlisting}[language=XML]
@prefix (*@\textcolor{red}{employee}@*): <(*@\textcolor{red}{http://www.ucf.edu/employee/}@*)> .
@prefix (*@\textcolor{blue}{foaf}@*): <(*@\textcolor{blue}{http://xmlns.com/foaf/0.1/}@*)> .

(*@\textcolor{red}{employee}@*):(*@\textcolor{green}{1}@*) 
(*@\textcolor{blue}{foaf}@*):(*@\textcolor{orange}{givenName}@*) 
"(*@\textcolor{purple}{John}@*)" .

<(*@\textcolor{red}{http://www.ucf.edu/employee/}@*)(*@\textcolor{green}{1}@*)> 
<(*@\textcolor{blue}{http://xmlns.com/foaf/0.1/}@*)(*@\textcolor{orange}{givenName}@*)> 
"(*@\textcolor{purple}{John}@*)" .
\end{lstlisting}
\end{frame}

\section{Query Language}
    \subsection{About}
        \begin{frame}{\subsecname}
            \begin{itemize}
                \item Supports Create, Read, Update, and Delete operations (CRUD)
                \item Read queries have multiple forms: SELECT, CONSTRUCT, ASK, DESCRIBE
                \item Has helper functions like FILTER, OPTIONAL, EXISTS, NOT EXISTS, BIND, etc \ldots
                \item Federated Queries via SERVICE
                \item And more! \tiny{(check the specs)}
            \end{itemize}
        \end{frame}

    \subsection{Example}
\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
@prefix employee: <http://www.ucf.edu/employee/> .
@prefix foaf:     <http://xmlns.com/foaf/0.1/> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .
employee:1 foaf:title "President" .

employee:2 foaf:givenName "John" .
employee:2 foaf:familyName "Schell" .
employee:2 foaf:title "Vice President" .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf: <http://xmlns.com/foaf/0.1/> .

SELECT
    ?person
WHERE {
    ?person foaf:familyName "Hitt" .
}
\end{lstlisting}
            \begin{center}
                \begin{tabular}{ | l | }
                    \hline
                    ?person \\ \hline
                    employee:1 \\
                    \hline
                \end{tabular}
            \end{center}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf: <http://xmlns.com/foaf/0.1/> .

SELECT
    ?person ?givenName ?familyName ?title
WHERE {
    ?person foaf:familyName "Hitt" .
    ?person foaf:givenName ?givenName .
    ?person foaf:familyName ?familyName .
    ?person foaf:title ?title .
}
\end{lstlisting}
            \begin{center}
                \begin{tabular}{ | l | l | l | l | }
                    \hline
                    ?person & ?givenName & ?familyName & ?title \\
                    \hline
                    employee:1 & John & Hitt & President \\
                    \hline
                \end{tabular}
            \end{center}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
@prefix employee: <http://www.ucf.edu/employee/> .
@prefix ucf:      <http://www.ucf.edu/ontology#> .
@prefix foaf:     <http://xmlns.com/foaf/0.1/> .
@prefix rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .
employee:1 foaf:title "President" .
employee:1 rdf:type ucf:Employee .

employee:2 foaf:givenName "John" .
employee:2 foaf:familyName "Schell" .
employee:2 foaf:title "Vice President" .
employee:2 rdf:type ucf:Employee .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontology#> .

SELECT
    ?person
WHERE {
    ?person a ucf:Employee .
}
\end{lstlisting}
            \begin{center}
                \begin{tabular}{ | l | }
                    \hline
                    ?person \\ \hline
                    employee:1 \\ \hline
                    employee:2 \\
                    \hline
                \end{tabular}
            \end{center}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

SELECT
    ?person ?givenName ?familyName ?title
WHERE {
    ?person a ucf:Employee .
    ?person foaf:givenName ?givenName .
    ?person foaf:familyName ?familyName .
    ?person foaf:title ?title .
}
\end{lstlisting}
            \begin{center}
                \begin{tabular}{ | l | l | l | l | }
                    \hline
                    ?person & ?givenName & ?familyName & ?title \\ \hline
                    employee:1 & John & Hitt & President \\ \hline
                    employee:2 & John & Schell & Vice President \\
                    \hline
                \end{tabular}
            \end{center}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

SELECT
    ?person ?givenName
WHERE {
    ?person foaf:givenName ?givenName .
    OPTIONAL { ?person a ucf:Employee . }
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

SELECT
    ?person ?givenName
WHERE {
    ?person foaf:givenName ?givenName .
    NOT EXISTS { ?person foaf:title "President" . }
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

SELECT
    ?person ?fullName
WHERE {
    ?person foaf:givenName ?givenName .
    ?person foaf:familyName ?familyName .
    BIND (concat (?givenName, " ", ?familyName) AS ?fullName)
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

CONSTRUCT {
    ?person foaf:name ?fullName
} WHERE {
    ?person foaf:givenName ?givenName .
    ?person foaf:familyName ?familyName .
    BIND (concat (?givenName, " ", ?familyName) AS ?fullName)
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
@prefix employee: <http://www.ucf.edu/employee/> .
@prefix ucf:      <http://www.ucf.edu/ontology#> .
@prefix foaf:     <http://xmlns.com/foaf/0.1/> .
@prefix rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .
employee:1 foaf:name "John Hitt" .
employee:1 foaf:title "President" .
employee:1 rdf:type ucf:Employee .

employee:2 foaf:givenName "John" .
employee:2 foaf:familyName "Schell" .
employee:1 foaf:name "John Schell" .
employee:2 foaf:title "Vice President" .
employee:2 rdf:type ucf:Employee .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf:     <http://xmlns.com/foaf/0.1/> .
PREFIX employee: <http://www.ucf.edu/employee/> .
PREFIX ucf:      <http://www.ucf.edu/ontoilogies#> .

INSERT {
    ?person foaf:title "Staff"
} WHERE {
    ?person foaf:givenName "John" .
    ?person foaf:familyName "Hitt" .
    ?person foaf:title "President" .
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
@prefix employee: <http://www.ucf.edu/employee/> .
@prefix ucf:      <http://www.ucf.edu/ontology#> .
@prefix foaf:     <http://xmlns.com/foaf/0.1/> .
@prefix rdf:      <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .
employee:1 foaf:title "Staff" .
employee:1 rdf:type ucf:Employee .
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf: <http://xmlns.com/foaf/0.1/> .

SELECT ?person
FROM <local_database.rtf>
WHERE {
    ?person foaf:giveName "John" .
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
# Named graph <http://www.ucf.edu/employee/1>
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

employee:1 foaf:givenName "John" .
employee:1 foaf:familyName "Hitt" .

# Query
PREFIX foaf: <http://xmlns.com/foaf/0.1/> .

SELECT ?person ?givenName ?familyName
FROM NAMED <http://www.ucf.edu/employee/1>
WHERE {
    ?person foaf:giveName "John" .
    ?person foaf:givenName ?givenName .
    ?person foaf:familyName ?familyName .
}
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{\subsecname}
\begin{lstlisting}[language=XML]
PREFIX foaf: <http://xmlns.com/foaf/0.1/> .
SELECT ?person
WHERE {
    SERVICE <http://www.ucf.edu/database> {
        SELECT ?person
        WHERE {
            ?person foaf:name "John Doe" .
        }
    }
    SERVICE <http://www.seminolestate.edu/database> {
        SELECT ?person2
        WHERE {
            ?person2 foaf:name "John Doe" .
        }
    }
    FILTER (?person = ?person2)
}
\end{lstlisting}
\end{frame}
        
        \begin{frame}{\subsecname}
            \begin{block}{}
                \centering We could keep going \ldots
            \end{block}
        \end{frame}

\section{Should I Use It?}
    \subsection{\secname}
        \begin{frame}{\subsecname}
            \centering Yes and no\\
            \centering \tiny Good answer right?
        \end{frame}
        
        \begin{frame}{Benefits \footnote{buzzwords}}
            \begin{itemize}
                \item Flexible
                \item Modern
                \item Standard
            \end{itemize}
        \end{frame}

\section{Resources}
    \subsection{\secname}
        \begin{frame}{\subsecname}
            \begin{description}
                \item[SPARQL v1.1 Spec] https://www.w3.org/TR/sparql11-overview/
                \item[SPARQL in 11 minutes] https://www.youtube.com/watch?v=FvGndkpa4K0 
            \end{description}
            
            \vfill
            \centering
            \LaTeX
        \end{frame}
    

\end{document}
