<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>Query-Driven Approach for SHACL Type Inference</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>David Haller</string-name>
          <email>david.haller@fau.de</email>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Workshop ProceedingsC(EUR-WS.org)</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="editor">
          <string-name>Semantic Web, Schema Inference, Query-Driven, Data Integration</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Supervised by Richard Lenz, Friedrich-Alexander-Universität Erlangen-Nürnberg</institution>
          ,
          <addr-line>Professorship for Evolutionary Data Management</addr-line>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Workshop Proce dings</institution>
        </aff>
      </contrib-group>
      <abstract>
        <p>The Semantic Web enables everyone to share knowledge that can be reused in diferent applications. While the use of a formal ontology describing the semantics of the shared data is encouraged, it cannot be enforced and is often done incorrectly, incompletely, or not at all. However, the semantics are present in the minds of those working with the data, as manifested in all the SPARQL queries they have written. Therefore, analyzing these query logs helps us to learn these semantics and allows us to construct a graph of SHACL shapes describing the types and their constraints of a data source, which can serve as the foundation for a human-in-the-loop approach to further extend and correct the generated schema.</p>
      </abstract>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>and errors in applications.</p>
      <p>In a perfect world, data always represents informationcourse, the Semantic Web also introduced methods to
with known semantics. This is easy to achieve when the formally define an ontology as the foundation of a
knowlapplications that produce and consume data are alwaysedge graph, using schema languages such as RDF Schema
built by the same people, and the underlying schema is (RDFS), Web Ontology Language (OWL), or Shapes
Conrarely changed and well understood. In reality, data isstraint Language (SHACL), which even allow us to derive
constantly being used in new contexts, shared with exter- new statements from existing statements by applying
nal parties, and combined with other data from diferent logical conclusions. While traditional database systems
sources. As a result, the meaning of data is not alwaystypically follow the closed-world assumption (a
stateclear, because everyone may have their own concept ofment is true if and only if it is explicitly stated), the
seeing the world, so data is not interpreted in the waySemantic Web is based on the open-world assumption
it was intended, which can lead to incorrect conclusions(a statement can be true even if it is not present in the</p>
      <p>The Semantic Web has introduced standards to make
be reused in a diferent context. In the past,
knowlit easier to share data across applications so that it cacnreating an ontology or in interpreting it, which can have
present, machines can also make use of the knowledge example in RDFS:
edge was only made available in human-readable forme,ven if 99.9% of a knowledge base is correct, a few wrong
such as texts and images, or in proprietary formats thasttatements can cause the reasoner to infer types that are
could only be processed by special programs. In theobviously nonsense. This issue is illustrated with a short
graph, because it may be added later or stated elsewhere).</p>
      <sec id="sec-1-1">
        <title>The problem is that humans make mistakes, either in</title>
        <p>huge consequences. For example, it is shown in2[] that
ing the same terms, meaning that there is no restriction
that only schema-compliant statements can be added. Of
available on the World Wide Web, similarly to humans.
This was done by establishing the Resource Description
Framework (RDF), a flexible data model based on directed</p>
      </sec>
      <sec id="sec-1-2">
        <title>Internationalized Resource Identifier (IRI) [1]. The advantage of this model is that you can reference anything in another dataset by simply inserting a new edge, the</title>
        <p>:Berlin :location :Germany
:location rdfs:domain :City</p>
        <p>A reasoner that is applying the rules given by
rdfs:domain and rdfs:range, would infer that Berlin
must be a city, and Germany must be a country, which is
graphs, whose nodes are globally addressable with an :location rdfs:range :Country
web page. The disadvantage is that nobody can prevent
same as you would use a hyperlink to point to a diferent correct in the real world. Let’s add the following triple:
multiple individuals talking about diferent things us- :Zugspitze :location :Germany
VLDB 2023 PhD Workshop, co-located with the 49th International
0000-0001-5287-7187 (D. Haller)</p>
      </sec>
      <sec id="sec-1-3">
        <title>Now the reasoner would infer that the Zugspitze is also</title>
        <p>a city, but in reality it is Germany’s highest mountain and
is located within Germany. The propert:ylocation was
used without considering its domain and scope.
Knowledge graphs often sufer from problems like this.
There</p>
      </sec>
    </sec>
    <sec id="sec-2">
      <title>2. Approach</title>
      <p>has aname attribute that is sortable, waeb attribute that
can contain URLs, and a multi-valuedchild attribute
The semantics of the data stored in a knowledge graph(because the edges are counted, which only makes sense
is determined by the people who created it in the first if you expect the result to be greater than 1 in some
place. They had a certain perception of reality in theiroccurrences). We also know that the ?person variable
minds, which guided their choice of data types and prop-must be an IRI, because only IRIs are allowed as subjects
erties. This can be called amental model. As described in RDF and SPARQL, and the variable is also used for
in [3], models used in databases or applications are notgrouping, so it must be some kind of candidate key for
created from objectively perceivable reality, but from thea Person. The ?child variable must be an IRI because it
individual perspectives of both their creators and theirhas aage attribute, which is numeric and has 18 as a
users (which may not always coincide). In cases wheresomewhat meaningful threshold.
an ontology is not available, erroneous, or incomplete, it This information extracted from a single SPARQL
is impossible to validate the data using a schema consis-query can be represented as a SHACL shape. As
mentency check. The mental model needs to be inferred from tioned earlier, SHACL is a schema language that can be
other sources. Doing so manually is a tedious and erroru-sed to describe the semantics of a knowledge graph and,
prone task, so it is desirable to automate the process aslike OWL and RDFS, is a W3C recommendation.
Unfar as possible. like OWL, SHACL focuses on restricting which triples</p>
      <p>When people write queries that access, transform, orare allowed by defining a set of patterns, called shapes,
modify data, they do so based on their own mental model.that a set of triples must match, which is similar to the
As a consequence, the approach presented in this paperclosed-world assumption, while OWL focuses on
buildis based on analyzing SPARQL query logs and using the ing complex ontologies, which is similar to the
openextracted information to automatically infer type con-world assumption. We infer the types present in the
straints that can be used to create SHACL shapes for eachmental model from queries and create SHACL shapes for
referenced resource or property. A large set of querietshem, so that each entity in the knowledge graph can
can thus be translated into a SHACL graph that serves asbe associated with a shape with some probability. Our
a schema definition for the actual RDF graph containing example query from earlier would result in the following
the data. Each SPARQL query using the verbsselect, shape graph.
ask, orconstruct contains information about the
entities it is interested in. Theirwhere clauses each contain &lt;PersonShape&gt; a sh:NodeShape ;
a basic graph pattern, which is a set of triple patterns hav- sh:property [ sh:path :name ] ;
ing the structure( ∪  ) × ( ∪  ) × ( ∪  ) , where is the sh:property [ sh:datatype xsd:string ;
set of query variables , the set of all IRIs, and =  ∪  ∪  sh:path :web] ;
with  and  being the set of literals and blank nodes. A sh:property [ sh:nodeKind sh:IRI ;
basic graph pattern restricts the possible structure of the rdfs:range &lt;ChildShape&gt; ;
subgraph a query searches for, queries can thus be seen sh:path :child] .
as partial schema definitions. They reflect, at least in part, &lt;ChildShape&gt; a sh:NodeShape ;
how the author envisions their knowledge graph; this sh:property [ sh:datatype xsd:integer ;
applies also to the usage of filters (boolean expressions sh:path :age] ; .
over query variables) and solution modifiers (likeorder
by). For example, someone could write a query such as: By analyzing further queries and checking with the
currently existing triples, we may find that persons and
s e l e c t ? name ? web count ( ? c h i l d ) children share almost the same attributes and are
therewhere fore related types, such as subclasses. The more queries
{ we have, the more detailed the results will be. From
this single query, we already know that being a child
has something to do with being associated with a person,
suggesting at least two entities involved in a relationship.
? p e r s o n : name ? name .
? p e r s o n : web ? web .
? p e r s o n : c h i l d ? c h i l d .
? c h i l d : a g e ? a g e .
f i l t e r ( ? a g e &lt; 18 &amp;&amp; ? web == ” h t t p
: / / e x a m p l e . com ” )</p>
      <p>2.1. Related Work
}
group by ( ? p e r s o n )
order by ( ? name )</p>
      <sec id="sec-2-1">
        <title>This approach is calledquery-driven in contrast to com</title>
        <p>mon data-driven approaches where semantics are
extracted from existing instance data using data profiling</p>
        <p>We learn from this query that its author expected themethods [4]. The advantage of this approach is that the
knowledge graph to have entities of typPeerson. A Person mental model may not be fully present in the data, but is
present in the queries; for example, a query may refer todata type. If a subject variable is also used as an object
unmet expectations that a user may have had. The twovariable, we can infer that the query is looking for a
relaapproaches can complement each other to provide a morteionship between two entities that is likely to be present
complete view of the mental model. Other query-driven in the mental model. Therefore, we declare the property
approaches are scarce, in5][they use queries todiscover in the predicate must point to an IRI, and the scope of
data sources satisfying a given data need, while6][ uses this property is the generated shape that was assigned
queries toexplain unsatisfactory answers in knowledge to the object variable.
bases andsuggest query modifications, but queries are Applying this procedure to a large set of queries
genernot yet used for schema inference. ates a large set of partial types. A major challenge of our
approach is to merge all the partial type definitions
describing the same type into a unified type definition. We
3. Contribution use a density-based clustering algorithm as in10[], but
instead of clustering triple instances of the dataset, we
The previous prototypeP,haros [7], focused on query- apply this approach to the SHACL shapes harvested from
driven schema inference based on SQL query logs. We the query logs. Similar shapes are merged, which is
simmoved away from SQL because there are few openlyply the union of their triple sets. To speed up the process,
available query logs that use many non-standard SQLwe use a preprocessing method according to11[]. SHACL
terms, making them dificult to analyze. Since the con- shapes generated from similar queries are grouped
tocept itself is language agnostic, it could also be applied to gether because they are more likely to be merged by the
SPARQL query logs, where more query logs are availableclustering algorithm.
and have already been extensively studied8[], perhaps The result will not always be complete and depend
due to the popularity of RDF for publishing open data,on the queries being analyzed. If some properties or
rewhile RDBMS are used more for internal purposes. sources are not used in queries, they will never be
discov</p>
        <p>In addition, there are standardized ways to transformered. This can be avoided by combining our query-driven
SPARQL queries from their textual form into an RDFapproach with the data-driven approaches found in the
graph [9], which allows queries to be treated like graphs,literature. But even then there will be inconsistencies
for example by applying graph distance measures to them.</p>
        <p>in the resulting shape graph that need to be corrected
Graph representations of queries also enabmleeta query- either manually, or by weighting conflicting statements
ing: using SPARQL queries to search for SPARQL queries by frequency, which means assuming that the majority
within a query repository. Finally, SPARQL is betterof users most likely have the more correct concept about
suited for query-driven schema inference because the in-some real-world entity. Either way, much more work
formation needs in the query are quite explicit, as you arewould be required if the shape graphs were created from
required to list all the properties your desired resource</p>
        <p>scratch, as it would require both domain knowledge and
shall have, which basically means giving a (partial) typethe need to check for inconsistencies in the data itself.
definition.</p>
        <p>We adapted our prototype to parse SPARQL query
logs and infer types from them. A type can be thought 4. Evaluation
of as a set of attributes. Subtypes are subsets of their
parent attribute sets, while the intersection of two at-The prototype was evaluated in a real-world scenario.
tribute sets could be called a category or type trait. MostThe grade management and analysis softwareThe Grade
SPARQL queries consist of a conjunction of RDF graph Explorer is a web application built on Semantic Web
techpatterns that specify conditions that must all be satisfied, niques. All data about exams, students, and grades is
efectively describing the subgraph relevant to the query. stored in an RDF knowledge graph managed by Apache
Some queries also have filters that must be satisfied by the Jena, and all data reads and writes are performed using
attribute values. These two types of queriesc,onjunctive SPARQL queries. The Grade Explorer was developed at
patterns (CQs) andconjunctive patterns with filters (CQFs), our chair over several semesters in the context of a
pracare the most common types of queries encountered8][, tical software development course. The application has
which is why we focus on them. had its stable release and is already used in production</p>
        <p>Since we know that subject variables in RDF must for various exams.
always be IRIs and thus describe resources, we can parti- The students were organized as a scrum team, while
tion the attribute sets by subject, as we did in the SHACL chair members acted as product owners or scrum masters.
shape example in the last section. Each SPARQL subject In scrum, developers implement feature requests, called
variable gets a SHACL node form, each property useduser stories, within an incremental process, delivering a
with that subject becomes a SHACL property. If literalsusable product after each iteration. As a side efect, the
were used as objects, we assign their type as a SHACL underlying knowledge graph was being expanded on the
lfy, with no semantic control. Over time, nobody had an sion by a user [13]. Analyzing these small changes could
overview of the meaning of the stored data, and the chaoshelp better understand user intent, as queries that are
corwas exacerbated by the fact that students changed everyrected likely didn’t return the desired results and don’t
semester. The mental model used for handling exams,need to be considered for schema inference. Applications
grades and students was never formalized, but hidden in can help users reformulate their queries until they meet
the used SPARQL queries. their needs [14], for example, by using auto-completion</p>
        <p>It can be dificult to handle exams properly, as it is nec- or by displaying queries from other users working with
essary to carefully follow the oficial exam regulations the same data sources. Finally, our approach could be
and deal with various special cases, like exmatriculatedleveraged by using a large language model trained on
students surprisingly showing up on exam day. The soft- our query logs, which can then be prompted to extract
ware should take away that burden from the shoulders otfhe implicit knowledge hidden in the queries, similar to
the examiners and should do that correctly, so we neededwhat has been done in 1[5].
to verify that all data was being stored and interpreted
as it should be. We had one student manually create a
SHACL shape graph that was manually validated by us,References
to serve as our ground truth. Then, the modifiedPharos
prototype analyzed a large query log generated by typical [1] A Semantic Web Primer, Cooperative Information
use of The Grade Explorer and by running the normal test Systems, 3rd ed., MIT Press, Cambridge, 2012.
cases. It created its own SHACL shape graph based on [2] H. Paulheim, C. Bizer, Type inference on noisy RDF
what could be inferred from the queries alone. These two data, in: ISWC, 2013.</p>
        <p>SHACL shape graphs share a Jaccard similarity of 70 %.[3] C. Floyd, R. Klischewski, Modellierung - ein
HandThe Jaccard similarity between two RDF grap hs1,  2 grif zur Wirklichkeit, in: Modellierung ’98,
Proof type sh:NodeShape with their attribute set s 1,  2 is ceedings des GI-Workshops in Münster, 1998.
[4] T. Papenbrock, et al., Data profiling with Metanome,
defined as   ( 1,  2) = || 11∪∩ 22|| . VLDB J. 8 (2015).</p>
        <p>The diferences can be explained by the fact that some [5] C. Diamantini, et al., A semantic data lake model for
constraints in the manually created shape graph, such as analytic query-driven discovery, in: iiWAS, 2022.
sh:maxCount orsh:minCount, cannot be guessed from [6] L. Parkin, Cooperative techniques for dealing with
the provided queries or the existing triples, and require unsatisfactory answers in RDF knowledge bases,
domain knowledge, for example that students cannot in: VLDB PhD Workshop, 2021.
take an exam twice on the same day, but may be able to [7] D. Haller, R. Lenz, Pharos: Query-driven schema
try again on a diferent date. inference for the Semantic Web, in: Machine
Learn</p>
        <p>While the result has some ambiguity, applying the ing and Knowledge Discovery in Databases, 2020.
prototype to a real-world example has shown that the[8] A. Bonifati, W. Martens, T. Timm, An analytical
approach provides a time-saving benefit whenever it is study of large SPARQL query logs, VLDB J. 29
necessary to reverse-engineer the schema of a knowledge (2020).
graph, by automating most of the work. [9] M. Saleem, et al., LSQ: The linked SPARQL queries
dataset, in: ISWC, 2015.
5. Future Work [10] K. Kellou-Menouer, Z. Kedad, Schema discovery in
RDF data sources, in: Conceptual Modeling, 2015.</p>
        <p>The approach is depended on the quality of the query[11] R. Bouhamoum, et al., Scaling up schema discovery
logs. Not all queries contain much useful information. If for RDF datasets, in: ICDEW, 2018.
no human-readable variable names were used, it is difi- [12] B. Ell, et al., Deriving human-readable labels from
cult to assign meaningful names to the generated shapes. SPARQL queries, in: SEMANTICS, 2011.
However, there are methods to derive variable names[13] A. Bonifati, W. Martens, T. Timm, SHARQL: Shape
from their query context1[2] which we will make use analysis of recursive SPARQL queries, in: SIGMOD,
of in the future. Queries can be classified into diferent 2020.
patterns with varying degrees of usefulness for schema[14] X. Zhang, et al., Revealing secrets in SPARQL
session level, in: ISWC, 2020.
inference; some queries just fetch a single triple with a
given IRI, while others contain detailed schema informa-[15] M. Urban, D. D. Nguyen, C. Binnig, OmniscientDB:
tion as in the example above. Queries that were classified A large language model-augmented DBMS that
unuseful could be discarded beforehand to speed up the knows what other DBMSs do not know, in:
Inprocess and avoid noise in the results. An interesting phe- ternational Workshop on Exploiting Artificial
Intelnomenon arestreaks, a series of queries that are incremen- ligence Techniques for Data Management, 2023.
tally expanded, representing an exploratory query
ses</p>
      </sec>
    </sec>
  </body>
  <back>
    <ref-list />
  </back>
</article>