Tech

RDF Turtle

euidong 2021. 3. 23. 13:17

자연어의 복잡한 텍스트 형태로 쓰여진 RDF graph를 datatype과 pattern을 생략 사용하여 표현하는 RDF 문법을 말한다.

  • Example

    RELATIONSHEEP between Green Goblin and Spiderman.

      @base <http://example.org/> .
      @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
      @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
      @prefix foaf: <http://xmlns.com/foaf/0.1/> .
      @prefix rel: <http://www.perceive.net/schemas/relationship/> .
    
      <#green-goblin>
          rel:enemyOf <#spiderman> ;
          a foaf:Person ;    # in the context of the Marvel universe
          foaf:name "Green Goblin" .
    
      <#spiderman>
          rel:enemyOf <#green-goblin> ;
          a foaf:Person ;
          foaf:name "Spiderman", "Человек-паук"@ru .
    • green goblin은 spiderman의 적이며, 사람이고, 이름이 Green Goblin이다.
    • spider man은 green goblin의 적이며, 사람이고, 이름이 Spiderman이다.

기초 문법

주석

주석을 표현할 때에는 #을 이용하고, 해당 라인은 모두 주석처리 된다.

Simple Triples

triple은 기본적으로 [subject] [predicate] [object] . 의 형태를 띈다.

각 요소 사이의 공백 문자와 triple의 종료 시점에는 . 을 이용하여 이를 표기한다.

<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enermyOf> <http://example.org/#green-goblin> .

Predicate Lists

종종 동일한 subject가 다양한 predicate를 요구할 때가 있다. 이럴 경우는 문장의 끝을 . 이 아닌 ; 으로 표기하면, 동일한 subject를 이용하는 triple이 이어짐을 명시할 수 있다.

따라서, 아래 나오는 두 예시는 모두 동일한 내용을 말한다.

# predicate list 사용
<http://example.org/#spiderman> 
                <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> ;
                <http://xmlns.com/foaf/0.1/name> "Spiderman" .
# predicate list 사용 x
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green-goblin> .
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman" .

Object Lists

종종 동일한 subject와 predicate에 대하여 동일한 object를 필요로 할 때가 있다. 이때는 , 를 통해서 이를 표현해주는 것이 가능하다.

따라서, 아래 나오는 두 예시는 모두 동일한 내용을 말한다.

# object list 사용
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman", "Человек-паук"@ru .
# object list 사용 x
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman" .
<http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Человек-паук"@ru .

변수

IRIs

상대 or 절대 경로 or Prefix 이름을 통해서 표기됩니다.

  • 상대 or 절대 경로 : <[IRI]>의 형태로 표현합니다. 유니코드를 표현하기 위해 numeric escape sequence를 사용하기도 합니다.

      절대경로 : <http://example.org/#green-goblin>
      상대경로 : <#green-goblin> 
      # 현재 base IRI를 기준으로 IRI를 완성합니다.
      # 만약, base를 임의로 지정해주고 싶다면, @base or Base를 이용하여 이를 지정해주는 것이 가능합니다.
  • prefix를 이용한 표기 : 선언부와 사용부 두 단계로 나뉘어집니다. 먼저, 문서 초반에 사용할 prefix에 대한 정의를 한 후, 이를 해당 파일 내에서 불러와 사용하는 것이 가능합니다.

    위의 예시에서 길게 표현하였던, green goblin의 적이 spiderman이라는 내용을 짧게 표현하는 것이 가능합니다.

    여기서, @prefix를 PREFIX 라고 적어도 무방합니다. ← SPARQL 문법

      # somePrefix에 해당 IRI를 지정합니다.
      @prefix somePrefix: <http://www.perceive.net/schemas/relationship/> .
    
      # somePrefix에 enermyOf라는 IRI를 가르킨다는 것을 표현합니다.
      <http://example.org/#green-goblin> somePrefix:enemyOf <http://example.org/#spiderman> .

이를 이용한 다양한 예제

# A triple with all absolute IRIs
<http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .

@base <http://one.example/> .
<subject2> <predicate2> <object2> .     # relative IRIs, e.g. http://one.example/subject2

BASE <http://one.example/>
<subject2> <predicate2> <object2> .     # relative IRIs, e.g. http://one.example/subject2

@prefix p: <http://two.example/> .
p:subject3 p:predicate3 p:object3 .     # prefixed name, e.g. http://two.example/subject3

PREFIX p: <http://two.example/>
p:subject3 p:predicate3 p:object3 .     # prefixed name, e.g. http://two.example/subject3

@prefix p: <path/> .                    # prefix p: now stands for http://one.example/path/
p:subject4 p:predicate4 p:object4 .     # prefixed name, e.g. http://one.example/path/subject4

@prefix : <http://another.example/> .    # empty prefix
:subject5 :predicate5 :object5 .        # prefixed name, e.g. http://another.example/subject5

:subject6 a :subject7 .                 # same as :subject6 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> :subject7 .

Literals

string, number, date와 같은 데이터를 표현하는데 사용됩니다.

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://example.org/#green-goblin> foaf:name "Green Goblin" .

<http://example.org/#spiderman> foaf:name "Spiderman" .

그 중에서 좀 특별한 형태를 가지는 것을 알아보면 다음 셋 중 하나이다.

  1. Quoted Literals : 이는 언어 태그나 datatype에 대한 태그 또는 둘 다 없는 형태로 표기됩니다.

    • 기본적으로 string은 " 으로 감싸져서 표기됩니다. (LF, RF같은 문자는 허용하지 않습니다.) 감싸진 내용에는 character와 numeric, string escape sequence가 허용됩니다.

    • 해당 과정으로 생성된 후에는 @ 를 이용하여 언어를 명시합니다. 만약 언어 태그가 없다면, ^^ 를 이용하여 datatype을 명시합니다. 이 datatype은 IRI의 값만 받을 수 있으며, @^^ 모두 표기 되지 않았다면, 뒤에 ^^xsd:string 의 함축형으로 이해합니다.

    • multi line의 입력인 경우에는 ''', """을 이용하여 표기합니다.

      @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
      @prefix show: <http://example.org/vocab/show/> .
      @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
      
      show:218 rdfs:label "That Seventies Show"^^xsd:string .            # literal with XML Schema string datatype
      show:218 rdfs:label "That Seventies Show"^^<http://www.w3.org/2001/XMLSchema#string> . # same as above
      show:218 rdfs:label "That Seventies Show" .                                            # same again
      show:218 show:localName "That Seventies Show"@en .                 # literal with a language tag
      show:218 show:localName 'Cette Série des Années Soixante-dix'@fr . # literal delimited by single quote
      show:218 show:localName "Cette Série des Années Septante"@fr-be .  # literal with a region subtag
      show:218 show:blurb '''This is a multi-line                        # literal with embedded new lines and quotes
      literal with many quotes (""""")
      and up to two sequential apostrophes ('').''' .
  2. Numbers

    어떤 숫자든 표기할 때에는 ^^xsd:decimal 을 사용하지만, 기본적으로 아무것도 없을 때에는 정규표현식(regexp)에 따라서 datatype을 함축한다.

    Number 표기법

     @prefix : <http://example.org/elements> .                                                                              
     <http://en.wikipedia.org/wiki/Helium>                                                                                  
         :atomicNumber 2 ;               # xsd:integer                                                                      
         :atomicMass 4.002602 ;          # xsd:decimal                                                                      
         :specificGravity 1.663E-4 .     # xsd:double
  3. Booleans

    true or false 의 형태로 주어지는 값이다.

     @prefix : <http://example.org/stats> .
     <http://somecountry.example/census2007>
         :isLandlocked false .           # xsd:boolean

RDF Blank Nodes

공백 node는 _: 로 표기합니다. : 뒤에는 character sequence 값의 label을 붙일 수 있습니다. 동일한 label을 붙이면 동일한 blank node로 인식합니다.

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

_:alice foaf:knows _:bob .
_:bob foaf:knows _:alice .

Blank Node 중첩

blank node의 중첩을 통해서 관계를 표현할 수도 있다. 이 때 predicate와 object로 이루어진 하나의 object를 표현함으로서 이 과정이 가능해진다. 따라서 중첩의 형태는 [ predicate [ predicate object ]; ... ] 를 통해서 표현된다.

따라서, 다음 두 표현은 서로 동일하다.

_:a <http://xmlns.com/foaf/0.1/name> "Alice" .
_:a <http://xmlns.com/foaf/0.1/knows> _:b .
_:b <http://xmlns.com/foaf/0.1/name> "Bob" .
_:b <http://xmlns.com/foaf/0.1/knows> _:c .
_:c <http://xmlns.com/foaf/0.1/name> "Eve" .
_:b <http://xmlns.com/foaf/0.1/mbox> <bob@example.com> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

[ foaf:name "Alice" ] foaf:knows [
    foaf:name "Bob" ;
    foaf:knows [
        foaf:name "Eve" ] ;
    foaf:mbox <bob@example.com> ] .

의미 : 이름이 "Alice"인 사람은 이름이 "Bob"이고, "Eve"라는 사람을 알며, 이메일은 "bob@example.com" 인 사람을 안다.

Collections

RDF node의 리스트 구조이다. 기본적으로 ([contents]) 으로 표기한다. 여기서 empty도 허용하기 때문에 () 역시 가능하다.

collection은 subject와 object에만 사용이 가능하다.

@prefix : <http://example.org/foo> .
# the object of this triple is the RDF collection blank node
:subject :predicate ( :a :b :c ) .

# an empty collection value - rdf:nil
:subject :predicate2 () .