CatalogCacheMgr.php
8.56 KB
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
<?php
/**
* @class CatalogCacheMgr
*/
class CatalogCacheMgr extends TimeTableCacheMgr
{
const DEFAULT_PARAM_ID_PREFIX = 'cat_param_id_';
const DEFAULT_NEW_PARAM_ID_PREFIX = 'added_param_id_';
private $isForVisu = FALSE;
function __construct($isForVisu = FALSE) {
$this->objectMgr = new CatalogMgr();
$this->isForVisu = $isForVisu;
}
protected function getCacheFileName() {
if ($this->isForVisu) {
return "cacheVisuCat";
}
return "cacheCat";
}
protected function resetCache($options = array()) {
$this->cache = new CatalogCacheObject();
if (!empty($options['nparams'])) {
for ($i = 0; $i < (int)$options['nparams']; $i++) {
$this->cache->addParameter(CatalogCacheMgr::DEFAULT_PARAM_ID_PREFIX.(string)($i+1), 'column_'.(string)($i+1), 1, 0, "","", FALSE);
}
}
else if (!empty($options['parameters'])) {
$index = 0;
foreach ($options['parameters'] as $parameter) {
if (array_key_exists('id',$parameter) && !empty($parameter['id'])) {
$id = $parameter['id'];
}
else if (array_key_exists('ID',$parameter) && !empty($parameter['ID'])) {
$id = $parameter['ID'];
}
else {
$id = 'cat_param_id_'.$index;
}
$description = "";
if (isset($parameter['description'])) {
$description = $parameter['description'];
}
$status = "";
if (isset($parameter['status'])) {
$status = $parameter['status'];
}
$this->cache->addParameter($id, $parameter['name'], intval($parameter['size']), intval($parameter['type']), $description, $status, FALSE);
++$index;
}
}
return array('parameters' => $this->cache->getParametersInfo());
}
protected function loadAdditionalDescription($options) {
$params_desc = $this->objectMgr->getCatalogParamDescription($options);
if (!$params_desc['success']) {
return array();
}
return array('parameters' => $params_desc['parameters']);
}
public function initFromUploadedFile($name, $format)
{
$result = parent::initFromUploadedFile($name, $format);
if (!$result['success']) {
return $result;
}
$info = $this->objectMgr->getUploadedObject($name, $format,TRUE);
foreach ($info['parameters'] as $parameter) {
$description = "";
if (isset($parameter['description'])) {
$description = $parameter['description'];
}
$status = "";
if (isset($parameter['status'])) {
$status = $parameter[''];
}
$this->cache->addParameter($parameter['id'], $parameter['name'], intval($parameter['size']), intval($parameter['type']), $parameter['description'],$parameter['status'], FALSE);
}
return $result+ array('parameters' => $info['parameters']);
}
public function initFromTimeTable($id, $nbParams, $type)
{
$params = $this->resetCache(array('nparams' => $nbParams));
$ttMgr= new TimeTableMgr();
$intervals_res = $ttMgr->loadIntervalsFromObject($id, $type);
if (!$intervals_res['success'])
return $intervals_res;
foreach ($intervals_res['intervals'] as $interval)
{
//Add interval
$this->cache->addInterval($interval);
}
unset($intervals_res);
//Update cache
$this->cache->updateIndexes();
//Save cache file
return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(), 'parameters' => $this->cache->getParametersInfo());
}
public function getIntervalsForChart() {
$result = $this->getIntervals(NULL, NULL, NULL, NULL);
if (!$result['success']) {
return FALSE;
}
$parameters = $this->cache->getParametersInfo();
$parameters_to_split = array();
$parameters_chart = array();
foreach ($parameters as $parameter) {
if ($parameter['size'] > 1) {
$parameters_to_split[] = $parameter;
for ($i = 0; $i < $parameter['size']; ++$i) {
$comp_param = $parameter;
$comp_param['id'] = $parameter['id'].'_COMPONENT_'.$i;
$comp_param['name'] = $parameter['name'].'['.$i.']';
unset ($comp_param['size']);
$parameters_chart[] = $comp_param;
}
}
else {
$comp_param = $parameter;
unset ($comp_param['size']);
$parameters_chart[] = $comp_param;
}
}
if (empty($parameters_to_split)) {
return $result + array('parameters' => $parameters_chart);
}
//split parameters
foreach ($result[intervals] as &$interval) {
foreach ($parameters_to_split as $parameter) {
$values = explode (',',$interval[$parameter['id']]);
unset($interval[$parameter['id']]);
foreach ($values as $key => $value) {
$interval[$parameter['id'].'_COMPONENT_'.$key] = $value;
}
}
}
return $result + array('parameters' => $parameters_chart);
}
public function deleteColumn($id){
if (!$this->loadFromFile())
return array('success' => false, 'message' => 'Cannot load cache file');
$result = $this->cache->deleteParameter($id);
if ($result){
$this->saveToFile();
}
return array('success' => $result , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
}
public function addColumn($id,$name, $type,$size, $description, $status){
if (!$this->loadFromFile())
return array('success' => false, 'message' => 'Cannot load cache file');
$isNew=true;
$this->cache->addParameter($id,$name, $size, $type, $description, $status, $isNew);
$this->saveToFile();
return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
}
public function getCatColumnInfo($id){
if (!$this->loadFromFile())
return array('success' => false, 'message' => 'Cannot load cache file');
return $this->cache->getParameterInfo($id);
}
public function editColumn($id,$name, $type,$size, $description, $status){
if (!$this->loadFromFile())
return array('success' => false, 'message' => 'Cannot load cache file');
$this->cache->editParameter($id,$name, $type, $size, $description, $status);
$this->saveToFile();
return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
}
public function stringToVector($str) {
// Remove parentheses and split the string by commas
$str = trim($str, '()');
return array_map('floatval', explode(',', $str)); // Use floatval for conversion
}
public function vectorToString($vector) {
// Convert each element to a string and join them with commas
$str = implode(',', array_map('strval', $vector));
// Wrap the result in parentheses
return $str;
}
public function calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj, $intervals) {
// Computing columns
$newColumnValues = array();
$isSingleValue = ($newColumnObj->size == 1);
foreach ($intervals['intervals'] as $interval) {
$fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
$sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
if ($isSingleValue) {
$newColumnValues[$interval['cacheId']] = $this->performOperation($interval[$firstColumnId], $interval[$secondColumnId], $operation);
} else {
$fCount = count($fColumnVectorValues);
$sCount = count($sColumnVectorValues);
if ($fCount !== $sCount) {
return array('success' => false, 'message' => "Vectors must be of the same length.");
}
$result = array();
for ($i = 0; $i < $fCount; $i++) {
$result[] = $this->performOperation($fColumnVectorValues[$i], $sColumnVectorValues[$i], $operation);
}
$newColumnValues[$interval['cacheId']] = $result;
}
}
$data = array();
foreach ($newColumnValues as $cacheId => $value) {
$data[$cacheId] = array(
$newColumnObj->id => (is_array($value)) ? $this->vectorToString($value) : $value
);
}
if (!$this->modifyIntervals($data)) {
return array('success' => false, 'message' => "Impossible to modify the columns");
}
return array('success' => true, 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(), 'parameters' => $this->cache->getParametersInfo());
}
private function performOperation($a, $b, $operation) {
switch ($operation) {
case 'plus':
return $a + $b;
case 'minus':
return $a - $b;
case 'multiplication':
return $a * $b;
case 'division':
return ($b == 0) ? NAN : $a / $b;
default:
return null;
}
}
}
?>