summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/Language/Disjunction.php
blob: 04fddc153ee7d9ae80b602f57987948d12d5191b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php

namespace SMW\Query\Language;

/**
 * Description of a collection of many descriptions, at least one of which
 * must be satisfied (OR, disjunction).
 *
 * Corresponds to disjunction in OWL and SPARQL. Not available in RDFS.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class Disjunction extends Description {

	/**
	 * @var Description[]
	 */
	private $descriptions = [];

	/**
	 * contains a single class description if any such disjunct was given;
	 * disjunctive classes are aggregated therei
	 * n
	 * @var null|ClassDescription
	 */
	private $classDescription = null;

	/**
	 * Used if disjunction is trivially true already
	 *
	 * @var boolean
	 */
	private $isTrue = false;

	public function __construct( array $descriptions = [] ) {
		foreach ( $descriptions as $desc ) {
			$this->addDescription( $desc );
		}
	}

	/**
	 * @see Description::getFingerprint
	 * @since 2.5
	 *
	 * @return string
	 */
	public function getFingerprint() {

		// Avoid a recursive tree
		if ( $this->fingerprint !== null ) {
			return $this->fingerprint;
		}

		$fingerprint = [];

		foreach ( $this->descriptions as $description ) {
			$fingerprint[$description->getFingerprint()] = true;
		}

		ksort( $fingerprint );

		return $this->fingerprint = 'D:' . md5( implode( '|', array_keys( $fingerprint ) ) );
	}

	/**
	 * @since 3.0
	 *
	 * @param integer $hierarchyDepth
	 */
	public function setHierarchyDepth( $hierarchyDepth ) {

		$this->fingerprint = null;

		if ( $this->classDescription !== null ) {
			$this->classDescription->setHierarchyDepth( $hierarchyDepth );
		}

		foreach ( $this->descriptions as $key => $description ) {
			if ( $description instanceof SomeProperty ) {
				$description->setHierarchyDepth( $hierarchyDepth );
			}
		}
	}

	public function getDescriptions() {
		return $this->descriptions;
	}

	public function addDescription( Description $description ) {

		$this->fingerprint = null;
		$fingerprint = $description->getFingerprint();

		if ( $description instanceof ThingDescription ) {
			$this->isTrue = true;
			$this->descriptions = []; // no conditions any more
			$this->classDescription = null;
		}

		if ( !$this->isTrue ) {
			 // Combine class descriptions only when those describe the same state
			if ( $description instanceof ClassDescription ) {
				if ( is_null( $this->classDescription ) ) { // first class description
					$this->classDescription = $description;
					$this->descriptions[$description->getFingerprint()] = $description;
				} elseif ( $this->classDescription->isMergableDescription( $description ) ) {
					$this->classDescription->addDescription( $description );
				} else {
					$this->descriptions[$description->getFingerprint()] = $description;
				}
			} elseif ( $description instanceof Disjunction ) { // absorb sub-disjunctions
				foreach ( $description->getDescriptions() as $subdesc ) {
					$this->descriptions[$subdesc->getFingerprint()] = $subdesc;
				}
			// } elseif ($description instanceof SMWSomeProperty) {
			   ///TODO: use subdisjunct. for multiple SMWSomeProperty descs with same property
			} else {
				$this->descriptions[$fingerprint] = $description;
			}
		}

		// move print descriptions downwards
		///TODO: This may not be a good solution, since it does modify $description and since it does not react to future cahges
		$this->m_printreqs = array_merge( $this->m_printreqs, $description->getPrintRequests() );
		$description->setPrintRequests( [] );
	}

	public function getQueryString( $asValue = false ) {

		if ( $this->isTrue ) {
			return '+';
		}

		$result = '';
		$sep = $asValue ? '||':' OR ';

		foreach ( $this->descriptions as $desc ) {
			$subdesc = $desc->getQueryString( $asValue );

			if ( $desc instanceof SomeProperty ) { // enclose in <q> for parsing
				if ( $asValue ) {
					$subdesc = ' <q>[[' . $subdesc . ']]</q> ';
				} else {
					$subdesc = ' <q>' . $subdesc . '</q> ';
				}
			}

			$result .= ( $result ? $sep:'' ) . $subdesc;
		}

		if ( $asValue ) {
			return $result;
		}

		return ' <q>' . $result . '</q> ';
	}

	public function isSingleton() {
		/// NOTE: this neglects the unimportant case where several disjuncts describe the same object.
		if ( count( $this->descriptions ) != 1 ) {
			return false;
		}

		return $this->descriptions[0]->isSingleton();
	}

	public function getSize() {
		$size = 0;

		foreach ( $this->descriptions as $desc ) {
			$size += $desc->getSize();
		}

		return $size;
	}

	public function getDepth() {
		$depth = 0;

		foreach ( $this->descriptions as $desc ) {
			$depth = max( $depth, $desc->getDepth() );
		}

		return $depth;
	}

	public function getQueryFeatures() {
		$result = SMW_DISJUNCTION_QUERY;

		foreach ( $this->descriptions as $desc ) {
			$result = $result | $desc->getQueryFeatures();
		}

		return $result;
	}

	public function prune( &$maxsize, &$maxdepth, &$log ) {

		if ( $maxsize <= 0 ) {
			$log[] = $this->getQueryString();
			return new ThingDescription();
		}

		$prunelog = [];
		$newdepth = $maxdepth;
		$result = new Disjunction();

		foreach ( $this->descriptions as $desc ) {
			$restdepth = $maxdepth;
			$result->addDescription( $desc->prune( $maxsize, $restdepth, $prunelog ) );
			$newdepth = min( $newdepth, $restdepth );
		}

		if ( count( $result->getDescriptions() ) > 0 ) {
			$log = array_merge( $log, $prunelog );
			$maxdepth = $newdepth;

			if ( count( $result->getDescriptions() ) == 1 ) { // simplify unary disjunctions!
				$descriptions = $result->getDescriptions();
				$result = array_shift( $descriptions );
			}

			$result->setPrintRequests( $this->getPrintRequests() );

			return $result;
		}

		$log[] = $this->getQueryString();

		$result = new ThingDescription();
		$result->setPrintRequests( $this->getPrintRequests() );

		return $result;
	}

}