Completed
Push — master ( 52b7dd...834530 )
by Enrico
02:14
created

FactsFactory::getTripleCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace BOTK;
3
4
class FactsFactory implements FactsFactoryInterface {
5
	
6
	protected $profile;
7
	protected $tripleCount =0;
8
	protected $errors = array();
9
	protected $errorCount=0;
10
	protected $unacceptableCount=0;
11
	protected $entityCount=0;
12
	
13
	
14 2
	public function __construct( array $profile )
15
	{
16 2
		assert(!empty($profile['model']) && class_exists('\BOTK\Model\\'.$profile['model']));
17 2
		assert(isset($profile['options']) && is_array($profile['options']));
18
		$defaults = array(
19 2
			'source' 			  	  => null,
20 2
			'resilience' 			  => 0.3,
21
			'datamapper'			  => function($rawdata){return array();},
0 ignored issues
show
Unused Code introduced by
The parameter $rawdata is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
			'rawDataValidationFilter' => function($rawdata){return is_array($rawdata);},	
23
		);
24 2
		$this->profile = array_merge($defaults,$profile);
25 2
	}
26
	
27
	
28
	/**
29
	 * two level filter array
30
	 */
31 2
	public function removeEmpty( array $data )
32
	{
33 2
		$a = array();
34 2
	    foreach ($data as $key => $value) {
35 2
	       $a[$key] = is_array($value)?array_filter($value):$value;
36
	    }
37 2
	    return array_filter($a);
38
	}
39
	
40
	
41 1
	public function factualize( array $rawData )
42
	{
43 1
		$datamapper = $this->profile['datamapper'];
44 1
		$class = '\BOTK\Model\\'.$this->profile['model'];
45 1
		$data =$this->removeEmpty($datamapper($rawData));
46
		
47 1
		$facts = new $class($data,$this->profile['options']);
48 1
		if($facts){
49 1
			$this->entityCount++;
50 1
			$this->tripleCount+=$facts->getTripleCount();
51
		}
52 1
		return $facts;
53
	}
54
	
55
	
56
	public function generateLinkedDataHeader()
57
	{
58
		$class = '\BOTK\Model\\'.$this->profile['model'];
59
		$model = new $class();
60
		return $model->getTurtleHeader(); 
61
	}
62
	
63
	
64
	public function generateLinkedDataFooter()
65
	{
66
		$now = date('c');
67
		$rdf = '<> ';
68
		$this->tripleCount += 6;
69
70
		// add  provenance info
71
		$verb=$this->tooManyErrors()?'invalidated':'generated';
72
		$rdf .= "prov:{$verb}AtTime \"$now\"^^xsd:dateTime;";
73
		if(empty($this->profile['source'])){
74
			$rdf.= "prov:wasDerivedFrom <{$this->profile['source']}>;";	
75
			$this->tripleCount++;
76
		}
77
		
78
		// add dataset info
79
		$rdf.= "foaf:primaryTopic [a void:Dataset; void:datadump <>;void:triples {$this->tripleCount} ;void:entities {$this->entityCount}] ;.";
80
		
81
		// add human readable comment
82
		$rdf.= "\n\n# File **$verb** with {$this->tripleCount} good triples from {$this->entityCount} entities ({$this->unacceptableCount} ignored), {$this->errorCount} errors\n";
83
		
84
		return $rdf;
85
	}
86
	
87
	
88
	public function  addToTripleCounter( $triplesCount)
89
	{
90
		$this->tripleCount += intval($triplesCount);
91
		return $this;
92
	}	
93
	
94
	
95
	public function getTripleCount()
96
	{
97
		return $this->tripleCount;
98
	}
99
	
100
		
101
	public function addError($error)
102
	{
103
		$this->errors[]= (string) $error;
104
		$this->errorCount++;
105
		return $this;
106
	}
107
108
109
	public function tooManyErrors()
110
	{
111
		if( $this->entityCount < 100){ return false; }  // if less than 100 entity do not check
112
		
113
		return ($this->errorCount/$this->entityCount) > $this->profile['resilience'];
114
	}
115
116
117
	public function acceptable( $rawdata)
118
	{
119
		$rawValisdator = $this->profile['rawDataValidationFilter'];
120
		
121
		if(!($valid = $rawValisdator($rawdata))){
122
			$this->unacceptableCount++;
123
		}
124
		return $valid;
125
	}
126
127
}