This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dog.php
325 lines (269 loc) · 8.77 KB
/
Dog.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
require_once "Logger.php";
class Dog implements \JsonSerializable {
private $id;
private $name;
private $sex;
private $uuid;
private $father;
private $mother;
private $tree;
private $wrightIndex;
private $logger;
public function __construct($id, $name, $sex, $uuid, Dog $father = NULL, Dog $mother = NULL, $wrightIndex = NULL) {
$this->id = $id;
$this->name = $name;
$this->sex = $sex;
$this->uuid = $uuid;
if( ($father instanceof Dog) && ($father->getSex() == Sex::MALE)) {
$this->father = $father;
}
if( ($mother instanceof Dog) && ($mother->getSex() == Sex::FEMALE)) {
$this->mother = $mother;
}
$this->wrightIndex = $wrightIndex;
$time = date('d-M-Y-H_i_s');
//Using timestamp in file name, e.g. log-02-06-2016.txt
$this->logger = new Logger('log-' . $time . '.txt');
$this->logger->setLevel(Logger::DEBUG);
$this->logger->debug("new Dog(" . $id . ",'" . $name . "', '" . $sex . "', '" . $uuid
. "', "
. ($father==NULL?"NULL":"new Dog(" . $father->getId() .",'" . $father->getName() . "','" . $father->getSex() . "','" . $father->getUuid() . "')") . " , "
. ($mother==NULL?"NULL":"new Dog(" . $mother->getId() .",'" . $mother->getName() . "','" . $mother->getSex() . "','" . $mother->getUuid() . "')") . " , "
. ($wrightIndex==NULL?"NULL":$wrightIndex) . ");");
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getSex()
{
return $this->sex;
}
/**
* @param mixed $sex
*/
public function setSex($sex)
{
$this->sex = $sex;
}
public function isMale() {
return $this->sex == Sex::MALE;
}
/**
* @return mixed
*/
public function getUuid()
{
return $this->uuid;
}
/**
* @param mixed $uuid
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;
}
/**
* @return Dog
*/
public function getFather()
{
return $this->father;
}
/**
* @param Dog $father
*/
public function setFather(Dog $father)
{
$this->father = $father;
}
/**
* @return Dog
*/
public function getMother()
{
return $this->mother;
}
/**
* @param Dog $mother
*/
public function setMother(Dog $mother)
{
$this->mother = $mother;
}
/**
* @return mixed
*/
public function getWrightIndex()
{
return $this->wrightIndex;
}
/**
* @param mixed $wrightIndex
*/
public function setWrightIndex($wrightIndex)
{
$this->wrightIndex = $wrightIndex;
}
/**
* @return mixed
*/
public function getTree()
{
return $this->tree;
}
/**
* @param mixed $tree
*/
public function setTree($tree)
{
$this->tree = $tree;
}
public function jsonSerialize() {
$vars = get_object_vars($this);
return $vars;
}
public function equals(\Dog $subject) {
return $this->getId() == $subject->getId();
}
public function __toString() {
return serialize($this);
}
public function walk($arrIndex = "", Dog $ancestor = NULL)
{
if ($this->tree == null) {
$this->tree = [];
}
if($ancestor != null) {
if($ancestor->getFather() != null) {
// f - male , father
$this->walk($arrIndex."f", $ancestor->getFather());
$this->tree[$arrIndex."f"] = $ancestor->getFather();
}
if($ancestor->getMother() != null) {
// m - female , mother
$this->walk($arrIndex."m", $ancestor->getMother());
$this->tree[$arrIndex."m"] = $ancestor->getMother();
}
}
}
public function calculateWrightIndex($debug = false) {
$this->logger->debug("#calculateWrightIndex(): Entering method");
$this->wrightIndex = 0;
$ancestors = array();
$duplicates = array();
$treeDuplicates = array();
$commonAncestorsCodes = array();
$wrightSummationRelevantDuplicates = array();
$wrightIndividualRelevantDuplicates = array();
$treeDupesCount = 0;
foreach ($this->tree as $key => $val) {
if(isset($ancestors[$val->getId()])) {
$duplicates[$val->getId()]=$val;
foreach ($this->tree as $treeKey => $treeItem) {
if($treeItem->getId() == $val->getId()) {
$treeDuplicates[$treeDupesCount][$treeKey] = $treeItem;
$commonAncestorsCodes[] = $treeKey;
}
}
$treeDupesCount++;
}
$ancestors[$val->getId()]=$val;
}
if(sizeof($duplicates) > 0) {
foreach ($treeDuplicates as $key => $val) {
$treelevels = array_keys($val);
foreach($treelevels as $treeKey) {
if (($this->tree[$treeKey])->getWrightIndex() == null) {
($this->tree[$treeKey])->walk("", $this->tree[$treeKey]);
($this->tree[$treeKey])->calculateWrightIndex();
}
}
$treelevels = Dog::isolateSummationTree($treelevels, $commonAncestorsCodes);
if($treelevels != null) {
$wrightSummationRelevantDuplicates[] = $treelevels;
} else {
$wrightIndividualRelevantDuplicates[] = array_keys($val);
}
}
foreach ($wrightSummationRelevantDuplicates as $xxDuplicate) {
foreach($xxDuplicate as $xDuplicate) {
$this->logger->info("Wright Index sum element: '" . $xDuplicate[0] . "'(" . $this->tree[$xDuplicate[0]]->name . ") and '" . $xDuplicate[1] . "'(" . $this->tree[$xDuplicate[1]]->name . ") with it's own WrightIndex: " . ($this->tree[$xDuplicate[0]])->getWrightIndex());
$additionToSum = pow(0.5, (strlen($xDuplicate[0]) - 1) + (strlen($xDuplicate[1]) - 1) + 1) * (1 + ($this->tree[$xDuplicate[0]])->getWrightIndex());
$this->logger->info("adding 0.5 ^ (" . (strlen($xDuplicate[0]) - 1) . " + " . (strlen($xDuplicate[1]) - 1) . " +1) * (1 + " . ($this->tree[$xDuplicate[0]])->getWrightIndex() . ")");
$this->wrightIndex += $additionToSum;
}
}
} else {
$this->wrightIndex = 0;
}
if($debug) {
echo json_encode(
array(
"this" => $this,
"ancestors" => $ancestors,
"duplicates" => $duplicates,
"treeDuplicates" => $treeDuplicates,
"commonAncestorsCodes" => $commonAncestorsCodes,
"wrightSummationRelevantDuplicates" => $wrightSummationRelevantDuplicates,
"wrightIndividualRelevantDuplicates" => $wrightIndividualRelevantDuplicates
)
);
}
$this->logger->debug("#calculateWrightIndex(): Leaving method for Dog " . json_encode($this));
}
private static function isolateSummationTree($treelevels = array(), $commonAncestorsCodes = array()) {
asort($commonAncestorsCodes);
$fatherLine = array();
$motherLine = array();
foreach ($treelevels as $val) {
if(!in_array(substr($val,0 , strlen($val)-1),$commonAncestorsCodes)) {
if(substr($val,0,1) == "f") {
$fatherLine[] = $val;
asort($fatherLine);
} else {
$motherLine[] = $val;
asort($motherLine);
}
}
}
if (sizeof($fatherLine) == 0 || sizeof($motherLine) == 0) {
return null;
}
$resultArray = array();
foreach ($fatherLine as $father) {
foreach ($motherLine as $mother) {
$resultArray[$father.".".$mother] = array($father, $mother);
}
}
return $resultArray;
}
}