Ontological model as a graph. SPARQL language

This page is a chapter from our teaching guide
"Introduction to Ontology Modeling" (click to go to the full version in PDF format).

A graph is a mathematical term meaning a collection of vertices and the edges connecting them. The simplest graph looks like this:


Rice. 17. The simplest graph

Note that the edge is directional.

Obviously, the structure of such a graph ideally corresponds to the structure of a triplet: the vertices are the subject and the object, and the edge is the predicate. For example, the triplet “A dog is an animal” can be represented as follows:


Rice. 18. Logical expression represented as a graph

Thus, any ontology can be represented as a graph. Let's illustrate this with an example from the previous chapter.


Rice. 19. Model presented as a graph

Looking at images like this, it is important to understand that a vertex corresponds, in fact, to a unique identifier of an object (with the exception of literal vertices, which we showed as rectangles). It does not contain any other information; all properties assigned to it are specified by individual edges of the graph. We have highlighted some vertices in color only for ease of perception.

The correctness of the image of our model can be easily checked by counting the number of edges. There are 15 of them, the same number as triplets in the table from the previous chapter.

Since a complete graphical representation of a model is usually too complex, it is simplified by not showing edges indicating the element type, pointing to literals, etc.

Note also that in our model, property entities (#Name, #Child) are present in two roles - as vertices, which corresponds to their description, and as edges, when assigning a property value to a specific object.

This representation of the semantic model determines the way of constructing queries to it, implemented in the SPARQL language. We have already seen an example of a simple SPARQL query:

SELECT * WHERE { ?a ?b ?c }

The words SELECT * WHERE are clear to anyone familiar with SQL - this is a selection of all results from a set of rows (in SQL) or triplets (in SPARQL) that meet the selection conditions. Selection conditions, in the case of SPARQL, are expressions enclosed in curly braces. It defines a pattern, or template, that triplets must conform to. Values ​​starting with a question mark are variables. They can take any value; these values ​​will be returned as a result of the request. Since all three positions in our query are occupied by variables, all three elements of the triplet can be anything. This means that such a query will return the entire contents of the ontology (you should not abuse such queries on large ontologies).

Here are some examples of meaningful SPARQL queries. Let's say we want to find out what type the #Alpha object is:

SELECT * WHERE { <http://example.com/#alpha> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?c }

For readability, prefixes can be declared before the request. Then our request will look like this:

PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema>

SELECT * WHERE { <http://example.com/#alpha> rdfs:type ?c }

The result of the query will be the values ​​of the variable ?c - the only remaining unknown:

?c
<http://www.w3.org/2002/07/owl#NamedIndividual>
<http://example.com/#Customer>

To get a list of all members of the Customer class, you need to run the following query:

SELECT * WHERE { ?cust rdfs:type  }

The result will be the client URIs:

?cust
<http://example.com/#alpha>
<http://example.com/#beta>

Let's assume that we want to get their names along with the clients' unique identifier. Then in the request you will need to specify a pattern corresponding to two edges of the graph:

SELECT * WHERE { ?cust rdfs:type <http://example.com/#Customer>.
                       ?cust <http://example.com/#Name> ?name }

As we can see, the triplet patterns are separated from each other by a dot. The result of this query will be like this:

?cust?name
<http://example.com/#alpha>"Alpha, JSC"
<http://example.com/#beta>"Beta, LLC"

Let's complicate the request even more. Suppose we immediately need the names of higher structures for each company. At the same time, we do not want to see the URI in the query results. It must be taken into account that some companies may not have higher structures, so the pattern has an optional part that may not be present in all results. The request will look like this:

SELECT ?name ?parent_name WHERE {
                 ?cust rdfs:type <http://example.com/#Customer>.
                       ?cust <http://example.com/#Name> ?name.
            OPTIONAL { ?cust <http://example.com/#Child> ?parent.
                       ?parent <http://example.com/#Name> ?parent_name }
                     }

Let's pay attention to the following syntax details:

  • Instead of * in the list of selected values, you can specify, separated by a space, the names of those variables that are needed in the list of returned values. The remaining variables are used in the structure of the request patterns, but will not be returned as results.
  • The OPTIONAL clause (there can be several of them in a query) is used to specify additional patterns that the query results may, but are not required to match. This is very useful in cases where objects and their properties are selected, but not every object has properties set. If a property is placed in the main pattern, and not in OPTIONAL, then the result will only include those objects whose property value is set.

The result of this query will be like this:

?name?parent_name
"Alpha, JSC"
"Beta, LLC""Alpha, JSC"
In semantic models, each property can have any number of values ​​- that is, each company in our example can have several parent organizations. Let's imagine that Beta has another superior, Gamma. Then the query result will look like this:
?name?parent_name
"Alpha, JSC"
"Beta, LLC""Alpha, JSC"
"Beta, LLC""Gamma"

As we can see, for each combination of vertices and edges of the graphs that correspond to the query results, a separate result line is displayed. Thus, the same entity, the same part of the graph, can appear in the query results several times. The more complex the query, the more likely it will become overloaded with duplicate results. To make life easier, the SELECT DISTINCT expression returns only unique query results. It is also useful to reduce the number of variables returned by a query.

To better understand the mechanism for executing a SPARQL query, let's look at how the pattern contained in the query is applied to the vertices and edges of the graph - using the example of the last query:


Rice. 20. Executing a SPARQL query - searching for graph edges that match patterns

In Fig. Figure 20 shows only one part of the graph corresponding to this query. However, it is easy to imagine the mechanism for executing a SPARQL query as “fitting” a pattern to all possible combinations of triplets that exist in the model.

We'll return to a deeper look at SPARQL syntax later, but for now we'll give a brief overview of some of the useful features of this language. Let's sort the results of a request for a list of all clients by client name, and display 10 results, starting from the 5th:

SELECT * WHERE { ?cust rdfs:type <http://example.com/#Customer>.
                 ?cust <http://example.com/#Name> ?name }
                 ORDER BY ASC(?name) OFFSET 5 LIMIT 10

Query results can be filtered using the FILTER clause. For example, we are only interested in those clients whose name contains the expression LLC:

SELECT * WHERE { ?cust rdfs:type <http://example.com/#Customer>.
                 ?cust <http://example.com/#Name> ?name
                 FILTER(CONTAINS(STR(?name), "LLC")) }

When filtering, you can compare numeric values ​​and perform mathematical operations, but you need to keep track of the types - most often, they need to be specified explicitly using the ^^ operator. Note that in this case the ?name variable is explicitly cast to a string type.

Let's complete our discussion of the SPARQL topic with examples of queries for adding and deleting data to the ontology. The request to add data looks simple:

INSERT DATA { <http://example.com/#alpha> <http://example.com/#Name> "Alpha, JSC."
<http://example.com/#beta> <http://example.com/#Name> "Beta, LLC" }

There is no analogue of the SQL UPDATE query in SPARQL; data can only be deleted or added. Therefore, if there is a need to make changes to already existing triplets, the existing data must first be deleted:

DELETE WHERE { <http://example.com/#beta> ?b ?c }

This request will remove all information about the Beta client (but will not remove references to it from other objects). For obvious reasons, an INSERT query cannot use variables, but a DELETE query can.

There is also a CONSTRUCT query, similar to SELECT, but returning the results not as a table, but as a new RDF graph (actually as a file).

You can check the existence of solutions for a SPARQL pattern using the ASK query, which returns true if there is at least one solution, and false otherwise. For example, we find out whether there are companies that have a parent organization:

ASK { ?cust <http://example.com/#Child> ?parent }

In our model, the result of such a query will be true.

In this chapter, we looked at the normal way of working with triplet storage through the SPARQL access point. In this mode, the point works like a regular database, that is, it returns exactly the information that was placed in it. Meanwhile, the power of semantic technologies lies in the ability to automatically obtain logical conclusions.

Next section: Machines and rules of inference