SHACL(Shapes Constraint Language) 란

데이터를 검증하는 데 사용되는 SHACL(Shapes Constraint Language)에 대해 공부하고 정리한 내용입니다.

SHACL(Shapes Constraint Language) 란

Shapes Constraint Language(SHACL)는 RDF(Resource Description Framework) 그래프를 설명하기 위한 W3C(World Wide Web Consortium) 표준 언어입니다 . SHACL은 RDF 그래프로 표현되는 온톨로지의 의미 체계 및 기술 상호 운용성 계층을 향상하도록 설계되었습니다.
SHACL 모델은 그래프의 내용, 구조 및 의미에 대한 제약 조건으로 정의됩니다. SHACL은 표현력이 뛰어난 언어입니다. 무엇보다도 속성이 가질 수 있는 값의 수, 이러한 값의 유형, 숫자 범위, 문자열 일치 패턴 및 이러한 제약 조건의 논리적 조합을 제한하는 조건을 표현하는 기능이 포함되어 있습니다. SHACL에는 SPARQL 및 JavaScript 와 같은 언어로 보다 복잡한 조건을 표현하는 확장 메커니즘도 포함되어 있습니다 . SHACL 규칙은 SHACL에 추론 기능을 추가하여 사용자가 기존(어설션된) 문에서 추론할 수 있는 새 문을 정의할 수 있도록 합니다. 위키백과

  • 2017년 7월 20일 이후 W3C 권장 사항(https://www.w3.org/TR/shacl/)
  • RDF 언어 사용
  • RDF의 검증을 허용하도록 생성되었습니다.
  • SHACL은 데이터를 검증하는 데 사용되는 "Shapes Graph"를 정의합니다.

RDF(Resource Description Framework) 란

RDF는 자원의 정보를 표현하고 상호 연결 및 교환하기 위하여 월드 와이드 웹 컨소시움(W3C)에 의해 개발되고 있는 언어 규격이다.
여기서 말하는 자원이란 웹상에 존재하는 대부분의 객체를 의미하는 것으로, 사람이나 웹페이지, 저작물과 같은 특정 지을 수 있는 개체들이 해당된다. RDF는 이러한 자원들의 메타데이터를 표현하기 위하여 개발된 규격으로, 예를 들어 웹페이지라면 제목, 저자, 수정날짜와 같은 정보가 RDF로 서술하는 대상이 된다.
나무위키

SHACL Validation

SHACL Processor

  • 두 개의 입력 데이터를 넣고 검증합니다.
    • shapes graph(확인하는 방법), data graph(검증 대상)
  • SHACL Processor는 graph를 변경해서는 안됩니다. 즉, 유효성 검사 종료 시 data 및 shapes graph는 유효성 검사 시작 시 graph와 동일해야 합니다.
  • 결과 graph를 생성합니다.

Shape 이란

targets(대상) 및 contraints(제약 조건) 모음입니다.

  • Targets: data graph에서 어떤 노드가 shape을 준수해야 하는지 정의합니다.
  • Constraint: 노드를 검증하는 방법을 정의합니다.

Shapes and Constraints

Targets (of a shape)

  • Data Graph에서 어떤 노드가 형태를 따라야 하는지 정의합니다.
  • Target 선언은 어떤 리소스(RDF 그래프 노드)가 Shape의 "Focus Node"가 될 것인지를 정의합니다.
Property Description
sh:targetClass 지정된 클래스의 인스턴스인 모든 리소스를 대상으로 합니다.
예) sh:targetClass schema:Person;
sh:targetNode 특정 리소스(예: 주어진 인스턴스)를 대상으로 합니다.
예) sh:targetNode ex:John;

Node Shapes

  • 노드(클래스)에 대한 제약 조건을 지정합니다.
  • Property Shapes을 그룹화하는데 사용됩니다.
  • sh:NodeShape의 SHACL 인스턴스로 선언하는 것이 권장되지만 필수는 아닙니다.
  • sh:NodeShape의 SHACL 인스턴스는 sh:path 속성 값을 가질 수 없습니다.
1
2
3
4
5
schema:ExampleNodeShape e a sh:NodeShape
sh:targetClass schema:Example ;
sh:property [
...
]

Property Shapes

  • 속성 및 해당 값에 대한 제약 조건을 지정합니다.
1
2
3
4
5
6
7
sh:property [
sh:path schema:name ;
sh:minCount 1 ;
sh:maxCount 1 ;
sh:datatype xsd:string ;
sh:maxLength 20 ;
] ;

Core Constraint Components

SHACL 프로세서에서 지원해야 하는 기본 제공 SHACL Core 제약 조건 구성 요소를 정의합니다.

Type Constraints
Types of values class, datatype, nodeKind
Cardinality minCount, maxCount
Values node, in, hasValue
Range of values minInclusive, maxInclusive, minExclusive, maxExclusive
String based minLength, maxLength, pattern, languageIn, uniqueLang
Logical constraints not, and, or, xone
Closed shapes closed, ignoredProperties
Property pair constraints equals, disjoint, lessThan, lessThanOrEquals
Non-validating constraints name, description, group, order, defaultValue
Qualified shapes qualifiedValueShape, qualifiedMinCount, qualifiedMaxCount

Value Type Constraint Components

Value 노드의 유형을 제한하는 데 사용합니다.

Property Description
sh:class 주어진 클래스의 인스턴스인 노드로 제한합니다.
sh:datatype 노드의 데이터 유형을 주어진 값으로 제한합니다. (예: xsd:integer)
sh:nodeKind 각 값 노드의 RDF 노드 종류가 만족할 조건을 지정합니다.
가능한 값: BlankNode, IRI, Literal, BlankNodeOrIRI, BlankNodeOrLiteral, IRIOrLiteral
1
2
3
4
5
6
<!-- sh:class -->
schema:Person a sh:NodeShape ;
sh:property [
sh:path schema:knows ;
sh:class :Person;
] .
1
2
3
4
5
6
<!-- sh:datatype -->
schema:Person a sh:NodeShape ;
sh:property [
sh:path schema:birthDate ;
sh:datatype xsd:date;
] .

sh:datatype 에 대한 데이터 유형은 XMLSchema를 참고하여 지정합니다.

1
2
3
4
5
6
7
8
9
10
11
<!-- sh:nodeKind -->
schema:Person a sh:NodeShape ;
sh:nodeKind sh:IRI ;
sh:property [
sh:path schema:knows ;
sh:nodeKind sh:BlankNodeOrIRI;
];
sh:property [
sh:path schema:name ;
sh:nodeKind sh:BlankNodeOrLiteral;
] .

Cardinality Constraint Components

Value 노드 수에 대한 제한을 나타냅니다.

Property Description
minCount 지정된 속성의 최소 발생 횟수를 제한합니다. 기본값: 0
maxCount 지정된 속성의 최대 발생 횟수를 제한합니다. 기본값: 0
1
2
3
4
5
6
schema:Person a sh:NodeShape ;
sh:property [
sh:path schema:knows ;
sh:minCount 1;
sh:maxCount 2;
] .

Value Range Constraint Components

<, <=, > 및 >= 와 같은 연산자를 통해 비교할 수 있는 값 노드가 충족할 값 범위 조건을 지정합니다.

Property Description
minInclusive 최소 포함(Inclusive) 값, <=
maxInclusive 최대 포함(Inclusive) 값, >=
minExclusive 최소 독점(Exclusive) 값, <
maxExclusive 최대 독점(Exclusive) 값, >
1
2
3
4
5
6
7
schema:Rating a sh:NodeShape;
sh:property [
sh:path schema:ratingValue ;
sh:minInclusive 1;
sh:maxExclusive 6;
sh:datatype xsd:integer;
] ;.

String-based Constraint Components

Value 노드의 문자열 표현에 대한 조건을 지정한다는 공통점이 있습니다.

Property Description
minLength 최소 문자열 길이를 제한합니다.
maxLength 최대 문자열 길이를 제한합니다.
pattern 문자열 값이 정규 표현식과 일치하는지 확인합니다.
uniqueLang 동일한 언어 태그를 사용하는 노드 쌍이 없는지 확인합니다.
languageIn 속성 값이 주어진 언어 목록으로 표현되는지 확인합니다.
1
2
3
4
5
6
7
<!-- sh:minLength, sh:maxLength -->
schema:Person a sh:NodeShape;
sh:property [
sh:path schema:name ;
sh:minLength 4;
sh:maxLength 15;
] .
1
2
3
4
5
6
<!-- sh:pattern -->
schema:Product a sh:NodeShape;
sh:property [
sh:path schema:productID ;
sh:pattern "^P" ; <!-- P or p로 시작하는지 확인 -->
] .
1
2
3
4
5
6
7
<!-- sh:uniqueLang, sh:languageIn -->
schema:Country a sh:NodeShape;
sh:property [
sh:path schema:name ;
sh:languageIn ("en" "ex") ;
sh:uniqueLang true ;
] .

Property Pair Constraint Components

다른 속성과 관련하여 값 노드 집합에 대한 조건을 지정합니다.

Property Description
equals 지정된 Focus 노드에서 두 속성의 값 집합은 동일해야 합니다.
disjoint 지정된 Focus 노드에서 두 속성의 값 집합은 달라야 합니다.
lessThan 값은 다른 속성 값보다 작아야 합니다.
lessThanOrEquals 값은 다른 속성의 값보다 작거나 같아야 합니다.
1
2
3
4
5
6
7
8
9
schema:Person a sh:NodeShape;
sh:property [
sh:path schema:givenName ;
sh:equals foaf:firstName ;
] ;
sh:property [
sh:path schema:givenName ;
sh:disjoint schema:lastName ;
] .

Logical Constraint Components

공통 논리 연산자 and, or 및 not과 xor(Exclusive-OR)을 구현합니다.

Property Description
not 각 노드가 주어진 Shape을 따를 수 없다는 조건을 준수하는지 확인합니다.
and 각 노드가 제공된 모든 Shape을 준수하는 조건을 준수하는지 확인합니다.
or 각 노드가 제공된 Shape 중 하나 이상 또는 둘 다를 준수하는지 확인합니다.
xone 각 노드가 제공된 Shape 중 정확히 하나를 준수한다는 조건을 준수하는지 확인합니다
1
2
3
4
5
6
7
<!-- sh:not -->
schema:Person a sh:NodeShape;
sh:not
[
sh:path foaf:name ;
sh:minCount 1;
] .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- sh:and -->
schema:Person a sh:NodeShape;
sh:and (
[
sh:property [
sh:path schema:name ;
sh:minCount 1;
]
]
[
sh:property [
sh:path schema:affiliation ;
sh:MinCount 1;
]
]
) .
1
2
3
4
5
6
7
8
9
10
11
12
<!-- sh:or -->
schema:Person a sh:NodeShape;
sh:or (
[
sh:path foaf:name ;
sh:minCount 1 ;
]
[
sh:path schema:name ;
sh:minCount 1 ;
]
) .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!-- sh:xone -->
schema:Person a sh:NodeShape;
sh:xone (
[
sh:property [
sh:path ex:fullName ;
sh:minCount 1;
]
]
[
sh:property [
sh:path ex:firstName ;
sh:minCount 1 ;
] ;
sh:property [
sh:path ex:lastName ;
sh:minCount 1 ;
]
]
).

Shape-based Constraint Components

특정 Shape에 대해 Value 노드의 유효성을 검사하여 복잡한 조건을 지정하는 데 사용할 수 있습니다.

Property Description
node 주어진 속성의 모든 값은 주어진 Shape을 준수해야 합니다.
qualifiedValueShape 지정된 노드 수가 준수해야 하는 Shape을 정의합니다.
qualifiedMinCount Shape에 맞는 최소 노드 수
qualifiedMaxCount Shape에 맞는 최대 노드 수
1
2
3
4
5
6
7
8
9
10
11
12
<!-- sh:node -->
schema:Person a sh:NodeShape;
sh:property [
sh:path schema:affiliation ;
sh:node schema:Company;
].

schema:Company a sh:Shape ;
sh:property [
sh:path schema:name ;
sh:datatype xsd:string ;
] .
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- sh:qualifiedValueShape, sh:qualifiedMinCount -->
schema:QualifiedExampleShape a sh:NodeShape;
sh:targetNode :Offspring ;
sh:property [
sh:path :parent;
sh:minCount 2;
sh:maxCount 2;
sh:qualifiedValueShape [
sh:path :gender ;
sh:hasValue :Female ;
] ;
sh:qualifiedMinCount 1 ;
] .

Closed Constraint Components

Shape에 명시적으로 열거된 속성에 대한 값만 갖는 조건을 지정합니다.

Property Description
closed true로 설정하면 Shape에 명시적으로 설명되지 않은 속성은 허용되지 않습니다.
ignoredProperties Shape에 명시적으로 열거된 속성 외에 허용되는 속성의 Optional 목록입니다.
1
2
3
4
5
6
7
8
9
schema:Person a sh:NodeShape;
sh:closed true ;
sh:ignoredProperties (rdf:type) ;
sh:property [
sh:path ex:firstName ;
] ;
sh:property [
sh:path ex:lastName ;
] .

Non-Validating Constraint

유효성 검사 조건에서 무시되는 속성입니다.

Property Description
name 속성 라벨명을 정의합니다.
description 속성에 대한 설명을 정의합니다.
order 속성의 상대적 순서를 정의합니다.
group 그룹을 정의합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
schema:Person a sh:NodeShape;
sh:property [
sh:path schema:url;
sh:name "URL";
sh:description "User URL";
sh:order 1;
] ;
sh:property [
sh:path schema:name;
sh:name "Name";
sh:description "Person name";
sh:order 2;
] .

Other Constraint Components

Property Description
hasValue Focus 노드에 주어진 값이 있는지 확인합니다.
in 속성이 가질 수 있는 값 옵션을 열거합니다.
1
2
3
4
5
6
7
8
9
schema:Person a sh:NodeShape;
sh:property [
sh:path schema:affiliation ;
sh:hasValue :OurCompany;
];
sh:property [
sh:path schema:gender ;
sh:in (schema:Male schema:Female);
] .

참고

Share