Add framing test case, update to latest jsonld.js. Closes #124.
authorDave Longley <dlongley@digitalbazaar.com>
Mon, 21 May 2012 17:42:13 -0400
changeset 665 3595fa871cbc
parent 664 28d69a9da4a9
child 666 996923b81826
Add framing test case, update to latest jsonld.js. Closes #124.
playground/jsonld.js
test-suite/tests/frame-0019-frame.jsonld
test-suite/tests/frame-0019-in.jsonld
test-suite/tests/frame-0019-out.jsonld
test-suite/tests/frame-0020-frame.jsonld
test-suite/tests/frame-0020-in.jsonld
test-suite/tests/frame-0020-out.jsonld
test-suite/tests/frame-0021-frame.jsonld
test-suite/tests/frame-0021-in.jsonld
test-suite/tests/frame-0021-out.jsonld
test-suite/tests/frame-manifest.jsonld
--- a/playground/jsonld.js	Mon May 21 15:52:15 2012 -0400
+++ b/playground/jsonld.js	Mon May 21 17:42:13 2012 -0400
@@ -1365,12 +1365,16 @@
   // create framing state
   var state = {
     options: options,
-    subjects: {}
+    graphs: {'@default': {}, '@merged': {}}
   };
 
-  // produce a map of all subjects and name each bnode
+  // produce a map of all graphs and name each bnode
   var namer = new UniqueNamer('_:t');
-  _flatten(state.subjects, input, namer);
+  _flatten(input, state.graphs, '@default', namer);
+  namer = new UniqueNamer('_:t');
+  _flatten(input, state.graphs, '@merged', namer);
+  // FIXME: currently uses subjects from @merged graph only
+  state.subjects = state.graphs['@merged'];
 
   // frame the subjects
   var framed = [];
@@ -2295,17 +2299,18 @@
 /**
  * Recursively flattens the subjects in the given JSON-LD expanded input.
  *
- * @param subjects a map of subject @id to subject.
  * @param input the JSON-LD expanded input.
+ * @param graphs a map of graph name to subject map.
+ * @param graph the name of the current graph.
  * @param namer the blank node namer.
  * @param name the name assigned to the current input if it is a bnode.
  * @param list the list to append to, null for none.
  */
-function _flatten(subjects, input, namer, name, list) {
+function _flatten(input, graphs, graph, namer, name, list) {
   // recurse through array
   if(_isArray(input)) {
     for(var i in input) {
-      _flatten(subjects, input[i], namer, undefined, list);
+      _flatten(input[i], graphs, graph, namer, undefined, list);
     }
   }
   // handle subject
@@ -2327,6 +2332,7 @@
     }
 
     // create new subject or merge into existing one
+    var subjects = graphs[graph];
     var subject = subjects[name] = subjects[name] || {};
     subject['@id'] = name;
     for(var prop in input) {
@@ -2335,8 +2341,19 @@
         continue;
       }
 
-      // copy keywords
-      if(_isKeyword(prop)) {
+      // recurse into graph
+      if(prop === '@graph') {
+        // add graph subjects map entry
+        if(!(name in graphs)) {
+          graphs[name] = {};
+        }
+        var g = (graph === '@merged') ? '@merged' : name;
+        _flatten(input[prop], graphs, g, namer);
+        continue;
+      }
+
+      // copy non-@type keywords
+      if(prop !== '@type' && _isKeyword(prop)) {
         subject[prop] = input[prop];
         continue;
       }
@@ -2353,14 +2370,18 @@
 
           // add reference and recurse
           jsonld.addValue(subject, prop, {'@id': id}, true);
-          _flatten(subjects, o, namer, id, null);
+          _flatten(o, graphs, graph, namer, id);
         }
         else {
           // recurse into list
           if(_isList(o)) {
-            var l = [];
-            _flatten(subjects, o['@list'], namer, name, l);
-            o = {'@list': l};
+            var _list = [];
+            _flatten(o['@list'], graphs, graph, namer, name, _list);
+            o = {'@list': _list};
+          }
+          // special-handle @type IRIs
+          else if(prop === '@type' && o.indexOf('_:') === 0) {
+            o = namer.getName(o);
           }
 
           // add non-subject
--- a/test-suite/tests/frame-0019-frame.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0019-frame.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -3,4 +3,4 @@
     "ex": "http://example.org/terms#"
   },
   "@type": "ex:Node"
-}
+}
\ No newline at end of file
--- a/test-suite/tests/frame-0019-in.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0019-in.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -17,4 +17,4 @@
     "ex:sees": "ex:node1",
     "ex:color": "red"
   }]
-}
+}
\ No newline at end of file
--- a/test-suite/tests/frame-0019-out.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0019-out.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -27,5 +27,4 @@
             "ex:color": "blue"
         }
     }]
-}
-
+}
\ No newline at end of file
--- a/test-suite/tests/frame-0020-frame.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0020-frame.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -1,1 +1,1 @@
-{}
+{}
\ No newline at end of file
--- a/test-suite/tests/frame-0020-in.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0020-in.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -31,4 +31,4 @@
     "step": 5,
     "description": "Garnish with a lime wedge."
   }]
-}
+}
\ No newline at end of file
--- a/test-suite/tests/frame-0020-out.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-0020-out.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -77,4 +77,4 @@
     },
     "http://rdf.data-vocabulary.org/#description": "Garnish with a lime wedge."
   }]
-}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/frame-0021-frame.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -0,0 +1,6 @@
+{
+  "@context": {
+    "dc": "http://purl.org/dc/elements/1.1/",
+    "ex": "http://example.org/vocab#"
+  }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/frame-0021-in.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -0,0 +1,31 @@
+{
+  "@context": {
+    "dc": "http://purl.org/dc/elements/1.1/",
+    "ex": "http://example.org/vocab#",
+    "xsd": "http://www.w3.org/2001/XMLSchema#",
+    "ex:contains": {
+      "@type": "@id"
+    }
+  },
+  "@graph": [
+  {
+    "@id": "_:Book",
+    "dc:label": "Book type"
+  }, {
+    "@id": "http://example.org/library",
+    "@type": "ex:Library",
+    "ex:contains": "http://example.org/library/the-republic"
+  }, {
+    "@id": "http://example.org/library/the-republic",
+    "@type": "_:Book",
+    "dc:creator": "Plato",
+    "dc:title": "The Republic",
+    "ex:contains": "http://example.org/library/the-republic#introduction"
+  }, {
+    "@id": "http://example.org/library/the-republic#introduction",
+    "@type": "ex:Chapter",
+    "dc:description": "An introductory chapter on The Republic.",
+    "dc:title": "The Introduction",
+    "dc:list": [1, 2, 3, 4, 4, 4, 5]
+  }]
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-suite/tests/frame-0021-out.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -0,0 +1,45 @@
+{
+  "@context": {
+    "dc": "http://purl.org/dc/elements/1.1/",
+    "ex": "http://example.org/vocab#"
+  },
+  "@graph": [
+  {
+    "@id": "_:t0",
+    "dc:label": "Book type"
+  }, {
+    "@id": "http://example.org/library",
+    "@type": "ex:Library",
+    "ex:contains": {
+      "@id": "http://example.org/library/the-republic",
+      "@type": "_:t0",
+      "dc:creator": "Plato",
+      "dc:title": "The Republic",
+      "ex:contains": {
+        "@id": "http://example.org/library/the-republic#introduction",
+        "@type": "ex:Chapter",
+        "dc:description": "An introductory chapter on The Republic.",
+        "dc:title": "The Introduction",
+        "dc:list": [1, 2, 3, 4, 5]
+      }
+    }
+  }, {
+    "@id": "http://example.org/library/the-republic",
+    "@type": "_:t0",
+    "ex:contains": {
+      "@id": "http://example.org/library/the-republic#introduction",
+      "@type": "ex:Chapter",
+      "dc:description": "An introductory chapter on The Republic.",
+      "dc:title": "The Introduction",
+      "dc:list": [1, 2, 3, 4, 5]
+    },
+    "dc:creator": "Plato",
+    "dc:title": "The Republic"
+  }, {
+    "@id": "http://example.org/library/the-republic#introduction",
+    "@type": "ex:Chapter",
+    "dc:description": "An introductory chapter on The Republic.",
+    "dc:list": [1, 2, 3, 4, 5],
+    "dc:title": "The Introduction"
+  }]
+}
\ No newline at end of file
--- a/test-suite/tests/frame-manifest.jsonld	Mon May 21 15:52:15 2012 -0400
+++ b/test-suite/tests/frame-manifest.jsonld	Mon May 21 17:42:13 2012 -0400
@@ -124,5 +124,11 @@
       "input": "frame-0020-in.jsonld",
       "frame": "frame-0020-frame.jsonld",
       "expect": "frame-0020-out.jsonld"
+   }, {
+      "@type": ["test:TestCase", "jld:FrameTest"],
+      "name": "Blank nodes in @type",
+      "input": "frame-0021-in.jsonld",
+      "frame": "frame-0021-frame.jsonld",
+      "expect": "frame-0021-out.jsonld"
    }]
 }