Add flattening algorithm to API spec
authorMarkus Lanthaler <mark_lanthaler@gmx.net>
Fri, 18 May 2012 18:51:04 +0800
changeset 658 e3d516132c6e
parent 657 6a4ce454ecc1
child 659 45e33e060f6c
Add flattening algorithm to API spec

To bring the discussion about whether or not to add a flatten() API call forward, a concrete proposal was added to the spec.

This addresses #109.
spec/latest/json-ld-api/index.html
--- a/spec/latest/json-ld-api/index.html	Fri May 18 00:06:50 2012 -0400
+++ b/spec/latest/json-ld-api/index.html	Fri May 18 18:51:04 2012 +0800
@@ -610,6 +610,52 @@
         </dl>
       </dd>
 
+      <dt>void flatten()</dt>
+      <dd>
+        <a href="#flattening">Flattens</a> the given <code>input</code> according to
+        the steps in the <a href="#flattening-algorithm">Flattening Algorithm</a>. The
+        <code>input</code> MUST be flattened and returned if there are
+        no errors. If the flattening fails, an appropriate exception MUST be thrown.
+
+        <p class="issue">It is still being discussed if the flatten() method should be
+          added or not. See <a href="https://github.com/json-ld/json-ld.org/issues/109">ISSUE-109</a>.</p>
+
+        <dl class="parameters">
+          <dt>object or object[] or URL input</dt>
+          <dd>The JSON-LD object or array of JSON-LD objects to flatten or an
+            <tref>IRI</tref> referencing the JSON-LD document to flatten.</dd>
+          <dt>string or URL graph</dt>
+          <dd>The graph in the document that should be flattened. To return the default
+            graph <code>@default</code> has to be passed, for the merged graph <code>@merged</code>
+            and for any other graph the <tref>IRI</tref> identifying the graph has to be passed. The
+            default value is <code>@merged</code>.</dd>
+          <dt>ObjectOrURL? context</dt>
+          <dd>An optional external context to use additionally to the context embedded in
+            <code>input</code> when expanding the <code>input</code>.</dd>
+          <dt>JsonLdCallback callback</dt>
+          <dd>A callback that is called when processing is complete on
+            the given <code>input</code>.</dd>
+          <dt>optional JsonLdOptions options</dt>
+          <dd>A set of options that MAY affect the expansion algorithm such as, e.g., the
+            input document's base <tref>IRI</tref>.</dd>
+        </dl>
+
+        <dl class="exception" title="InvalidContext">
+          <dt>INVALID_SYNTAX</dt>
+          <dd>A general syntax error was detected in the <code>@context</code>.
+            For example, if a <code>@type</code> key maps to anything other than
+            <code>@id</code> or an <tref>absolute IRI</tref>, this exception would be raised.</dd>
+          <dt>LOAD_ERROR</dt>
+          <dd>There was a problem encountered loading a remote context.</dd>
+        </dl>
+
+        <dl class="exception" title="ProcessingError">
+          <dt>LIST_OF_LISTS_DETECTED</dt>
+          <dd>A list of lists was detected. This is not supported in this
+            version of JSON-LD.</dd>
+        </dl>
+      </dd>
+
       <dt>void fromRDF()</dt>
       <dd>Creates a JSON-LD document given an set of <a>Statement</a>s.
         <dl class="parameters">
@@ -1969,6 +2015,77 @@
 </section>
 </section>
 
+<section>
+<h1>Flattening</h1>
+
+<p>Flattening is the process of taking a JSON-LD document, <a href="#expansion">expanding</a>
+  it, labeling all unlabeled nodes with a <tref>blank node</tref> identifier, and returning
+  an array of the nodes defined in the document.</p>
+
+<p class="issue">It is still being discussed if the flatten() method should be added or not.
+  See <a href="https://github.com/json-ld/json-ld.org/issues/109">ISSUE-109</a>.</p>
+
+<p>For example, assume the following JSON-LD input document:</p>
+
+<pre class="example" data-transform="updateExample">
+<!--
+{
+  "@context": {
+    "name": "http://xmlns.com/foaf/0.1/name",
+    "knows": "foaf:knows"
+  },
+  "@id": "http://example.com/markus",
+  "name": "Markus Lanthaler",
+  "knows": {
+    "name": "Manu Sporny"
+  }
+}
+-->
+</pre>
+
+<p>Running the JSON-LD Flattening algorithm for the merged graph (<code>@merged</code>) against
+  the JSON-LD input document provided above would result in the following output:</p>
+
+<pre class="example" data-transform="updateExample">
+<!--
+[
+  {
+    "@id": "http://example.com/markus",
+    "foaf:knows": [ { "@id": "_:t0" }
+    ],
+    "http://xmlns.com/foaf/0.1/name": [ { "@value": "Markus Lanthaler" } ]
+  },
+  {
+    "@id": "_:t0",
+    "http://xmlns.com/foaf/0.1/name": [ { "@value": "Manu Sporny" } ]
+  }
+]
+-->
+</pre>
+
+<section>
+<h2>Flattening Algorithm</h2>
+<p>The algorithm takes two input variables, an <em>element</em> to flatten and the
+  <em>graph</em> for which the node definitions should be returned. If <em>graph</em>
+  is not set, it will default to <code>@merged</code> which represents the result of
+  merging all graphs including the default graph (<code>@default</code>).</p>
+
+<ol class="algorithm">
+  <li>Expand <em>element</em> according the <a href="#expansion-algorithm">Expansion Algorithm</a>.</li>
+  <li>Generate a <em>subjectMap</em> according the <a href="#subject-map-generation">Subject Map Generation Algorithm</a>.</li>
+  <li>Initialize an empty array <em>result</em>.</li>
+  <li>If <em>subjectMap</em> has no property <em>graph</em>, return <em>result</em>, otherwise set <em>definitions</em> to its value.</li>
+  <li>Foreach <em>property</em> and <em>value</em> of of <em>definitions</em>:
+    <ol class="algorithm">
+      <li>Add <em>value</em> to <em>result</em>.</li>
+    </ol>
+  </li>
+  <li>Return <em>result</em>.</li>
+</ol>
+
+</section>
+</section>
+
 </section>
 
 <section>