Ask Your Question
0

Can a SHACL schema incorporate the definition of a discriminated union?

asked 2021-04-28 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-05-12 03:00:00 +0000

woof gravatar image

Yes, a SHACL schema can incorporate the definition of a discriminated union using the sh:or constraint. The sh:or constraint allows the definition of a union of constraints, where only one of the constraints needs to be satisfied for the data to be valid. The sh:or constraint can be used to define a discriminated union by using a combination of sh:nodeKind and sh:or constraints. Here is an example of how this can be done:

@prefix ex: <http://example.com/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Document
  a sh:NodeShape ;
  sh:targetClass ex:Document .

ex:TextDocument
  a sh:NodeShape ;
  sh:targetClass ex:TextDocument ;
  sh:nodeKind sh:BlankNodeOrLiteral ;
  sh:or (
    [ sh:datatype xsd:string ] 
    [ sh:nodeKind sh:Literal ; sh:hasValue "text" ]
  ) .

ex:ImageDocument
  a sh:NodeShape ;
  sh:targetClass ex:ImageDocument ;
  sh:nodeKind sh:Resource ;
  sh:or (
    [ sh:class ex:JpegImage ] 
    [ sh:class ex:PngImage ]
  ) .

ex:JpegImage
  a sh:NodeShape ;
  sh:targetClass ex:JpegImage .

ex:PngImage
  a sh:NodeShape ;
  sh:targetClass ex:PngImage .

In this example, we have defined a discriminated union between ex:TextDocument and ex:ImageDocument. The ex:TextDocument shape specifies that it must either be a string or have the value "text". The ex:ImageDocument shape specifies that it must either be an instance of ex:JpegImage or ex:PngImage.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-28 11:00:00 +0000

Seen: 18 times

Last updated: May 12 '21