<commit_before><?php declare(strict_types = 1);
/*
 * Copyright (c) 2019, <NAME>  <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

namespace Smalldb\StateMachine\Definition\Builder;

use Smalldb\StateMachine\Definition\ActionDefinition;
use Smalldb\StateMachine\Definition\DefinitionErrorInterface;
use Smalldb\StateMachine\Definition\PropertyDefinition;
use Smalldb\StateMachine\Definition\StateDefinition;
use Smalldb\StateMachine\Definition\StateMachineDefinition;
use Smalldb\StateMachine\Definition\TransitionDefinition;
use Smalldb\StateMachine\Definition\UndefinedStateException;
use Smalldb\StateMachine\InvalidArgumentException;


class StateMachineDefinitionBuilder extends ExtensiblePlaceholder
{

	/** @var int|null */
	private $mtime;

	/** @var PreprocessorInterface[] */
	private $preprocessorQueue = [];

	/** @var StatePlaceholder[] */
	private $states = [];

	/** @var ActionPlaceholder[] */
	private $actions = [];

	/** @var TransitionPlaceholder[] */
	private $transitions = [];

	/** @var TransitionPlaceholder[][] */
	private $transitionsByState = [];

	/** @var PropertyPlaceholder[] */
	private $properties = [];

	/** @var string */
	private $machineType;

	/** @var DefinitionErrorInterface[] */
	private $errors = [];

	/** @var string|null */
	private $referenceClass = null;

	/** @var string|null */
	private $repositoryClass = null;

	/** @var string|null */
	private $transitionsClass = null;


	public function __construct()
	{
		parent::__construct([]);
	}


	public function addPreprocessor(PreprocessorInterface $preprocessor): void
	{
		$this->preprocessorQueue[] = $preprocessor;
	}


	public function runPreprocessors(): void
	{
		while (($preprocessor = array_shift($this->preprocessorQueue))) {
			$preprocessor->preprocessDefinition($this);
		}
	}


	public function build(): StateMachineDefinition
	{
		$this->runPreprocessors();

		if (empty($this->machineType)) {
			throw new StateMachineBuilderException("Machine type not set.");
		}

		// Check if we have the Not Exists state
		if (!isset($this->states[''])) {
			$this->addState('');
		}

		/** @var StateDefinition[] $states */
		$states = [];
		foreach ($this->states as $statePlaceholder) {
			$state = $this->buildStateDefinition($statePlaceholder);
			$states[$state->getName()] = $state;
		}

		/** @var TransitionDefinition[] $transitions */
		$transitions = [];
		$transitionsByAction = [];
		foreach ($this->transitions as $transitionPlaceholder) {
			$transition = $this->buildTransitionDefinition($transitionPlaceholder, $states);
			$transitions[] = $transition;

			$transitionName = $transition->getName();
			$sourceStateName = $transition->getSourceState()->getName();
			$transitionsByAction[$transitionName][$sourceStateName] = $transition;
		}

		/** @var ActionDefinition[] $actions */
		$actions = [];
		foreach ($this->actions as $actionPlaceholder) {
			$action = $this->buildActionDefinition($actionPlaceholder, $transitionsByAction[$actionPlaceholder->name] ?? []);
			$actions[$action->getName()] = $action;
		}

		/** @var PropertyDefinition[] $properties */
		$properties = [];
		foreach ($this->properties as $propertyPlaceholder) {
			$property = $this->buildPropertyDefinition($propertyPlaceholder);
			$properties[$property->getName()] = $property;
		}

		return new StateMachineDefinition($this->machineType, $this->mtime ?? time(),
			$states, $actions, $transitions, $properties, $this->errors,
			$this->referenceClass, $this->transitionsClass, $this->repositoryClass,
			$this->buildExtensions());
	}

	protected function buildStateDefinition(StatePlaceholder $statePlaceholder): StateDefinition
	{
		return $statePlaceholder->buildStateDefinition();
	}

<commit_msg>StateMachineDefinitionBuilder: Wrap exceptions to add machine type to the errors messages<commit_after><?php declare(strict_types = 1);
/*
 * Copyright (c) 2019, <NAME>  <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

namespace Smalldb\StateMachine\Definition\Builder;

use Smalldb\StateMachine\Definition\ActionDefinition;
use Smalldb\StateMachine\Definition\DefinitionErrorInterface;
use Smalldb\StateMachine\Definition\PropertyDefinition;
use Smalldb\StateMachine\Definition\StateDefinition;
use Smalldb\StateMachine\Definition\StateMachineDefinition;
use Smalldb\StateMachine\Definition\TransitionDefinition;
use Smalldb\StateMachine\Definition\UndefinedStateException;
use Smalldb\StateMachine\InvalidArgumentException;


class StateMachineDefinitionBuilder extends ExtensiblePlaceholder
{

	/** @var int|null */
	private $mtime;

	/** @var PreprocessorInterface[] */
	private $preprocessorQueue = [];

	/** @var StatePlaceholder[] */
	private $states = [];

	/** @var ActionPlaceholder[] */
	private $actions = [];

	/** @var TransitionPlaceholder[] */
	private $transitions = [];

	/** @var TransitionPlaceholder[][] */
	private $transitionsByState = [];

	/** @var PropertyPlaceholder[] */
	private $properties = [];

	/** @var string */
	private $machineType;

	/** @var DefinitionErrorInterface[] */
	private $errors = [];

	/** @var string|null */
	private $referenceClass = null;

	/** @var string|null */
	private $repositoryClass = null;

	/** @var string|null */
	private $transitionsClass = null;


	public function __construct()
	{
		parent::__construct([]);
	}


	public function addPreprocessor(PreprocessorInterface $preprocessor): void
	{
		$this->preprocessorQueue[] = $preprocessor;
	}


	public function runPreprocessors(): void
	{
		while (($preprocessor = array_shift($this->preprocessorQueue))) {
			$preprocessor->preprocessDefinition($this);
		}
	}


	public function build(): StateMachineDefinition
	{
		$this->runPreprocessors();

		if (empty($this->machineType)) {
			throw new StateMachineBuilderException("Machine type not set.");
		}

		// Check if we have the Not Exists state
		if (!isset($this->states[''])) {
			$this->addState('');
		}

		try {
			/** @var StateDefinition[] $states */
			$states = [];
			foreach ($this->states as $statePlaceholder) {
				$state = $this->buildStateDefinition($statePlaceholder);
				$states[$state->getName()] = $state;
			}

			/** @var TransitionDefinition[] $transitions */
			$transitions = [];
			$transitionsByAction = [];
			foreach ($this->transitions as $transitionPlaceholder) {
				$transition = $this->buildTransitionDefinition($transitionPlaceholder, $states);
				$transitions[] = $transition;

				$transitionName = $transition->getName();
				$sourceStateName = $transition->getSourceState()->getName();
				$transitionsByAction[$transitionName][$sourceStateName] = $transition;
			}

			/** @var ActionDefinition[] $actions */
			$actions = [];
			foreach ($this->actions as $actionPlaceholder) {
				$action = $this->buildActionDefinition($actionPlaceholder, $transitionsByAction[$actionPlaceholder->name] ?? []);
				$actions[$action->getName()] = $action;
			}

			/** @var PropertyDefinition[] $properties */
			$properties = [];
			foreach ($this->properties as $propertyPlaceholder) {
				$property = $this->buildPropertyDefinition($propertyPlaceholder);
				$properties[$property->getName()] = $property;
			}

			return new StateMachineDefinition($this->machineType, $this->mtime ?? time(),
				$states, $actions, $transitions, $properties, $this->errors,
				$this->referenceClass, $this->transitionsClass, $this->repositoryClass,
				$this->buildExtensions());
		}
		catch (\Exception $ex) {
			throw new StateMachineBuilderException("Failed to build '$this->machineType' state machine definition: " . $ex->getMessage(), $ex->getCode(), $ex);
		}
	}

	protected function buildStateDefinition(StatePlaceholder $statePlaceholder): StateDefinition
	{
		return $statePlaceholder->buildStateDefinition();
	}


{'fraction_non_alphanumeric': 0.14139110604332952, 'fraction_numerical': 0.0020524515393386543, 'mean_word_length': 4.866889632107023, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1214273', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2408, 'n_words': 586}