source: Dev/branches/rest-dojo-ui/server/rdfapi/util/ModelComparator.php @ 303

Last change on this file since 303 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.9 KB
Line 
1<?php
2
3// ----------------------------------------------------------------------------------
4// Class: ModelComparator
5// ----------------------------------------------------------------------------------
6
7
8/**
9 * This class compares to models. This comparator bases on the labelling algorithm
10 * described in <href="http://www.hpl.hp.com/techreports/2003/HPL-2003-142.pdf" >Signing RDF Graphs</href>
11 * by Jeremy J. Carroll.
12 *
13 *
14 * @version  $Id$
15 * @author Tobias Gauß <tobias.gauss at web.de>
16 *
17 * @package utility
18 * @access      public
19 *
20 **/
21 class ModelComparator extends Object {
22       
23        /**
24        * Compares two models.
25        *
26        * @param $thisModel First Model.
27        * @param $thatModel Second Model.
28        *
29        * @return boolean
30        */
31        function compare($thisModel, $thatModel){
32       
33        $thisModeltriples = null;
34        $thatModeltriples = null;
35       
36                if(is_a($thisModel,"DbModel")){
37                        $thisModeltriples =     $thisModel->getMemModel();
38                }else{
39                        $thisModeltriples =     $thisModel;
40                }
41               
42                if(is_a($thatModel,"DbModel")){
43                        $thatModeltriples =     $thatModel->getMemModel();
44                }else{
45                        $thatModeltriples =     $thatModel;
46                }
47                       
48                $sortArray1 = ModelComparator::buildSortArray($thisModeltriples->triples);
49                $sortArray2 = ModelComparator::buildSortArray($thatModeltriples->triples);     
50               
51                $renamedArray1 = ModelComparator::renameBlanks($sortArray1);
52                $renamedArray2 = ModelComparator::renameBlanks($sortArray2);
53               
54                return ModelComparator::compareTriples($renamedArray1,$renamedArray2);
55        }
56               
57       
58        /**
59        * Builds a sorted array.
60        *
61        * @param $tripleList A List that contains the models triples.
62        *
63        * @return Array
64        */
65        function buildSortArray($tripleList){
66                $sortedArray = Array();
67               
68                foreach($tripleList as $index => $triple){
69                        $sub = null;
70                        $obj = null;
71                        $orgSub  = $triple->getSubject();
72                        $orgObj  = $triple->getObject();
73                       
74                        if(is_a($orgSub,"Blanknode")){
75                                $sub = $orgSub->getID();
76                                $triple->subj = new Blanknode("~");
77                        }
78                        if(is_a($orgObj,"Blanknode")){
79                                $obj = $orgObj->getID();
80                                $triple->obj = new Blanknode("~");
81                        }
82                        $sortedArray[$index]['string']  = $triple->toString();
83                        $sortedArray[$index]['index']  = $index;
84                        $sortedArray[$index]['triple'] = $triple;
85                        $sortedArray[$index]['sub']    = $sub;
86                        $sortedArray[$index]['obj']    = $obj; 
87                }
88                sort($sortedArray);
89                return $sortedArray;
90        }
91       
92        /**
93        * Renames the models Blanknodes.
94        *
95        * @param $sortedArray A List that contains the models triples.
96        *
97        * @return Array
98        */
99        function renameBlanks($sortedArray){
100        $i = 0;
101        $labelmap = Array();
102               
103                foreach ($sortedArray as $value){       
104                        //new label
105                        if($value['sub']!=null){
106                                $label = null;
107                                if(isset($labelmap[$value['sub']])){
108                                        $label = $labelmap[$value['sub']];
109                                        $value['triple']->subj = new BlankNode($labelmap[$value['sub']]);
110                                }else{
111                                        $label = $i."Bnode";
112                                        $labelmap[$value['sub']]=$label;
113                                        $value['triple']->subj = new BlankNode($labelmap[$value['sub']]);
114                                        $i++;
115                                }
116                        }
117                       
118                        if($value['obj']!=null){
119                                $label = null;
120                                if(isset($labelmap[$value['obj']])){
121                                        $label = $labelmap[$value['obj']];
122                                        $value['triple']->obj = new BlankNode($labelmap[$value['obj']]);
123                                }else{
124                                        $label = $i."Bnode";
125                                        $labelmap[$value['obj']]=$label;
126                                        $value['triple']->obj = new BlankNode($labelmap[$value['obj']]);
127                                        $i++;
128                                }
129                        }
130                       
131                }
132                return $sortedArray;
133       
134        }
135       
136        /**
137        * Compares the Triples in the lists.
138        *
139        * @param $tripleList A List that contains the models triples.
140        * @param $tripleList A List that contains the models triples.
141        *
142        * @return boolean
143        */
144        function compareTriples($array1, $array2){
145                foreach($array1 as $key => $value){
146                        if(!$value['triple']->equals($array2[$key]['triple']))
147                                return false;
148                }
149                return true;
150       
151        }
152       
153 
154 }
155 
156 
157 
158 ?>
Note: See TracBrowser for help on using the repository browser.