Пример #1
0
 /**
  * @dataProvider syntaxErrorArithmeticProvider
  *
  * @param $expression string
  * @param $description string
  */
 public function test_syntaxErrorArithmetic_EvaluateAst_ThrowsException($expression, $description)
 {
     $parser = new Parser();
     $evaluator = new AstEvaluator();
     $evaluator->setVariableEvaluatorCallback(function ($options) {
         return $options['val'];
     });
     try {
         $parser->parse($expression);
     } catch (Exception $e) {
         return;
     }
     $this->fail('Expected exception, but parsing succeeded for test: ' . $description);
 }
Пример #2
0
 public function createFormulaAction($formula, $nid, DateTime $start, DateTime $end, $resolution, $padding)
 {
     $parser = new Parser();
     $ast = $parser->parse($formula);
     $astEvaluator = new AstEvaluator();
     $astEvaluator->setPaddingValue($padding);
     $astEvaluator->setVariableEvaluatorCallback(function ($options) use($nid, $start, $end, $resolution, $padding) {
         if (!isset($options['sid'])) {
             throw new Exception('sid was not specified in variable. Need this to determine which sensor to get for the calculation of the formula.');
         }
         if (!isset($options['agg'])) {
             throw new Exception('agg was not specified in variable. Need this in order to aggregate up to the correct resolution before calculating formula.');
         }
         $pipeline = new Pipeline();
         return $pipeline->run([new Find($this->conn, $options['sid'], $nid, $start, $end), new RollupTime($resolution, $options['agg'], $padding), new Pad($resolution, $start, $end, $padding)])->vals;
     });
     return new Formula($ast, $astEvaluator);
 }
Пример #3
0
 public function test_AddTwoSeriesAndAConstant_FormulaAction_RetrieveSum()
 {
     $formula = '[sid=0] + [sid=1] + 5';
     $vals1 = [(new DateTime('2015-12-05 00:00:00'))->getTimestamp() => 100, (new DateTime('2015-12-05 01:00:00'))->getTimestamp() => 200];
     $vals2 = [(new DateTime('2015-12-05 00:00:00'))->getTimestamp() => 1000, (new DateTime('2015-12-05 01:00:00'))->getTimestamp() => 2000];
     $expected = ['2015-12-05 00:00:00' => 1105, '2015-12-05 01:00:00' => 2205];
     $seriesList = [];
     $seriesList[] = new Series($vals1);
     $seriesList[] = new Series($vals2);
     $parser = new Parser();
     $ast = $parser->parse($formula);
     $astEvaluator = new AstEvaluator();
     $astEvaluator->setPaddingValue(false);
     $astEvaluator->setVariableEvaluatorCallback(function ($options) use($seriesList) {
         $series = $seriesList[$options['sid']];
         return $series->vals;
     });
     $convert = new ConvertToDateStringKeys();
     $action = new Formula($ast, $astEvaluator);
     $output = $action->run();
     $output = $convert->run($output);
     $this->assertEquals($expected, $output);
 }