Added 80% of the framing algorithm.
authorManu Sporny <msporny@digitalbazaar.com>
Fri, 05 Aug 2011 18:02:53 -0400
changeset 115 eb97b73b6ac2
parent 114 684e1dee3105
child 118 6bae0190ffea
Added 80% of the framing algorithm.
spec/latest/index.html
--- a/spec/latest/index.html	Thu Aug 04 19:54:20 2011 -0400
+++ b/spec/latest/index.html	Fri Aug 05 18:02:53 2011 -0400
@@ -1755,15 +1755,15 @@
 
   <p>The algorithm for generating a compacted IRI is:
     <ol class="algorithm">
-      <li>Search every key-value pair in the <tref>local context</tref> that is 
-        a <tref>term</tref> or a <tref>prefix</tref> for a complete match 
+      <li>Search every key-value pair in the <tref>active context</tref> for
+        a <tref>term</tref> that is a complete match 
         against the IRI. If a complete match is found, the resulting compacted
         IRI is the <tref>term</tref> associated with the IRI in the 
-        <tref>local context</tref>.</li>
+        <tref>active context</tref>.</li>
       <li>If a complete match is not found, search for a partial match from
         the beginning of the IRI. For all matches that are found, the resulting
         compacted IRI is the <tref>prefix</tref> associated with the partially
-        matched IRI in the <tref>local context</tref> concatenated with a 
+        matched IRI in the <tref>active context</tref> concatenated with a 
         colon (:) character and the unmatched part of the string. If there is
         more than one compacted IRI produced, the final value is the 
         lexicographically least value of the entire set of compacted IRIs.</li>
@@ -1900,7 +1900,168 @@
 
 <section>
 <h2>Framing</h2>
-<p class="issue">TBD: Explain framing algorithm.</p>
+
+<p class="issue">This algorithm is a work in progress, do not implement it.</p>
+
+<p>A JSON-LD document is a representation of a directed graph. A single
+directed graph can have many different serializations, each expressing
+exactly the same information. Developers typically don't work directly with
+graphs, but rather, prefer trees when dealing with JSON. While mapping a graph 
+to a tree can be done, the layout of the end result must be specified in 
+advance. This section defines an algorithm for mapping a graph to 
+a tree given a <tref>frame</tref>.
+</p>
+
+<section>
+<h3>Framing Algorithm Terms</h3>
+ <dl>
+   <dt><tdef>input frame</tdef></dt>
+   <dd>
+     the initial <tref>frame</tref> provided to the framing algorithm.
+   </dd>
+   <dt><tdef>framing context</tdef></dt>
+   <dd>
+     a context containing the <tref>object embed flag</tref>, the
+     <tref>explicit inclusion flag</tref> and the 
+     <tref>omit default flag</tref>.
+   </dd>
+   <dt><tdef>object embed flag</tdef></dt>
+   <dd>
+     a flag specifying that objects should be directly embedded in the output,
+     instead of being referred to by their IRI.
+   </dd>
+   <dt><tdef>explicit inclusion flag</tdef></dt>
+   <dd>
+     a flag specifying that for properties to be included in the output, they
+     must be explicitly declared in the <tref>framing context</tref>.
+   </dd>
+   <dt><tdef>omit missing properties flag</tdef></dt>
+   <dd>
+     a flag specifying that properties that are missing from the 
+     <tref>JSON-LD input</tref> should be omitted from the output.
+   </dd>
+   <dt><tdef>match limit</tdef></dt>
+   <dd>
+     A value specifying the maximum number of matches to accept when building
+     arrays of values during the framing algorithm. A value of -1 specifies
+     that there is no match limit.
+   </dd>
+   <dt><tdef>map of embedded subjects</tdef></dt>
+   <dd>
+     A map that tracks if a subject has been embedded in the output of the
+     <a href="#framing-algorithm">Framing Algorithm</a>.
+ </dl>
+</section>
+
+<section>
+<h3>Framing Algorithm</h3>
+
+<p>The framing algorithm takes <tref>JSON-LD input</tref> that has been 
+normalized according to the 
+<a href="#normalization-algorithm">Normalization Algorithm</a> 
+(<strong>normalized input</strong>), an 
+<tref>input frame</tref> that has been expanded according to the
+<a href="#expansion-algorithm">Expansion Algorithm</a> 
+(<strong>expanded frame</strong>), and a number of options and produces
+<tref>JSON-LD output</tref>. 
+</p>
+
+<ol class="algorithm">
+  <li>Initialize the <tref>framing context</tref> by setting the
+   <tref>object embed flag</tref>, clearing the 
+   <tref>explicit inclusion flag</tref>, and clearing the
+   <tref>omit missing properties flag</tref>. Override these values
+   based on input options provided to the algorithm by the application.
+  </li>
+  <li>Generate a <tdef>list of frames</tdef> by processing the 
+    <strong>expanded frame</strong>:
+    <ol class="algorithm">
+      <li>If the <strong>expanded frame</strong> is not an array, set 
+        <tref>match limit</tref> to 1, place the 
+        <strong>expanded frame</strong> into the <tref>list of frames</tref>,
+        and set the <tref>JSON-LD output</tref> to <code>null</code>.</li>
+      <li>If the <strong>expanded frame</strong> is an empty array, place an
+        empty object into the <tref>list of frames</tref>,
+        and set the <tref>JSON-LD output</tref> to an array, and set
+        <tref>match limit</tref> to -1.</li>
+      <li>If the <strong>expanded frame</strong> is a non-empty array, add
+        each item in the <strong>expanded frame</strong> into the 
+        <tref>list of frames</tref> and set <tref>match limit</tref> to -1.</li>
+    </ol></li>
+  <li>Create a <tdef>match array</tdef> for each <strong>expanded frame</strong> 
+    in the <tref>list of frames</tref> halting when either the 
+    <tref>match limit</tref> is zero or the end of the 
+    <tref>list of frames</tref> is reached. If an 
+    <strong>expanded frame</strong> is 
+    not an object, the processor MUST throw a <code>Invalid Frame Format</code> 
+    exception. Add each matching item from the <strong>normalized input</strong>
+    to the <tref>matches array</tref> and decrement the 
+    <tref>match limit</tref> by 1 if:
+    <ol class="algorithm">
+       <li>The <strong>expanded frame</strong> has an <code>rdf:type</code> 
+         that exists in the item's list of <code>rdf:type</code>s. Note: 
+         the <code>rdf:type</code> can be an array, but only one value needs 
+         to be in common between the item and the 
+         <strong>expanded frame</strong> for a match.</li>
+       <li>The <strong>expanded frame</strong> does not have an 
+         <code>rdf:type</code> property, but every property in the 
+         <strong>expanded frame</strong> exists in the item.</li>
+    </ol></li>
+  <li>Process each item in the <tref>match array</tref> with its associated
+    <tdef>match frame</tdef>:
+    <ol class="algorithm">
+      <li>If the <tref>match frame</tref> contains an <code>@embed</code>
+        keyword, set the <tref>object embed flag</tref> to its value. Note: 
+        valid values are <code>true</code> and <code>false</code>.
+      <li>If the <tref>match frame</tref> contains an <code>@explicit</code>
+        keyword, set the <tref>explicit inclusion flag</tref> to its value. 
+        Note: valid values are <code>true</code> and <code>false</code>.
+      <li>If the <tref>match frame</tref> contains an <code>@omitDefault</code>
+        keyword, set the <tref>omit missing properties flag</tref> to its value. 
+        Note: valid values are <code>true</code> and <code>false</code>.
+      <li>If the <tref>object embed flag</tref> is cleared and the item has
+        the <code>@subject</code> property, replace the item with the value
+        of the <code>@subject</code> property.</li>
+      <li>If the <tref>object embed flag</tref> is set and the item has
+        the <code>@subject</code> property, and its IRI is in the 
+        <tref>map of embedded subjects</tref>, throw a
+        <code>Duplicate Embed</code> exception.</li>
+      <li>If the <tref>object embed flag</tref> is set and the item has
+        the <code>@subject</code> property and its IRI is not in the
+        <tref>map of embedded subjects</tref>:
+        <ol class="algorithm">
+          <li>If the <tref>explicit inclusion flag</tref> is set, 
+            then delete any key from the item that does not exist in the
+            <tref>match frame</tref>, except <code>@subject</code>.</li>
+          <li>For each key in the <tref>match frame</tref>, except for
+            keywords and <code>rdf:type</code>:
+          <ol class="algorithm">
+            <li>If the key is in the item, then build a new input list 
+              using the objects from item[key]. If any object is an IRI that 
+              is a subject in the graph, use the subject in the new input 
+              list, not the IRI. Set value[key] to the return value of 
+              Frame(newInput, frame[key]).</li>
+            <li>If the key is not in the value, set value[key] to an empty 
+              array if the frame[key] is an array and null if it isn't.</li>
+            <li>If value[key] is null, set a default value:
+              <ol class="algorithm">
+                <li>Set f to frame[key]. If frame[key] is an array, use the first 
+                  frame from the array, using an empty object frame is it is 
+                  empty.</li>
+                <li>If @omitDefault is on (f has @omitDefault set to true or the 
+                  default is true), delete the key in value. Otherwise, if 
+                  @default is set in f, set value[key] to f['@default'].</li>
+              </ol></li>
+          </ol></li>
+        </ol>
+      <li>If the output is null, set the output to the value. If the output 
+        is an array, push the value onto the array.</li>
+    </ol>
+  <li>Return the output.</li>
+  <li>Compact the output using the frame's saved context.</li>
+</ol>  
+</section>
+
 </section>
 
 <section>