<!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>Magic Shapes for Validation in SHACL?</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Shqiponja Ahmetaj</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Bianca Lohnert</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Magdalena Ortiz</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Mantas Simkus</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Copyright 2021 for this paper by its authors. Use permitted under Creative Commons License Attribution 4.0 International</institution>
          ,
          <addr-line>CC BY 4.0</addr-line>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>TU Wien</institution>
          ,
          <country country="AT">Austria</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>WU Wien</institution>
          ,
          <country country="AT">Austria</country>
        </aff>
      </contrib-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>-</title>
      <p>
        The Shape Constraint Language (SHACL) was recently standardized by the
W3C as a formalism for checking the quality of RDF graphs; we refer to [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] for
an introduction. In SHACL, the main problem is to check whether a given RDF
graph G validates a SHACL document (C; T ), where C is a set of constraints, also
called shapes graph, each associated to a so-called shape name, and T (targets)
is a speci cation of nodes from the data graph which should validate certain
shapes from C. For illustration, consider a graph G = fenrolledIn(Ben; C1)g
and a SHACL document (C; T ), where C = fStudent $ 9enrolledIn:Courseg,
and T is the shape atom Student(Ben). The constraint states that each Student
must be enrolled in some course; Student is a shape name, and enrolledIn and
Course are data predicates, i.e., property and class name, respectively. Clearly,
G does not validate (C; T ), but the extended graph G0 = G[fCourse(C1)g does.
      </p>
      <p>
        The standard speci es a syntax for expressing SHACL constraints and
describes when they are validated by RDF graphs. However, it leaves unde ned the
semantics of recursive constraints, i.e., constraints that involve cyclic
dependencies. To address this, some logic-based proposals to formalize the semantics of
full SHACL have emerged recently. Andresel et al. [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] proposed a semantics based
on the stable models semantics for logic programs, stricter than the semantics
based on classical logic due to Corman el al. [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]. Both semantics coincide with
the o cial recommendation for non-recursive constraints, and unfortunately, the
validation problem is NP-complete under both.
      </p>
      <p>
        To make SHACL truly useful and facilitate its adoption, we need automated
tools that e ciently implement validation and scale well in the presence of large
RDF graphs and sets of constraints. There are already signi cant e orts in this
direction for fragments of SHACL [
        <xref ref-type="bibr" rid="ref3 ref6">3, 6</xref>
        ]. Shacl2Sparql [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] is a SHACL
validation engine that checks conformance of RDF graphs with SHACL constraints
by evaluating SPARQL queries against the data, which optimzes the order in
which shapes are processed. Further optimization techniques are implemented in
Trav-Shacl [
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. However, these works focus on tractable fragments of SHACL.
They do not handle unrestricted interaction of recursion and negation in SHACL
constraints, which calls for verifying whether there exists some global assignment
of shapes to nodes in the graph that is consistent with all the constraints. This
cannot be easily done using top-down approaches implemented in existing
validators. To our knowledge, the only implementation that validates full SHACL
is the Shacl-Asp prototype1 from [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ], which translates SHACL constraints into
ASP programs and evaluates them using the DLV system2. We note that the
authors of [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] propose an algorithm for full SHACL that involves a SAT solver,
but to our knowledge, with no available implementation. The focus of this work
is SHACL validation in the presence of unrestricted negation and recursion.
      </p>
      <p>
        In this extended abstract we present preliminary work on adapting the Magic
Sets technique, known from logic programs with (unstrati ed) negation [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ],
to SHACL constraints, as a way to improve performance and applicability of
SHACL validators. Magic Sets [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] is a well-known optimization method from
logic programming and deductive databases. In a nutshell, it uses the query goal
to adorn the input program with binding information, obtaining a new program
whose bottom-up evaluation, analogously to the top-down one, involves ground
atoms only from the relevant part of the data. When applied to SHACL shape
graphs, it allows us to do validation on a potentially much smaller fragment of
the input RDF graph, while also ingoring shape constraints in the input that do
not a ect validation of the targets. This is particularly useful in the presence of
negation and recursion, which may result in inconsistency and non-validation,
even when they are irrelevant to the target of interest. We report some
preliminary experiments showing that the technique signi cantly improves the
performance of the Shacl-Asp validator, and it may also allow the exploitation of
the more optimized engines Shacl2Sparql and Trav-Shacl for input shape
graphs that they could not originally handle.
1
      </p>
    </sec>
    <sec id="sec-2">
      <title>Preliminaries</title>
      <p>
        Let N, C, and P denote countably in nite, mutually disjoint sets of nodes, class
names, and property names, respectively. A data graph G is a nite set of atoms
of the form B(c) and p(c; d), where B 2 C, p 2 P, and c; d 2 N. The set of
nodes appearing in G is denoted with V (G). We assume a countably in nite set
S of shape names, disjoint from N [ C [ P. A shape atom is an expression of the
form s(a), where s 2 S and a 2 N. A path expression E is a regular expression
built using the usual operators , , [ from symbols in P+ = P [ fp j p 2 Pg. If
p 2 P, then p is the inverse property of p. A (complex) shape is an expression
obeying the syntax: ; 0 ::= &gt; j s j A j a j ^ 0 j : j n E: j E = E0; where
s 2 S, A 2 C, c 2 N, n is a positive integer, and E, E0 are path expressions.
A (shape) constraint is an expression s where s 2 S and is a possibly
complex shape; we may refer to as the body of the constraint and s as head.
W.l.o.g. we view targets as shape atoms of the form s(a), where s 2 S and a 2 N.
A SHACL document is a pair (C; T ), where (i) C is a set of constraints and (ii)
T is a set of targets. The evaluation of a shape expression is given by assigning
nodes of the data graph to shape names. A (shape) assignment for G is a set
I = G [ L, where L is a set of shape atoms such that a 2 V (G) for each s(a) 2 I.
The evaluation of a shape w.r.t. I is given in terms of a function that maps a
1 https://github.com/medinaandresel/shacl-asp
2 http://www.dlvsystem.com/dlv/
shape expression to a set of nodes, and a path expression to a set of pairs of
nodes. We refer to [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] for more details. We focus on the stable model semantics.
2
      </p>
    </sec>
    <sec id="sec-3">
      <title>Magic Shape Algorithm for SHACL</title>
      <p>We now brie y describe the Magic algorithm for SHACL, which takes as input a
document (C; T ) and outputs a new optimized shapes graph against which data
graphs can be validated, see Figure 1. The method comprises four main steps:
T
C</p>
      <p>Magic Shape Algorithm</p>
      <p>Create Magic Seeds</p>
      <p>Adorn
Generate
Modify</p>
      <p>C_magic</p>
      <p>T</p>
      <p>
        data graph
SHACL
Validator
1. Create Magic Seeds: for each target shape atom s(a) 2 T , the algorithm
adds a constraint of the form s magic a, called a magic seed.
2. Adorn: for each shape name s occurring in the targets T , an adorned shape
sb is pushed onto a stack. Procedure Adorn then pops sb and propagates the
adornment to the body of the constraints with head s, pushing each freshly
adorned shape onto the stack.
3. Generate. The adorned set of constraints is then used to generate \magic"
constraints through the procedure Generate. Roughly, for a constraint r of
the form s , it creates a new constraint with s magic in the body and
s0 magic in the head for each adorned shape name s0 in . In case that in
the constraint two shapes are connected by a path expression E, the path
expression has to be inverted.
4. Modify enhances the body of each adorned constraint with a magic version
of the shape occurring in the head of the constraint, i.e. r will be rewritten
as s s magic ^ and added to the set of modi ed constrains.
In the presence of arbitrary negation and recursion, we also need to identify the
so-called dangerous constraints that appear under the scope of negation. If they
are relevant for validating the target, their adornments are propagated also from
the body to the head [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ].
      </p>
      <p>Example 1. Consider a SHACL document (C; T ), where C consists of the
constraints s1 s2, s2 s3 ^ s4, s3 5 p:s5, s6 :s6, and T = fs1(a)g. The
algorithm rst adds s1 magic a. Since s1 appears in T , the shape names s1 to
s5 and the corresponding constraints will be adorned. The last (disconnected)
constraint will not participate in the resulting program. Generate(ra) adds
s3 magic^
s2 magic ^ s3 ^ s4, and s3
s2 magic s1 magic, s3 magic s2 magic, s4 magic s2 magic and s5 magic
p :s3 magic. Finally, the modi ed constraints are: s1 s1 magic ^ s2, s2
5 p:s5.</p>
      <p>1
3</p>
    </sec>
    <sec id="sec-4">
      <title>Implementation and Experiments</title>
      <p>We implemented a prototype of the Magic Shapes Algorithm. It receives as input
a shapes graph (in Turtle syntax) and produces as output a new shapes graph.
To evaluate the usefulness of the approach, we perform experiments with the
Shacl-Asp validator. For each test case, we run the validator once with the
original shapes graph and once with the magic variant.</p>
      <p>
        Data Graph. The data for the experiments was obtained from DBPedia
(version 2016-10)3. More precisely, we used the datasets \PersonData", \Instance
Types", \Labels", \Mappingbased Literals" and \Mappingbased Objects". The
datasets are in Turtle syntax and contain about 61 million triples (7.7 GB).
Shapes Graph. We created six shapes graphs4 S1 to S6 from the domain of
movies and actors, similar to those in [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Some of the shapes graphs are
inconsistent, i.e., there is no valid shape assignment. As target we use either the
class \dbo:Person" for \MusicianShape" (S1; S2; S4) or the class \dbo:Film" for
\MovieShape" (S3; S5; S6).
      </p>
      <p>Setting. The experiments were performed on a Linux server with a 24 core Intel
Xeon CPU running at 2.20 GHz and 264 GB of RAM. We used the DLV based
Shacl-Asp validator. An Apache Jena TDB5 was used as an RDF triple store
to retrieve the relevant triples from the data graph, which are transformed into
ASP facts; this was done for the input shapes graph and for its magic version.
Results. Table 1 summarizes the results of our experiments. Although all inputs
except S1 are recursive, the output of the Magic Shapes algorithm is only
recursive for S3, S5 and S6. The shapes graphs S4 to S6 are inconsistent in general. S4
and S5 become consistent after running the algorithm since the constraints
causing inconsistency are not relevant for validating the targets; this is not the case
for S6. In some cases the output of the algorithm falls in a fragment that is
supported by Shacl2Sparql or Trav-Shacl although the original shapes graphs
are not, e.g., S4 and S5. We distinguish the non-recursive fragment Lnon-rec and
the fragment Ls, which restricts the interaction between recursion and negation.
The last column shows that the Magic Shapes algorithm signi cantly improves
the performance of the DLV validator.
4</p>
    </sec>
    <sec id="sec-5">
      <title>Conclusion</title>
      <p>In this extended abstract, we have presented initial results on adapting the Magic
Set technique for improving the performance of SHACL validators. This is
par3 http://downloads.dbpedia.org/wiki-archive/downloads-2016-10.html
4 https://github.com/biziloehnert/magicSHACL/tree/master/experiments
5 https://jena.apache.org/documentation/tdb/
case
S1
S2
S3
S4
S5
S6</p>
      <p>#shapes recursive inconsistent Lnon-rec Ls shacl-asp
orig. magic orig. magic orig. magic orig. magic orig. magic orig. magic
ticularly useful when the input shapes graphs contain unrestricted interaction of
recursion and negation. We performed experiments with Shacl-Asp, the only
SHACL validator that can support such shape graphs, and showed that the
simpler shapes graphs produced by the Magic Shapes algorithm are evaluated
signi cantly more e ciently than the original ones. The algorithm may also discard
constraints with recursion or negation that are not relevant for validating the
targets, possibly eliminating some irrelevant inconsistency. The resulting shapes
graphs may fall into SHACL fragments that can be handled by more optimized
SHACL adopters such as Shacl2Sparql and Trav-Shacl, thus enabling the
use of these engines on graphs that they could not originally support.</p>
      <p>We are running some experiments to identify cases where the Magic Shapes
technique can also improve the performance of Shacl2Sparql or Trav-Shacl
on inputs that they can handle.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1.
          <string-name>
            <surname>Andresel</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Corman</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ortiz</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Reutter</surname>
            ,
            <given-names>J.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Savkovic</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Simkus</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Stable model semantics for recursive SHACL</article-title>
          .
          <source>In: Proc. of The Web Conference</source>
          <year>2020</year>
          . p.
          <fpage>15701580</fpage>
          .
          <string-name>
            <surname>ACM</surname>
          </string-name>
          (
          <year>2020</year>
          ). https://doi.org/10.1145/3366423.3380229
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Bancilhon</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Maier</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Sagiv</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Ullman</surname>
          </string-name>
          , J.:
          <article-title>Magic sets and other strange ways to implement logic programs (extended abstract)</article-title>
          .
          <source>In: PODS '86</source>
          (
          <year>1985</year>
          )
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Corman</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Florenzano</surname>
            ,
            <given-names>F.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Reutter</surname>
            ,
            <given-names>J.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Savkovic</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          :
          <article-title>Validating shacl constraints over a sparql endpoint</article-title>
          .
          <source>In: ISWC</source>
          . Springer (
          <year>2019</year>
          ). https://doi.org/10.1007/978-3-
          <fpage>030</fpage>
          -30793-6 9
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4.
          <string-name>
            <surname>Corman</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Reutter</surname>
            ,
            <given-names>J.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Savkovic</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          :
          <article-title>Semantics and validation of recursive SHACL</article-title>
          .
          <source>In: Proc. of ISWC'18</source>
          . Springer (
          <year>2018</year>
          ). https://doi.org/10.1007/978-3-
          <fpage>030</fpage>
          - 00671-6 19
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Faber</surname>
            ,
            <given-names>W.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Greco</surname>
            ,
            <given-names>G.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Leone</surname>
          </string-name>
          , N.:
          <article-title>Magic sets and their application to data integration</article-title>
          .
          <source>J. Comput. Syst. Sci</source>
          . pp.
          <volume>584</volume>
          {
          <issue>609</issue>
          (
          <year>2007</year>
          ). https://doi.org/10.1016/j.jcss.
          <year>2006</year>
          .
          <volume>10</volume>
          .012
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <surname>Figuera</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Rohde</surname>
            ,
            <given-names>P.D.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Vidal</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          :
          <article-title>Trav-shacl: E ciently validating networks of SHACL constraints</article-title>
          .
          <source>In: WWW</source>
          . pp.
          <volume>3337</volume>
          {
          <fpage>3348</fpage>
          . ACM / IW3C2 (
          <year>2021</year>
          ). https://doi.org/10.1145/3442381.3449877
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7.
          <string-name>
            <surname>Gayo</surname>
            ,
            <given-names>J.E.L.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Prud</surname>
          </string-name>
          'hommeaux, E.,
          <string-name>
            <surname>Boneva</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kontokostas</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          :
          <article-title>Validating RDF Data</article-title>
          .
          <source>Synthesis Lectures on the Semantic Web: Theory and Technology</source>
          , Morgan &amp; Claypool Publishers (
          <year>2017</year>
          ). https://doi.org/10.2200/S00786ED1V01Y201707WBE016
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>