<commit_before><?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class Math_Formula_Runner
{
	private $sources = array();
	private $collected = array();
	private $element;
	private $known = array();
	private $variables = array();

	function __construct( array $sources )
	{
		foreach ($sources as $prefix => $factory) {
			if (empty($factory)) {
				$factory = $this->getPrefixFactory($prefix);
			}

			$this->sources[] = $factory;
		}
	}

	function setFormula( $element )
	{
		$this->element = $this->getElement($element);
		$this->collected = array();
	}

	function setVariables( array $variables )
	{
		$this->variables = $variables;
	}

	function inspect()
	{
		if ( $this->element ) {
			$this->inspectElement($this->element);		
			return $this->collected;
		} else {
			throw new Math_Formula_Runner_Exception(tra('No formula provided.'));
		}
	}

	function evaluate()
	{
		return $this->evaluateData($this->element);
	}

	function evaluateData( $data )
	{
		if ( $data instanceof Math_Formula_Element ) {
			$op = $this->getOperation($data);
			return $op->evaluateTemplate($data, array( $this, 'evaluateData' ));
		} elseif ( is_numeric($data) ) {
			return (double) $data;
		} elseif ( isset($this->variables[$data]) ) {
			return $this->variables[$data];
		} elseif (false !== $value = $this->findVariable(explode('.', $data), $this->variables)) {
			return $value;
		} else {
			throw new Math_Formula_Exception(tr('Variable not found "%0".', $data));
		}
	}

	private function findVariable($path, $variables)
	{
		if (count($path) === 0) {
			return $variables;
		}

		$first = array_shift($path);

		if (isset($variables[$first])) {
			if (count($path == 1)) {
				return $variables[$first];
			} else {
				return $this->findVariable($path, $variables[$first]);
			}
		} else {
			return false;
		}
	}

	private function inspectElement( $element )
	{
		$op = $this->getOperation($element);

		$op->evaluateTemplate($element, array( $this, 'inspectData' ));
	}

	function inspectData( $data )
	{
		if ( $data instanceof Math_Formula_Element ) {<commit_msg>[FIX] Variable not found errors (sorry, refix r47197)<commit_after><?php
// (c) Copyright 2002-2013 by authors of the Tiki Wiki CMS Groupware Project
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$

class Math_Formula_Runner
{
	private $sources = array();
	private $collected = array();
	private $element;
	private $known = array();
	private $variables = array();

	function __construct( array $sources )
	{
		foreach ($sources as $prefix => $factory) {
			if (empty($factory)) {
				$factory = $this->getPrefixFactory($prefix);
			}

			$this->sources[] = $factory;
		}
	}

	function setFormula( $element )
	{
		$this->element = $this->getElement($element);
		$this->collected = array();
	}

	function setVariables( array $variables )
	{
		$this->variables = $variables;
	}

	function inspect()
	{
		if ( $this->element ) {
			$this->inspectElement($this->element);		
			return $this->collected;
		} else {
			throw new Math_Formula_Runner_Exception(tra('No formula provided.'));
		}
	}

	function evaluate()
	{
		return $this->evaluateData($this->element);
	}

	function evaluateData( $data )
	{
		if ( $data instanceof Math_Formula_Element ) {
			$op = $this->getOperation($data);
			return $op->evaluateTemplate($data, array( $this, 'evaluateData' ));
		} elseif ( is_numeric($data) ) {
			return (double) $data;
		} elseif ( isset($this->variables[$data]) ) {
			return $this->variables[$data];
		} elseif (false !== $value = $this->findVariable(explode('.', $data), $this->variables)) {
			return $value;
		} else {
			throw new Math_Formula_Exception(tr('Variable not found "%0".', $data));
		}
	}

	private function findVariable($path, $variables)
	{
		if (count($path) === 0) {
			return $variables;
		}

		$first = array_shift($path);

		if (isset($variables[$first])) {
			if (!count($path)) {
				return $variables[$first];
			} else {
				return $this->findVariable($path, $variables[$first]);
			}
		} else {
			return false;
		}
	}

	private function inspectElement( $element )
	{
		$op = $this->getOperation($element);

		$op->evaluateTemplate($element, array( $this, 'inspectData' ));
	}

	function inspectData( $data )
	{
		if ( $data instanceof Math_Formula_Element ) {
{'fraction_non_alphanumeric': 0.1634865568083261, 'fraction_numerical': 0.005637467476149176, 'mean_word_length': 3.907446808510638, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '6865735', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1484, 'n_words': 368}