Skip to content

Commit

Permalink
add error_handling & Evaluator class
Browse files Browse the repository at this point in the history
  • Loading branch information
cturbelin committed Apr 17, 2016
1 parent 507faf4 commit bada195
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
63 changes: 63 additions & 0 deletions example/error_handling.php
@@ -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);
}

30 changes: 30 additions & 0 deletions example/evaluator.php
@@ -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
83 changes: 83 additions & 0 deletions src/Evaluator.php
@@ -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);
}
}

0 comments on commit bada195

Please sign in to comment.