function get_differences($query_results)
 {
     $res = $query_results;
     $g_before = new SimpleGraph();
     $g_after = new SimpleGraph();
     $row_count = $res->num_rows();
     $node_index = 0;
     $diffs = array();
     if ($row_count === 0) {
         if ($this->_subject) {
             $s = $this->_subject;
         } else {
             $s = "_:s";
         }
         $g_after = $this->get_data_as_graph($s);
         $index = $g_after->get_index();
         if (array_key_exists($s, $index)) {
             $additions = $index[$s];
         } else {
             $additions = array();
         }
         $diffs[$s] = array('additions' => $additions, 'removals' => array());
     } else {
         $subjects = array();
         for ($i = 0; $i < $row_count; $i++) {
             $row = $res->row_array($i);
             $rowdata = $res->rowdata($i);
             if (array_key_exists('_uri', $row)) {
                 $s = $row['_uri'];
             } else {
                 if ($this->_subject) {
                     $s = $this->_subject;
                 } else {
                     $s = "_:s";
                 }
             }
             $subjects[] = $s;
             foreach ($this->_data as $field => $field_info) {
                 if (array_key_exists($field, $row)) {
                     $p = $this->_rmap[$field];
                     if ($rowdata[$field]['type'] === 'literal') {
                         $g_before->add_literal_triple($s, $p, $row[$field], $rowdata[$field]['lang'], $rowdata[$field]['datatype']);
                     } else {
                         if ($rowdata[$field]['type'] === 'uri') {
                             $g_before->add_resource_triple($s, $p, $row[$field]);
                         } else {
                             if ($rowdata[$field]['type'] === 'bnode') {
                                 $g_before->add_resource_triple($s, $p, '_:' . $row[$field]);
                             }
                         }
                     }
                 }
             }
         }
         if (count($subjects) > 0) {
             $subjects = array_unique($subjects);
             foreach ($subjects as $s) {
                 $g_after->add_graph($this->get_data_as_graph($s));
             }
         }
         if ($g_after->is_empty()) {
             $additions = array();
             if ($g_before->is_empty()) {
                 $removals = array();
             } else {
                 $removals = $g_before->get_index();
             }
         } else {
             if ($g_before->is_empty()) {
                 $additions = $g_after->get_index();
                 $removals = array();
             } else {
                 $removals = SimpleGraph::diff($g_before->get_index(), $g_after->get_index());
                 $additions = SimpleGraph::diff($g_after->get_index(), $g_before->get_index());
             }
         }
         foreach ($subjects as $s) {
             $diff = array('additions' => array(), 'removals' => array());
             if (array_key_exists($s, $additions)) {
                 $diff['additions'] = $additions[$s];
             }
             if (array_key_exists($s, $removals)) {
                 $diff['removals'] = $removals[$s];
             }
             $diffs[$s] = $diff;
         }
     }
     return $diffs;
 }
 function test_diff_to_ensure_type_insensitive_comparison()
 {
     $_1 = array('#x' => array('#lcn' => array(array('value' => '1521278'))));
     $_2 = array('#x' => array('#lcn' => array(array('value' => '00001521278'))));
     $actual = SimpleGraph::diff($_1, $_2);
     $this->assertEquals($_1, $actual);
 }
 function __init()
 {
     $csIndex = array();
     $CSNS = 'http://purl.org/vocab/changeset/schema#';
     // Get the triples to be added
     if (empty($this->before)) {
         $additions = $this->after;
     } else {
         $additions = SimpleGraph::diff($this->after, $this->before);
     }
     //Get the triples to be removed
     if (empty($this->after)) {
         $removals = $this->before;
     } else {
         $removals = SimpleGraph::diff($this->before, $this->after);
     }
     // $removals = !empty($this->after)? SimpleGraph::diff($this->before, $this->after) : $this->before;
     //remove etag triples
     foreach (array('removals' => $removals, 'additions' => $additions) as $name => $graph) {
         foreach ($graph as $uri => $properties) {
             if (isset($properties["http://schemas.talis.com/2005/dir/schema#etag"])) {
                 unset(${$name}[$uri]["http://schemas.talis.com/2005/dir/schema#etag"]);
                 if (count(${$name}[$uri]) == 0) {
                     unset(${$name}[$uri]);
                 }
             }
         }
     }
     //    print_r(array_keys($additions));
     //    print_r(array_keys($removals));
     //    print_r(array_merge(array_keys($additions), array_keys($removals)));
     // Get an array of all the subject uris
     $subjectIndex = !empty($this->a['subjectOfChange']) ? array($this->a['subjectOfChange']) : array_unique(array_merge(array_keys($additions), array_keys($removals)));
     //    print_r($subjectIndex);
     // Get the metadata for all the changesets
     $date = !empty($this->a['createdDate']) ? $this->a['createdDate'] : date(DATE_ATOM);
     $creator = !empty($this->a['creatorName']) ? $this->a['creatorName'] : 'Moriarty ChangeSet Builder';
     $reason = !empty($this->a['changeReason']) ? $this->a['changeReason'] : 'Change using Moriarty ChangeSet Builder';
     $csCount = 0;
     foreach ($subjectIndex as $subjectOfChange) {
         $csID = '_:cs' . $csCount;
         $csIndex[$subjectOfChange] = $csID;
         $this->addT($csID, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', $CSNS . 'ChangeSet', 'uri');
         $subjectType = strpos($subjectOfChange, '_:') === 0 ? 'bnode' : 'uri';
         $this->addT($csID, $CSNS . 'subjectOfChange', $subjectOfChange, $subjectType);
         $this->addT($csID, $CSNS . 'createdDate', $date, 'literal');
         $this->addT($csID, $CSNS . 'creatorName', $creator, 'literal');
         $this->addT($csID, $CSNS . 'changeReason', $reason, 'literal');
         /* add extra user-given properties to each changeset*/
         if (!empty($this->a['properties'])) {
             foreach ($this->a['properties'] as $p => $objs) {
                 $this->addT($csID, $p, $objs);
             }
         }
         $csCount++;
     }
     /*iterate through the triples to be added, 
       reifying them, 
       and linking to the Statements from the appropriate changeset
       */
     $reifiedAdditions = SimpleGraph::reify($additions, 'Add');
     if (!empty($reifiedAdditions)) {
         foreach ($reifiedAdditions as $nodeID => $props) {
             $subject = $props['http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'][0]['value'];
             if (in_array($subject, $subjectIndex)) {
                 $csID = $csIndex[$subject];
                 $this->addT($csID, $CSNS . 'addition', $nodeID, 'bnode');
             }
             // if dc:source is given in the instantiating arguments, add it to the statement as provenance
             if (isset($this->a['http://purl.org/dc/terms/source'])) {
                 $this->addT($nodeID, 'http://purl.org/dc/terms/source', $this->a['http://purl.org/dc/terms/source'], 'uri');
             }
         }
     }
     /*iterate through the triples to be removed, 
       reifying them, 
       and linking to the Statements from the appropriate changeset
       */
     $reifiedRemovals = SimpleGraph::reify($removals, 'Remove');
     foreach ($reifiedRemovals as $nodeID => $props) {
         $subject = $props['http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'][0]['value'];
         if (in_array($subject, $subjectIndex)) {
             $csID = $csIndex[$subject];
             $this->addT($csID, $CSNS . 'removal', $nodeID, 'bnode');
         }
     }
     // foreach($this->_index as $uri => $props){
     //  if(
     //      !isset($props[$CSNS.'removal'])
     //      AND
     //      !isset($props[$CSNS.'addition'])
     //      ){
     //        unset($this->_index[$uri]);
     //    }
     //
     // }
     $this->_index = SimpleGraph::merge($this->_index, $reifiedAdditions, $reifiedRemovals);
 }