Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add error_handling & Evaluator class
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
require __DIR__."/../src/autoload.php"; | ||
|
||
/** | ||
* To run example create config.php | ||
* defining following constants | ||
* | ||
* define('RSERVE_HOST', 'localhost'); // Host to use | ||
* | ||
*/ | ||
require __DIR__.'/config.php'; | ||
|
||
use Sentiweb\Rserve\Connection; | ||
use Sentiweb\Rserve\Parser\NativeArray; | ||
use Sentiweb\Rserve\Exception as Rserve_Exception; | ||
use Sentiweb\Rserve\Parser\REXP; | ||
use Sentiweb\Rserve\Parser\Sentiweb\Rserve\Parser; | ||
|
||
$cnx = new Connection(RSERVE_HOST); | ||
|
||
// Using NativeArray | ||
|
||
// Run an error | ||
try { | ||
$command = 'seq(1,10, by=NA)'; | ||
$command = 'r= try({'.$command.'}, silent=T); if( inherits(r,"try-error")) { r = structure(list("try-error"=TRUE, message=unclass(r))) }; r'; | ||
$r = $cnx->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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
require __DIR__."/../src/autoload.php"; | ||
|
||
/** | ||
* To run example create config.php | ||
* defining following constants | ||
* | ||
* define('RSERVE_HOST', 'localhost'); // Host to use | ||
* | ||
*/ | ||
require __DIR__.'/config.php'; | ||
|
||
use Sentiweb\Rserve\Connection; | ||
use Sentiweb\Rserve\Evaluator; | ||
use Sentiweb\Rserve\Parser\NativeArray; | ||
|
||
$cnx = new Connection(RSERVE_HOST); | ||
|
||
$eval = new Evaluator($cnx, Evaluator::PARSER_WRAPPED); | ||
// Run an bad command | ||
$r = $eval->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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Sentiweb\Rserve; | ||
|
||
use Sentiweb\Rserve\Parser\REXP; | ||
use Sentiweb\Rserve\Parser\NativeArray; | ||
|
||
/** | ||
* Simple command handler using a connexion | ||
* | ||
* This class handle basic script evaluation & error handling | ||
* | ||
* It decorates R command with try() in order to catch error occuring in R | ||
* | ||
* if PARSER_NATIVE is used: | ||
* result array will have an entry "try-error" set to true | ||
* | ||
* if PARSER_WRAPPED | ||
* result will have an attribute "class" with value "try-error" and an entry try-error | ||
* | ||
* if PARSER_REXP | ||
* result class will be an Sentiweb\RServeREXP\Error | ||
* | ||
*/ | ||
|
||
class Evaluator { | ||
|
||
const PARSER_NATIVE = 1; | ||
const PARSER_WRAPPED = 2; | ||
const PARSER_REXP = 3; | ||
|
||
protected $connexion; | ||
|
||
protected $parserType; | ||
|
||
protected $parser; | ||
|
||
public function __construct($connexion, $parser) { | ||
if( is_array($connexion) ) { | ||
$this->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); | ||
} | ||
} |