Drop duplicates when creating node maps.
This commit is contained in:
parent
505202c213
commit
e5f1e28ada
|
@ -62,6 +62,62 @@ function deep_compare($expect, $result) {
|
||||||
return $expect === $result;
|
return $expect === $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function expanded_compare($x, $y, $is_list=false) {
|
||||||
|
if($x === $y) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if(gettype($x) !== gettype($y)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(is_array($x)) {
|
||||||
|
if(!is_array($y) || count($x) !== count($y)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$rval = true;
|
||||||
|
if($is_list) {
|
||||||
|
// compare in order
|
||||||
|
for($i = 0; $rval && $i < count($x); ++$i) {
|
||||||
|
$rval = expanded_compare($x[$i], $y[$i], false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// compare in any order
|
||||||
|
$iso = array();
|
||||||
|
for($i = 0; $rval && $i < count($x); ++$i) {
|
||||||
|
$rval = false;
|
||||||
|
for($j = 0; !$rval && $j < count($y); ++$j) {
|
||||||
|
if(!isset($iso[$j])) {
|
||||||
|
if(expanded_compare($x[$i], $y[$j], false)) {
|
||||||
|
$iso[$j] = $i;
|
||||||
|
$rval = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$rval = $rval && (count($iso) === count($x));
|
||||||
|
}
|
||||||
|
return $rval;
|
||||||
|
}
|
||||||
|
if(is_object($x)) {
|
||||||
|
$x_keys = array_keys((array)$x);
|
||||||
|
$y_keys = array_keys((array)$y);
|
||||||
|
if(count($x_keys) !== count($y_keys)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
foreach($x_keys as $key) {
|
||||||
|
if(!property_exists($y, $key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!expanded_compare($x->{$key}, $y->{$key}, $key === '@list')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads test JSON files.
|
* Reads test JSON files.
|
||||||
*
|
*
|
||||||
|
@ -173,14 +229,35 @@ class TestRunner {
|
||||||
echo $line;
|
echo $line;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function check($test, $expect, $result, $compare_json) {
|
public function check($test, $expect, $result, $type) {
|
||||||
global $eol;
|
global $eol;
|
||||||
|
|
||||||
$json_equal = true;
|
$compare_json = !in_array('jld:ToRDFTest', $type);
|
||||||
if($compare_json) {
|
$relabel = in_array('jld:FlattenTest', $type);
|
||||||
$json_equal = (json_encode($expect) === json_encode($result));
|
$expanded = $relabel || in_array('jld:ExpandTest', $type);
|
||||||
|
if($relabel) {
|
||||||
|
$expect = jsonld_relabel_blank_nodes($expect);
|
||||||
|
$result = jsonld_relabel_blank_nodes($result);
|
||||||
}
|
}
|
||||||
if($json_equal || deep_compare($expect, $result)) {
|
|
||||||
|
$pass = false;
|
||||||
|
if($compare_json) {
|
||||||
|
$pass = (json_encode($expect) === json_encode($result));
|
||||||
|
}
|
||||||
|
if(!$pass) {
|
||||||
|
$pass = deep_compare($expect, $result) ||
|
||||||
|
($expanded && expanded_compare($expect, $result, $relabel));
|
||||||
|
}
|
||||||
|
if(!$pass && $relabel) {
|
||||||
|
echo "WARN tried normalization...";
|
||||||
|
$expect_normalized = jsonld_normalize(
|
||||||
|
$expect, array('format' => 'application/nquads'));
|
||||||
|
$result_normalized = jsonld_normalize(
|
||||||
|
$result, array('format' => 'application/nquads'));
|
||||||
|
$pass = ($expect_normalized === $result_normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($pass) {
|
||||||
$this->passed += 1;
|
$this->passed += 1;
|
||||||
echo "PASS$eol";
|
echo "PASS$eol";
|
||||||
}
|
}
|
||||||
|
@ -261,7 +338,6 @@ class TestRunner {
|
||||||
foreach($manifest->sequence as $test) {
|
foreach($manifest->sequence as $test) {
|
||||||
// read test input files
|
// read test input files
|
||||||
$type = $test->{'@type'};
|
$type = $test->{'@type'};
|
||||||
$compare_json = true;
|
|
||||||
$options = array(
|
$options = array(
|
||||||
'base' => 'http://json-ld.org/test-suite/tests/' . $test->input);
|
'base' => 'http://json-ld.org/test-suite/tests/' . $test->input);
|
||||||
|
|
||||||
|
@ -316,7 +392,6 @@ class TestRunner {
|
||||||
$test->expect = read_test_nquads($test->expect, $filepath);
|
$test->expect = read_test_nquads($test->expect, $filepath);
|
||||||
$options['format'] = 'application/nquads';
|
$options['format'] = 'application/nquads';
|
||||||
$result = jsonld_to_rdf($input, $options);
|
$result = jsonld_to_rdf($input, $options);
|
||||||
$compare_json = false;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
echo "Skipping test \"{$test->name}\" of type: " .
|
echo "Skipping test \"{$test->name}\" of type: " .
|
||||||
|
@ -325,7 +400,7 @@ class TestRunner {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check results
|
// check results
|
||||||
$this->check($test, $test->expect, $result, $compare_json);
|
$this->check($test, $test->expect, $result, $type);
|
||||||
}
|
}
|
||||||
catch(JsonLdException $e) {
|
catch(JsonLdException $e) {
|
||||||
echo $eol . $e;
|
echo $eol . $e;
|
||||||
|
|
27
jsonld.php
27
jsonld.php
|
@ -162,6 +162,16 @@ function jsonld_to_rdf($input, $options=array()) {
|
||||||
return $p->toRDF($input, $options);
|
return $p->toRDF($input, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Relabels all blank nodes in the given JSON-LD input.
|
||||||
|
*
|
||||||
|
* @param mixed input the JSON-LD input.
|
||||||
|
*/
|
||||||
|
function jsonld_relabel_blank_nodes($input) {
|
||||||
|
$p = new JsonLdProcessor();
|
||||||
|
return $p->_labelBlankNodes(new UniqueNamer('_:b'), $input);
|
||||||
|
}
|
||||||
|
|
||||||
/** JSON-LD shared in-memory cache. */
|
/** JSON-LD shared in-memory cache. */
|
||||||
global $jsonld_cache;
|
global $jsonld_cache;
|
||||||
$jsonld_cache = new stdClass();
|
$jsonld_cache = new stdClass();
|
||||||
|
@ -1905,7 +1915,8 @@ class JsonLdProcessor {
|
||||||
|
|
||||||
// add copy of value for each property from property generator
|
// add copy of value for each property from property generator
|
||||||
if(is_array($expanded_property)) {
|
if(is_array($expanded_property)) {
|
||||||
$this->_labelBlankNodes($active_ctx->namer, $expanded_value);
|
$expanded_value = $this->_labelBlankNodes(
|
||||||
|
$active_ctx->namer, $expanded_value);
|
||||||
foreach($expanded_property as $iri) {
|
foreach($expanded_property as $iri) {
|
||||||
self::addValue(
|
self::addValue(
|
||||||
$rval, $iri, self::copy($expanded_value),
|
$rval, $iri, self::copy($expanded_value),
|
||||||
|
@ -2044,10 +2055,12 @@ class JsonLdProcessor {
|
||||||
|
|
||||||
// add all non-default graphs to default graph
|
// add all non-default graphs to default graph
|
||||||
$default_graph = $graphs->{'@default'};
|
$default_graph = $graphs->{'@default'};
|
||||||
foreach($graphs as $graph_name => $node_map) {
|
$graph_names = array_keys((array)$graphs);
|
||||||
|
foreach($graph_names as $graph_name) {
|
||||||
if($graph_name === '@default') {
|
if($graph_name === '@default') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
$node_map = $graphs->{$graph_name};
|
||||||
if(!property_exists($default_graph, $graph_name)) {
|
if(!property_exists($default_graph, $graph_name)) {
|
||||||
$default_graph->{$graph_name} = (object)array(
|
$default_graph->{$graph_name} = (object)array(
|
||||||
'@id' => $graph_name, '@graph' => array());
|
'@id' => $graph_name, '@graph' => array());
|
||||||
|
@ -2706,7 +2719,7 @@ class JsonLdProcessor {
|
||||||
*
|
*
|
||||||
* @return mixed the element.
|
* @return mixed the element.
|
||||||
*/
|
*/
|
||||||
protected function _labelBlankNodes($namer, $element) {
|
public function _labelBlankNodes($namer, $element) {
|
||||||
if(is_array($element)) {
|
if(is_array($element)) {
|
||||||
$length = count($element);
|
$length = count($element);
|
||||||
for($i = 0; $i < $length; ++$i) {
|
for($i = 0; $i < $length; ++$i) {
|
||||||
|
@ -3018,7 +3031,7 @@ class JsonLdProcessor {
|
||||||
// add reference and recurse
|
// add reference and recurse
|
||||||
self::addValue(
|
self::addValue(
|
||||||
$subject, $property, (object)array('@id' => $id),
|
$subject, $property, (object)array('@id' => $id),
|
||||||
array('propertyIsArray' => true));
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
||||||
$this->_createNodeMap($o, $graphs, $graph, $namer, $id, null);
|
$this->_createNodeMap($o, $graphs, $graph, $namer, $id, null);
|
||||||
}
|
}
|
||||||
// handle $list
|
// handle $list
|
||||||
|
@ -3028,13 +3041,15 @@ class JsonLdProcessor {
|
||||||
$o->{'@list'}, $graphs, $graph, $namer, $name, $_list);
|
$o->{'@list'}, $graphs, $graph, $namer, $name, $_list);
|
||||||
$o = (object)array('@list' => (array)$_list);
|
$o = (object)array('@list' => (array)$_list);
|
||||||
self::addValue(
|
self::addValue(
|
||||||
$subject, $property, $o, array('propertyIsArray' => true));
|
$subject, $property, $o,
|
||||||
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
||||||
}
|
}
|
||||||
// handle @value
|
// handle @value
|
||||||
else {
|
else {
|
||||||
$this->_createNodeMap($o, $graphs, $graph, $namer, $name, null);
|
$this->_createNodeMap($o, $graphs, $graph, $namer, $name, null);
|
||||||
self::addValue(
|
self::addValue(
|
||||||
$subject, $property, $o, array('propertyIsArray' => true));
|
$subject, $property, $o,
|
||||||
|
array('propertyIsArray' => true, 'allowDuplicate' => false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue