source: Dev/trunk/rdfapi/sparql/GraphPattern.php @ 12

Last change on this file since 12 was 12, checked in by basvannuland, 14 years ago

Added RAP RDF API
Added RDF reader writer for save and load survey

File size: 6.5 KB
Line 
1<?php
2// ---------------------------------------------
3// class: GraphPattern
4// ---------------------------------------------
5
6/**
7* A graph pattern which consists of triple patterns, optional
8* or union graph patterns and filters.
9*
10* @author   Tobias Gauss <tobias.gauss@web.de>
11* @version     $Id$
12* @license http://www.gnu.org/licenses/lgpl.html LGPL
13*
14* @package sparql
15*/
16
17class GraphPattern extends Object{
18
19    /**
20    * Graphname. 0 if its in the default graph.
21    */
22    protected $graphname = null;
23
24    /**
25    * Array of QueryTriple objects
26    * @var array
27    */
28    protected $triplePatterns = array();
29
30    /**
31    * A List of Constraint objects
32    * @var array
33    */
34    protected $constraints = array();
35
36    /**
37    * @var int Pointer to optional pattern.
38    */
39    protected $optional = null;
40
41    /**
42    * @var int Pointer to union pattern.
43    */
44    protected $union = null;
45
46    /**
47    * ID of the pattern in the pattern list that
48    * this pattern is subpattern of.
49    * @var int
50    */
51    protected $subpatternOf = null;
52
53    /**
54    * @var boolean TRUE if the pattern is open- FALSE if closed.
55    */
56    public $open = false;
57
58    /**
59    * @var boolean TRUE if the GraphPattern is a construct pattern.
60    */
61    public $isConstructPattern = false;
62
63
64    /**
65    * @var int The GraphPatterns id.
66    */
67    public $patternId = null;
68
69
70    /**
71    * Constructor
72    */
73    public function GraphPattern() {
74        $this->open               = true;
75        $this->isConstructPattern = false;
76        $this->constraints        = array();
77        $this->triplePatterns     = array();
78    }
79
80    /**
81    * Returns the graphname.
82    *
83    * @return String
84    */
85    public function getGraphname(){
86        return $this->graphname;
87    }
88
89    /**
90    * Returns the triple pattern of the graph pattern.
91    *
92    * @return Array
93    */
94    public function getTriplePatterns(){
95        return $this->triplePatterns;
96    }
97
98    /**
99    * Returns a constraint if there is one false if not.
100    *
101    * @return Constraint
102    */
103    public function getConstraints() {
104        return $this->constraints;
105    }
106
107    /**
108    * Returns a pointer to an optional graph pattern.
109    *
110    * @return integer
111    */
112    public function getOptional() {
113        return $this->optional;
114    }
115
116    /**
117    * Returns a pointer to the parent pattern this pattern
118    * is subpattern of.
119    *
120    * @return integer
121    */
122    public function getSubpatternOf() {
123        return $this->subpatternOf;
124    }
125
126    /**
127    * Returns a pointer to a union graph pattern.
128    *
129    * @return integer
130    */
131    public function getUnion() {
132        return $this->union;
133    }
134
135    /**
136    * Sets the graphname.
137    *
138    * @param  String $name
139    * @return void
140    */
141    public function setGraphname($name) {
142        $this->graphname = $name;
143    }
144
145    /**
146    * Adds List of QueryTriples to the GraphPattern.
147    *
148    * @param  array $trpP
149    * @return void
150    */
151    public function addTriplePatterns($trpP) {
152        $this->triplePatterns = array_merge($this->triplePatterns, $trpP);
153    }
154
155    /**
156    * Sets the List of QueryTriples to the GraphPattern.
157    *
158    * @param  array $trpP
159    * @return void
160    */
161    public function setTriplePatterns($trpP) {
162        $this->triplePatterns = $trpP;
163    }
164
165    /**
166    * Adds a single Constraint to the GraphPattern.
167    *
168    * @param  Constraint $cons
169    */
170    public function addConstraint(&$cons) {
171        $this->constraints[] = $cons;
172    }
173
174    /**
175    * Adds an array of Constraint objects to the GraphPattern.
176    *
177    * @param  array $cons
178    */
179    public function addConstraints(&$cons) {
180        $this->constraints = array_merge($this->constraints, $cons);
181    }
182
183    /**
184    * Adds a pointer to an optional graphPattern.
185    *
186    * @param  integer $patternId
187    * @return void
188    */
189    public function setOptional($patternId) {
190        $this->optional = &$patternId;
191    }
192
193    /**
194    * Adds a pointer to a union graphPattern.
195    *
196    * @param  integer $patternId
197    * @return void
198    */
199    public function setUnion($patternId) {
200        $this->union = &$patternId;
201    }
202
203    /**
204    * Adds a pointer to a pattern that
205    * this one is subpattern of
206    *
207    * @param  integer $patternId
208    */
209    public function setSubpatternOf($patternId) {
210        $this->subpatternOf = $patternId;
211    }
212
213
214    /**
215    * Sets the GraphPatterns Id.
216    *
217    * @param  integer $id
218    * @return void
219    */
220    public function setId($id){
221        $this->patternId = $id;
222    }
223
224    /**
225    * Returns the GraphPatterns id.
226    *
227    * @return integer
228    */
229    public function getId(){
230        return $this->patternId;
231    }
232
233
234
235    /**
236    *   Returns an array of all variables used in this
237    *   graph pattern.
238    *   All variables are listed only once (unique).
239    *
240    *   @return array   Array of variable names
241    */
242    public function getVariables()
243    {
244        $arVars = array();
245
246        foreach ($this->triplePatterns as $pattern) {
247            $arVars = array_merge($arVars, $pattern->getVariables());
248        }
249
250        return array_unique($arVars);
251    }//public function getVariables()
252
253
254
255    /**
256    *   Checks if the graph pattern is empty (contains no
257    *   usable data).
258    *   Occurs when using "{}" without pre- or suffixes
259    *   WHERE
260    *   {
261    *    { ?person rdf:type foaf:Person } .
262    *   }
263    *
264    *   @return boolean     True if the pattern is empty.
265    */
266    public function isEmpty()
267    {
268        return
269               count($this->triplePatterns) == 0
270            && count($this->constraints)    == 0
271            && $this->getGraphname()        === null
272        ;
273    }//public function isEmpty()
274
275
276
277    /**
278    *   When cloning, we need to clone some subobjects, too
279    */
280    public function __clone()
281    {
282        if (count($this->triplePatterns) > 0) {
283            foreach ($this->triplePatterns as $nId => $pattern) {
284                $this->triplePatterns[$nId] = clone $this->triplePatterns[$nId];
285            }
286        }
287        if (count($this->constraints) > 0) {
288            foreach ($this->constraints as $nId => $constraint) {
289                $this->constraints[$nId] = clone $this->constraints[$nId];
290            }
291        }
292    }
293
294}// end class: GraphPattern.php
295?>
Note: See TracBrowser for help on using the repository browser.