|
1
|
|
|
<?php |
|
2
|
|
|
namespace BOTK; |
|
3
|
|
|
|
|
4
|
|
|
class FactsFactory implements FactsFactoryInterface { |
|
5
|
|
|
|
|
6
|
|
|
protected $profile; |
|
7
|
|
|
protected $className; |
|
8
|
|
|
protected $tripleCount =0; |
|
9
|
|
|
protected $errors = array(); |
|
10
|
|
|
protected $errorCount=0; |
|
11
|
|
|
protected $unacceptableCount=0; |
|
12
|
|
|
protected $entityCount=0; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
2 |
|
public function __construct( array $profile ) |
|
16
|
|
|
{ |
|
17
|
|
|
$defaults = array( |
|
18
|
2 |
|
'model' => 'LocalBusiness', |
|
19
|
|
|
'options' => array(), |
|
20
|
|
|
'source' => null, |
|
21
|
2 |
|
'resilience' => 0.3, |
|
22
|
|
|
'datamapper' => function($rawdata){return array();}, |
|
|
|
|
|
|
23
|
|
|
'rawDataValidationFilter' => function($rawdata){return is_array($rawdata);}, |
|
24
|
|
|
); |
|
25
|
2 |
|
$this->profile = array_merge($defaults,$profile); |
|
26
|
2 |
|
$this->className = class_exists($this->profile['model']) |
|
27
|
|
|
?$this->profile['model'] |
|
28
|
2 |
|
:('\BOTK\Model\\'.$this->profile['model']); |
|
29
|
|
|
|
|
30
|
2 |
|
assert(class_exists($this->className)); |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* two level filter array |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function removeEmpty( array $data ) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$a = array(); |
|
40
|
2 |
|
foreach ($data as $key => $value) { |
|
41
|
2 |
|
$a[$key] = is_array($value)?array_filter($value):$value; |
|
42
|
|
|
} |
|
43
|
2 |
|
return array_filter($a); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
1 |
|
protected function createFacts($data=array()) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$facts = new $this->className($data,$this->profile['options']); |
|
50
|
1 |
|
assert($facts instanceof ModelInterface); |
|
51
|
1 |
|
return $facts; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
1 |
|
public function factualize( array $rawData ) |
|
56
|
|
|
{ |
|
57
|
1 |
|
$datamapper = $this->profile['datamapper']; |
|
58
|
1 |
|
$data =$this->removeEmpty($datamapper($rawData)); |
|
59
|
|
|
|
|
60
|
1 |
|
$facts = $this->createFacts($data); |
|
61
|
|
|
|
|
62
|
1 |
|
if($facts){ |
|
63
|
1 |
|
$this->entityCount++; |
|
64
|
1 |
|
$this->tripleCount+=$facts->getTripleCount(); |
|
65
|
|
|
} |
|
66
|
1 |
|
return $facts; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
public function generateLinkedDataHeader() |
|
71
|
|
|
{ |
|
72
|
|
|
assert(is_subclass_of($this->className,'\BOTK\ModelInterface')); |
|
|
|
|
|
|
73
|
|
|
$class = $this->className; |
|
74
|
|
|
return $class::getTurtleHeader(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function generateLinkedDataFooter() |
|
79
|
|
|
{ |
|
80
|
|
|
$now = date('c'); |
|
81
|
|
|
$rdf = "\n<> "; |
|
82
|
|
|
$this->tripleCount += 6; |
|
83
|
|
|
|
|
84
|
|
|
// add provenance info |
|
85
|
|
|
$verb=$this->tooManyErrors()?'invalidated':'generated'; |
|
86
|
|
|
$rdf .= "prov:{$verb}AtTime \"$now\"^^xsd:dateTime;"; |
|
87
|
|
|
if(!empty($this->profile['source'])){ |
|
88
|
|
|
$rdf.= "dct:source <{$this->profile['source']}>;"; |
|
89
|
|
|
$this->tripleCount++; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// add dataset info and a human readable comment as last line |
|
93
|
|
|
$rdf.= "foaf:primaryTopic <#dataset>.\n"; |
|
94
|
|
|
$rdf.= "<#dataset> a void:Dataset; void:datadump <>;void:triples {$this->tripleCount} ;void:entities {$this->entityCount}] .\n"; |
|
95
|
|
|
$rdf.= "# File **$verb** with {$this->tripleCount} good triples from {$this->entityCount} entities ({$this->unacceptableCount} ignored), {$this->errorCount} errors\n"; |
|
96
|
|
|
|
|
97
|
|
|
return $rdf; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
public function addToTripleCounter( $triplesCount) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->tripleCount += intval($triplesCount); |
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
public function getTripleCount() |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->tripleCount; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function addError($error) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->errors[]= (string) $error; |
|
117
|
|
|
$this->errorCount++; |
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
public function tooManyErrors() |
|
123
|
|
|
{ |
|
124
|
|
|
if( $this->entityCount < 100){ return false; } // if less than 100 entity do not check |
|
125
|
|
|
|
|
126
|
|
|
return ($this->errorCount/$this->entityCount) > $this->profile['resilience']; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
public function acceptable( $rawdata) |
|
131
|
|
|
{ |
|
132
|
|
|
$rawValisdator = $this->profile['rawDataValidationFilter']; |
|
133
|
|
|
|
|
134
|
|
|
if(!($valid = $rawValisdator($rawdata))){ |
|
135
|
|
|
$this->unacceptableCount++; |
|
136
|
|
|
} |
|
137
|
|
|
return $valid; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.