-
Notifications
You must be signed in to change notification settings - Fork 89
/
ARC2_Graph.php
234 lines (202 loc) · 5.5 KB
/
ARC2_Graph.php
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
<?php
/**
* ARC2 Graph object.
*
* @author Benjamin Nowack <mail@bnowack.de>
* @license W3C Software License
*
* @homepage <https://github.com/semsol/arc2>
*/
ARC2::inc('Class');
class ARC2_Graph extends ARC2_Class
{
protected $index;
public function __construct($a, &$caller)
{
parent::__construct($a, $caller);
}
public function __init()
{
parent::__init();
$this->index = [];
}
public function setIndex($index)
{
$this->index = $index;
return $this;
}
public function getIndex()
{
return $this->index;
}
public function addIndex($index)
{
$this->index = ARC2::getMergedIndex($this->index, $index);
return $this;
}
public function addGraph($graph)
{
// namespaces
foreach ($graph->ns as $prefix => $ns) {
$this->setPrefix($prefix, $ns);
}
// index
$this->addIndex($graph->getIndex());
return $this;
}
public function addRdf($data, $format = null)
{
if ('json' == $format) {
return $this->addIndex(json_decode($data, true));
} else {// parse any other rdf format
return $this->addIndex($this->toIndex($data));
}
}
public function hasSubject($s)
{
return isset($this->index[$s]);
}
public function hasTriple($s, $p, $o)
{
if (!is_array($o)) {
return $this->hasLiteralTriple($s, $p, $o) || $this->hasLinkTriple($s, $p, $o);
}
if (!isset($this->index[$s])) {
return false;
}
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) {
return false;
}
return in_array($o, $this->index[$s][$p]);
}
public function hasLiteralTriple($s, $p, $o)
{
if (!isset($this->index[$s])) {
return false;
}
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) {
return false;
}
$os = $this->getObjects($s, $p, false);
foreach ($os as $object) {
if ($object['value'] == $o && 'literal' == $object['type']) {
return true;
}
}
return false;
}
public function hasLinkTriple($s, $p, $o)
{
if (!isset($this->index[$s])) {
return false;
}
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) {
return false;
}
$os = $this->getObjects($s, $p, false);
foreach ($os as $object) {
if ($object['value'] == $o && ('uri' == $object['type'] || 'bnode' == $object['type'])) {
return true;
}
}
return false;
}
public function addTriple($s, $p, $o, $oType = 'literal')
{
$p = $this->expandPName($p);
if (!is_array($o)) {
$o = ['value' => $o, 'type' => $oType];
}
if ($this->hasTriple($s, $p, $o)) {
return;
}
if (!isset($this->index[$s])) {
$this->index[$s] = [];
}
if (!isset($this->index[$s][$p])) {
$this->index[$s][$p] = [];
}
$this->index[$s][$p][] = $o;
return $this;
}
public function getSubjects($p = null, $o = null)
{
if (!$p && !$o) {
return array_keys($this->index);
}
$result = [];
foreach ($this->index as $s => $ps) {
foreach ($ps as $predicate => $os) {
if ($p && $predicate != $p) {
continue;
}
foreach ($os as $object) {
if (!$o) {
$result[] = $s;
break;
} elseif (is_array($o) && $object == $o) {
$result[] = $s;
break;
} elseif ($o && $object['value'] == $o) {
$result[] = $s;
break;
}
}
}
}
return array_unique($result);
}
public function getPredicates($s = null)
{
$result = [];
$index = $s ? ([$s => isset($this->index[$s]) ? $this->index[$s] : []]) : $this->index;
foreach ($index as $subject => $ps) {
if ($s && $s != $subject) {
continue;
}
$result = array_merge($result, array_keys($ps));
}
return array_unique($result);
}
public function getObjects($s, $p, $plain = false)
{
if (!isset($this->index[$s])) {
return [];
}
$p = $this->expandPName($p);
if (!isset($this->index[$s][$p])) {
return [];
}
$os = $this->index[$s][$p];
if ($plain) {
array_walk($os, function (&$o) {
$o = $o['value'];
});
}
return $os;
}
public function getObject($s, $p, $plain = false, $default = null)
{
$os = $this->getObjects($s, $p, $plain);
return empty($os) ? $default : $os[0];
}
public function getNTriples()
{
return parent::toNTriples($this->index, $this->ns);
}
public function getTurtle()
{
return parent::toTurtle($this->index, $this->ns);
}
public function getRDFXML()
{
return parent::toRDFXML($this->index, $this->ns);
}
public function getJSON()
{
return json_encode($this->index);
}
}