Commit 5cdfbf21949f0a24c6b10db8e3f2910713745f13

Authored by Furkan
1 parent 536056b1

Last commit - Done.

Showing 2 changed files with 126 additions and 136 deletions   Show diff stats
php/classes/AmdaAction.php
... ... @@ -823,19 +823,6 @@ class AmdaAction
823 823 return array('success' => true ,"result" => $columnNames);
824 824 }
825 825  
826   - function stringToVector($str) {
827   - // Remove parentheses and split the string by commas
828   - $str = trim($str, '()');
829   - return array_map('floatval', explode(',', $str)); // Use floatval for conversion
830   - }
831   -
832   - function vectorToString($vector) {
833   - // Convert each element to a string and join them with commas
834   - $str = implode(',', array_map('strval', $vector));
835   - // Wrap the result in parentheses
836   - return $str;
837   - }
838   -
839 826 public function calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj){
840 827 $cacheMgr = new CatalogCacheMgr(); // Initiating the cache
841 828 $intervals = $cacheMgr->getIntervals(NULL, NULL, NULL, NULL); // Getting all the values
... ... @@ -848,124 +835,8 @@ class AmdaAction
848 835 if(!$resultAdd['success'])
849 836 return array('success' => false , 'message'=>'Error : Cannot add new column.');
850 837  
851   - // Computing columns
852   - $newColumnValues = array();
853   -
854   - switch ($operation) {
855   - case 'plus':
856   - foreach($intervals['intervals'] as $interval){
857   - if ($newColumnObj->size == 1)
858   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] + $interval[$secondColumnId];
859   - else if ($newColumnObj->size > 1){
860   - $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
861   - $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
862   - if (count($fColumnVectorValues) !== count($sColumnVectorValues))
863   - return array('success' => false, 'message' => "Addition not possible");
864   -
865   - $result = [];
866   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
867   - $result[] = $fColumnVectorValues[$i] + $sColumnVectorValues[$i];
868   - $newColumnValues[$interval['cacheId']] = $result;
869   - }
870   - }
871   - break;
872   - case 'minus':
873   - foreach($intervals['intervals'] as $interval){
874   - if ($newColumnObj->size == 1)
875   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] - $interval[$secondColumnId];
876   - else if ($newColumnObj->size > 1){
877   - $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
878   - $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
879   - if (count($fColumnVectorValues) !== count($sColumnVectorValues))
880   - return array('success' => false, 'message' => "Vectors must be of the same length.");
881   -
882   - $result = [];
883   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
884   - $result[] = $fColumnVectorValues[$i] - $sColumnVectorValues[$i];
885   - $newColumnValues[$interval['cacheId']] = $result;
886   - }
887   - else{
888   - return array('success' => false, 'message' => "Substraction not possible");
889   - }
890   - }
891   - break;
892   - case 'multiplication':
893   - foreach($intervals['intervals'] as $interval){
894   - if ($newColumnObj->size == 1)
895   - $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] * $interval[$secondColumnId];
896   - else{
897   - $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
898   - $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
899   - if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
900   - $result = [];
901   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
902   - $result[] = $fColumnVectorValues[$i] * $sColumnVectorValues[$i];
903   - $newColumnValues[$interval['cacheId']] = $result;
904   - }
905   - else if(count($fColumnVectorValues) == 1){
906   - $result = [];
907   - for ($i = 0; $i < count($sColumnVectorValues); $i++)
908   - $result[] = $fColumnVectorValues[0] * $sColumnVectorValues[$i];
909   - $newColumnValues[$interval['cacheId']] = $result;
910   - }
911   - else if(count($sColumnVectorValues) == 1){
912   - $result = [];
913   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
914   - $result[] = $sColumnVectorValues[0] * $fColumnVectorValues[$i];
915   - $newColumnValues[$interval['cacheId']] = $result;
916   - }
917   - else{
918   - return array('success' => false, 'message' => "Multiplication not possible");
919   - }
920   - }
921   - }
922   - break;
923   - case 'division':
924   - foreach($intervals['intervals'] as $interval){
925   - if ($newColumnObj->size == 1){
926   - $newColumnValues[$interval['cacheId']] =($interval[$secondColumnId] == 0) ? NAN : $interval[$firstColumnId] / $interval[$secondColumnId];
927   - }
928   - else{
929   - $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
930   - $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
931   - error_log(print_r($interval[$firstColumnId], true));
932   - error_log(print_r($interval[$secondColumnId], true));
933   - if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
934   - $result = [];
935   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
936   - $result[] = ($sColumnVectorValues[$i] == 0) ? NAN :$fColumnVectorValues[$i] / $sColumnVectorValues[$i];
937   - $newColumnValues[$interval['cacheId']] = $result;
938   - }
939   - else if(count($sColumnVectorValues) == 1){
940   - $result = [];
941   - for ($i = 0; $i < count($fColumnVectorValues); $i++)
942   - $result[] = ($sColumnVectorValues[0] == 0) ? NAN : $fColumnVectorValues[$i] / $sColumnVectorValues[0];
943   - $newColumnValues[$interval['cacheId']] = $result;
944   - }
945   - else{
946   - return array('success' => false, 'message' => "Division not possible");
947   - }
948   - }
949   - }
950   - break;
951   - default:
952   - break;
953   - }
954   - for ($i = 0; $i < count($newColumnValues); $i++)
955   - {
956   - $data= array();
957   - $data[$newColumnObj->id] = (count($newColumnValues[$i]) > 1) ? $this->vectorToString($newColumnValues[$i]) : $newColumnValues[$i];
958   - $valueArray = array(
959   - 'cacheId' => $i,
960   - 'isCatalog' => true,
961   - 'data' => $data
962   - );
963   - if(!$this->modifyCacheInterval((object)$valueArray)){
964   - return array('success' => false, 'message' => "Impossible to modify the column");
965   - }
966   - }
967   - // return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
968   - return $cacheMgr->modifyAllIntervals();
  838 + // Compute and modify the created column
  839 + return $cacheMgr->calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj, $intervals);
969 840 }
970 841 /*
971 842 * $obj = { id: node.id, leaf: node.leaf, nodeType: node.nodeType }
... ...
php/classes/CatalogCacheMgr.php
... ... @@ -185,11 +185,6 @@ class CatalogCacheMgr extends TimeTableCacheMgr
185 185 return $this->cache->getParameterInfo($id);
186 186 }
187 187  
188   - public function modifyAllIntervals(){
189   -
190   - return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
191   - }
192   -
193 188 public function editColumn($id,$name, $type,$size, $description, $status){
194 189 if (!$this->loadFromFile())
195 190 return array('success' => false, 'message' => 'Cannot load cache file');
... ... @@ -199,5 +194,129 @@ class CatalogCacheMgr extends TimeTableCacheMgr
199 194 $this->saveToFile();
200 195 return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
201 196 }
  197 +
  198 + public function stringToVector($str) {
  199 + // Remove parentheses and split the string by commas
  200 + $str = trim($str, '()');
  201 + return array_map('floatval', explode(',', $str)); // Use floatval for conversion
  202 + }
  203 +
  204 + public function vectorToString($vector) {
  205 + // Convert each element to a string and join them with commas
  206 + $str = implode(',', array_map('strval', $vector));
  207 + // Wrap the result in parentheses
  208 + return $str;
  209 + }
  210 + public function calculateAddColumn($firstColumnId, $secondColumnId, $operation, $newColumnObj, $intervals){
  211 + // Computing columns
  212 + $newColumnValues = array();
  213 + foreach($intervals['intervals'] as $interval){
  214 + $fColumnVectorValues = $this->stringToVector($interval[$firstColumnId]);
  215 + $sColumnVectorValues = $this->stringToVector($interval[$secondColumnId]);
  216 +
  217 + if ($newColumnObj->size == 1){
  218 + switch ($operation) {
  219 + case 'plus':
  220 + $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] + $interval[$secondColumnId];
  221 + break;
  222 + case 'minus':
  223 + $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] - $interval[$secondColumnId];
  224 + break;
  225 + case 'multiplication':
  226 + $newColumnValues[$interval['cacheId']] = $interval[$firstColumnId] * $interval[$secondColumnId];
  227 + break;
  228 + case 'division':
  229 + $newColumnValues[$interval['cacheId']] = ($interval[$secondColumnId] == 0) ? NAN : $interval[$firstColumnId] / $interval[$secondColumnId];;
  230 + break;
  231 + }
  232 + }
  233 + else{
  234 + switch ($operation) {
  235 + case 'plus':
  236 + if ($newColumnObj->size > 1){
  237 + if (count($fColumnVectorValues) !== count($sColumnVectorValues))
  238 + return array('success' => false, 'message' => "Addition not possible");
  239 +
  240 + $result = [];
  241 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  242 + $result[] = $fColumnVectorValues[$i] + $sColumnVectorValues[$i];
  243 + $newColumnValues[$interval['cacheId']] = $result;
  244 + }
  245 + break;
  246 + case 'minus':
  247 + if ($newColumnObj->size > 1){
  248 + if (count($fColumnVectorValues) !== count($sColumnVectorValues))
  249 + return array('success' => false, 'message' => "Vectors must be of the same length.");
  250 +
  251 + $result = [];
  252 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  253 + $result[] = $fColumnVectorValues[$i] - $sColumnVectorValues[$i];
  254 + $newColumnValues[$interval['cacheId']] = $result;
  255 + }
  256 + else{
  257 + return array('success' => false, 'message' => "Substraction not possible");
  258 + }
  259 +
  260 + break;
  261 + case 'multiplication':
  262 + if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
  263 + $result = [];
  264 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  265 + $result[] = $fColumnVectorValues[$i] * $sColumnVectorValues[$i];
  266 + $newColumnValues[$interval['cacheId']] = $result;
  267 + }
  268 + else if(count($fColumnVectorValues) == 1){
  269 + $result = [];
  270 + for ($i = 0; $i < count($sColumnVectorValues); $i++)
  271 + $result[] = $fColumnVectorValues[0] * $sColumnVectorValues[$i];
  272 + $newColumnValues[$interval['cacheId']] = $result;
  273 + }
  274 + else if(count($sColumnVectorValues) == 1){
  275 + $result = [];
  276 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  277 + $result[] = $sColumnVectorValues[0] * $fColumnVectorValues[$i];
  278 + $newColumnValues[$interval['cacheId']] = $result;
  279 + }
  280 + else{
  281 + return array('success' => false, 'message' => "Multiplication not possible");
  282 + }
  283 +
  284 + break;
  285 + case 'division':
  286 + if (count($fColumnVectorValues) == count($sColumnVectorValues)) {
  287 + $result = [];
  288 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  289 + $result[] = ($sColumnVectorValues[$i] == 0) ? NAN :$fColumnVectorValues[$i] / $sColumnVectorValues[$i];
  290 + $newColumnValues[$interval['cacheId']] = $result;
  291 + }
  292 + else if(count($sColumnVectorValues) == 1){
  293 + $result = [];
  294 + for ($i = 0; $i < count($fColumnVectorValues); $i++)
  295 + $result[] = ($sColumnVectorValues[0] == 0) ? NAN : $fColumnVectorValues[$i] / $sColumnVectorValues[0];
  296 + $newColumnValues[$interval['cacheId']] = $result;
  297 + }
  298 + else{
  299 + return array('success' => false, 'message' => "Division not possible");
  300 + }
  301 +
  302 +
  303 + break;
  304 + default:
  305 + break;
  306 + }
  307 + }
  308 +
  309 +
  310 +
  311 + }
  312 + for ($i = 0; $i < count($newColumnValues); $i++)
  313 + {
  314 + $data= array();
  315 + $data[$newColumnObj->id] = (count($newColumnValues[$i]) > 1) ? $this->vectorToString($newColumnValues[$i]) : $newColumnValues[$i];
  316 + if(!$this->modifyIntervalFromId($i, $data))
  317 + return array('success' => false, 'message' => "Impossible to modify the column");
  318 + }
  319 + return array('success' => true , 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus(),'parameters' => $this->cache->getParametersInfo());
  320 + }
202 321 }
203 322 ?>
... ...