diff --git a/example/error_handling.php b/example/error_handling.php new file mode 100644 index 0000000..de6b5df --- /dev/null +++ b/example/error_handling.php @@ -0,0 +1,63 @@ +evalString($command); + //var_dump($r); + } catch(Rserve_Exception $e) { + var_dump($e); + } + + echo "NativeArray\n"; + $parser = new NativeArray(array('wrapper'=>true)); + try { + $command = 'seq(1,10, by=NA)'; + $command = 'r= try({'.$command.'}, silent=T); if( inherits(r,"try-error")) { r = structure(list(message=unclass(r)), class="try-error") }; r'; + $r = $cnx->evalString($command, $parser); + + var_dump($r); + } catch(Rserve_Exception $e) { + var_dump($e); + } + + echo "REXP\n"; + $parser = new REXP(); + try { + $command = 'seq(1,10, by=NA)'; + $command = 'try({'.$command.'}, silent=T)'; + $r = $cnx->evalString($command, $parser); + + $class = $r->getAttribute('class'); + if($class) { + $class = $class->getValues(); + } + + //var_dump($class); + + //var_dump($r); + } catch(Rserve_Exception $e) { + var_dump($e); + } + diff --git a/example/evaluator.php b/example/evaluator.php new file mode 100644 index 0000000..7e0602b --- /dev/null +++ b/example/evaluator.php @@ -0,0 +1,30 @@ +evaluate('seq(1,10, by=NA)'); + var_dump($r); + // Should have an class attribute set to "try-error" + + + // Create evaluator with REXP parser + $eval = new Evaluator($cnx, Evaluator::PARSER_REXP); + $r = $eval->evaluate('seq(1,10, by=NA)'); + var_dump($r); + // Return an Error instance \ No newline at end of file diff --git a/src/Evaluator.php b/src/Evaluator.php new file mode 100644 index 0000000..899d89d --- /dev/null +++ b/src/Evaluator.php @@ -0,0 +1,83 @@ +connexion = new Connection($connexion); + } else { + $this->connexion = $connexion; + } + $this->parser = $this->createParser($parser); + } + + public function createParser($parser) { + if( is_integer($parser) ) { + $this->parserType = $parser; + if($parser == self::PARSER_NATIVE) { + return null; // no need to create parser + } + if($parser == self::PARSER_WRAPPED) { + return new NativeArray(array('wrapper'=>true)); + } + if($parser == self::PARSER_REXP) { + return new REXP(); + } + } else { + return $parser; + } + } + + public function decorate($command) { + switch($this->parserType) { + case self:: PARSER_NATIVE: + case self:: PARSER_WRAPPED: + return 'r= try({'.$command.'}, silent=T); if( inherits(r,"try-error")) { r = structure(list("try-error"=1,message=unclass(r)), class="try-error") }; r'; + break; + + case self::PARSER_REXP: + return 'r= try({'.$command.'}, silent=T);'; + break; + } + if(!$this->parserType) { + throw new Exception('Unhandled parser type'); + } + } + + public function evaluate($command) { + return $this->connexion->evalString($this->decorate($command), $this->parser); + } +} \ No newline at end of file