Started adding algorithms for expansion/compaction.
authorManu Sporny <msporny@digitalbazaar.com>
Thu, 04 Aug 2011 08:44:38 -0400
changeset 100 0872ff171f11
parent 99 6ceea19fa51b
child 103 2ef62cb29d75
Started adding algorithms for expansion/compaction.
spec/latest/index.html
--- a/spec/latest/index.html	Wed Aug 03 17:10:03 2011 -0700
+++ b/spec/latest/index.html	Thu Aug 04 08:44:38 2011 -0400
@@ -926,7 +926,7 @@
 }
 -->
 </pre>
-d
+
 <p>The example above would generate the following triple:</p>
 
 <pre class="example" data-transform="updateExample">
@@ -1018,6 +1018,53 @@
 </section>
 
 <section>
+<h2>Expansion</h2>
+
+<p>Expansion is the process of taking a JSON-LD document and applying a 
+context such that all IRI, datatypes, and literal values are expanded so
+that the context is no longer necessary. JSON-LD document expansion 
+is typically used when re-mapping JSON-LD documents to application-specific
+JSON documents or as a part of the <a href="#normalization">Normalization</a>
+process.</p>
+
+<p>For example, assume the following JSON-LD input document:</p>
+
+<pre class="example" data-transform="updateExample">
+<!--
+{
+   "name": "Manu Sporny",
+   "homepage": "http://manu.sporny.org/",
+   "@context": 
+   {
+      "name": "http://xmlns.com/foaf/0.1/name",
+      "homepage": "http://xmlns.com/foaf/0.1/homepage",
+      "@coerce": 
+      {
+         "@iri": "homepage"
+      }
+   }
+}
+-->
+</pre>
+
+<p>Running the JSON-LD Expansion algorithm against the JSON-LD input document 
+provided above would result in the following output:</p>
+
+<pre class="example" data-transform="updateExample">
+<!--
+{
+   "http://xmlns.com/foaf/0.1/name": "Manu Sporny",
+   "http://xmlns.com/foaf/0.1/homepage": 
+   {
+      "@iri": "http://manu.sporny.org/"
+   }
+}
+-->
+</pre>
+
+</section> 
+
+<section>
 <h2>Compaction</h2>
 
 <p>Compaction is the process of taking a JSON-LD document and applying a 
@@ -1089,53 +1136,6 @@
 </section>
 
 <section>
-<h2>Expansion</h2>
-
-<p>Expansion is the process of taking a JSON-LD document and applying a 
-context such that all IRI, datatypes, and literal values are expanded so
-that the context is no longer necessary. JSON-LD document expansion 
-is typically used when re-mapping JSON-LD documents to application-specific
-JSON documents or as a part of the <a href="#normalization">Normalization</a>
-process.</p>
-
-<p>For example, assume the following JSON-LD input document:</p>
-
-<pre class="example" data-transform="updateExample">
-<!--
-{
-   "name": "Manu Sporny",
-   "homepage": "http://manu.sporny.org/",
-   "@context": 
-   {
-      "name": "http://xmlns.com/foaf/0.1/name",
-      "homepage": "http://xmlns.com/foaf/0.1/homepage",
-      "@coerce": 
-      {
-         "@iri": "homepage"
-      }
-   }
-}
--->
-</pre>
-
-<p>Running the JSON-LD Expansion algorithm against the JSON-LD input document 
-provided above would result in the following output:</p>
-
-<pre class="example" data-transform="updateExample">
-<!--
-{
-   "http://xmlns.com/foaf/0.1/name": "Manu Sporny",
-   "http://xmlns.com/foaf/0.1/homepage": 
-   {
-      "@iri": "http://manu.sporny.org/"
-   }
-}
--->
-</pre>
-
-</section>
-
-<section>
 <h2>Framing</h2>
 
 <p>A JSON-LD document is a representation of a directed graph. A single
@@ -1528,14 +1528,199 @@
 <section>
 <h1>Algorithms</h1>
 
+<p>All algorithms described in this section are intended to operate on
+language-native data structures. That is, the serialization to a text-based
+JSON document isn't required as input or output to any of these algorithms and 
+language-native data structures MUST be used where applicable.</p>
+
 <section>
-<h2>Compaction</h2>
-<p class="issue">TBD: Explain compaction algorithm.</p>
+  <h2>Syntax Tokens and Keywords</h2>
+  
+  <p>JSON-LD specifies a number of syntax tokens and keywords that are using
+  in all algorithms described in this section:</p>
+  
+  <dl>
+  <dt><code>@context</code></dt><dd>Used to set the <tref>local context</tref>.</dd>
+  <dt><code>@base</code></dt><dd>Used to set the base IRI for all object IRIs affected  by the <tref>active context</tref>.</dd>
+  <dt><code>@vocab</code></dt><dd>Used to set the base IRI for all property IRIs affected by the <tref>active context</tref>.</dd>
+  <dt><code>@coerce</code></dt><dd>Used to specify type coercion rules.</dd>
+  <dt><code>@literal</code></dt><dd>Used to specify a literal value.</dd>
+  <dt><code>@iri</code></dt><dd>Used to specify an IRI value.</dd>
+  <dt><code>@language</code></dt><dd>Used to specify the language for a literal.</dd>
+  <dt><code>@datatype</code></dt><dd>Used to specify the datatype for a literal.</dd>
+  <dt><code>:</code></dt><dd>The separator for CURIEs when used in JSON keys or JSON values.</dd>
+  <dt><code>@subject</code></dt><dd>Sets the active subjects.</dd>
+  <dt><code>@type</code></dt><dd>Used to set the rdf:type of the active subjects. This token may be conferred as syntactic sugar for rdf:type.</dd>
+  </dl>
+</section>
+
+<section>
+  <h2>Algorithm Terms</h2>
+  <dl>
+    <dt><tdef>initial context</tdef></dt>
+    <dd>
+      a context that is specified to the algorithm before processing begins.
+    </dd>
+    <dt><tdef>active subject</tdef></dt>
+    <dd>
+      the currently active subject that the processor should use when 
+      processing.
+    </dd>
+    <dt><tdef>active property</tdef></dt>
+    <dd>
+      the currently active property that the processor should use when 
+      processing.
+    </dd>
+    <dt><tdef>active object</tdef></dt>
+    <dd>
+      the currently active object that the processor should use when
+      processing.
+    </dd>
+    <dt><tdef>active context</tdef></dt>
+    <dd>
+      a context that is used to resolve CURIEs while the processing
+      algorithm is running. The <tref>active context</tref> is the context 
+      contained within the <tref>processor state</tref>.
+    </dd>
+    <dt><tdef>local context</tdef></dt>
+    <dd>
+      a context that is specified at the JSON associative-array level,
+      specified via the <code>@context</code> keyword.
+    </dd>
+    <dt><tdef>processor state</tdef></dt>
+    <dd>
+      the <tref>processor state</tref>, which includes the <tref>active
+      context</tref>, <tref>current subject</tref>, and 
+      <tref>current property</tref>. The <tref>processor state</tref> is managed
+      as a stack with elements from the previous <tref>processor state</tref>
+      copied into a new <tref>processor state</tref> when entering a new 
+      associative array.
+    </dd>
+    <dt><tdef>JSON-LD input</tdef></dt>
+    <dd>
+      The JSON-LD data structure that is provided as input to the algorithm.
+    </dd>
+    <dt><tdef>JSON-LD output</tdef></dt>
+    <dd>
+      The JSON-LD data structure that is produced as output by the algorithm.
+    </dd>
+
+  </dl>
+</section>
+
+<section>
+  <h2 id="context">Context</h2>
+  <p>
+    Processing of JSON-LD data structure is managed recursively.
+    During processing, each rule is applied using information provided by the <tref>active context</tref>.
+    Processing begins by pushing a new <tref>processor state</tref> onto the <tref>processor state</tref> stack and
+    initializing the <tref>active context</tref> with the <tref>initial context</tref>. If a <tref>local context</tref> is encountered,
+    information from the <tref>local context</tref> is merged into the <tref>active context</tref>.
+  </p>
+  <p class="issue">Should the document URL be used as the default for <code>@base</code> in the <tref>initial context</tref>?</p>
+  <p>
+    The <tref>active context</tref> is used for expanding keys and values of an associative array (or elements
+    of a list (see <span a="#list-processing">List Processing</span>)).
+  </p>
+  <p>
+    A <tref>local context</tref> is identified within an associative array having a key of <code>@context</code> with an associative
+    array value. When processing a <tref>local context</tref>, special rules apply:
+  </p>
+  <ul>
+    <li>The key <code>@base</code> MUST have a value of a simple string with the lexical
+    form of IRI and is saved in the <tref>active context</tref> to perform term mapping as described in <a
+    href="#iri-processing">IRI Processing</a>.</li>
+    <li>The key <code>@vocab</code> MUST have a value of a simple string with the lexical
+    form of IRI and is saved in the <tref>active context</tref> to perform term mapping as described in <a
+    href="#iri-processing">IRI Processing</a>.</li>
+    <li>The key <code>@coerce</code> MUST have a value of an associative array. Processing
+      of the associative array is described <a href="#coerce">below</a></li>
+    <li>Otherwise, the key MUST have the lexical form of
+      <cite><a href="http://www.w3.org/TR/2009/REC-xml-names-20091208/#NT-NCName">NCName</a></cite>
+      and MUST have the value of a simple string with the lexical form of IRI.
+      Merge each key-value pair into the <tref>active context</tref>, overwriting
+      any duplicate values.
+    </li>
+  </ul>
+
+  <section>
+    <h3>Coerce</h3>
+    <p>
+      Map each key-value pair in the <tref>local context</tref>'s
+      <code>@coerce</code> mapping into the <tref>active context</tref>'s
+      <code>@coerce</code> mapping, overwriting any duplicate values in 
+      the <tref>active context</tref>'s <code>@coerce</code> mapping.
+      The <code>@coerce</code> mapping has either a single CURIE or an
+      array of CURIEs. When merging with an existing mapping in the <tref>active context</tref>,
+      map all CURIE values to array form and replace with the union of the value from
+      the <tref>local context</tref> and the value of the <tref>active context</tref>. If the result is an array
+      with a single CURIE, the processor MAY represent this as a string value.
+    </p>
+  </section>
+</section>
+
+<section>
+  <h2>IRI Expansion</h2>
+  <p>Keys and some values are evaluated to produce an IRI. This section defines an algorithm for
+    transforming a value representing an IRI into an actual IRI.</p>
+  <p>IRIs may be represented as an explicit string, or as a CURIE, as a value relative to <code>@base</code>
+    or <code>@vocab</code>.</p>
+  <p>
+    CURIEs are defined more formally in [[RDFA-CORE]] <cite><a href="http://www.w3.org/TR/rdfa-core/#s_curies">section 6 &quot;CURIE Syntax Definition&quot;</a></cite>.
+    Generally, a CURIE is composed of a <em>prefix</em> and a <em>suffix</em> separated by a ':'. In
+    JSON-LD, either the prefix may be the empty string, denoting the <tdef>default prefix</tdef>.
+  </p>
+  <p>The algorithm for generating an IRI is:
+    <ol class="algorithm">
+      <li>Split the value into a <em>prefix</em> and <em>suffix</em> from the first occurrence of ':'.</li>
+      <li>If the prefix is a '_' (underscore), the IRI is unchanged.</li>
+      <li>If the <tref>active context</tref> contains a mapping for <em>prefix</em>, generate an IRI
+        by prepending the mapped prefix to the (possibly empty) suffix. Note that an empty
+        suffix and no suffix (meaning the value contains no ':' string at all) are treated equivalently.</li>
+      <li>If the IRI being processed is for a property (i.e., a key value in an associative array, or a
+        value in a <code>@coerce</code> mapping) and the active context has a <code>@vocab</code> mapping,
+        join the mapped value to the suffix using the method described in [[!RFC3987]].</li>
+      <li>If the IRI being processed is for a subject or object (i.e., not a property) and the active context has a <code>@base</code> mapping,
+        join the mapped value to the suffix using the method described in [[!RFC3987]].</li>
+      <li>Otherwise, use the value directly as an IRI.</li>
+    </ol>
+  </p>
 </section>
 
 <section>
 <h2>Expansion</h2>
-<p class="issue">TBD: Explain expansion algorithm.</p>
+
+<p class="issue">This algorithm is a work in progress, do not implement it.</p>
+
+<p>As stated previously, expansion is the process of taking a JSON-LD 
+input and expanding all IRIs to their fully-expanded form. The output
+will not contain a single context declaration and will have all IRIs 
+and typed literals fully expanded.
+</p>
+
+<section>
+<h3>Expansion Algorithm</h3>
+
+<ol class="algorithm">
+  <li>Process each key in the <tref>JSON-LD input</tref> by expanding
+  the key according to the <a href="#iri-expansion">IRI Expansion</a>
+  rules.</li>
+  <li>Process each value 
+  <ol class="algorithm">
+    <li>If the value is an array, process each item in the array starting on
+    step #2.</li>
+    <li>If the value is an object, process the object recursively, starting
+    on step #1.</li>
+    <li>Otherwise, check to see the value has an associated coercion rule. 
+    If the value should be coerced, expand the value according to the 
+    <a href="#value-expansion">Value Expansion</a> rules. If the value does
+    not need to be coerced, leave the value as-is.
+    </li>
+  <li>Remove the context from the object</li>
+  </ol>
+</ol>
+</section>
+
 </section>
 
 <section>
@@ -1723,151 +1908,17 @@
 </section>
 
 <section>
-  <h4>Processing Algorithm Terms</h4>
+  <h4>RDF Conversion Algorithm Terms</h4>
   <dl>
-    <dt><tdef>initial context</tdef></dt>
-    <dd>
-      a context that is specified to the JSON-LD processing algorithm
-      before processing begins.
-    </dd>
     <dt><tdef>default graph</tdef></dt>
     <dd>
       the destination graph for all triples generated by JSON-LD markup.
     </dd>
-    <dt><tdef>active subject</tdef></dt>
-    <dd>
-      the currently active subject that the processor should use when
-      generating triples.
-    </dd>
-    <dt><tdef>active property</tdef></dt>
-    <dd>
-      the currently active property that the processor should use when
-      generating triples.
-    </dd>
-    <dt><tdef>active object</tdef></dt>
-    <dd>
-      the currently active object that the processor should use when
-      generating triples.
-    </dd>
-    <dt><tdef>active context</tdef></dt>
-    <dd>
-      a context that is used to resolve CURIEs while the processing
-      algorithm is running. The <tref>active context</tref> is the context contained within the
-      <tref>processor state</tref>.
-    </dd>
-    <dt><tdef>local context</tdef></dt>
-    <dd>
-      a context that is specified at the JSON associative-array level,
-      specified via the <code>@context</code> keyword.
-    </dd>
-    <dt><tdef>processor state</tdef></dt>
-    <dd>
-      the <tref>processor state</tref>, which includes the <tref>active
-      context</tref>, <tref>current subject</tref>, and <tref>current property</tref>. The <tref>processor
-      state</tref> is managed as a stack with elements from the previous <tref>processor state</tref>
-      copied into a new <tref>processor state</tref> when entering a new associative array.
-    </dd>
   </dl>
 </section>
 
 <section>
-  <h4>Processing Tokens and Keywords</h4>
-  <dl>
-  <dt><code>@context</code></dt><dd>Used to set the <tref>local context</tref>.</dd>
-  <dt><code>@base</code></dt><dd>Used to set the base IRI for all object IRIs affected  by the <tref>active context</tref>.</dd>
-  <dt><code>@vocab</code></dt><dd>Used to set the base IRI for all property IRIs affected by the <tref>active context</tref>.</dd>
-  <dt><code>@coerce</code></dt><dd>Used to specify type coercion rules.</dd>
-  <dt><code>@literal</code></dt><dd>Used to specify a literal value.</dd>
-  <dt><code>@iri</code></dt><dd>Used to specify an IRI value.</dd>
-  <dt><code>@language</code></dt><dd>Used to specify the language for a literal.</dd>
-  <dt><code>@datatype</code></dt><dd>Used to specify the datatype for a literal.</dd>
-  <dt><code>:</code></dt><dd>The separator for CURIEs when used in JSON keys or JSON values.</dd>
-  <dt><code>@subject</code></dt><dd>Sets the active subjects.</dd>
-  <dt><code>@type</code></dt><dd>Used to set the rdf:type of the active subjects. This token may be conferred as syntactic sugar for rdf:type.</dd>
-  </dl>
-</section>
-
-<section>
-  <h4 id="context">Context</h4>
-  <p>
-    Processing of JSON-LD is managed recursively using a process described in <a href="sequence">Sequence</a>.
-    During processing, each rule is applied using information provided by the <tref>active context</tref>.
-    Processing begins by pushing a new <tref>processor state</tref> onto the <tref>processor state</tref> stack and
-    initializing the <tref>active context</tref> with the <tref>initial context</tref>. If a <tref>local context</tref> is encountered,
-    information from the <tref>local context</tref> is merged into the <tref>active context</tref>.
-  </p>
-  <p class="issue">Should the document URL be used as the default for <code>@base</code> in the <tref>initial context</tref>?</p>
-  <p>
-    The <tref>active context</tref> is used for expanding keys and values of an associative array (or elements
-    of a list (see <span a="#list-processing">List Processing</span>)).
-  </p>
-  <p>
-    A <tref>local context</tref> is identified within an associative array having a key of <code>@context</code> with an associative
-    array value. When processing a <tref>local context</tref>, special rules apply:
-  </p>
-  <ul>
-    <li>The key <code>@base</code> MUST have a value of a simple string with the lexical
-    form of IRI and is saved in the <tref>active context</tref> to perform term mapping as described in <a
-    href="#iri-processing">IRI Processing</a>.</li>
-    <li>The key <code>@vocab</code> MUST have a value of a simple string with the lexical
-    form of IRI and is saved in the <tref>active context</tref> to perform term mapping as described in <a
-    href="#iri-processing">IRI Processing</a>.</li>
-    <li>The key <code>@coerce</code> MUST have a value of an associative array. Processing
-      of the associative array is described <a href="#coerce">below</a></li>
-    <li>Otherwise, the key MUST have the lexical form of
-      <cite><a href="http://www.w3.org/TR/2009/REC-xml-names-20091208/#NT-NCName">NCName</a></cite>
-      and MUST have the value of a simple string with the lexical form of IRI.
-      Merge each key-value pair into the <tref>active context</tref>, overwriting
-      any duplicate values.
-    </li>
-  </ul>
-
-  <section>
-    <h5>Coerce</h5>
-    <p>
-      Map each key-value pair in the <tref>local context</tref>'s
-      <code>@coerce</code> mapping into the <tref>active context</tref>'s
-      <code>@coerce</code> mapping, overwriting any duplicate values in 
-      the <tref>active context</tref>'s <code>@coerce</code> mapping.
-      The <code>@coerce</code> mapping has either a single CURIE or an
-      array of CURIEs. When merging with an existing mapping in the <tref>active context</tref>,
-      map all CURIE values to array form and replace with the union of the value from
-      the <tref>local context</tref> and the value of the <tref>active context</tref>. If the result is an array
-      with a single CURIE, the processor MAY represent this as a string value.
-    </p>
-  </section>
-</section>
-
-<section>
-  <h4>IRI Processing</h4>
-  <p>Keys and some values are evaluated to produce an IRI. This section defines an algorithm for
-    transforming a value representing an IRI into an actual IRI.</p>
-  <p>IRIs may be represented as an explicit string, or as a CURIE, as a value relative to <code>@base</code>
-    or <code>@vocab</code>.</p>
-  <p>
-    CURIEs are defined more formally in [[RDFA-CORE]] <cite><a href="http://www.w3.org/TR/rdfa-core/#s_curies">section 6 &quot;CURIE Syntax Definition&quot;</a></cite>.
-    Generally, a CURIE is composed of a <em>prefix</em> and a <em>suffix</em> separated by a ':'. In
-    JSON-LD, either the prefix may be the empty string, denoting the <tdef>default prefix</tdef>.
-  </p>
-  <p>The procedure for generating an IRI is:
-    <ol class="algorithm">
-      <li>Split the value into a <em>prefix</em> and <em>suffix</em> from the first occurrence of ':'.</li>
-      <li>If the prefix is a '_', generate a named BNode using the suffix as the name.</li>
-      <li>If the <tref>active context</tref> contains a mapping for <em>prefix</em>, generate an IRI
-        by prepending the mapped prefix to the (possibly empty) suffix. Note that an empty
-        suffix and no suffix (meaning the value contains no ':' string at all) are treated equivalently.</li>
-      <li>If the IRI being processed is for a property (i.e., a key value in an associative array, or a
-        value in a <code>@coerce</code> mapping) and the active context has a <code>@vocab</code> mapping,
-        join the mapped value to the suffix using the method described in [[!RFC3987]].</li>
-      <li>If the IRI being processed is for a subject or object (i.e., not a property) and the active context has a <code>@base</code> mapping,
-        join the mapped value to the suffix using the method described in [[!RFC3987]].</li>
-      <li>Otherwise, use the value directly as an IRI.</li>
-    </ol>
-  </p>
-</section>
-
-<section>
-  <h3>Sequence</h3>
+  <h3>RDF Conversion Algorithm</h3>
 
   <p>
     The algorithm below is designed for in-memory implementations with random access to associative