Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php |
— | — | @@ -1,317 +1,263 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * This DV implements the handling of n-ary relations. |
| 5 | + * The SMWDataValue in this file implements the handling of n-ary relations. |
| 6 | + * @author Jörg Heizmann |
| 7 | + * @author Markus Krötzsch |
6 | 8 | */ |
7 | 9 | |
8 | 10 | require_once('SMW_DataValue.php'); |
9 | 11 | |
10 | 12 | class SMWNAryValue extends SMWDataValue { |
11 | 13 | |
12 | | - /** |
13 | | - * The array of the data values within this container value |
14 | | - */ |
15 | | - private $m_values = Array(); |
| 14 | + private $m_scount = 0; |
16 | 15 | |
17 | | - /** |
18 | | - * Is this n-ary datavalue valid? |
19 | | - */ |
20 | | - private $isValid; |
| 16 | + /** |
| 17 | + * The array of the data values within this container value |
| 18 | + */ |
| 19 | + private $m_values = array(); |
21 | 20 | |
22 | | - /** |
23 | | - * types as we received them when datafactory called us |
24 | | - */ |
25 | | - private $m_type = Array(); |
| 21 | + /** |
| 22 | + * typevalue as we received them when datafactory called us |
| 23 | + */ |
| 24 | + private $m_type; |
26 | 25 | |
27 | | - /** |
28 | | - * Set type array. Must be done before setting any values. |
29 | | - */ |
30 | | - function setType($type) { |
31 | | - $this->m_type = $type; |
32 | | - } |
| 26 | + /** |
| 27 | + * Set type array. Must be done before setting any values. |
| 28 | + */ |
| 29 | + function setType($type) { |
| 30 | + $this->m_type = $type; |
| 31 | + $this->m_count = count($this->m_type->getTypeLabels()); |
| 32 | + $this->m_values = array(); // careful: do not iterate to m_count if DV is not valid! |
| 33 | + } |
33 | 34 | |
34 | | - private function parseValues($commaValues) { |
35 | | - return preg_split('/[\s]*;[\s]*/', $commaValues, sizeof($this->m_type->getTypeLabels())); |
36 | | - } |
| 35 | + private function parseValues($commaValues) { |
| 36 | + return preg_split('/[\s]*;[\s]*/', trim($commaValues), $this->m_count); |
| 37 | + } |
37 | 38 | |
38 | | - protected function parseUserValue($value) { |
39 | | - $types = $this->m_type->getTypeValues(); |
40 | | - if ($value!='') { |
41 | | - $values = $this->parseValues($value); |
42 | | - // check if all values were specified |
43 | | - if (sizeof($values) < sizeof($types)) { |
44 | | - $valueindex = 0; |
45 | | - // less values specified -> test for closest matchings |
46 | | - for ($i = 0; $i < sizeof($types); $i++) { |
47 | | - // check if enough slots available... |
48 | | - if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
49 | | - $this->isValid = false; |
50 | | - $this->addError("N-ary DV is invalid"); |
51 | | - $this->m_values[$i] = null; |
52 | | - } else { |
53 | | - // are values left to set? |
54 | | - if (sizeof($values) > $valueindex) { |
55 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$valueindex]); |
56 | | - if ($this->m_values[$i]->isValid()) { |
57 | | - $valueindex++; |
58 | | - } else { |
59 | | - /// TODO: Remove previously set (user)value from container!!! |
60 | | - $this->m_values[$i] = null; |
61 | | - } |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - } else { |
66 | | - // everything specified and passed correctly |
67 | | - for ($i = 0; $i < sizeof($types); $i++) { |
68 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$i]); |
69 | | - } |
70 | | - $this->isValid = true; |
71 | | - } |
72 | | - } else { |
73 | | - // no values specified -> create empty DVs |
74 | | - for ($i = 0; $i < sizeof($types); $i++) { |
75 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], false); |
76 | | - } |
77 | | - } |
78 | | - } |
| 39 | + protected function parseUserValue($value) { |
| 40 | + $this->m_values = array(); |
| 41 | + if ($value == '') { |
| 42 | + $this->addError('No values specified.'); |
| 43 | + return; |
| 44 | + } |
79 | 45 | |
80 | | - protected function parseXSDValue($value, $unit) { |
81 | | - // get DVtypes |
82 | | - $types = $this->m_type->getTypeValues(); |
83 | | - // get values supplied by user |
84 | | - $values = $this->parseValues($value); |
85 | | - $units = $this->parseValues($unit); |
| 46 | + $types = $this->m_type->getTypeValues(); |
| 47 | + $values = $this->parseValues($value); |
| 48 | + $vi = 0; // index in value array |
| 49 | + $empty = true; |
| 50 | + for ($i = 0; $i < $this->m_count; $i++) { // iterate over slots |
| 51 | + if ( (count($values) > $vi) && |
| 52 | + ( ($values[$vi] == '') || ($values[$vi] == '?') ) ) { // explicit omission |
| 53 | + $this->m_values[$i] = NULL; |
| 54 | + $vi++; |
| 55 | + } elseif (count($values) > $vi) { // some values left, try next slot |
| 56 | + $dv = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$vi]); |
| 57 | + if ($dv->isValid()) { // valid DV: keep |
| 58 | + $this->m_values[$i] = $dv; |
| 59 | + $vi++; |
| 60 | + $empty = false; |
| 61 | + } elseif ( (count($values)-$vi) == (count($types)-$i) ) { |
| 62 | + // too many errors: keep this one to have enough slots left |
| 63 | + $this->m_values[$i] = $dv; |
| 64 | + $vi++; |
| 65 | + } else { // assume implicit omission, reset to NULL |
| 66 | + $this->m_values[$i] = NULL; |
| 67 | + } |
| 68 | + } else { // fill rest with NULLs |
| 69 | + $this->m_values[$i] = NULL; |
| 70 | + } |
| 71 | + } |
| 72 | + if ($empty) { |
| 73 | + $this->addError('No values specified.'); |
| 74 | + } |
| 75 | + } |
86 | 76 | |
87 | | - // create user specified DVs |
88 | | - if (sizeof($values) < sizeof($types)) { |
89 | | - $valueindex = 0; |
90 | | - // less values specified -> test for closest matchings |
91 | | - for ($i = 0; $i < sizeof($types); $i++) { |
92 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
93 | | - // check if enough slots available -> if not set isValid to false... |
94 | | - if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
95 | | - $this->isValid = false; |
96 | | - $this->addError("N-ary DV is invalid"); |
97 | | - } else { |
98 | | - // is value valid? |
99 | | - if ($this->m_values[$i]) { |
100 | | - $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$valueindex] : null)); |
101 | | - if ($this->m_values[$i]->isValid()) { |
102 | | - $valueindex++; |
103 | | - } else { |
104 | | - $this->m_values[$i] = null; |
105 | | - } |
106 | | - } |
107 | | - } |
108 | | - } |
109 | | - } else { |
110 | | - // everything specified and passed correctly - set XSDValue of DV containers. |
111 | | - for ($i = 0; $i < sizeof($types); $i++) { |
112 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
113 | | - $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$i] : null)); |
114 | | - } |
115 | | - } |
116 | | - } |
| 77 | + protected function parseXSDValue($value, $unit) { |
| 78 | + $types = $this->m_type->getTypeValues(); |
| 79 | + // Note: we can always assume this to be the form that getXSDValue returns, |
| 80 | + // unless it is complete junk. So be strict in parsing. |
| 81 | + $values = explode(';', $value, $this->m_count); |
| 82 | + $units = explode(';', $unit, $this->m_count); |
117 | 83 | |
118 | | - public function setOutputFormat($formatstring) { |
119 | | - /// TODO |
120 | | - } |
| 84 | + if (count($values) != $this->m_count) { |
| 85 | + $this->addError('This is not an nary value.'); |
| 86 | + return; |
| 87 | + } |
121 | 88 | |
122 | | - public function getShortWikiText($linked = NULL) { |
123 | | - if ($this->m_caption !== false) { |
124 | | - return $this->m_caption; |
125 | | - } |
126 | | - /// TODO: beautify with (...) like LongText |
127 | | - $result = ''; |
128 | | - $first = true; |
129 | | - $second = true; |
130 | | - foreach ($this->m_values as $value) { |
131 | | - if ($first) { |
132 | | - $first = false; |
133 | | - } else if ($second) { |
134 | | - $result .= ' ('; |
135 | | - $second = false; |
136 | | - } else { |
137 | | - $result .= ", "; |
138 | | - } |
139 | | - if ($value) { |
140 | | - if ($value->getShortWikiText($linked)) { |
141 | | - $result .= $value->getShortWikiText($linked); |
142 | | - } |
143 | | - } else { |
144 | | - $result .= "?"; |
145 | | - } |
146 | | - } |
147 | | - return $second ? $result : $result .= ')'; |
148 | | - } |
| 89 | + $this->m_values = array(); |
| 90 | + for ($i = 0; $i < $this->m_count; $i++) { |
| 91 | + if ($values[$i] == '') { |
| 92 | + $this->m_values[$i] = NULL; |
| 93 | + } else { |
| 94 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$i]); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
149 | 98 | |
150 | | - public function getShortHTMLText($linker = NULL) { |
151 | | - if ($this->m_caption !== false) { |
152 | | - return $this->m_caption; |
153 | | - } |
154 | | - /// TODO: beautify with (...) like LongText |
155 | | - $result = ''; |
156 | | - $first = true; |
157 | | - foreach ($this->m_values as $value) { |
158 | | - if ($first) { |
159 | | - $first = false; |
160 | | - } else { |
161 | | - $result .= ", "; |
162 | | - } |
163 | | - if ($value) { |
164 | | - if ($value->getShortHTMLText($linker)) { |
165 | | - $result .= $value->getShortHTMLText($linker); |
166 | | - } |
167 | | - } else { |
168 | | - $result .= "?"; |
169 | | - } |
170 | | - } |
171 | | - return $result; |
172 | | - } |
| 99 | + public function setOutputFormat($formatstring) { |
| 100 | + /// TODO |
| 101 | + } |
173 | 102 | |
174 | | - public function getLongWikiText($linked = NULL) { |
175 | | - $result = ''; |
176 | | - $first = true; |
177 | | - $second = true; |
178 | | - foreach ($this->m_values as $value) { |
179 | | - if ($first) { |
180 | | - $first = false; |
181 | | - } else if ($second) { |
182 | | - $result .= ' ('; |
183 | | - $second = false; |
184 | | - } |
185 | | - else { |
186 | | - $result .= ", "; |
187 | | - } |
188 | | - if ($value) { |
189 | | - $result .= $value->getLongWikiText($linked); |
190 | | - } else { |
191 | | - $result .= null; |
192 | | - } |
193 | | - } |
194 | | - return $second ? $result : $result .= ')'; |
195 | | - } |
| 103 | + public function getShortWikiText($linked = NULL) { |
| 104 | + if ($this->m_caption !== false) { |
| 105 | + return $this->m_caption; |
| 106 | + } |
| 107 | + return $this->makeOutputText(0, $linked); |
| 108 | + } |
196 | 109 | |
197 | | - public function getLongHTMLText($linker = NULL) { |
198 | | - /// TODO: beautify with (...) like WikiText |
199 | | - $result = ''; |
200 | | - $first = true; |
201 | | - foreach ($this->m_values as $value) { |
202 | | - if ($first) { |
203 | | - $first = false; |
204 | | - } else { |
205 | | - $result .= ", "; |
206 | | - } |
207 | | - if ($value) { |
208 | | - $result .= $value->getLongHTMLText($linker); |
209 | | - } else { |
210 | | - $result .= "?"; |
211 | | - } |
212 | | - } |
213 | | - return $result; |
214 | | - } |
| 110 | + public function getShortHTMLText($linker = NULL) { |
| 111 | + if ($this->m_caption !== false) { |
| 112 | + return $this->m_caption; |
| 113 | + } |
| 114 | + return $this->makeOutputText(1, $linker); |
| 115 | + } |
215 | 116 | |
216 | | - public function getXSDValue() { |
217 | | - $xsdvals = Array(); |
218 | | - for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
219 | | - if ($this->m_values[$i]) { |
220 | | - $xsdvals[$i] = $this->m_values[$i]->getXSDValue(); |
221 | | - } |
222 | | - } |
223 | | - return implode(';', $xsdvals); |
224 | | - } |
| 117 | + public function getLongWikiText($linked = NULL) { |
| 118 | + return $this->makeOutputText(2, $linked); |
| 119 | + } |
225 | 120 | |
226 | | - public function getWikiValue() { |
227 | | - $result = ''; |
228 | | - $first = true; |
229 | | - foreach ($this->m_values as $value) { |
230 | | - if ($first) { |
231 | | - $first = false; |
232 | | - } else { |
233 | | - $result .= ";"; |
234 | | - } |
| 121 | + public function getLongHTMLText($linker = NULL) { |
| 122 | + return $this->makeOutputText(3, $linker); |
| 123 | + } |
235 | 124 | |
236 | | - if ($value) { |
237 | | - $result .= $value->getWikiValue(); |
238 | | - } |
239 | | - } |
240 | | - return $result; |
241 | | - } |
| 125 | + private function makeOutputText($type = 0, $linker = NULL) { |
| 126 | + if (!$this->isValid()) { |
| 127 | + return ( ($type == 0)||($type == 1) )? '' : $this->getErrorText(); |
| 128 | + } |
| 129 | + $result = ''; |
| 130 | + for ($i = 0; $i < $this->m_count; $i++) { |
| 131 | + if ($i == 1) { |
| 132 | + $result .= ' ('; |
| 133 | + } elseif ($i > 1) { |
| 134 | + $result .= ", "; |
| 135 | + } |
| 136 | + if ($this->m_values[$i] !== NULL) { |
| 137 | + $result .= $this->makeValueOutputText($type, $i, $linker); |
| 138 | + } else { |
| 139 | + $result .= '?'; |
| 140 | + } |
| 141 | + if ($i == sizeof($this->m_values) - 1) { |
| 142 | + $result .= ')'; |
| 143 | + } |
| 144 | + } |
| 145 | + return $result; |
| 146 | + } |
| 147 | + |
| 148 | + private function makeValueOutputText($type, $index, $linker) { |
| 149 | + switch ($type) { |
| 150 | + case 0: return $this->m_values[$index]->getShortWikiText($linker); |
| 151 | + case 1: return $this->m_values[$index]->getShortHTMLText($linker); |
| 152 | + case 2: return $this->m_values[$index]->getLongWikiText($linker); |
| 153 | + case 3: return $this->m_values[$index]->getLongHTMLText($linker); |
| 154 | + } |
| 155 | + } |
242 | 156 | |
243 | | - public function getNumericValue() { |
244 | | - return false; |
245 | | - } |
| 157 | + public function getXSDValue() { |
| 158 | + $first = true; |
| 159 | + $result = ''; |
| 160 | + foreach ($this->m_values as $value) { |
| 161 | + if ($first) { |
| 162 | + $first = false; |
| 163 | + } else { |
| 164 | + $result .= ';'; |
| 165 | + } |
| 166 | + if ($value !== NULL) { |
| 167 | + $result .= $value->getXSDValue(); |
| 168 | + } |
| 169 | + } |
| 170 | + return $result; |
| 171 | + } |
246 | 172 | |
247 | | - public function getUnit() { |
248 | | - $units = Array(); |
249 | | - for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
250 | | - if ($this->m_values[$i]) { |
251 | | - $units[$i] = $this->m_values[$i]->getUnit(); |
252 | | - } |
253 | | - } |
254 | | - return implode(';', $units); |
255 | | - } |
| 173 | + public function getWikiValue() { |
| 174 | + $result = ''; |
| 175 | + $first = true; |
| 176 | + foreach ($this->m_values as $value) { |
| 177 | + if ($first) { |
| 178 | + $first = false; |
| 179 | + } else { |
| 180 | + $result .= "; "; |
| 181 | + } |
| 182 | + if ($value !== NULL) { |
| 183 | + $result .= $value->getWikiValue(); |
| 184 | + } else { |
| 185 | + $result .= "?"; |
| 186 | + } |
| 187 | + } |
| 188 | + return $result; |
| 189 | + } |
256 | 190 | |
257 | | - public function getHash() { |
258 | | - $hash = ''; |
259 | | - foreach ($this->m_values as $value) { |
260 | | - // is DV set? |
261 | | - if ($value) { |
262 | | - $hash .= $value->getHash(); |
263 | | - } |
264 | | - /// FIXME: this is wrong (different value combinations yield same hash) |
265 | | - } |
266 | | - return $hash; |
267 | | - } |
| 191 | + public function getNumericValue() { |
| 192 | + return false; |
| 193 | + } |
268 | 194 | |
269 | | - public function isNumeric() { |
270 | | - return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers) |
271 | | - } |
| 195 | + public function getUnit() { |
| 196 | + $first = true; |
| 197 | + $result = ''; |
| 198 | + foreach ($this->m_values as $value) { |
| 199 | + if ($first) { |
| 200 | + $first = false; |
| 201 | + } else { |
| 202 | + $result .= ';'; |
| 203 | + } |
| 204 | + if ($value !== NULL) { |
| 205 | + $result .= $value->Unit(); |
| 206 | + } |
| 207 | + } |
| 208 | + return $result; |
| 209 | + } |
272 | 210 | |
273 | | - // |
274 | | - // custom functions for n-ary attributes |
275 | | - // |
| 211 | + public function getHash() { |
| 212 | + $first = true; |
| 213 | + $result = ''; |
| 214 | + foreach ($this->m_values as $value) { |
| 215 | + if ($first) { |
| 216 | + $first = false; |
| 217 | + } else { |
| 218 | + $result .= ' - '; |
| 219 | + } |
| 220 | + if ($value !== NULL) { |
| 221 | + $result .= str_replace('-', '--', $value->getHash()); |
| 222 | + } |
| 223 | + } |
| 224 | + return $result; |
| 225 | + } |
276 | 226 | |
277 | | - public function getDVTypeIDs() { |
278 | | - return implode(';', $this->m_type->getTypeLabels()); |
279 | | - } |
| 227 | + public function isNumeric() { |
| 228 | + return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers) |
| 229 | + } |
280 | 230 | |
281 | | - public function getType() { |
282 | | - return $this->m_type; |
283 | | - } |
| 231 | +////// Custom functions for n-ary attributes |
284 | 232 | |
285 | | - public function getDVs() { |
286 | | - return $this->isValid() ? $this->m_values : null; |
287 | | - } |
| 233 | + public function getDVTypeIDs() { |
| 234 | + return implode(';', $this->m_type->getTypeLabels()); |
| 235 | + } |
288 | 236 | |
289 | | - public function setDVs($datavalues) { |
290 | | - // less values specified... |
291 | | - $typelabels = $this->m_type->getTypeLabels(); |
292 | | - if (sizeof($datavalues) < sizeof($typelabels)) { |
293 | | - for ($i = 0; $i < sizeof($datavalues); $i++) { |
294 | | - if ($datavalues[$i]) { |
295 | | - if ($datavalues[$i]->getTypeID() == SMWTypesValue::findTypeID($typelabels[$i])) { |
296 | | - $this->m_values[$i] = $datavalues[$i]; |
297 | | - } else { |
298 | | - $this->m_values[$i] = null; |
299 | | - } |
300 | | - } |
301 | | - } |
302 | | - } else if (sizeof($datavalues) == sizeof($typelabels)) { |
303 | | - // everything specified as desired |
304 | | - for ($i = 0; $i < sizeof($typelabels); $i++) { |
305 | | - if ($datavalues[$i]) { |
306 | | - // check if dv matches expected one |
307 | | - if ($datavalues[$i]->getTypeID() == SMWTypesValue::findTypeID($typelabels[$i])) { |
308 | | - $this->m_values[$i] = $datavalues[$i]; |
309 | | - } else { |
310 | | - $this->m_values[$i] = null; |
311 | | - } |
312 | | - } |
313 | | - } |
314 | | - } |
315 | | - } |
| 237 | + public function getType() { |
| 238 | + return $this->m_type; |
| 239 | + } |
316 | 240 | |
| 241 | + public function getDVs() { |
| 242 | + return $this->isValid() ? $this->m_values : NULL; |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Directly set the values to the given array of values. The given values |
| 247 | + * should correspond to the types and arity of the nary container, with |
| 248 | + * NULL as an indication for omitted values. |
| 249 | + */ |
| 250 | + public function setDVs($datavalues) { |
| 251 | + $typelabels = $this->m_type->getTypeLabels(); |
| 252 | + for ($i = 0; $i < $this->m_count; $i++) { |
| 253 | + if ( ($i < count($datavalues) ) && ($datavalues[$i] !== NULL) ) { |
| 254 | + //&& ($datavalues[$i]->getTypeID() == SMWTypesValue::findTypeID($typelabels[$i])) ) { |
| 255 | + ///TODO: is the above typcheck required, or can we assume responsible callers? |
| 256 | + $this->m_values[$i] = $datavalues[$i]; |
| 257 | + } else { |
| 258 | + $this->m_values[$i] = NULL; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
317 | 263 | } |
318 | 264 | |