\chapter{Semantic Data Model}\label{chap:semanticdatamodel}

\section{Linked Data}

Linked Data \cite{LinkedData} represents data that are available on the web. They are stored in machine-readable form. Data are interlinked and are represented by entities. Each entity can have an indefinite number of attributes. Attributes describe properties of the given entity. Entities can be found by attributes that are assigned to them or list all attributes for the given entity. Each entity has an IRI, an Internationalized Resource Identifier which symbolizes the name or the identifier of the given entity. Usually, they are similar to classic URLs which are used to access web pages. Indeed they are similar as each URL is an IRI, but not each IRI is an URL because of special characters which can be also used in the IRIs. IRI can represent anything, define a person from the real world, an object, or some fictional items - there is no limit to what it can represent, it is just an identifier for something with attributes that can provide more information about the given entity. Each attribute is also an identifier which can have other attributes. Links between entities are described by RDF and are encoded as HTTP URIs. This allows the content to be accessible in a readable way for humans and also for machines.

Entities are located on the given domain and can be accessed from this domain by the identifier. The domain represents a dataset. Each dataset can be then connected to another dataset. By multiple datasets, we are creating a network of connected datasets which usually are also interlinked. Linked data are usually represented in a graph way, where datasets and entities are presented as nodes that are linked.

\begin{figure}
\centering
\includegraphics[scale=0.35]{images/linkedDataAndDpPedia}
\caption{Links between datasets represented as graph nodes from \cite{LinkedDataDBPedia}}
\label{fig:interlinkedDatasets}
\end{figure}

\section{Graph representation}

Graph representation use graph structures to represent the structure of data. Nodes represent entities and each entity can have multiple multiple attributes and relations with other nodes. Attributes and relations are represented by links (or edges in the graph sense). Graphs used in this sense are oriented, therefore the relation or attributes goes from one node to another but might not go the other way.

\section{Resource Description Framework (RDF)}

Resource Description Framework \cite{RDFPrimer} is used to describe data (entities) and information about them (connections between them). Its mainly used for applications and processes which should be capable to read the information. Description can be made for any object, or record, including individuals or some conceptual ideas. The syntax is commonly based on XML, Terse RDF Triple Language (Turtle), or JavaScript Object Notation for Linked Data (JSON-LD).

The description of entities is made in form of triples. Each triple represents a statement or information about the given entity. The first parameter of the triple is a subject or entity about which we are creating a statement. The second parameter is a predicate which tells what we want to describe the given relationship between subject and object. A third parameter is an object which represents another entity, which we interlinked with the subject. The statement (or relation) for one triple is always defined from subject to object and not in the other way.

\begin{lstlisting}[caption={Notation of statement as triple}, label={lst:triplesNotation},  captionpos={b}, xleftmargin=.2\textwidth, xrightmargin=.2\textwidth]
<subject> <predicate> <object>
\end{lstlisting}

\subsection{Structure of triple}

An entity represents an IRI, an identifier. IRI is an object, a record, a person, or some conceptual idea. The subject of the triple must be always an IRI, while the object can be an IRI or some data-type object (called literal). Literal is plain values that do not represent an IRI. Literal can be any data types, like string, date, or number. When we supply a literal to an object we always associate the respecting datatype so that the value can be properly processed by machines.

Statements can be made about any IRI. Even if the IRI does not have any statements about it, the IRI exists and a statement can be made about it in the future. We call IRIs that does not have any statements about them as blank nodes. These nodes in the graph representation do not have any edges and are not linked with any other nodes, however, they can appear in the subject or object position of the triple.

\begin{lstlisting}[caption={Statements about entity}, label={lst:triplesExample},  captionpos={b}, xleftmargin=.2\textwidth, xrightmargin=.2\textwidth]
<John> <is a> <User>.
<John> <is a student of> <Mathematics 2022/23>.
<John> <nickname is> <Legolas>.
<John> <has public profile> <True>.
<John> <is a member of> <Ninjas>.
\end{lstlisting}

In the examples \ref{lst:triplesExample}, we made some statements about John, the subject. John also represents an IRI, an identifier. As we already know, a statement consists of a triple. The first argument in our case is John. The second argument of the triple is reserved for the predicate that implies what we want to tell about John and in which relation he is with the object. The third argument, the object, tells us that John is specifically in relation to this object.

Examples represent the existing MatFyz Courses system structure of Users and Teams and show relations between multiple models. The MatFyz Courses system's design is described in depth in \autoref{chap:existingcoursesystem}.

In the first example, we are saying that John is a User. This statement is also human-readable. A User, in this matter, might present a literal (a data type) or some other IRI. Usually, we do not mark these types as literal but as IRIs, which we call classes. Classes define some information about the given object in a more general matter. In the following example, we are stating that John is a student of Mathematics 2022/23. We are linking these two IRIs together, creating a relationship between John and Mathematics 2022/23. There might also be some statements about Mathematics 2022/23. In the third example, we assign to John his nickname. A nickname, the subject is, in this case, a string that we represent as a literal - a data type. The last example tells us that John is a member of another entity identified as Ninjas.

\begin{figure}[H]
\centering
\includegraphics[scale=0.86]{images/RDFexample}
\caption{An example of entities with relations and attributes.}
\label{fig:RDFexample}
\end{figure}

\subsection{Turtle Syntax}

The statements in the example \ref{lst:triplesExample} abstractly represent the triples. Therefore, we can use one of the possible syntaxes to write the triples. Each syntax has its own syntax rules. One of the most common is Turtle syntax. They represent an extension of N-Triples, which are just plain triples. Turtle syntax helps to make the notation straightforward by providing syntax shortcuts, like defining prefixes so that we don't have to type relative or absolute IRIs for each triple.

\begin{lstlisting}[caption={Notation in Turtle syntax}, label={lst:turtleExample},  captionpos={b}, basicstyle=\small]
@PREFIX courses: <http://www.courses.matfyz.sk/> .
@PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@PREFIX schema: <http://schema.org/> .
@PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> .

courses:User a rdfs:Class ;

courses:John
    a courses:User ;
    courses:studentOf courses:Mathematics 2022/23 ;
    courses:publicProfile "false"^^xsd:boolean ;
    courses:nickName "Legolas" ;
    course:memberOf courses:Ninjas ;

courses:Mathematics 2022/23
    a courses:CourseInstance ;
    courses:instanceOf courses:Mathematics for programmers ;
\end{lstlisting}

Each prefix starts with a '@' character. With prefixes, we are making pointers to ontologies. After that, we can begin typing triples from \ref{lst:triplesExample}. So instead of absolute IRI, we use the prefix of the given ontology, followed by a predicate. If we have multiple triples about the same subject, we don't have to type the subject for each triple. Instead, we write a subject in one line and add the predicate and object in additional lines. In this case, each statement ends with the ';' character. When we look at the first example, it uses the predicate 'a', a short notation for rdf:type, which is taken from the RDF schema.

\section{Data-modelling vocabulary}

Triples define relationships between entities. For a better understanding of these relationships, it's beneficial to assign them a semantic meaning. We can create our semantic vocabulary or use the existing one. For some standard types, it is better to use the global semantics that is already commonly used. With that, we ensure that the meaning of these relationships will be identical when we create them, and other ontologies will easily recognize them. Semantic vocabularies in RDF used to describe the relationships between entities are called schemas.

\subsection{RDF schema}

RDF schema mainly consists of class and property systems.

The most basic notation of the RDF schema is an rdfs:Resource. Therefore, each entity written by the RDF is also defined as a resource. One of the most commonly used notations is an rdfs:Class. It helps us to determine the category of the RDF entity. Class meaning is similar to the one used in object-oriented programming languages like Java. The statement that some subject type is a Class means that when we create an object with this type of class, we know that it's an instance of the given class.

\begin{figure}[H]
\includegraphics[scale=0.65]{images/RDFschema}
\caption{Common used notations in RDF schema.}
\label{fig:RDFschema}
\end{figure}


Other commonly used notations are rdfs: Literal and rdfs:Property. All of them are subtypes of the rdfs:Resource type. Each subject that is entitled as rdfs:Literal represents a literal. A literal represents string, integer, or other data type. Therefore, we usually assign a notation rdfs: Datatype to these subjects and a subtype of rdfs: Literal.

Notation rdfs:Property defines a relation between subject and object resources. There exist multiple instances of this notation, but these are the most commonly used:

With notations rdfs:range and rdfs:domain we describe what can be used on the subject's or the object's side - field of values and the domain. 
The field of values is described through rdfs:range - that the values, subjects, are instances of one or more specified classes. 
The domain defined by the rdfs:domain means that any resource, the object, is an instance of one or more classes.

To state that a given resource belongs to the instance of a class, we use the notation rdf:type or, in short, the notation 'a'.

If we want to define that one class is a subtype of another class, we use the notation rdfs:subClassOf. This notation is also transitive, meaning that if A is a class and is a subclass of B and B is a class and a subclass of another class C, then A is a subclass of C.

The same things can also be defined for the property - one property can be a subproperty of another. This notation is also transitive.

\subsection{OWL}

OWL \cite{OWL} is used to describe ontologies. It provides rich notations and streamlines the process of creating new ontologies. Owl can be written in multiple syntaxes, like Turtle, RDF/XML, or OWL2 XML syntax. It is an extension of RDFS notations. For example, a notation owl:ObjectProperty is a subclass of the RDF class rdf:Property. Notations are usually the same or similar to the ones used in the RDFS. However, it adds new attributes and properties. For sets, we can define union, intersection, or conjunction. For example, rdfs:domain should be represented as a conjunction as we should be able to explain one or multiple subjects belonging to the class. Thus, in that case, we should use owl:unionOf. For example, we have an attribute name that should be a part of a Team and Course. In this case, we might write:

\begin{lstlisting}[caption={Domain of attribute name is Team and Course}, label={lst:OWLexample}, captionpos={b}, basicstyle=\small]
<owl:ObjectProperty rdf:ID="name">
  <rdfs:domain>
    <owl:Class>    
      <owl:unionOf rdf:parseType="Collection">
        <owl:Class rdf:about="#Team"/>
        <owl:Class rdf:about="#Course"/>
      </owl:unionOf>
    </owl:Class>    
  </rdfs:domain>
</owl:ObjectProperty>
\end{lstlisting}

\section{Representation in JSON-LD}

The JSON for Linked Data represents \cite{RDFPrimer} linked data using the JSON format. It provides an easy change of JSON documents to RDF. Developers can effortlessly write in this format as it is human-readable and writable. On the web, it is, for example, used with the REST API. Each object defined through JSON-LD must contain required fields, like id. Field id starts with the character '@' followed by the object's identifier. Field @id can be an object identifier or a reference to an object identifier across multiple JSON files.

\section{SPARQL}

SPARQL \cite{SPARQL} is a query language used for data written in the RDF format. When we write a query, it is run over the database and returns an output shown in one of the possible syntaxes of the RDF. The basic actions in SPARQL are SELECT, INSERT and DELETE actions. The syntax of the queries is similar to the one used in relational databases. The query can return a triple or part of the triple. SELECT will return data in the graph that matches the given condition. 

\begin{lstlisting}[caption={SELECT query for finding subjects that are of type User}, label={lst:sparqlExample}, captionpos={b}, basicstyle=\small]
@PREFIX courses: <http://www.courses.matfyz.sk/> .
@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

SELECT ?user
WHERE {
?user a courses:User .
}
\end{lstlisting}

INSERT is used to insert data into the database in the specific graph. A graph is similar to a relational database, which has multiple tables. However, there are no tables in the RDF databases as in the relational databases. Instead, we have a graph containing all of its specified triples. If the graph is not specified in the query, then the default one is used for querying. DELETE action serves for the deletion of specific triples from the graph.

\begin{lstlisting}[caption={INSERT query to the specific graph}, label={lst:sparqlExample}, captionpos={b}, basicstyle=\small]
@PREFIX courses: <http://www.courses.matfyz.sk/> .
@PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

INSERT DATA { GRAPH <https://courses.matfyz.sk> {
	courses:John a courses:User .
	courses:John courses:nickName "Legolas" .
 }
}
\end{lstlisting}

Specific notations make it possible to perform INSERT and DELETE as one action (as one transaction), which in the end serves as an UPDATE action. We can also perform an ASK query, which does not return a triple, but a simple answer, True or False, for the given matter.

\section{Virtuoso}

Virtuoso \cite{Virtuoso} is a database engine that supports the querying of RDF data through SPARQL. In the sense of RDF, it consists of multiple graphs. The web interface is used to configure the Virtuoso server, providing management of the Virtuoso users, rules, and other configs. The query editor is also bundled in the basic version. Users execute queries.
Each user has a set of rules defining which actions might be executed. Rules can be defined for the whole database server or the specific graph. Two versions of Virtuoso exist. One is a free and open source edition, and paid one.


