# SHACL Exercise Gibran Brahmanta Patriajati 1806186553 --- ## Question for Constraint Graph 1 and Data Graph 1 1. Is the data valid from the given constraint graph? If not valid, give explanation why the data is not valid and modify the shape so that the data is evaluated as valid. **Jawaban** Constraint graph yang diberikan tidak bersifat valid karena pada graf yang ada, properti foaf:birthday memiliki objek bertipe xsd:date. Sedangkan, pada constraint graph yang diberikan, tipe objek dari properti tersebut adalah xsd:string. Agar constraint graph yang diberikan bersifat valid, perlu adanya perubahan menjadi: ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; ] . ``` 2. Add additional property node constraints using sh:alternativePath to validate the datatypes for foaf:givenName, foaf:familyName and foaf:name as xsd:string. Modify the data graph to verify that your new constraints are working. **Jawaban** Constraint Graph ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; ] ; sh:property [ sh:path [ sh:alternativePath (foaf:givenName foaf:familyName foaf:name) ]; sh:datatype xsd:string ; ] . ``` Data Graph ``` dbr:Satya_Nadella a foaf:Person ; rdfs:label "Satya Nadella" ; foaf:birthday "1967-08-19"^^xsd:date ; dbo:birthDate "1967-08-19"^^xsd:date ; dbo:employer dbo:Microsoft ; dbo:birthPlace dbr:India ; foaf:familyName "Nadella" ; foaf:givenName "Satya" ; foaf:name "Satya Nadella" ; foaf:gender "male" . dbo:Microsoft a dbr:Public_company ; rdfs:label "Microsoft" . dbr:India a dbo:Country ; rdfs:label "India" . ``` 3. Add another property constraint that requires dbo:birthDate and foaf:birthday to have equal values. Will the data graph pass the validation if one of the properties are missing? Why? **Jawaban** Constraint Graph ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; sh:equals dbo:birthDate ; ] ; sh:property [ sh:path [ sh:alternativePath (foaf:givenName foaf:familyName foaf:name) ]; sh:datatype xsd:string ; ] . ``` Proses validasi pada kasus tersebut akan gagal karena pada constraint yang ditetapkan kedua properti tersebut harus memiliki nilai yang sama 4. Try to model a missing property from number 3 using sh:or and cardinality restrictions. **Jawaban** ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:or ( sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] sh:property [ sh:path dbo:birthDate ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] ) ; sh:property [ sh:path [ sh:alternativePath (foaf:givenName foaf:familyName foaf:name) ]; sh:datatype xsd:string ; ] . ``` 5. Add a property constraint that limits the values of foaf:gender to either "male" or "female". Then, add a node representing someone whose gender is female and make sure that the node is evaluated as valid. **Jawaban** Constraint Graph ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:or ( sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] sh:property [ sh:path dbo:birthDate ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] ) ; sh:property [ sh:path [ sh:alternativePath (foaf:givenName foaf:familyName foaf:name) ]; sh:datatype xsd:string ; ] ; sh:property [ sh:path foaf:gender; sh:in ( "male" "female"); sh:maxCount 1; sh:minCount 1; ] . ``` Data Graph ``` dbr:Satya_Nadella a foaf:Person ; rdfs:label "Satya Nadella" ; foaf:birthday "1967-08-19"^^xsd:date ; dbo:birthDate "1967-08-19"^^xsd:date ; dbo:employer dbo:Microsoft ; dbo:birthPlace dbr:India ; foaf:familyName "Nadella" ; foaf:givenName "Satya" ; foaf:name "Satya Nadella" ; foaf:gender "male" . dbr:Natasha_Udu a foaf:Person ; rdfs:label "Natasha Udu" ; foaf:birthday "1967-08-19"^^xsd:date ; dbo:birthDate "1967-08-19"^^xsd:date ; dbo:employer dbo:Microsoft ; dbo:birthPlace dbr:India ; foaf:familyName "Udu" ; foaf:givenName "Natasha" ; foaf:name "Natasha Udu" ; foaf:gender "female" . dbo:Microsoft a dbr:Public_company ; rdfs:label "Microsoft" . dbr:India a dbo:Country ; rdfs:label "India" . ``` 6. Create property shapes that validate foaf:name, foaf:givenName, and foaf:familyName with regular expressions, using sh:pattern. Names should not be allowed to be shorter than two letters and must not contain underscores and numbers. **Jawaban** ``` ex:PersonShape a sh:NodeShape ; sh:targetClass foaf:Person ; sh:or ( sh:property [ sh:path foaf:birthday ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] sh:property [ sh:path dbo:birthDate ; sh:datatype xsd:date ; sh:minCount 1; sh:maxCount 1; ] ) ; sh:property [ sh:path [ sh:alternativePath (foaf:givenName foaf:familyName foaf:name) ]; sh:datatype xsd:string ; sh:pattern "^[^0-9_]{2,}$" ; ] ; sh:property [ sh:path foaf:gender; sh:in ( "male" "female"); sh:maxCount 1; sh:minCount 1; ] . ``` ## Question for Constraint Graph 2 and Data Graph 2 1. Add a property constraint that staffID must have a length of 10 digits number. **Jawaban** ``` :StaffShape a sh:NodeShape ; sh:targetClass :Staff ; sh:class :Employee ; sh:property [ sh:path [ sh:inversePath :supervises ] ; sh:minCount 1 ; sh:maxCount 1 ; sh:node :ManagerShape ; ] ; sh:property [ sh:path :hasStaffID ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:Literal ; sh:pattern "^[0-9]{10}$" ; ] . :ManagerShape a sh:NodeShape ; sh:class :Manager ; sh:class :Employee . ``` 2. Add a property constraint that a :Manager must have minimal 5 years experience (:hasYearExperience), with cardinality and datatype constraint. **Jawaban** ``` :StaffShape a sh:NodeShape ; sh:targetClass :Staff ; sh:class :Employee ; sh:property [ sh:path [ sh:inversePath :supervises ] ; sh:minCount 1 ; sh:maxCount 1 ; sh:node :ManagerShape ; ] ; sh:property [ sh:path :hasStaffID ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:Literal ; sh:pattern "^[0-9]{10}$" ; ] . :ManagerShape a sh:NodeShape ; sh:class :Manager ; sh:class :Employee ; sh:property [ sh:path dbo:hasYearExperience; sh:minInclusive 5; sh:datatype xsd:int ; sh:minCount 1 ; sh:maxCount 1 ; ] . ``` 3. Complete the data graph 2 (in Turtle format) without adding a new instance of :Staff and :Manager classes so that there is no violation if the graph is validated with the constraint graph obtained from number 2 above. **Jawaban** ``` :potter rdf:type :Staff ; :hasStaffId "1806186553" . :malfoy rdf:type :Staff ; :hasStaffId "1806186554" . :snape rdf:type :Manager ; dbo:hasYearExperience "6"^^xsd:int ; :supervises :potter ; :supervises :malfoy . ``` 4. Complete the data graph 2 (in Turtle format) without adding a new instance of :Staff and :Manager classes so that there will be at least 3 violations if the graph is validated with the constraint graph obtained from number 2. above Explain what constraint violations occurred and where they occurred. **Jawaban** ``` :potter rdf:type :Staff ; :hasStaffId "18061865532" . :malfoy rdf:type :Staff ; :hasStaffId "1806186554" . :snape rdf:type :Manager ; dbo:hasYearExperience "3"^^xsd:int ; :supervises :malfoy . ``` Constraint violations: * Objek dari properti :hasStaffId pada instance :potter hanya berjumlah 11 digit. Namun, pada constraint yang ditetapkan, jumlah digit yang diminta berjumlah tepat 10 * Objek dari properti :hasYearExperience pada instance :snape hanya bernilai 3. Namun, pada constraint yang ditetapkan, nilai yang diminta minimal 5 * Instance :potter yang memiliki tipe :Staff tidak memiliki properti :supervises yang menjadikanya sebagai objek. Hal tersebut melanggar constraint yang ada karena instance yang memiliki tipe :Staff harus memiliki tepat 1 properti :supervises yang menjadikanya sebagai objek