Modified:
/trunk/simplegraph.class.php
/trunk/tests/simplegraph.test.php
=======================================
--- /trunk/simplegraph.class.php Thu Mar 8 00:52:57 2012
+++ /trunk/simplegraph.class.php Wed Mar 21 12:34:44 2012
@@ -804,7 +804,7 @@
* @param string p the predicate to search for
* @return array list of URIs and blank nodes that are the objects of
triples with the supplied subject and predicate
*/
- function get_resource_triple_values($s, $p) {
+ function get_resource_triple_values($s, $p, $sort_property=false,
$sort_order_descending=false) {
$values = array();
if (array_key_exists($s, $this->_index) ) {
if (array_key_exists($p, $this->_index[$s]) ) {
@@ -815,6 +815,9 @@
}
}
}
+ if($sort_property){
+ $values = $this->order_uris_by_property($values, $sort_property,
$sort_order_descending);
+ }
return $values;
}
@@ -1436,5 +1439,35 @@
}
return array_unique($bnodes);
}
+
+ public function order_uris_by_property($uri_list, $property,
$descending=false){
+ $value_uri_map = array();
+ foreach($uri_list as $uri){
+ $vals = $this->get_subject_property_values($uri, $property);
+ foreach($vals as $object){
+ $value_uri_map[$object['value']][] = $uri;
+ }
+ if(empty($vals)){
+ $value_uri_map[null][]=$uri;
+ }
+ }
+ if(!$descending) ksort($value_uri_map);
+ else krsort($value_uri_map);
+ $sorted_uris = array();
+ $uri_arrays = array_values($value_uri_map);
+ while(
+ count($uri_list)>count($sorted_uris)
+ AND
+ $uris = array_shift($uri_arrays)
+ ){
+ foreach($uris as $uri){
+ if(!in_array($uri,$sorted_uris)){
+ $sorted_uris[]=$uri;
+ }
+ }
+ }
+
+ return $sorted_uris;
+ }
}
?>
=======================================
--- /trunk/tests/simplegraph.test.php Thu Jan 12 01:59:37 2012
+++ /trunk/tests/simplegraph.test.php Wed Mar 21 12:34:44 2012
@@ -1251,6 +1251,50 @@
$this->assertContains('<a href="#bn123">_:bn123</a>', $html, "html
should contain links to bnode anchors");
$this->assertContains('<a id="bn123" href="#bn123">_:bn123</a>',
$html, "html should contain anchors for bnodes");
}
+
+ function test_order_uris_by_property(){
+ $ex = "http://example.com/";
+ $turtle = <<<_TTL_
+@base <{$ex}> .
+
+<a> <p> 2 .
+<b> <p> 3 .
+<c> <p> 1 .
+
+_TTL_;
+ $graph = new SimpleGraph();
+ $graph->from_turtle($turtle);
+ $uris = array($ex.'a',$ex.'b',$ex.'c');
+ $expected = array($ex.'c',$ex.'a',$ex.'b');
+ $actual = $graph->order_uris_by_property($uris, $ex.'p');
+ $this->assertEquals($expected, $actual, "order_uris_by_property
should order the uris in ascending order of the value of the property");
+ $expected_reverse = array($ex.'b',$ex.'a',$ex.'c');
+ $actual = $graph->order_uris_by_property($uris, $ex.'p', 1);
+ $this->assertEquals($expected_reverse,
$actual, "order_uris_by_property should order the uris in ascending order
of the value of the property");
+ }
+
+ function test_get_resource_property_values(){
+ $ex = "http://example.com/";
+ $turtle = <<<_TTL_
+@base <{$ex}> .
+
+<s> <has> <a> , <b> , <c> , <d> , <e> .
+<a> <p> 2 .
+<b> <p> 3 .
+<c> <p> 1 .
+<e> <p> "!" , "_" .
+
+_TTL_;
+ $graph = new SimpleGraph();
+ $graph->from_turtle($turtle);
+ $expected = array($ex.'d', $ex.'e', $ex.'c',$ex.'a',$ex.'b');
+ $actual = $graph->get_resource_triple_values($ex.'s', $ex.'has',
$ex.'p');
+ $actual_r = $graph->get_resource_triple_values($ex.'s', $ex.'has',
$ex.'p', true);
+ $actual_unsorted = $graph->get_resource_triple_values($ex.'s',
$ex.'has');
+ $this->assertEquals($expected, $actual, "should return the resource
values of one property, ordered by a property of those resources");
+ $this->assertEquals(array_reverse($expected), $actual_r, "should
return the resource values of one property, ordered by a property of those
resources, sorted in descending order");
+ $this->assertEquals(count($actual),count($actual_unsorted), "number
of results should be the same whether sorted or not");
+ }
}
?>