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