Update to latest jsonld.js.
authorDave Longley <dlongley@digitalbazaar.com>
Wed, 06 Mar 2013 14:12:45 -0500
changeset 1383 e0d3e39da181
parent 1382 10a457f7b664
child 1384 13cf8bd58396
Update to latest jsonld.js.
playground/jsonld.js
--- a/playground/jsonld.js	Wed Mar 06 14:12:29 2013 -0500
+++ b/playground/jsonld.js	Wed Mar 06 14:12:45 2013 -0500
@@ -1403,30 +1403,11 @@
       return _compactValue(activeCtx, activeProperty, element);
     }
 
-    // shallow copy element and arrays so keys and values can be removed
-    // during property generator compaction
-    var shallow = {};
-    for(var expandedProperty in element) {
-      if(_isArray(element[expandedProperty])) {
-        shallow[expandedProperty] = element[expandedProperty].slice();
-      }
-      else {
-        shallow[expandedProperty] = element[expandedProperty];
-      }
-    }
-    element = shallow;
-
     // process element keys in order
     var keys = Object.keys(element).sort();
     var rval = {};
     for(var ki = 0; ki < keys.length; ++ki) {
       var expandedProperty = keys[ki];
-
-      // skip key if removed during property generator duplicate handling
-      if(!(expandedProperty in element)) {
-        continue;
-      }
-
       var expandedValue = element[expandedProperty];
 
       // compact @id and @type(s)
@@ -1476,7 +1457,7 @@
       // preserve empty arrays
       if(expandedValue.length === 0) {
         var itemActiveProperty = _compactIri(
-          activeCtx, expandedProperty, expandedValue, {vocab: true}, element);
+          activeCtx, expandedProperty, expandedValue, {vocab: true});
         jsonld.addValue(
           rval, itemActiveProperty, expandedValue, {propertyIsArray: true});
       }
@@ -1487,19 +1468,10 @@
 
         // compact property and get container type
         var itemActiveProperty = _compactIri(
-          activeCtx, expandedProperty, expandedItem, {vocab: true}, element);
+          activeCtx, expandedProperty, expandedItem, {vocab: true});
         var container = jsonld.getContextValue(
           activeCtx, itemActiveProperty, '@container');
 
-        // remove any duplicates that were (presumably) generated by a
-        // property generator
-        var mapping = activeCtx.mappings[itemActiveProperty];
-        if(mapping && mapping.propertyGenerator) {
-          _findPropertyGeneratorDuplicates(
-            activeCtx, element, expandedProperty, expandedItem,
-            itemActiveProperty, true);
-        }
-
         // get @list value if appropriate
         var isList = _isList(expandedItem);
         var list = null;
@@ -1656,7 +1628,6 @@
     for(var ki = 0; ki < keys.length; ++ki) {
       var key = keys[ki];
       var value = element[key];
-      var expandedProperty;
       var expandedValue;
 
       // skip @context
@@ -1664,21 +1635,12 @@
         continue;
       }
 
-      // expand key using property generator
-      var mapping = activeCtx.mappings[key];
-      if(mapping && mapping.propertyGenerator) {
-        expandedProperty = mapping['@id'];
-      }
       // expand key to IRI
-      else {
-        expandedProperty = _expandIri(activeCtx, key, {vocab: true});
-      }
+      var expandedProperty = _expandIri(activeCtx, key, {vocab: true});
 
       // drop non-absolute IRI keys that aren't keywords
       if(expandedProperty === null ||
-        !(_isArray(expandedProperty) ||
-        _isAbsoluteIri(expandedProperty) ||
-        _isKeyword(expandedProperty))) {
+        !(_isAbsoluteIri(expandedProperty) || _isKeyword(expandedProperty))) {
         continue;
       }
 
@@ -1797,24 +1759,13 @@
         expandedValue = {'@list': expandedValue};
       }
 
-      // add copy of value for each property from property generator
-      if(_isArray(expandedProperty)) {
-        expandedValue = _labelBlankNodes(activeCtx.namer, expandedValue);
-        for(var i = 0; i < expandedProperty.length; ++i) {
-          jsonld.addValue(
-            rval, expandedProperty[i], _clone(expandedValue),
-            {propertyIsArray: true});
-        }
-      }
       // add value for property
-      else {
-        // use an array except for certain keywords
-        var useArray =
-          ['@index', '@id', '@type', '@value', '@language'].indexOf(
-            expandedProperty) === -1;
-        jsonld.addValue(
-          rval, expandedProperty, expandedValue, {propertyIsArray: useArray});
-      }
+      // use an array except for certain keywords
+      var useArray =
+        ['@index', '@id', '@type', '@value', '@language'].indexOf(
+          expandedProperty) === -1;
+      jsonld.addValue(
+        rval, expandedProperty, expandedValue, {propertyIsArray: useArray});
     }
 
     // get property count on expanded output
@@ -3687,7 +3638,6 @@
  * @param activeCtx the active context.
  * @param iri the IRI to pick the term for.
  * @param value the value to pick the term for.
- * @param parent the parent of the value (required for property generators).
  * @param containers the preferred containers.
  * @param typeOrLanguage either '@type' or '@language'.
  * @param typeOrLanguageValue the preferred value for '@type' or '@language'.
@@ -3695,8 +3645,7 @@
  * @return the preferred term.
  */
 function _selectTerm(
-  activeCtx, iri, value, parent, containers,
-  typeOrLanguage, typeOrLanguageValue) {
+  activeCtx, iri, value, containers, typeOrLanguage, typeOrLanguageValue) {
   containers.push('@none');
   if(typeOrLanguageValue === null) {
     typeOrLanguageValue = '@null';
@@ -3708,8 +3657,7 @@
   // determine options for @id based on whether or not value compacts to a term
   if(typeOrLanguageValue === '@id' && _isSubjectReference(value)) {
     // try to compact value to a term
-    var term = _compactIri(
-      activeCtx, value['@id'], null, {vocab: true});
+    var term = _compactIri(activeCtx, value['@id'], null, {vocab: true});
     if(term in activeCtx.mappings &&
       activeCtx.mappings[term] &&
       activeCtx.mappings[term]['@id'] === value['@id']) {
@@ -3742,25 +3690,8 @@
         continue;
       }
 
-      var termInfo = typeOrLanguageValueMap[option];
-
-      // see if a property generator matches
-      if(_isObject(parent)) {
-        for(var pi = 0; pi < termInfo.propertyGenerators.length; ++pi) {
-          var propertyGenerator = termInfo.propertyGenerators[pi];
-          var match = _findPropertyGeneratorDuplicates(
-            activeCtx, parent, iri, value, propertyGenerator, false);
-          if(match) {
-            term = propertyGenerator;
-            break;
-          }
-        }
-      }
-
-      // no matching property generator, use a simple term instead
-      if(term === null) {
-        term = termInfo.term;
-      }
+      // select term
+      term = typeOrLanguageValueMap[option];
     }
   }
 
@@ -3777,11 +3708,10 @@
  * @param relativeTo options for how to compact IRIs:
  *          base: true to resolve against the base IRI, false not to.
  *          vocab: true to split after @vocab, false not to.
- * @param parent the parent element for the value.
  *
  * @return the compacted term, prefix, keyword alias, or the original IRI.
  */
-function _compactIri(activeCtx, iri, value, relativeTo, parent) {
+function _compactIri(activeCtx, iri, value, relativeTo) {
   // can't compact null
   if(iri === null) {
     return iri;
@@ -3798,9 +3728,6 @@
   if(_isUndefined(value)) {
     value = null;
   }
-  if(_isUndefined(parent)) {
-    parent = null;
-  }
   relativeTo = relativeTo || {};
 
   // use inverse context to pick a term if iri is relative to vocab
@@ -3893,8 +3820,7 @@
 
     // do term selection
     var term = _selectTerm(
-      activeCtx, iri, value, parent,
-      containers, typeOrLanguage, typeOrLanguageValue);
+      activeCtx, iri, value, containers, typeOrLanguage, typeOrLanguageValue);
     if(term !== null) {
       return term;
     }
@@ -3909,7 +3835,7 @@
     }
     // skip entries with @ids that are not partial matches
     var definition = activeCtx.mappings[term];
-    if(!definition || definition.propertyGenerator ||
+    if(!definition ||
       definition['@id'] === iri || iri.indexOf(definition['@id']) !== 0) {
       continue;
     }
@@ -4047,72 +3973,6 @@
 }
 
 /**
- * Finds and, if specified, removes any duplicate values that were presumably
- * generated by a property generator in the given element.
- *
- * @param activeCtx the active context.
- * @param element the element to remove duplicates from.
- * @param expandedProperty the property to map to a property generator.
- * @param value the value to compare against when duplicate checking.
- * @param activeProperty the property generator term.
- * @param remove true to remove the duplicates found, false not to.
- *
- * @return true if duplicates were found for every IRI.
- */
-function _findPropertyGeneratorDuplicates(
-  activeCtx, element, expandedProperty, value, activeProperty, remove) {
-  var rval = true;
-
-  // get property generator IRIs
-  var iris = activeCtx.mappings[activeProperty]['@id'];
-
-  // for each IRI that isn't 'expandedProperty', remove a single duplicate
-  // from element, if found
-  for(var i = 0; rval && i < iris.length; ++i) {
-    var iri = iris[i];
-    if(iri === expandedProperty) {
-      continue;
-    }
-
-    rval = false;
-    if(!(iri in element)) {
-      break;
-    }
-
-    var prospects = element[iri];
-
-    // handle empty array case
-    if(_isArray(value) && value.length === 0) {
-      rval = true;
-      if(remove) {
-        delete element[iri];
-      }
-      continue;
-    }
-
-    // handle other cases
-    for(var pi = 0; pi < prospects.length; ++pi) {
-      var prospect = prospects[pi];
-      if(jsonld.compareValues(prospect, value)) {
-        // duplicate found
-        rval = true;
-
-        if(remove) {
-          // remove it in place
-          prospects.splice(pi, 1);
-          if(prospects.length === 0) {
-            delete element[iri];
-          }
-        }
-        break;
-      }
-    }
-  }
-
-  return rval;
-}
-
-/**
  * Creates a term definition during context processing.
  *
  * @param activeCtx the current active context.
@@ -4197,42 +4057,10 @@
 
   // create new mapping
   var mapping = {};
-  mapping.propertyGenerator = false;
 
   if('@id' in value) {
     var id = value['@id'];
-    // handle property generator
-    if(_isArray(id)) {
-      if(activeCtx.namer === null) {
-        throw new JsonLdError(
-          'Incompatible JSON-LD options; a property generator was found ' +
-          'in the @context, but blank node renaming has been disabled; ' +
-          'it must be enabled to use property generators.',
-          'jsonld.OptionsError', {context: localCtx});
-      }
-
-      var propertyGenerator = [];
-      var ids = id;
-      for(var i = 0; i < ids.length; ++i) {
-        // expand @id
-        id = ids[i];
-        if(_isString(id)) {
-          id = _expandIri(
-            activeCtx, id, {vocab: true, base: true}, localCtx, defined);
-        }
-        if(!_isString(id) || _isKeyword(id)) {
-          throw new JsonLdError(
-            'Invalid JSON-LD syntax; property generators must consist of an ' +
-            '@id array containing only strings and no string can be "@type".',
-            'jsonld.SyntaxError', {context: localCtx});
-        }
-        propertyGenerator.push(id);
-      }
-      // add sorted property generator as @id in mapping
-      mapping['@id'] = propertyGenerator.sort();
-      mapping.propertyGenerator = true;
-    }
-    else if(!_isString(id)) {
+    if(!_isString(id)) {
       throw new JsonLdError(
         'Invalid JSON-LD syntax; a @context @id value must be an array ' +
         'of strings or a string.',
@@ -4361,22 +4189,15 @@
   var rval = null;
 
   if(relativeTo.vocab) {
-    // term dependency cannot be a property generator
     var mapping = activeCtx.mappings[value];
-    if(localCtx && mapping && mapping.propertyGenerator) {
-      throw new JsonLdError(
-        'Invalid JSON-LD syntax; a term definition cannot have a property ' +
-        'generator as a dependency.',
-        'jsonld.SyntaxError', {context: localCtx, value: value});
-    }
 
     // value is explicitly ignored with a null mapping
     if(mapping === null) {
       return null;
     }
 
-    // value is a term
-    if(mapping && !mapping.propertyGenerator) {
+    if(mapping) {
+      // value is a term
       rval = mapping['@id'];
     }
   }
@@ -4396,10 +4217,10 @@
           _createTermDefinition(activeCtx, localCtx, prefix, defined);
         }
 
-        // use mapping if prefix is defined and not a property generator
+        // use mapping if prefix is defined
         var mapping = activeCtx.mappings[prefix];
-        if(mapping && !mapping.propertyGenerator) {
-          rval = activeCtx.mappings[prefix]['@id'] + suffix;
+        if(mapping) {
+          rval = mapping['@id'] + suffix;
         }
       }
     }
@@ -4709,7 +4530,7 @@
   }
 
   /**
-   * Adds or updates the term or property generator for the given entry.
+   * Adds the term for the given entry if not already added.
    *
    * @param mapping the term mapping.
    * @param term the term to add.
@@ -4718,15 +4539,7 @@
    */
   function _addPreferredTerm(mapping, term, entry, typeOrLanguageValue) {
     if(!(typeOrLanguageValue in entry)) {
-      entry[typeOrLanguageValue] = {term: null, propertyGenerators: []};
-    }
-
-    var e = entry[typeOrLanguageValue];
-    if(mapping.propertyGenerator) {
-      e.propertyGenerators.push(term);
-    }
-    else if(e.term === null) {
-      e.term = term;
+      entry[typeOrLanguageValue] = term;
     }
   }