/**
  * Akce pro přiřazení rule setu ke zvolenému mineru
  * @param int $miner
  * @param int $ruleSet
  * @throws BadRequestException
  * @throws ForbiddenRequestException
  */
 public function actionSetRuleSet($miner, $ruleSet)
 {
     $miner = $this->findMinerWithCheckAccess($miner);
     $ruleSet = $this->ruleSetsFacade->findRuleSet($ruleSet);
     $miner->ruleSet = $ruleSet;
     $this->minersFacade->saveMiner($miner);
     $this->sendJsonResponse(['state' => 'ok', 'miner' => $miner->minerId, 'ruleset_id' => $ruleSet->ruleSetId]);
 }
 /**
  * Akce pro odebrání celého obsahu rule clipboard z konkrétního rulesetu
  * @param int $id
  * @param int $miner
  * @param int $ruleset
  * @param string $returnRules ='' - IDčka oddělená čárkami, případně jedno ID
  * @throws ForbiddenRequestException
  * @throws BadRequestException
  */
 public function actionRemoveRulesFromRuleSet($id = null, $miner, $ruleset, $returnRules = '')
 {
     //načtení dané úlohy a zkontrolování přístupu k mineru
     $task = $this->tasksFacade->findTask($id);
     $this->checkMinerAccess($task->miner);
     $ruleIdsArr = explode(',', str_replace(';', ',', $returnRules));
     //najití RuleSetu a kontroly
     $ruleSet = $this->ruleSetsFacade->findRuleSet($ruleset);
     $this->ruleSetsFacade->checkRuleSetAccess($ruleSet, $this->user->id);
     //přidání pravidel
     $this->ruleSetsFacade->removeAllRuleClipboardRulesFromRuleSet($task, $ruleSet);
     $result = array();
     if (count($ruleIdsArr) > 0) {
         foreach ($ruleIdsArr as $ruleId) {
             try {
                 $rule = $this->rulesFacade->findRule($ruleId);
                 //TODO optimalizovat kontroly...
                 $ruleTask = $rule->task;
                 if ($ruleTask->taskId != $task->taskId) {
                     throw new InvalidArgumentException();
                 }
                 if ($ruleTask->miner->minerId != $miner) {
                     throw new InvalidArgumentException();
                 }
                 $result[$rule->ruleId] = $rule->getBasicDataArr();
             } catch (\Exception $e) {
                 continue;
             }
         }
     }
     $this->sendJsonResponse(array('rules' => $result));
 }
 /**
  * Akce pro odebrání všech pravidel z rulesetu
  * @param int $id
  */
 public function actionRemoveAllRules($id)
 {
     //najití RuleSetu a kontroly
     $ruleSet = $this->ruleSetsFacade->findRuleSet($id);
     $this->ruleSetsFacade->checkRuleSetAccess($ruleSet, $this->user->id);
     //smazání
     $this->ruleSetsFacade->removeAllRulesFromRuleSet($ruleSet);
     $this->sendJsonResponse(['state' => 'ok']);
 }
 /**
  * Funkce pro nalezení rule setu dle zadaného ID a kontrolu oprávnění aktuálního uživatele pracovat s daným rule setem
  * @param int $ruleSetId
  * @return RuleSet
  * @throws \Nette\Application\BadRequestException
  */
 private function findRuleSetWithCheckAccess($ruleSetId)
 {
     try {
         /** @var RuleSet $ruleSet */
         $ruleSet = $this->ruleSetsFacade->findRuleSet($ruleSetId);
     } catch (EntityNotFoundException $e) {
         $this->error('Requested rule set was not found.');
         return null;
     }
     //kontrola možnosti pracovat s daným rule setem
     $this->ruleSetsFacade->checkRuleSetAccess($ruleSet, $this->getCurrentUser()->userId);
     return $ruleSet;
 }
 /**
  * Akce pro vyhodnocení klasifikace
  * @SWG\Get(
  *   tags={"Evaluation"},
  *   path="/evaluation/classification",
  *   summary="Evaluate classification model",
  *   produces={"application/json","application/xml"},
  *   security={{"apiKey":{}},{"apiKeyHeader":{}}},
  *   @SWG\Parameter(
  *     name="scorer",
  *     description="Scorer type",
  *     required=true,
  *     type="string",
  *     in="query",
  *     enum={"easyMinerScorer","modelTester"}
  *   ),
  *   @SWG\Parameter(
  *     name="task",
  *     description="Task ID",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="ruleSet",
  *     description="Rule set ID",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Parameter(
  *     name="datasource",
  *     description="Datasource ID (if not specified, task datasource will be used)",
  *     required=false,
  *     type="integer",
  *     in="query"
  *   ),
  *   @SWG\Response(
  *     response=200,
  *     description="Evaluation result",
  *     @SWG\Schema(
  *       ref="#/definitions/ScoringResultResponse"
  *     )
  *   ),
  *   @SWG\Response(
  *     response=400,
  *     description="Invalid API key supplied",
  *     @SWG\Schema(ref="#/definitions/StatusResponse")
  *   )
  * )
  *
  * @throws BadRequestException
  * @throws NotImplementedException
  */
 public function actionClassification()
 {
     $this->setXmlMapperElements('classification');
     $inputData = $this->getInput()->getData();
     /** @var IScorerDriver $scorerDriver */
     if (empty($inputData['scorer'])) {
         $scorerDriver = $this->scorerDriverFactory->getDefaultScorerInstance();
     } else {
         $scorerDriver = $this->scorerDriverFactory->getScorerInstance($inputData['scorer']);
     }
     if (!empty($inputData['datasource'])) {
         try {
             $datasource = $this->datasourcesFacade->findDatasource(@$inputData['datasource']);
             if (!$this->datasourcesFacade->checkDatasourceAccess($datasource, $this->getCurrentUser())) {
                 throw new \Exception();
             }
         } catch (\Exception $e) {
             throw new BadRequestException("Requested data source was not found!");
         }
     } elseif (!empty($inputData['task'])) {
         $task = $this->findTaskWithCheckAccess($inputData['task']);
         $datasource = $task->miner->datasource;
     } else {
         throw new BadRequestException("Data source was not specified!");
     }
     if (!empty($inputData['task'])) {
         if (empty($task)) {
             $task = $this->findTaskWithCheckAccess($inputData['task']);
         }
         $result = $scorerDriver->evaluateTask($task, $datasource)->getCorrectIncorrectDataArr();
         $result['task'] = $task->getDataArr(false);
     } elseif (!empty($inputData['ruleSet'])) {
         $ruleSet = $this->ruleSetsFacade->findRuleSet($inputData['ruleSet']);
         //TODO kontrola oprávnění k rule setu
         $result = $scorerDriver->evaluateRuleSet($ruleSet, $datasource)->getDataArr();
         $result['ruleSet'] = $ruleSet->getDataArr();
     } else {
         throw new BadRequestException("No task or rule set found!");
     }
     $this->resource = $result;
     $this->sendResource();
 }
 /**
  * Funkce pro kontrolu vstupů pro vytvoření nového mineru
  */
 public function validateCreate()
 {
     $currentUser = $this->getCurrentUser();
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('name')->addRule(IValidator::REQUIRED, 'Name is required!');
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('type')->addRule(IValidator::REQUIRED, 'Miner type is required!');
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('datasourceId')->addRule(IValidator::REQUIRED, 'Datasource ID is required!')->addRule(IValidator::CALLBACK, 'Requested datasource was not found, or is not accessible!', function ($value) use($currentUser) {
         try {
             $this->datasource = $this->datasourcesFacade->findDatasource($value);
             if ($this->datasourcesFacade->checkDatasourceAccess($this->datasource, $currentUser)) {
                 //kontrola dostupnosti kompatibilního mineru s vybraným datasource
                 $availableMinerTypes = $this->minersFacade->getAvailableMinerTypes($this->datasource->type);
                 return !empty($availableMinerTypes[$this->getInput()->getData()['type']]);
             } else {
                 return false;
             }
         } catch (\Exception $e) {
             return false;
         }
     });
     /** @noinspection PhpMethodParametersCountMismatchInspection */
     $this->input->field('ruleSetId')->addRule(IValidator::CALLBACK, 'Requested rule set was not found, or is not accessible!', function ($value) use($currentUser) {
         if (empty($value)) {
             return true;
         }
         try {
             $this->ruleSet = $this->ruleSetsFacade->findRuleSet($value);
             $this->ruleSetsFacade->checkRuleSetAccess($this->ruleSet, $currentUser);
             return true;
         } catch (\Exception $e) {
             return false;
         }
     });
 }