Cleaned up compact handling of disjoint graphs.

- Simplification of add/remove/etc context methods.
- Removed default context.
This commit is contained in:
Dave Longley 2011-08-04 16:33:26 -04:00
parent a2924c8d2d
commit 334cac4986

View file

@ -17,39 +17,6 @@ define('JSONLD_XSD_DOUBLE', JSONLD_XSD . 'double');
define('JSONLD_XSD_INTEGER', JSONLD_XSD . 'integer'); define('JSONLD_XSD_INTEGER', JSONLD_XSD . 'integer');
define('JSONLD_XSD_ANY_URI', JSONLD_XSD . 'anyURI'); define('JSONLD_XSD_ANY_URI', JSONLD_XSD . 'anyURI');
/**
* Creates the JSON-LD default context.
*
* @return the JSON-LD default context.
*/
function jsonld_create_default_context()
{
return (object)array(
'rdf' => JSONLD_RDF,
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
'owl' => 'http://www.w3.org/2002/07/owl#',
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
'dcterms' => 'http://purl.org/dc/terms/',
'foaf' => 'http://xmlns.com/foaf/0.1/',
'cal' => 'http://www.w3.org/2002/12/cal/ical#',
'vcard' => 'http://www.w3.org/2006/vcard/ns#',
'geo' => 'http://www.w3.org/2003/01/geo/wgs84_pos#',
'cc' => 'http://creativecommons.org/ns#',
'sioc' => 'http://rdfs.org/sioc/ns#',
'doap' => 'http://usefulinc.com/ns/doap#',
'com' => 'http://purl.org/commerce#',
'ps' => 'http://purl.org/payswarm#',
'gr' => 'http://purl.org/goodrelations/v1#',
'sig' => 'http://purl.org/signature#',
'ccard' => 'http://purl.org/commerce/creditcard#',
'@coerce' => (object)array(
'xsd:anyURI' => array('foaf:homepage', 'foaf:member'),
'xsd:integer' => 'foaf:age'
),
'@vocab' => ''
);
}
/** /**
* Normalizes a JSON-LD object. * Normalizes a JSON-LD object.
* *
@ -64,89 +31,81 @@ function jsonld_normalize($input)
}; };
/** /**
* Removes the context from a JSON-LD object. * Removes the context from a JSON-LD object, expanding it to full-form.
* *
* @param input the JSON-LD object to remove the context from. * @param input the JSON-LD object to remove the context from.
* *
* @return the context-neutral JSON-LD object. * @return the context-neutral JSON-LD object.
*/ */
function jsonld_remove_context($input) function jsonld_expand($input)
{ {
$rval = null; $rval = null;
if($input !== null) if($input !== null)
{ {
$ctx = jsonld_create_default_context(); $rval = _expand(new stdClass(), null, $input, false);
$rval = _expand($ctx, null, $input, false);
} }
return $rval; return $rval;
}; };
function jsonld_expand($input)
{
return jsonld_remove_context($input);
}
/** /**
* Adds the given context to the given context-neutral JSON-LD object. * Expands the given JSON-LD object and then compacts it using the
* * given context.
* @param ctx the new context to use.
* @param input the context-neutral JSON-LD object to add the context to.
*
* @return the JSON-LD object with the new context.
*/
function jsonld_add_context($ctx, $input)
{
$rval;
// TODO: should context simplification be optional? (ie: remove context
// entries that are not used in the output)
$ctx = jsonld_merge_contexts(jsonld_create_default_context(), $ctx);
// setup output context
$ctxOut = new stdClass();
// compact
$rval = _compact($ctx, null, $input, $ctxOut);
// add context if used
if(count(array_keys((array)$ctxOut)) > 0)
{
// add copy of context to every entry in output array
if(is_array($rval))
{
foreach($rval as $v)
{
$v->{'@context'} = _clone($ctxOut);
}
}
else
{
$rval->{'@context'} = $ctxOut;
}
}
return $rval;
}
/**
* Changes the context of JSON-LD object "input" to "context", returning the
* output.
* *
* @param ctx the new context to use. * @param ctx the new context to use.
* @param input the input JSON-LD object. * @param input the input JSON-LD object.
* *
* @return the output JSON-LD object. * @return the output JSON-LD object.
*/ */
function jsonld_change_context($ctx, $input)
{
// remove context and then add new one
return jsonld_add_context($ctx, jsonld_remove_context($input));
}
function jsonld_compact($ctx, $input) function jsonld_compact($ctx, $input)
{ {
return jsonld_change_context($ctx, $input); $rval = null;
// TODO: should context simplification be optional? (ie: remove context
// entries that are not used in the output)
if($input !== null)
{
// fully expand input
$input = jsonld_expand($input);
if(is_array($input))
{
$rval = array();
$tmp = $input;
}
else
{
$tmp = array($input);
}
foreach($tmp as $value)
{
// setup output context
$ctxOut = new stdClass();
// compact
$out = _compact(_clone($ctx), null, $value, $ctxOut);
// add context if used
if(count(array_keys((array)$ctxOut)) > 0)
{
$out->{'@context'} = $ctxOut;
}
if($rval === null)
{
$rval = $out;
}
else
{
$rval[] = $out;
}
}
}
return $rval;
} }
/** /**
@ -333,12 +292,11 @@ function jsonld_frame($input, $frame, $options=null)
$ctx = null; $ctx = null;
if(property_exists($frame, '@context')) if(property_exists($frame, '@context'))
{ {
$ctx = jsonld_merge_contexts( $ctx = _clone($frame->{'@context'});
jsonld_create_default_context(), $frame->{'@context'});
} }
// remove context from frame // remove context from frame
$frame = jsonld_remove_context($frame); $frame = jsonld_expand($frame);
// create framing options // create framing options
// TODO: merge in options from function parameter // TODO: merge in options from function parameter
@ -361,7 +319,7 @@ function jsonld_frame($input, $frame, $options=null)
// apply context // apply context
if($ctx !== null and $rval !== null) if($ctx !== null and $rval !== null)
{ {
$rval = jsonld_add_context($ctx, $rval); $rval = jsonld_compact($ctx, $rval);
} }
return $rval; return $rval;
@ -503,10 +461,14 @@ function _expandTerm($ctx, $term, $usedCtx)
// 5. The property is a relative IRI, prepend the default vocab. // 5. The property is a relative IRI, prepend the default vocab.
else else
{ {
$rval = $ctx->{'@vocab'} . $term; $rval = $term;
if($usedCtx !== null) if(property_exists($ctx, '@vocab'))
{ {
$usedCtx->{'@vocab'} = $ctx->{'@vocab'}; $rval = $ctx->{'@vocab'} . $rval;
if($usedCtx !== null)
{
$usedCtx->{'@vocab'} = $ctx->{'@vocab'};
}
} }
} }
@ -600,7 +562,7 @@ function _getCoerceType($ctx, $property, $usedCtx)
$rval = JSONLD_XSD_ANY_URI; $rval = JSONLD_XSD_ANY_URI;
} }
// check type coercion for property // check type coercion for property
else else if(property_exists($ctx, '@coerce'))
{ {
// force compacted property // force compacted property
$p = _compactIri($ctx, $p, null); $p = _compactIri($ctx, $p, null);
@ -1593,11 +1555,8 @@ class JsonLdProcessor
if($input !== null) if($input !== null)
{ {
// get default context
$ctx = jsonld_create_default_context();
// expand input // expand input
$expanded = _expand($ctx, null, $input, true); $expanded = _expand(new stdClass(), null, $input, true);
// assign names to unnamed bnodes // assign names to unnamed bnodes
$this->nameBlankNodes($expanded); $this->nameBlankNodes($expanded);