summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Query/Language/SomeProperty.php
blob: 7260c48d05ba65c7841195cff7346afc4bd3207c (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
<?php

namespace SMW\Query\Language;

use SMW\DIProperty;

/**
 * Description of a set of instances that have an attribute with some value
 * that fits another (sub)description.
 *
 * Corresponds to existential quantification ("SomeValuesFrom" restriction) on
 * properties in OWL. In conjunctive queries (OWL) and SPARQL (RDF), it is
 * represented by using variables in the object part of such properties.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Markus Krötzsch
 */
class SomeProperty extends Description {

	/**
	 * @var Description
	 */
	protected $description;

	/**
	 * @var DIProperty
	 */
	protected $property;

	/**
	 * @var integer|null
	 */
	protected $hierarchyDepth;

	/**
	 * @since 1.6
	 *
	 * @param DIProperty $property
	 * @param Description $description
	 */
	public function __construct( DIProperty $property, Description $description ) {
		$this->property = $property;
		$this->description = $description;
	}

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

		if ( $hierarchyDepth > $GLOBALS['smwgQSubpropertyDepth'] ) {
			$hierarchyDepth = $GLOBALS['smwgQSubpropertyDepth'];
		}

		$this->hierarchyDepth = $hierarchyDepth;
	}

	/**
	 * @since 3.0
	 *
	 * @return integer|null
	 */
	public function getHierarchyDepth() {
		return $this->hierarchyDepth;
	}

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

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

		$subDescription = $this->description;
		$property = $this->property->getSerialization();

		// Resolve property.chains and connect its members
		while ( $subDescription instanceof SomeProperty ) {
			$subDescription = $subDescription->getDescription();
			$subDescription->setMembership( $property );
		}

		// During a recursive chain use the hash from a stored
		// member to distinguish Foo.Bar.Foobar.Bam from Foo.Bar.Foobar
		$membership = $this->getMembership() . $subDescription->getMembership();

		return $this->fingerprint = 'S:' . md5( $property . '|' . $membership . '|' . $this->description->getFingerprint() . $this->hierarchyDepth );
	}

	/**
	 * @return DIProperty
	 */
	public function getProperty() {
		return $this->property;
	}

	/**
	 * @since 1.6
	 *
	 * @return Description
	 */
	public function getDescription() {
		return $this->description;
	}

	/**
	 * @since 1.6
	 *
	 * @return string
	 */
	public function getQueryString( $asValue = false ) {
		$subDescription = $this->description;

		// Use the canonical label to ensure that conditions contain
		// language indep. references
		$propertyChainString = $this->property->getCanonicalLabel();
		$propertyname = $propertyChainString;
		$final = '';

		while ( ( $propertyname !== '' ) && ( $subDescription instanceof SomeProperty ) ) { // try to use property chain syntax
			$propertyname = $subDescription->getProperty()->getCanonicalLabel();

			if ( $propertyname !== '' ) {
				$propertyChainString .= '.' . $propertyname;
				$subDescription = $subDescription->getDescription();
			}
		}

		if ( $this->hierarchyDepth !== null ) {
			$final = '|+depth=' . $this->hierarchyDepth;
		}

		if ( $asValue ) {
			return '<q>[[' . $propertyChainString . '::' . $subDescription->getQueryString( true ) . $final . ']]</q>';
		}

		return '[[' . $propertyChainString . '::' . $subDescription->getQueryString( true ) . $final . ']]';
	}

	/**
	 * @since 1.6
	 *
	 * @return boolean
	 */
	public function isSingleton() {
		return false;
	}

	/**
	 * @since 1.6
	 *
	 * @return integer
	 */
	public function getSize() {
		return 1 + $this->getDescription()->getSize();
	}

	/**
	 * @since 1.6
	 *
	 * @return integer
	 */
	public function getDepth() {
		return 1 + $this->getDescription()->getDepth();
	}

	/**
	 * @since 1.6
	 *
	 * @return integer
	 */
	public function getQueryFeatures() {
		return SMW_PROPERTY_QUERY | $this->description->getQueryFeatures();
	}

	/**
	 * @since 1.6
	 *
	 * @return SomeProperty
	 */
	public function prune( &$maxsize, &$maxdepth, &$log ) {

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

		$maxsize--;
		$maxdepth--;

		$result = new SomeProperty(
			$this->property,
			$this->description->prune( $maxsize, $maxdepth, $log )
		);

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

		return $result;
	}

}