Remove blank node relabeling from expansion.

This commit is contained in:
Dave Longley 2013-03-27 10:52:25 -04:00
parent d5c0460356
commit 0778e0f7c3

View file

@ -1,7 +1,7 @@
<?php <?php
/** /**
* PHP implementation of the JSON-LD API. * PHP implementation of the JSON-LD API.
* Version: 0.0.17 * Version: 0.0.18
* *
* @author Dave Longley * @author Dave Longley
* *
@ -620,8 +620,6 @@ class JsonLdProcessor {
// set default options // set default options
isset($options['base']) or $options['base'] = ''; isset($options['base']) or $options['base'] = '';
isset($options['strict']) or $options['strict'] = true; isset($options['strict']) or $options['strict'] = true;
isset($options['renameBlankNodes']) or $options['renameBlankNodes'] =
true;
isset($options['compactArrays']) or $options['compactArrays'] = true; isset($options['compactArrays']) or $options['compactArrays'] = true;
isset($options['graph']) or $options['graph'] = false; isset($options['graph']) or $options['graph'] = false;
isset($options['skipExpansion']) or $options['skipExpansion'] = false; isset($options['skipExpansion']) or $options['skipExpansion'] = false;
@ -735,8 +733,6 @@ class JsonLdProcessor {
* @param mixed $input the JSON-LD object to expand. * @param mixed $input the JSON-LD object to expand.
* @param assoc $options the options to use: * @param assoc $options the options to use:
* [base] the base IRI to use. * [base] the base IRI to use.
* [renameBlankNodes] true to rename blank nodes, false not to,
* defaults to true.
* [keepFreeFloatingNodes] true to keep free-floating nodes, * [keepFreeFloatingNodes] true to keep free-floating nodes,
* false not to, defaults to false. * false not to, defaults to false.
* [loadContext(url)] the context loader. * [loadContext(url)] the context loader.
@ -746,8 +742,6 @@ class JsonLdProcessor {
public function expand($input, $options) { public function expand($input, $options) {
// set default options // set default options
isset($options['base']) or $options['base'] = ''; isset($options['base']) or $options['base'] = '';
isset($options['renameBlankNodes']) or $options['renameBlankNodes'] =
true;
isset($options['keepFreeFloatingNodes']) or isset($options['keepFreeFloatingNodes']) or
$options['keepFreeFloatingNodes'] = false; $options['keepFreeFloatingNodes'] = false;
isset($options['loadContext']) or $options['loadContext'] = isset($options['loadContext']) or $options['loadContext'] =
@ -1062,8 +1056,6 @@ class JsonLdProcessor {
* @param stdClass $active_ctx the current active context. * @param stdClass $active_ctx the current active context.
* @param mixed $local_ctx the local context to process. * @param mixed $local_ctx the local context to process.
* @param assoc $options the options to use: * @param assoc $options the options to use:
* [renameBlankNodes] true to rename blank nodes, false not to,
* defaults to true.
* [loadContext(url)] the context loader. * [loadContext(url)] the context loader.
* *
* @return stdClass the new active context. * @return stdClass the new active context.
@ -1071,8 +1063,6 @@ class JsonLdProcessor {
public function processContext($active_ctx, $local_ctx, $options) { public function processContext($active_ctx, $local_ctx, $options) {
// set default options // set default options
isset($options['base']) or $options['base'] = ''; isset($options['base']) or $options['base'] = '';
isset($options['renameBlankNodes']) or $options['renameBlankNodes'] =
true;
isset($options['loadContext']) or $options['loadContext'] = isset($options['loadContext']) or $options['loadContext'] =
'jsonld_get_url'; 'jsonld_get_url';
@ -2695,7 +2685,6 @@ class JsonLdProcessor {
if(property_exists($jsonld_cache, 'activeCtx')) { if(property_exists($jsonld_cache, 'activeCtx')) {
$rval = $jsonld_cache->activeCtx->get($active_ctx, $local_ctx); $rval = $jsonld_cache->activeCtx->get($active_ctx, $local_ctx);
if($rval) { if($rval) {
$rval->namer = $active_ctx->namer;
return $rval; return $rval;
} }
} }
@ -2715,7 +2704,6 @@ class JsonLdProcessor {
// reset to initial context // reset to initial context
if($ctx === null) { if($ctx === null) {
$rval = $this->_getInitialContext($options); $rval = $this->_getInitialContext($options);
$rval->namer = $active_ctx->namer;
continue; continue;
} }
@ -2933,10 +2921,6 @@ class JsonLdProcessor {
// other type // other type
if($type !== null) { if($type !== null) {
// rename blank node if requested
if($active_ctx->namer !== null && strpos($type, '_:') === 0) {
$type = $active_ctx->namer->getName($type);
}
$rval->{'@type'} = $type; $rval->{'@type'} = $type;
} }
// check for language tagging for strings // check for language tagging for strings
@ -4614,8 +4598,6 @@ class JsonLdProcessor {
$this->_createTermDefinition($active_ctx, $local_ctx, $value, $defined); $this->_createTermDefinition($active_ctx, $local_ctx, $value, $defined);
} }
$rval = null;
if(isset($relative_to['vocab']) && $relative_to['vocab']) { if(isset($relative_to['vocab']) && $relative_to['vocab']) {
if(property_exists($active_ctx->mappings, $value)) { if(property_exists($active_ctx->mappings, $value)) {
$mapping = $active_ctx->mappings->{$value}; $mapping = $active_ctx->mappings->{$value};
@ -4626,55 +4608,44 @@ class JsonLdProcessor {
} }
// value is a term // value is a term
$rval = $mapping->{'@id'}; return $mapping->{'@id'};
} }
} }
if($rval === null) { // split value into prefix:suffix
// split value into prefix:suffix $colon = strpos($value, ':');
$colon = strpos($value, ':'); if($colon !== false) {
if($colon !== false) { $prefix = substr($value, 0, $colon);
$prefix = substr($value, 0, $colon); $suffix = substr($value, $colon + 1);
$suffix = substr($value, $colon + 1);
// do not expand blank nodes (prefix of '_') or already-absolute // do not expand blank nodes (prefix of '_') or already-absolute
// IRIs (suffix of '//') // IRIs (suffix of '//')
if($prefix !== '_' && strpos($suffix, '//') !== 0) { if($prefix !== '_' && strpos($suffix, '//') !== 0) {
// prefix dependency not defined, define it // prefix dependency not defined, define it
if($local_ctx !== null && property_exists($local_ctx, $prefix)) { if($local_ctx !== null && property_exists($local_ctx, $prefix)) {
$this->_createTermDefinition( $this->_createTermDefinition(
$active_ctx, $local_ctx, $prefix, $defined); $active_ctx, $local_ctx, $prefix, $defined);
} }
// use mapping if prefix is defined // use mapping if prefix is defined
if(property_exists($active_ctx->mappings, $prefix)) { if(property_exists($active_ctx->mappings, $prefix)) {
$mapping = $active_ctx->mappings->{$prefix}; $mapping = $active_ctx->mappings->{$prefix};
if($mapping) { if($mapping) {
$rval = $mapping->{'@id'} . $suffix; return $mapping->{'@id'} . $suffix;
}
} }
} }
} }
} }
if($rval === null) { // already absolute IRI
$rval = $value; if(self::_isAbsoluteIri($value)) {
return $value;
} }
// keywords need no expanding (aliasing already handled by now) $rval = $value;
if(self::_isKeyword($rval)) {
return $rval;
}
if(self::_isAbsoluteIri($rval)) {
// rename blank node if requested
if(!$local_ctx && strpos($rval, '_:') === 0 &&
$active_ctx->namer !== null) {
$rval = $active_ctx->namer->getName($rval);
}
}
// prepend vocab // prepend vocab
else if(isset($relative_to['vocab']) && $relative_to['vocab'] && if(isset($relative_to['vocab']) && $relative_to['vocab'] &&
property_exists($active_ctx, '@vocab')) { property_exists($active_ctx, '@vocab')) {
$rval = $active_ctx->{'@vocab'} . $rval; $rval = $active_ctx->{'@vocab'} . $rval;
} }
@ -4869,14 +4840,9 @@ class JsonLdProcessor {
* @return stdClass the initial context. * @return stdClass the initial context.
*/ */
protected function _getInitialContext($options) { protected function _getInitialContext($options) {
$namer = null;
if(isset($options['renameBlankNodes']) && $options['renameBlankNodes']) {
$namer = new UniqueNamer('_:b');
}
return (object)array( return (object)array(
'@base' => jsonld_parse_url($options['base']), '@base' => jsonld_parse_url($options['base']),
'mappings' => new stdClass(), 'mappings' => new stdClass(),
'namer' => $namer,
'inverse' => null); 'inverse' => null);
} }
@ -4996,7 +4962,6 @@ class JsonLdProcessor {
$child = new stdClass(); $child = new stdClass();
$child->{'@base'} = $active_ctx->{'@base'}; $child->{'@base'} = $active_ctx->{'@base'};
$child->mappings = self::copy($active_ctx->mappings); $child->mappings = self::copy($active_ctx->mappings);
$child->namer = $active_ctx->namer;
$child->inverse = null; $child->inverse = null;
if(property_exists($active_ctx, '@language')) { if(property_exists($active_ctx, '@language')) {
$child->{'@language'} = $active_ctx->{'@language'}; $child->{'@language'} = $active_ctx->{'@language'};
@ -5020,9 +4985,6 @@ class JsonLdProcessor {
$rval = new stdClass(); $rval = new stdClass();
$rval->{'@base'} = $active_ctx->{'@base'}; $rval->{'@base'} = $active_ctx->{'@base'};
$rval->mappings = $active_ctx->mappings; $rval->mappings = $active_ctx->mappings;
if($active_ctx->namer !== null) {
$rval->namer = new UniqueNamer('_:b');
}
$rval->inverse = $active_ctx->inverse; $rval->inverse = $active_ctx->inverse;
if(property_exists($active_ctx, '@language')) { if(property_exists($active_ctx, '@language')) {
$rval->{'@language'} = $active_ctx->{'@language'}; $rval->{'@language'} = $active_ctx->{'@language'};