ArticleslgStudy

science

SHACL

SHACL is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand SHACL rather than just read about it. In short: Shapes Constraint Language (SHACL) is a World Wide Web Consortium (W3C) standard language for describing Resource Description Framework (RDF) graphs. SHACL has been designed to enhance the semantic and technical interoperability layers of ontologies expressed as RDF graphs.

Key takeaways

  • SHACL belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect SHACL to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of SHACL from memory before moving on to harder problems.

Reference excerpt

Shapes Constraint Language (SHACL) is a World Wide Web Consortium (W3C) standard language for describing Resource Description Framework (RDF) graphs. SHACL has been designed to enhance the semantic and technical interoperability layers of ontologies expressed as RDF graphs. SHACL models are defined in terms of constraints on the content, structure and meaning of a graph. SHACL is a highly expressive language. Among others, it includes features to express conditions that constrain the number of values that a property may have, the type of such values, numeric ranges, string matching patterns, and logical combinations of such constraints. SHACL also includes an extension mechanism to express more complex conditions in languages such as SPARQL and JavaScript. SHACL Rules add inferencing capabilities to SHACL, allowing users to define what new statements can be inferred from existing (asserted) statements.

Terminology SHACL lets its users describe shapes of data, targeting where a specific shape applies.

Property shapes A property shape describes characteristics of graph nodes that can be reached via a specific path. A path can be a single predicate (property) or a chain of predicates. A property shape must always specify a path. This is done by using sh:path predicate. One can think of property shapes that use simple paths as describing values of certain properties e.g., values of an age property or values of a works for property. Complex paths can specify a combination of different predicates in a chain, including the inverse direction, alternative predicates and transitive chains. Property shapes can be defined as part of a node shape. In this case, a node shape points to property shapes using sh:property predicate. Property shapes can also be "stand-alone" i.e., completely independent from any node shapes.

Node shapes A node shape describes characteristics of specific graph nodes irrespective of how you get to them. It can, for example, be said that certain graph nodes must be literals or a URIs, etc. It is common to include property shapes into a node shape, effectively defining values of many different properties of a node. For example, a node shape for an employee may incorporate property shapes for age and works for properties.

Constraints A constraint is a way to describe characteristics of values. A shape contains one or more constraint declarations. SHACL provides many pre-built constraint types. For example, sh:datatype is used to describe the type of literal values e.g., if they are strings or integers or dates. sh:minCount is used to describe the minimum required number of values. sh:length is used to describe the number of characters for a value.

Targets A target connects a shape with data it describes. The ways to define a shape target are:

State that a node shape is a class; the constraint applies to all instances of the class State that a shape targets members of a certain class. State that a shape targets a specific resource by giving its URI. State that a shape targets all subjects or all objects of triples with a certain predicate. Use a SPARQL query to select the targeted resources. Target declarations can be included in a node shape or in a property shape. However, when a property shape is a part of a node shape, its own targets are ignored. SHACL uses rdfs:subClassOf statements to identify targets. A shape targeting members of a class, also targets members of all its subclasses. In other words, all SHACL definitions for a class are inherited by subclasses.

Validation SHACL enables validation of graphs. A SHACL validation engine takes as input a graph to be validated (called data graph) and a graph containing SHACL shapes declarations (called shapes graph) and produces a validation report, also expressed as a graph. All these graphs can be represented in any Resource Description Framework (RDF) serialization formats including JSON-LD or Turtle. SHACL is fairly unique in its approach in that it builds-in not only the ability to specify a severity level of validation results, but also the ability to return suggestions on how data may be fixed if the validation result is raised. Built-in levels are Violation, Warning and Info, defaulting to Violation if no sh:severity has been specified for a shape. Users of SHACL can add other, custom levels of severity. Validation results may also have values for other properties, as described in the specification. For example, the property sh:resultMessage is designed to communicate additional textual details to users, including recommendations on how data may be fixed to address to validation result. In cases where a constraint does not have any values for sh:message in the shapes graph the SHACL processor may automatically generate other values for sh:resultMessage. Some SHACL processors (e.g., the one implemented by TopQuadrant) made these suggestions actionable in software, automating their application on user's request.

Specifications World Wide Web Consortium published the following SHACL Specifications:

SHACL (W3C Technical Recommendation) is the main document, defining the features of SHACL Core and its extension mechanism called SHACL-SPARQL. SHACL Core defines the basic syntax and structure of shapes, constraints, the built-in kinds of constraints, and how to link shapes to data nodes. SHACL-SPARQL defines how to express constraints that are not covered by the built-in constraint kinds. SHACL Advanced Features (W3C Working Group Note), the most recent version of which is maintained by the SHACL Community Group defines support for SHACL Rules, a powerful feature (inspired by SPIN rules) for data transformations, inferences and mappings based on data shapes. Also includes extensions of SHACL-SPARQL such as user-defined functions. SHACL JavaScript Extensions (W3C Working Group Note) defines how JavaScript can be used to express constraints, rules, functions and other features. This covers similar ground as SHACL-SPARQL, but using JavaScript as its execution language. SHACL Compact Syntax (SHACL Community Group Report).

Open-source tools The SHACL Test Suite and Implementation Report linked to from the SHACL W3C specification lists some open source tools that could be used for SHACL validation as of June 2019. By the end of 2019 many commercial RDF database and framework vendors announced support for at least SHACL Core. Some of the open source tools listed in the report are:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with SHACL

Start with the simplest possible case. Write down what SHACL claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to SHACL before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about SHACL ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of SHACL

In research
SHACL appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses SHACL in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
SHACL is common in secondary-school and first-year university syllabi. It links to neighbouring topics Resource Description Framework, World Wide Web Consortium standards, so understanding it makes those chapters shorter.
In everyday life
Look for SHACL outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study SHACL in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what SHACL means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain SHACL out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is SHACL in simple terms?

Shapes Constraint Language (SHACL) is a World Wide Web Consortium (W3C) standard language for describing Resource Description Framework (RDF) graphs. SHACL has been designed to enhance the semantic and technical interoperability layers of ontologies expressed as RDF graphs.

Why does SHACL matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study SHACL?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on SHACL.

Tags

  • Resource Description Framework
  • World Wide Web Consortium standards

Keep exploring