示例#1
0
 /**
  * Evaluates two double operands.
  *
  * @param \Tbm\Peval\Types\Double $leftOperand
  * @param \Tbm\Peval\Types\Double $rightOperand
  * @return int|\Tbm\Peval\Types\Double
  * @internal param $leftOperand The left operand being evaluated.
  *            The left operand being evaluated.
  * @internal param $rightOperand The right operand being evaluated.
  *            The right operand being evaluated.
  *
  * @return Double
  */
 public function evaluate2Doubles(Double $leftOperand, Double $rightOperand)
 {
     if ($leftOperand->getValue() < $rightOperand->getValue()) {
         return new Double(1);
     }
     return new Double(0);
 }
示例#2
0
 /**
  * Evaluates two double operands.
  *
  * @param \Tbm\Peval\Types\Double $leftOperand
  * @param \Tbm\Peval\Types\Double $rightOperand
  * @param \Tbm\Peval\Types\Double $rightOperand
  * @return \Tbm\Peval\Types\Double
  * @internal param $leftOperand The left operand being evaluated.
  *            The left operand being evaluated.
  * @internal param $rightOperand The right operand being evaluated.
  *            The right operand being evaluated.
  */
 public function evaluate(Double $leftOperand, Double $rightOperand = null)
 {
     if ($leftOperand->getValue() == (double) 1 && $rightOperand->getValue() == (double) 1) {
         return new Double(1);
     }
     return new Double(0);
 }
示例#3
0
 /**
  * Evaluate one double operand.
  *
  * @param \Tbm\Peval\Types\Double $leftOperand
  * @internal param $operand The operand being evaluated.
  *            The operand being evaluated.
  * @return int
  */
 public function evaluate1Double(Double $leftOperand)
 {
     if ($leftOperand->getValue() == (double) 1) {
         return new Double(0);
     }
     return new Double(1);
 }
示例#4
0
 /**
  * Evaluate one double operand.
  *
  * @param \Tbm\Peval\Types\Double $operand
  * @internal param $operand The operand being evaluated.
  *            The operand being evaluated.
  * @return Double
  */
 protected function evaluate1Double(Double $operand)
 {
     return new Double(-1 * $operand->getValue());
 }
示例#5
0
 public function evaluate(Double $leftOperand, Double $rightOperand = null)
 {
     $rtnValue = $leftOperand->add($rightOperand);
     return $rtnValue;
 }
示例#6
0
 /**
  * Evaluates two double operands.
  *
  * @param \Tbm\Peval\Types\Double $leftOperand
  * @param \Tbm\Peval\Types\Double $rightOperand
  * @internal param $leftOperand The left operand being evaluated.
  *            The left operand being evaluated.
  * @internal param $rightOperand The right operand being evaluated.
  *            The right operand being evaluated.
  *
  * @return Double
  */
 public function evaluate2Doubles(Double $leftOperand, Double $rightOperand)
 {
     $rtnValue = $leftOperand->divide($rightOperand);
     return $rtnValue;
 }
示例#7
0
 /**
  * This method loads the system variables is necessary.
  */
 private function loadSystemVariables()
 {
     // Install the math variables.
     if ($this->loadMathVariables) {
         // Add the two math variables.
         $e = new Double(Math::E);
         $pi = new Double(Math::PI);
         $this->putVariable("E", $e->toString());
         $this->putVariable("PI", $pi->toString());
     }
 }
示例#8
0
 /**
  * Evaluates two double operands.
  *
  * @param \Tbm\Peval\Types\Double $leftOperand
  * @param \Tbm\Peval\Types\Double $rightOperand
  * @internal param $leftOperand The left operand being evaluated.
  *            The left operand being evaluated.
  * @internal param $rightOperand The right operand being evaluated.
  *            The right operand being evaluated.
  * @return Double
  */
 public function evaluate2Doubles(Double $leftOperand, Double $rightOperand)
 {
     $rtnValue = new Double($leftOperand->getValue() % $rightOperand->getValue());
     return $rtnValue;
 }
示例#9
0
 /**
  * Evaluates the operands for this tree using the operator and the unary
  * operator.
  * 
  * @param wrapStringFunctionResults
  *            Indicates if the results from functions that return strings
  *            should be wrapped in quotes. The quote character used will be
  *            whatever is the current quote character for this object.
  * 
  * @exception EvaluateException
  *                Thrown is an error is encountered while processing the
  *                expression.
  *
  * @return String
  */
 public function evaluate($wrapStringFunctionResults)
 {
     $rtnResult = null;
     // Get the left operand.
     $leftResultString = null;
     $leftResultDouble = null;
     if ($this->leftOperand instanceof ExpressionTree) {
         $leftResultString = $this->leftOperand->evaluate($wrapStringFunctionResults);
         try {
             $leftResultDouble = new Double($leftResultString);
             $leftResultString = null;
         } catch (NumberFormatException $exception) {
             $leftResultDouble = null;
         }
     } else {
         if ($this->leftOperand instanceof ExpressionOperand) {
             $leftExpressionOperand = $this->leftOperand;
             $leftResultString = $leftExpressionOperand->getValue();
             $leftResultString = $this->evaluator->replaceVariables($leftResultString);
             // Check if the operand is a string or not. If it is not a string,
             // then it must be a number.
             if (!$this->evaluator->isExpressionString($leftResultString)) {
                 try {
                     $leftResultDouble = new Double($leftResultString);
                     $leftResultString = null;
                 } catch (NumberFormatException $nfe) {
                     throw new EvaluationException("Expression is invalid.", $nfe);
                 }
                 if ($leftExpressionOperand->getUnaryOperator() != null) {
                     $leftResultDouble = new Double($leftExpressionOperand->getUnaryOperator()->evaluate($leftResultDouble));
                 }
             } else {
                 if ($leftExpressionOperand->getUnaryOperator() != null) {
                     throw new EvaluationException("Invalid operand for unary operator.");
                 }
             }
         } else {
             if ($this->leftOperand instanceof ParsedFunction) {
                 $parsedFunction = $this->leftOperand;
                 $function = $parsedFunction->getFunction();
                 $arguments = $parsedFunction->getArguments();
                 $arguments = $this->evaluator . replaceVariables($arguments);
                 if ($this->evaluator->getProcessNestedFunctions()) {
                     $arguments = $this->evaluator->processNestedFunctions($arguments);
                 }
                 try {
                     $functionResult = $function->execute($this->evaluator, $arguments);
                     $leftResultString = $functionResult->getResult();
                     if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_NUMERIC) {
                         $resultDouble = new Double($leftResultString);
                         // Process a unary operator if one exists.
                         if ($parsedFunction->getUnaryOperator() != null) {
                             $resultDouble = new Double($parsedFunction->getUnaryOperator()->evaluate($resultDouble . getValue()));
                         }
                         // Get the final result.
                         $leftResultString = $resultDouble . toString();
                     } else {
                         if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_STRING) {
                             // The result must be a string result.
                             if ($wrapStringFunctionResults) {
                                 $leftResultString = $this->evaluator->getQuoteCharacter() . $leftResultString . $this->evaluator->getQuoteCharacter();
                             }
                             if ($parsedFunction->getUnaryOperator() != null) {
                                 throw new EvaluationException("Invalid operand for unary operator.");
                             }
                         }
                     }
                 } catch (FunctionException $fe) {
                     throw new EvaluationException($fe->getMessage(), $fe);
                 }
                 if (!$this->evaluator->isExpressionString($leftResultString)) {
                     try {
                         $leftResultDouble = new Double($leftResultString);
                         $leftResultString = null;
                     } catch (NumberFormatException $nfe) {
                         throw new EvaluationException("Expression is invalid.", $nfe);
                     }
                 }
             } else {
                 if ($this->leftOperand != null) {
                     throw new EvaluationException("Expression is invalid.");
                 }
             }
         }
     }
     // Get the right operand.
     $rightResultString = null;
     $rightResultDouble = null;
     if ($this->rightOperand instanceof ExpressionTree) {
         $rightResultString = $this->rightOperand->evaluate($wrapStringFunctionResults);
         try {
             $rightResultDouble = new Double($rightResultString);
             $rightResultString = null;
         } catch (NumberFormatException $exception) {
             $rightResultDouble = null;
         }
     } else {
         if ($this->rightOperand instanceof ExpressionOperand) {
             $rightExpressionOperand = $this->rightOperand;
             $rightResultString = $this->rightOperand->getValue();
             $rightResultString = $this->evaluator->replaceVariables($rightResultString);
             // Check if the operand is a string or not. If it not a string,
             // then it must be a number.
             if (!$this->evaluator->isExpressionString($rightResultString)) {
                 try {
                     $rightResultDouble = new Double($rightResultString);
                     $rightResultString = null;
                 } catch (NumberFormatException $nfe) {
                     throw new EvaluationException("Expression is invalid.", $nfe);
                 }
                 if ($rightExpressionOperand->getUnaryOperator() != null) {
                     $rightResultDouble = new Double($rightExpressionOperand->getUnaryOperator()->evaluate($rightResultDouble));
                 }
             } else {
                 if ($rightExpressionOperand->getUnaryOperator() != null) {
                     throw new EvaluationException("Invalid operand for unary operator.");
                 }
             }
         } else {
             if ($this->rightOperand instanceof ParsedFunction) {
                 $parsedFunction = $this->rightOperand;
                 $function = $parsedFunction->getFunction();
                 $arguments = $parsedFunction->getArguments();
                 $arguments = $this->evaluator->replaceVariables($arguments);
                 if ($this->evaluator->getProcessNestedFunctions()) {
                     $arguments = $this->evaluator->processNestedFunctions($arguments);
                 }
                 try {
                     $functionResult = $function->execute($this->evaluator, $arguments);
                     $rightResultString = $functionResult->getResult();
                     if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_NUMERIC) {
                         $resultDouble = new Double($rightResultString);
                         // Process a unary operator if one exists.
                         if ($parsedFunction->getUnaryOperator() != null) {
                             $resultDouble = new Double($parsedFunction->getUnaryOperator()->evaluate($resultDouble->getValue()));
                         }
                         // Get the final result.
                         $rightResultString = $resultDouble->toString();
                     } else {
                         if ($functionResult->getType() == FunctionConstants::FUNCTION_RESULT_TYPE_STRING) {
                             // The result must be a string result.
                             if ($wrapStringFunctionResults) {
                                 $rightResultString = $this->evaluator->getQuoteCharacter() . $rightResultString . $this->evaluator->getQuoteCharacter();
                             }
                             if ($parsedFunction->getUnaryOperator() != null) {
                                 throw new EvaluationException("Invalid operand for unary operator.");
                             }
                         }
                     }
                 } catch (FunctionException $fe) {
                     throw new EvaluationException($fe . getMessage(), $fe);
                 }
                 if (!$this->evaluator->isExpressionString($rightResultString)) {
                     try {
                         $rightResultDouble = new Double($rightResultString);
                         $rightResultString = null;
                     } catch (NumberFormatException $nfe) {
                         throw new EvaluationException("Expression is invalid.", $nfe);
                     }
                 }
             } else {
                 if ($this->rightOperand == null) {
                     // Do nothing.
                 } else {
                     throw new EvaluationException("Expression is invalid.");
                 }
             }
         }
     }
     // Evaluate the the expression.
     if ($leftResultDouble != null && $rightResultDouble != null) {
         $doubleResult = $this->operator->evaluate($leftResultDouble, $rightResultDouble);
         if ($this->getUnaryOperator() != null) {
             $doubleResult = $this->getUnaryOperator()->evaluate($doubleResult);
         }
         $rtnResult = new Double($doubleResult);
         $rtnResult = $rtnResult->toString();
     } else {
         if ($leftResultString != null && $rightResultString != null) {
             $rtnResult = $this->operator->evaluate($leftResultString, $rightResultString);
         } else {
             if ($leftResultDouble != null && $rightResultDouble == null) {
                 $doubleResult = new Double(-1);
                 if ($this->unaryOperator != null) {
                     $doubleResult = $this->unaryOperator->evaluate($leftResultDouble);
                 } else {
                     // Do not allow numeric (left) and
                     // string (right) to be evaluated together.
                     throw new EvaluationException("Expression is invalid.");
                 }
                 $rtnResult = new Double($doubleResult);
                 $rtnResult = $rtnResult->toString();
             } else {
                 throw new EvaluationException("Expression is invalid.");
             }
         }
     }
     return $rtnResult;
 }
示例#10
0
文件: Double.php 项目: ghooning/peval
 public function divide(Double $other)
 {
     // TODO: add tests
     return new Double($this->getValue() / $other->getValue());
 }
示例#11
0
 /**
  * This methods takes a string of input function arguments, evaluates each
  * argument and creates a one String and two Integers value for each
  * argument from the result of the evaluations.
  *
  * @param \Tbm\Peval\Types\String $arguments
  * @param $delimiter
  *            The delimiter to use while parsing.
  *
  * @return \Tbm\Peval\Collection\ArrayList An array list of object values found in the input string.
  *
  * @throws \Tbm\Peval\Func\FunctionException
  *                Thrown if the string does not properly parse into the
  *                proper objects.
  */
 public static function getOneStringAndTwoIntegers(string $arguments, $delimiter)
 {
     $returnValues = new ArrayList();
     try {
         $tokenizer = new ArgumentTokenizer($arguments, $delimiter);
         $tokenCtr = 0;
         while ($tokenizer->hasMoreTokens()) {
             if ($tokenCtr == 0) {
                 $token = $tokenizer->nextToken()->trim();
                 $returnValues . add($token);
             } else {
                 if ($tokenCtr == 1 || $tokenCtr == 2) {
                     $token = $tokenizer->nextToken()->trim();
                     $tokenDouble = new Double($token);
                     $tokenInteger = new Integer($tokenDouble->intValue());
                     $returnValues->add($tokenInteger);
                 } else {
                     throw new FunctionException("Invalid values in string.");
                 }
             }
             $tokenCtr++;
         }
     } catch (Exception $e) {
         throw new FunctionException("Invalid values in string.", $e);
     }
     return $returnValues;
 }