<commit_before><?php

namespace Inowas\ModflowBundle\Service;

use Doctrine\ORM\EntityManager;
use Inowas\Flopy\Model\Factory\CalculationPropertiesFactory;
use Inowas\ModflowBundle\Exception\InvalidArgumentException;
use Inowas\ModflowBundle\Model\Calculation;
use Inowas\ModflowBundle\Model\CalculationFactory;
use Inowas\ModflowBundle\Model\ModflowModel;
use Inowas\ScenarioAnalysisBundle\Model\Scenario;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpKernel\KernelInterface;

class CalculationManager
{
    /**
     * @var EntityManager $entityManager
     */
    private $entityManager;

    /**
     * @var KernelInterface $kernel
     */
    private $kernel;

    /**
     * CalculationManager constructor.
     * @param EntityManager $entityManager
     * @param KernelInterface $kernel
     */
    public function __construct(EntityManager $entityManager, KernelInterface $kernel){
        $this->entityManager = $entityManager;
        $this->kernel = $kernel;
    }

    /**
     * @param ModflowModel $model
     * @return Calculation
     */
    public function createFromModel(ModflowModel $model)
    {
        $type = 'modflow';
        $calculationProperties = CalculationPropertiesFactory::loadFromApiRunAndSubmit($model);
        $calculation = CalculationFactory::create($calculationProperties, $model);
        $calculation->setModelId($model->getId());

        $baseUrl = $this->kernel->getContainer()->getParameter('inowas.api_base_url');
        $port = $this->kernel->getContainer()->getParameter('inowas.api_port');
        $calculationId = $calculation->getId();
        $modelId = $calculation->getModelId();
        $dataFolder = sprintf('%s/%s', $this->kernel->getContainer()->getParameter('inowas.modflow.data_folder'), $modelId);
        $calculationUrl = sprintf('%s:%d/api/%s/calculation/%s/packages.json', $baseUrl, $port, $type, $calculationId);
        $modelUrl = sprintf('%s:%d/api/%s/calculation/%s/packages/packageName.json', $baseUrl, $port, $type, $modelId);
        $submitHeadsUrl = sprintf('%s:%d/api/%s/calculation/%s/heads.json', $baseUrl, $port, $type, $modelId);

        $calculation->setDataFolder($dataFolder);
        $calculation->setCalculationUrl($calculationUrl);
        $calculation->setModelUrl($modelUrl);
        $calculation->setSubmitHeadsUrl($submitHeadsUrl);
        $calculation->setApiKey($this->kernel->getContainer()->get('inowas.modflow.toolmanager')->findApiKeyByModelId($modelId));

        return $calculation;
    }

    /**
     * @param Scenario $scenario
     * @return Calculation
     */
    public function createFromScenario(Scenario $scenario)
    {
        $baseModelId = $scenario->getBaseModelId();
        $model = $this->kernel->getContainer()->get('inowas.modflow.toolmanager')->findModelById($baseModelId);
        if (! $model instanceof ModflowModel){
            throw new InvalidArgumentException(sprintf('Model with Id=%s not available.', $baseModelId));
        }
        $type = 'scenarioanalysis';
        $model = $scenario->applyTo($model);
        $calculationProperties = CalculationPropertiesFactory::loadFromApiRunAndSubmit($model);
        $calculation = CalculationFactory::create($calculationProperties, $model);

        $scenarioId = $scenario->getId();
        $calculation->setModelId($scenarioId);
        $baseUrl = $this->kernel->getContainer()->getParameter('inowas.api_base_url');
        $port = $this->kernel->getContainer()->getParameter('inowas.api_port');
        $calculationId = $calculation->getId();

        $dataFolder = sprintf('%s/%s', $this->kernel->getContainer()->getParameter('inowas.modflow.data_folder'), $scenarioId);
        $calculationUrl = sprintf('%s:%d/api/%s/calculation/%s/packages.json', $baseUrl, $port, $type, $calculationId);
        $modelUrl = sprintf('%s:%d/api/%s/calculation/%s/packages/packageName.json', $baseUrl, $port, $type, $scenarioId);
        $submitHeadsUrl = sprintf('%s:%d/api/%s/calculation/%s/heads.json', $baseUrl, $port, $type, $scenario);

        $calculation->setDataFolder($dataFolder);
        $calculation->setCalculationUrl($calculationUrl);
        $calculation->setModelUrl($modelUrl);
        $calculation->setSubmitHeadsUrl($submitHeadsUrl);
        $calculation->setApiKey($this->kernel->getContainer()->get('inowas.scenarioanalysis.scenarioanalysismanager')->findApiKeyByScenarioId($scenarioId));<commit_msg>BugFix<commit_after><?php

namespace Inowas\ModflowBundle\Service;

use Doctrine\ORM\EntityManager;
use Inowas\Flopy\Model\Factory\CalculationPropertiesFactory;
use Inowas\ModflowBundle\Exception\InvalidArgumentException;
use Inowas\ModflowBundle\Model\Calculation;
use Inowas\ModflowBundle\Model\CalculationFactory;
use Inowas\ModflowBundle\Model\ModflowModel;
use Inowas\ScenarioAnalysisBundle\Model\Scenario;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpKernel\KernelInterface;

class CalculationManager
{
    /**
     * @var EntityManager $entityManager
     */
    private $entityManager;

    /**
     * @var KernelInterface $kernel
     */
    private $kernel;

    /**
     * CalculationManager constructor.
     * @param EntityManager $entityManager
     * @param KernelInterface $kernel
     */
    public function __construct(EntityManager $entityManager, KernelInterface $kernel){
        $this->entityManager = $entityManager;
        $this->kernel = $kernel;
    }

    /**
     * @param ModflowModel $model
     * @return Calculation
     */
    public function createFromModel(ModflowModel $model)
    {
        $type = 'modflow';
        $calculationProperties = CalculationPropertiesFactory::loadFromApiRunAndSubmit($model);
        $calculation = CalculationFactory::create($calculationProperties, $model);
        $calculation->setModelId($model->getId());

        $baseUrl = $this->kernel->getContainer()->getParameter('inowas.api_base_url');
        $port = $this->kernel->getContainer()->getParameter('inowas.api_port');
        $calculationId = $calculation->getId();
        $modelId = $calculation->getModelId();
        $dataFolder = sprintf('%s/%s', $this->kernel->getContainer()->getParameter('inowas.modflow.data_folder'), $modelId);
        $calculationUrl = sprintf('%s:%d/api/%s/calculation/%s/packages.json', $baseUrl, $port, $type, $calculationId);
        $modelUrl = sprintf('%s:%d/api/%s/calculation/%s/packages/packageName.json', $baseUrl, $port, $type, $modelId);
        $submitHeadsUrl = sprintf('%s:%d/api/%s/calculation/%s/heads.json', $baseUrl, $port, $type, $modelId);

        $calculation->setDataFolder($dataFolder);
        $calculation->setCalculationUrl($calculationUrl);
        $calculation->setModelUrl($modelUrl);
        $calculation->setSubmitHeadsUrl($submitHeadsUrl);
        $calculation->setApiKey($this->kernel->getContainer()->get('inowas.modflow.toolmanager')->findApiKeyByModelId($modelId));

        return $calculation;
    }

    /**
     * @param Scenario $scenario
     * @return Calculation
     */
    public function createFromScenario(Scenario $scenario)
    {
        $baseModelId = $scenario->getBaseModelId();
        $model = $this->kernel->getContainer()->get('inowas.modflow.toolmanager')->findModelById($baseModelId);
        if (! $model instanceof ModflowModel){
            throw new InvalidArgumentException(sprintf('Model with Id=%s not available.', $baseModelId));
        }
        $type = 'scenarioanalysis';
        $model = $scenario->applyTo($model);
        $calculationProperties = CalculationPropertiesFactory::loadFromApiRunAndSubmit($model);
        $calculation = CalculationFactory::create($calculationProperties, $model);

        $scenarioId = $scenario->getId();
        $calculation->setModelId($scenarioId);
        $baseUrl = $this->kernel->getContainer()->getParameter('inowas.api_base_url');
        $port = $this->kernel->getContainer()->getParameter('inowas.api_port');
        $calculationId = $calculation->getId();

        $dataFolder = sprintf('%s/%s', $this->kernel->getContainer()->getParameter('inowas.modflow.data_folder'), $scenarioId);
        $calculationUrl = sprintf('%s:%d/api/%s/calculation/%s/packages.json', $baseUrl, $port, $type, $calculationId);
        $modelUrl = sprintf('%s:%d/api/%s/calculation/%s/packages/packageName.json', $baseUrl, $port, $type, $scenarioId);
        $submitHeadsUrl = sprintf('%s:%d/api/%s/calculation/%s/heads.json', $baseUrl, $port, $type, $scenarioId);

        $calculation->setDataFolder($dataFolder);
        $calculation->setCalculationUrl($calculationUrl);
        $calculation->setModelUrl($modelUrl);
        $calculation->setSubmitHeadsUrl($submitHeadsUrl);
        $calculation->setApiKey($this->kernel->getContainer()->get('inowas.scenarioanalysis.scenarioanalysismanager')->findApiKeyByScenarioId($scenarioId));
{'fraction_non_alphanumeric': 0.15286479097847136, 'fraction_numerical': 0.0, 'mean_word_length': 4.107620709714951, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5932744', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2610, 'n_words': 349}