Created unit tests for some of the core/cortex classes

Some of these unit tests brought to sight a few problems, which have been fixed
Esse commit está contido em:
Felipe Nascimento de Moura
2011-02-21 16:53:41 -03:00
commit a012e0a41d
16 arquivos alterados com 478 adições e 85 exclusões
@@ -0,0 +1,197 @@
<?php
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/Mind.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/Lexer/Lexer.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/canonic/Canonic.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/utils/constants.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/VersionManager.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProject.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindRelation.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProperty.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/analyst/Analyst.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Token.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Tokenizer.php';
/**
* Test class for Analyst.
* Generated by PHPUnit on 2011-02-21 at 01:40:36.
*/
class AnalystPtTest extends PHPUnit_Framework_TestCase {
/**
* @var Analyst
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
// you gotta set the idiom simply to start some required variables
// once these tests wont use the idiom as a key subject
MindProject::loadIdiom('en');
Mind::$langPath= dirname(__FILE__) . '/../../../../../mind3rd/API/languages/en/';
Mind::$lexer= new Lexer();
$this->object = new Analyst;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called afhas a test is executed.
*/
protected function tearDown() {
}
/**
* the used words doesn't metter here, the main spine is the keys group,
* so, it doesn't metter what language is set
*/
public function testAnalize1() {
$this->assertEquals($this->object->analize(
Array
('teacher', '', 'has', '', '', '', 'student'),
'SQVNONS',
Array
(2, 128, 1, 8, 16, 32, 2)),
Array('min'=>1,
'max'=>'n',
'linkType'=>'possibility',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize2() {
$this->assertEquals($this->object->analize(
Array
('teacher', '', 'has', '', '', '', 'student'),
'SQVNONS',
Array
(2, 64, 1, 8, 16, 32, 2)),
Array('min'=>1,
'max'=>'n',
'linkType'=>'must',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize3() {
$this->assertEquals($this->object->analize(
Array
('teacher', '', 'have', '', '', '', 'student'),
'SQVNONS',
Array
(2, 128, 1, 4, 16, 8, 2)),
Array('min'=>0,
'max'=>1,
'linkType'=>'possibility',
'linkVerb'=>'have',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize4() {
$this->assertEquals($this->object->analize(
Array
('teacher', 'have', '', '', '', 'student'),
'SQVNONS',
Array
(2, 1, 4, 16, 8, 2)),
Array('min'=>0,
'max'=>1,
'linkType'=>'action',
'linkVerb'=>'have',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize5() {
$this->assertEquals($this->object->analize(
Array
('teacher', 'has', '', '', '', 'student'),
'SQVNONS',
Array
(2, 1, 8, 16, 32, 2)),
Array('min'=>1,
'max'=>'n',
'linkType'=>'action',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize6() {
$this->assertEquals($this->object->analize(
Array
('teachera', 'has', '', '', '', 'aluna'),
'SQVNONS',
Array
(2, 1, 4, 16, 32, 2)),
Array('min'=>0,
'max'=>'n',
'linkType'=>'action',
'linkVerb'=>'has',
'rel'=>'aluna',
'focus'=>'teachera'));
}
public function testAnalize7() {
$this->assertEquals($this->object->analize(
Array
('teacher', 'has', '', 'student'),
'SQVNONS',
Array
(2, 1, 32, 2)),
Array('min'=>0,
'max'=>'n',
'linkType'=>'action',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize8() {
$this->assertEquals($this->object->analize(
Array
('teacher', 'has', '', 'student'),
'SQVNONS',
Array
(2, 1, 8, 2)),
Array('min'=>0,
'max'=>1,
'linkType'=>'action',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize9() {
$this->assertEquals($this->object->analize(
Array
('teacher', '', 'has', '', 'student'),
'SQVNONS',
Array
(2, 64, 1, 8, 2)),
Array('min'=>1,
'max'=>1,
'linkType'=>'must',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
public function testAnalize10() {
$this->assertEquals($this->object->analize(
Array
('teacher', '', 'has', '', 'student'),
'SQVNONS',
Array
(2, 128, 1, 32, 2)),
Array('min'=>0,
'max'=>'n',
'linkType'=>'possibility',
'linkVerb'=>'has',
'rel'=>'student',
'focus'=>'teacher'));
}
}
?>
@@ -0,0 +1,75 @@
<?php
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/Mind.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/Lexer/Lexer.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/canonic/Canonic.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/utils/constants.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/VersionManager.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProject.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindRelation.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProperty.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/analyst/Analyst.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Token.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Tokenizer.php';
/**
* Test class for Tokenizer.
* Generated by PHPUnit on 2011-02-21 at 11:46:35.
*/
class TokenizerTest extends PHPUnit_Framework_TestCase {
/**
* @var Tokenizer
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
Tokenizer::loadModifiers(dirname(__FILE__) . '/../../../../../mind3rd/API/languages/en/');
$this->object = new Tokenizer;
//$this->object->loadModifiers('../../languages/en/');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
/**
* @todo Implement testIsQuantifier().
*/
public function testIsQuantifier() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @todo Implement testIsQualifier().
*/
public function testIsQualifier() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testSweep() {
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}
@@ -0,0 +1,52 @@
<?php
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/Mind.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/Lexer/Lexer.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/canonic/Canonic.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/utils/constants.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/VersionManager.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProject.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindRelation.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindEntity.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/classes/MindProperty.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/analyst/Analyst.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Token.php';
require_once dirname(__FILE__) . '/../../../../../mind3rd/API/cortex/tokenizer/Tokenizer.php';
/**
* Test class for Tokenizer.
* Generated by PHPUnit on 2011-02-21 at 11:46:35.
*/
class TokenizerTest extends PHPUnit_Framework_TestCase {
/**
* @var Tokenizer
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
Tokenizer::loadModifiers(dirname(__FILE__) . '/../../../../../mind3rd/API/languages/pt/');
$this->object = new Tokenizer;
//$this->object->loadModifiers('../../languages/en/');
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown() {
}
public function testSweep() {
// $this->object->sweep(); // AQUI
}
}
@@ -0,0 +1,6 @@
SVS
SQVS
SVNONS
SVNS
SQVNONS
SQVNS
@@ -0,0 +1,6 @@
SVS
SQVS
SVNONS
SVNS
SQVNONS
SQVNS
+42 -1
Ver Arquivo
@@ -8,9 +8,11 @@
class MindEntity {
public $name;
public $relevance;
public $relevance= 0;
public $properties= Array();
public $relations= Array();
private $refTo= Array();
private $refBy= Array();
/**
* Verifies if the definition describes an entity or not
@@ -35,6 +37,28 @@
return $this;
}
/**
* Defines another entity pointed/refered by this entity
* @param MindEntity $ref
* @return MindEntity
*/
public function addRefTo(MindEntity &$ref)
{
$this->refTo[$ref->name]= &$ref;
return $this;
}
/**
* Specifies that another entity is pointing to this one
* @param MindEntity $ref
* @return MindEntity
*/
public function addRefBy(MindEntity &$ref)
{
$this->refBy[$ref->name]= &$ref;
return $this;
}
/**
* Adds a reference to the current entity
*
@@ -44,6 +68,23 @@
public function addRef(MindRelation &$rel)
{
$this->relations[]= &$rel;
if($rel->focus->name == $this->name)
{
if($rel->max == QUANTIFIER_MAX_MAX)
{
//echo $rel->max;
$this->relevance++;
$this->addRefBy($rel->rel);
}else
$this->addRefTo($rel->rel);
}else{
if($rel->max == QUANTIFIER_MAX_MIN)
{
$this->relevance++;
$this->addRefBy($rel->focus);
}else
$this->addRefTo($rel->focus);
}
return $this;
}
+1 -1
Ver Arquivo
@@ -47,7 +47,7 @@ class MindProject extends VersionManager{
public static function loadIdiom($idiom)
{
$idiom= str_replace('\\', DIRECTORY_SEPARATOR, $idiom);
$langPath= $path.'/mind3rd/API/languages/'.$idiom.'/';
$langPath= '/mind3rd/API/languages/'.$idiom.'/';
set_include_path(get_include_path() . PATH_SEPARATOR . $langPath);
}
-1
Ver Arquivo
@@ -118,7 +118,6 @@
}
$this->default= $default;
echo $default."\n";
}
// identifying if it is required
+4 -2
Ver Arquivo
@@ -52,11 +52,12 @@
*/
public function setMin($min)
{
if($min != 'n' && in_array($min, $this->quantifiers))
if($min === 0 || $min === 1)
{
$this->min= $min;
return $this;
}
throw new Exception("Invalid minimum quantifier: ".$min, 0);
return false;
}
@@ -67,11 +68,12 @@
*/
public function setMax($max)
{
if($max !== 0 && in_array($max, $this->quantifiers))
if($max == 1 || $max == 'n')
{
$this->max= $max;
return $this;
}
throw new Exception("Invalid maximum quantifier: ".$max, 0);
return false;
}
+24 -18
Ver Arquivo
@@ -50,7 +50,7 @@ class Analyst {
foreach(self::$entities as $entity)
{
if($detailed)
echo " ".$entity->name."\n";
echo " (".$entity->relevance.")".$entity->name."\n";
foreach($entity->properties as $prop)
{
$props++;
@@ -102,8 +102,9 @@ class Analyst {
$focus= null;
$linkVerb= null;
$min= null;
$max= 'n';
$max= null;
$linkType= 'action';
$relation= false;
// foreach token
foreach($structureKeys as $token)
@@ -118,25 +119,15 @@ class Analyst {
}
// setting quantifiers
if(
$min == null &&
(
$token == Tokenizer::MT_NONE ||
$token == Tokenizer::MT_ONE
)
)
if($token == Tokenizer::MT_NONE)
{
$min= ($token == Tokenizer::MT_ONE)? 1: 0;
$min= 0;
continue;
}
if(
$min != null &&
(
$token == Tokenizer::MT_MANY ||
$token == Tokenizer::MT_ONE
)
)
if($token == Tokenizer::MT_ONE || $token == Tokenizer::MT_MANY)
{
if(!is_null($max))
$min= $max;
$max= ($token == Tokenizer::MT_ONE)? 1: 'n';
}
@@ -173,6 +164,13 @@ class Analyst {
{
$focus= &self::$entities[$word];
}else{
$relation= true;
// if min or max quantifier have not been set
if(is_null($min))
$min= ($linkType == 'must')? 1: 0;
if(is_null($max))
$max= 'n';
/*
* here, if it is an entity and the focused
* entity has already been selected, it means
@@ -190,6 +188,7 @@ class Analyst {
// let's create the relation itself
$curRelation= new MindRelation($relationName);
$curRelation->setLinkType($linkType)
->setMin($min)
->setMax($max)
@@ -220,11 +219,18 @@ class Analyst {
foreach($tmpProperties as $prop)
$focus->addProperty($prop);
}
echo $linkType;
if($relation)
return Array('min'=>$min,
'max'=>$max,
'linkVerb'=>$linkVerb,
'linkType'=>$linkType,
'focus'=>$focus->name,
'rel'=>$rel->name);
}
public static function sweep($matches)
{
// let's clear the Analyst memory as it uses static properties
self::reset();
+4 -1
Ver Arquivo
@@ -1,7 +1,10 @@
<?php
/**
* Will normalize the data and entities structure applying
* rules and patterns
* rules and patterns. Thanks for Edgar F. Codd for all he
* created and wondered for the Relational Model
*
* firs:
*
* foreach entity
*
+21 -21
Ver Arquivo
@@ -4,35 +4,35 @@ class Token
// Tokens to be used
// MT stands for MindTokenizer
// MS stands for MindSyntaxer
const MT_PERIOD = -2;
const MT_PERIOD = -2;
const MS_PERIOD = '.';
const MT_COMA = -1;
const MS_COMA = ',';
const MT_VOID = 0;
const MS_COMA = ',';
const MT_VOID = 0;
const MS_VOID = '';
const MT_VERB = 1;
const MS_VERB = 'V';
const MT_SUBST = 2;
const MS_SUBST = 'S';
const MT_NONE = 4;
const MS_NONE = 'N';
const MT_ONE = 8;
const MT_VERB = 1;
const MS_VERB = 'V';
const MT_SUBST = 2;
const MS_SUBST = 'S';
const MT_NONE = 4;
const MS_NONE = 'N';
const MT_ONE = 8;
const MS_ONE = 'N';
const MT_OR = 16;
const MS_OR = 'O';
const MS_OR = 'O';
const MT_MANY = 32;
const MS_MANY = 'N';
const MT_QMUST = 64;
const MS_MANY = 'N';
const MT_QMUST = 64;
const MS_QMUST = 'Q';
const MT_QMAY = 128;
const MS_QMAY = 'Q';
const MT_QMAY = 128;
const MS_QMAY = 'Q';
const MT_QNOTNULL= 254;
const MT_QKEY = 564;
const MT_QOF = 1024;
const MS_QOF = 'C';
const MT_QBE = 2048;
const MT_ANY = 4096;
const MS_ANY = '*';
const MT_QKEY = 564;
const MT_QOF =1024;
const MS_QOF = 'C';
const MT_QBE =2048;
const MT_ANY =4096;
const MS_ANY = '*';
public static $spine= Array();
public static $words= Array();
+15 -9
Ver Arquivo
@@ -149,9 +149,11 @@ class Tokenizer extends Token{
/**
* This method is called to load each possible modifier
*/
public static function loadModifiers()
public static function loadModifiers($modifiersSrc= false)
{
if(!file_exists('sintatics.list'))
if(self::$quantifiers) // it is already loaded
return true;
if(!$modifiersSrc && !file_exists('sintatics.list'))
{
self::loadSintatics(fopen(Mind::$langPath.Mind::$currentProject['idiom'].
'/sintatics.list', 'rb'));
@@ -168,10 +170,10 @@ class Tokenizer extends Token{
self::loadQualifiers($qlf);
self::loadTypes($tps);
}else{
self::loadSintatics(fopen('sintatics.list', 'rb'));
$qnt= simplexml_load_file('quantifiers.xml');
$qlf= simplexml_load_file('qualifiers.xml');
$tps= simplexml_load_file('datatypes.xml');
self::loadSintatics(fopen($modifiersSrc.'sintatics.list', 'rb'));
$qnt= simplexml_load_file($modifiersSrc.'quantifiers.xml');
$qlf= simplexml_load_file($modifiersSrc.'qualifiers.xml');
$tps= simplexml_load_file($modifiersSrc.'datatypes.xml');
self::loadQuantifiers($qnt);
self::loadQualifiers($qlf);
self::loadTypes($tps);
@@ -185,10 +187,13 @@ class Tokenizer extends Token{
*
* @return Array the spine, the whole structure of the abstracted text
*/
public function sweep()
public function sweep($content=false)
{
$cont= &Mind::$content;
if($content)
$cont= $content;
else
$cont= &Mind::$content;
// seek for data types
foreach(self::$dataTypes as $type=>$options)
{
@@ -206,6 +211,7 @@ class Tokenizer extends Token{
}
Mind::$syntaxer= new Syntaxer();
//print_r(Token::$spine);
return Token::$spine;
}
+1 -1
Ver Arquivo
@@ -7,7 +7,7 @@
This file describes the words that may be used as qualifiers
-->
<root>
<must>dever,precisar,demandar,necessitar</must>
<must>precisa,dever,precisar,demandar,necessitar</must>
<may>poder</may>
<notnull>obrigatório,not null,notnull,não nulo,requerido,required,necessário</notnull>
<of>de,do,da,dos,das</of>
+23 -19
Ver Arquivo
@@ -6,24 +6,28 @@
*/
// REGULAR EXPRESSIONS
define('PROP_DETAILS', "/\(.*/");
define('PROP_DEFAULT', "/(?<!\\\)\".+?(?<!\\\)\"/");
define('PROP_OPTIONS', '/\{(.+?)\}/');
define('PROP_OPTIONS_CLEAR', '/^\{|\}$/');
define('PROP_DEFEXEC', "/(^(\"=)|(\"exec\:))|(\"$)/i");
define('PROP_SIZE', "/\d+(\.?\d+)/");
define('COMA_SEPARATOR', '/\s/');
define('SINGLE_COMMENT', '/\/\/.+\n/');
define('MULTILINE_COMMENT', '/\/\*.+?\*\//');
define('NEW_LINE', "/\n/");
define('EXEC_STRING', 'exec:'); // equal(=) is also acceptable
define('VALID_SUBST_SYNTAX', 'S((( )?\,( )?S)?)+');
define('PROP_DETAILS', "/\(.*/");
define('PROP_DEFAULT', "/(?<!\\\)\".+?(?<!\\\)\"/");
define('PROP_OPTIONS', "/\{(.+?)\}/");
define('PROP_OPTIONS_CLEAR', "/^\{|\}$/");
define('PROP_DEFEXEC', "/(^(\"=)|(\"exec\:))|(\"$)/i");
define('PROP_SIZE', "/\d+(\.?\d+)/");
define('COMA_SEPARATOR', "/\s/");
define('SINGLE_COMMENT', "/\/\/.+\n/");
define('MULTILINE_COMMENT', "/\/\*.+?\*\//");
define('NEW_LINE', "/\n/");
define('EXEC_STRING', "exec:"); // equal(=) is also acceptable
define('VALID_SUBST_SYNTAX', "S((( )?\,( )?S)?)+");
// addresses
define('PROJECTS_DIR', '/mind3rd/projects/');
define('MODELS_DIR', '/mind3rd/API/models/');
define('ABOUT_INI', '/mind3rd/env/about.ini');
define('DEFAULTS_INI', '/mind3rd/env/defaults.ini');
define('MIND_CONF', '/mind3rd/env/mind.ini');
define('L10N_DIR', '/mind3rd/API/L10N/');
define('LANG_PATH', '/mind3rd/API/languages/');
define('PROJECTS_DIR', '/mind3rd/projects/');
define('MODELS_DIR', '/mind3rd/API/models/');
define('ABOUT_INI', '/mind3rd/env/about.ini');
define('DEFAULTS_INI', '/mind3rd/env/defaults.ini');
define('MIND_CONF', '/mind3rd/env/mind.ini');
define('L10N_DIR', '/mind3rd/API/L10N/');
define('LANG_PATH', '/mind3rd/API/languages/');
// other constants
define('QUANTIFIER_MAX_MAX', 'n');
define('QUANTIFIER_MAX_MIN', 1);
+7 -11
Ver Arquivo
@@ -1,20 +1,16 @@
aluno tem professor
cada professor deve ter um ou muitos alunos.
// professor tem aluno
//aluno tem foto:arquivo(xx)
professor tem aluno
aluno tem foto:arquivo(xx)
/*
blablabla
mimimi
xxx
blabelaebamimi
*/
aluno tem pai, mãe, avós, e papagaio.
//cada mãe pode ter um ou vários alunos.
aluno tem pai, mãe, avós e papagaio.
//aluno tem pai, mãe.
//aluno tem tio, tia.
aluno tem nome:caractere(128, obrigatório, não nulo, "josé's da \"conceição\" machado", "hffd")
pai tem \pilha.
mãe tem nome:varchar, idade:int e sexo:char(1, {F=Feminino|M=Masculino}).
pai tem nome:varchar, idade:int e sexo:char(), cpf:int(obrigatório).
*/