Reputation: 35
I try to validate my ontology instances using SHACL shapes. However, I cannot find how to say that a given property instance is valid only if it has an instance of Class1 as a subject and an instance of Class2 as an object.
In other words, I want to specify the domain (i.e., Class1) and the range (i.e., Class2) of this property.
In the following example, we precise that the range is (customer and person), but the domain is not specified.
ex:InvoiceShape
a sh:NodeShape ;
sh:property [
sh:path ex:customer ;
sh:class ex:Customer ;
sh:class ex:Person ;
] .
I know it is possible to specify a target class (TC) for the shape, but this limits the range of the property ex:customer when the domain is TC and not in all cases.
Is it possible to write a shape that fix domain and range of a given property?
Thank you!
Upvotes: 1
Views: 513
Reputation: 1421
To state that the property constraint above applies to all instances of ex:Invoice, you either add ex:InvoiceShape rdf:type rdfs:Class or ex:InvoiceShape sh:targetClass ex:Invoice. This however does not specify that all subjects of an ex:customer triple must be instances of ex:Invoice.
To make sure that the property ex:customer can only be used at instances of ex:Invoice, you can use:
ex:InverseInvoiceShape
a sh:NodeShape ;
sh:targetSubjectsOf ex:customer ;
sh:class ex:Invoice .
The shape above applies to all subjects of an ex:customer triple. A violation will be reported if that subject is not an instance of ex:Invoice.
FWIW your original example states that the values of ex:customer must be both ex:Customer and ex:Person instances. If you meant to express 'either customer or person' then use
ex:InvoiceShape
a sh:NodeShape ;
sh:targetClass ex:Invoice ;
sh:property [
sh:path ex:customer ;
sh:or (
[ sh:class ex:Customer ]
[ sh:class ex:Person ]
)
] .
Upvotes: 2