Re: Compacting rdf:Seq

> On Dec 19, 2015, at 9:56 AM, Jindřich Mynarz <mynarzjindrich@gmail.com> wrote:
> 
> Hi,
> 
> is it possible to compact rdf:Seq?
> 
> For example, let's have this JSON-LD:
> 
> {
>   "@graph": [{
>     "ex:list": "_:b1"
>   }, {
>     "@id": "_:b1",
>     "rdf:first": "_:b2",
>     "rdf:rest": "_:b3"
>   }, {
>     "@id": "_:b2",
>     "foaf:name": "Alice"
>   }, {
>     "@id": "_:b3",
>     "rdf:first": "_:b4",
>     "rdf:rest": "rdf:nil"
>   }, {
>     "@id": "_:b4",
>     "foaf:name": "Bob"
>   }]
> }
> 
> We try to compact it with this context:
> 
> {
>  "@context": {
>     "ex": "http://example.com/ <http://example.com/>",
>     "foaf": "http://xmlns.com/foaf/0.1/ <http://xmlns.com/foaf/0.1/>",
>     "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns# <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
>     "ex:list": {"@container": "@list",
>                 "@type": "@id"},
>     "rdf:first": {"@type": "@id"},
>     "rdf:rest": {"@type": "@id"}
>   }
> }
> 
> JSON-LD playground (jsonld.js) returns the following:
> 
> {
>   "@context": {
>     "ex": "http://example.com/ <http://example.com/>",
>     "foaf": "http://xmlns.com/foaf/0.1/ <http://xmlns.com/foaf/0.1/>",
>     "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns# <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
>     "ex:list": {
>       "@container": "@list",
>       "@type": "@id"
>     },
>     "rdf:first": {
>       "@type": "@id"
>     },
>     "rdf:rest": {
>       "@type": "@id"
>     }
>   },
>   "@graph": [
>     {
>       "ex:list": [
>         "_:b1"
>       ]
>     },
>     {
>       "@id": "_:b1",
>       "rdf:first": "_:b2",
>       "rdf:rest": "_:b3"
>     },
>     {
>       "@id": "_:b2",
>       "foaf:name": "Alice"
>     },
>     {
>       "@id": "_:b3",
>       "rdf:first": "_:b4",
>       "rdf:rest": "rdf:nil"
>     },
>     {
>       "@id": "_:b4",
>       "foaf:name": "Bob"
>     }
>   ]
> }
> 
> Is it possible to get to a compact representation of rdf:Seq (http://www.w3.org/TR/json-ld/#sets-and-lists <http://www.w3.org/TR/json-ld/#sets-and-lists>), like the following?
> 
> {
>   "@context": {
>     "ex": "http://example.com/ <http://example.com/>",
>     "foaf": "http://xmlns.com/foaf/0.1/ <http://xmlns.com/foaf/0.1/>",
>     "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns# <http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
>     "ex:list": {
>       "@container": "@list",
>       "@type": "@id"
>     },
>     "rdf:first": {
>       "@type": "@id"
>     },
>     "rdf:rest": {
>       "@type": "@id"
>     }
>   },
>   "@graph": [
>     {
>       "ex:list": [
>         "_:b2", "_:b4"
>       ]
>     },
>     {
>       "@id": "_:b2",
>       "foaf:name": "Alice"
>     },
>     {
>       "@id": "_:b4",
>       "foaf:name": "Bob"
>     }
>   ]
> }

You can do much of what you want by going through RDF. If you turn the original input into N-Quads, and then serialize that back to JSON-LD you’ll get an @list. They playground doesn’t show N-Quads to JSON-LD, but you can do this through my Distiller (http://rdf.greggkellogg.net/distiller <http://rdf.greggkellogg.net/distiller>). I modified your input a bit:

{
 "@context": {
    "ex": "http://example.com/",
    "foaf": "http://xmlns.com/foaf/0.1/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  },
  "@graph": [{
    "ex:list": {"@id": "_:b1"}
  }, {
    "@id": "_:b1",
    "rdf:first": {"@id": "_:b2"},
    "rdf:rest": {"@id": "_:b3"}
  }, {
    "@id": "_:b2",
    "foaf:name": "Alice"
  }, {
    "@id": "_:b3",
    "rdf:first": {"@id": "_:b4"},
    "rdf:rest": {"@id": "rdf:nil"}
  }, {
    "@id": "_:b4",
    "foaf:name": "Bob"
  }]
}

If you transform from JSON-LD to RDF and then serialize back to JSON-LD you get the following:

{
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/"
  },
  "@graph": [
    {
      "@id": "_:b0",
      "http://example.com/list": {
        "@list": [
          {
            "@id": "_:b2"
          },
          {
            "@id": "_:b4"
          }
        ]
      }
    },
    {
      "@id": "_:b2",
      "foaf:name": "Alice"
    },
    {
      "@id": "_:b4",
      "foaf:name": "Bob"
    }
  ]
}

To improve further requires framing, but not that it is using the @list shorthand. You can use the playground with your context to get the following:

{
  "@context": {
    "ex": "http://example.com/",
    "foaf": "http://xmlns.com/foaf/0.1/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "ex:list": {
      "@container": "@list",
      "@type": "@id"
    },
    "rdf:first": {
      "@type": "@id"
    },
    "rdf:rest": {
      "@type": "@id"
    }
  },
  "@graph": [
    {
      "@id": "_:b0",
      "ex:list": [
        "_:b2",
        "_:b4"
      ]
    },
    {
      "@id": "_:b2",
      "foaf:name": "Alice"
    },
    {
      "@id": "_:b4",
      "foaf:name": "Bob"
    }
  ]
}

Gregg

> - Jindřich
> 
> -- 
> Jindřich Mynarz
> http://mynarz.net/#jindrich <http://mynarz.net/#jindrich>

Received on Sunday, 20 December 2015 01:06:09 UTC