Analyzer
[ class tree: Analyzer ] [ index: Analyzer ] [ all elements ]

Source for file Pattern.php

Documentation is available at Pattern.php

  1. <?php
  2. /**
  3.  * Mai_Analyzer_Pattern
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Analyzer
  27.  * @subpackge Pattern
  28.  * 
  29.  */
  30.  
  31. /** Load required file */
  32. {
  33.     require_once 'Mai/Analyzer/Interface.php';
  34. }
  35.  
  36. /**
  37.  * Mai_Analyzer_Pattern compares a pattern with given sources.
  38.  * 
  39.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  40.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  41.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  42.  * @link http://www.launchpad.net/mai
  43.  * 
  44.  * @category Mai
  45.  * @package Analyzer
  46.  * @subpackage Pattern
  47.  * 
  48.  */
  49.  
  50. class Mai_Analyzer_Pattern implements Mai_Analyzer_Interface
  51. {
  52.     /**
  53.      * @var string The pattern to analyze.
  54.      */
  55.     private $_patternString;
  56.  
  57.     /**
  58.      * @var array The pattern (in array) to analyze.
  59.      */
  60.     private $_patternArray;
  61.     
  62.     /**
  63.      * @var array An array with different input sources.
  64.      */
  65.     private $_sources = array();
  66.  
  67.         
  68.     /**
  69.      * @var array An array with information about the response.
  70.      */
  71.     private $_response = array();
  72.     
  73.     /**
  74.      * @var int The type of the response.
  75.      */
  76.     private $_responseType;
  77.     
  78.     /**
  79.      * Check if Mai has been setup and that the creator is Mai_Analyzer.
  80.      */
  81.     public function __construct($creator)
  82.     {
  83.         if(!Mai::isInit())
  84.         {
  85.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  86.         }
  87.         
  88.         if(!($creator instanceof Mai_Analyzer&& !Mai_Config::getVar()->analyzer->allow_direct_access)
  89.         {
  90.             trigger_error('Please use an instance of Mai_Analyzer to create an instance of Mai_Analyzer_Pattern.');
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * Analyze the pattern.
  96.      */
  97.     public function run()
  98.     {
  99.         //patterns must be valid
  100.         if(empty($this->_patternString|| empty($this->_patternArray))
  101.         {
  102.             throw new UnexpectedValueException('Pattern (string or array) has not been correctly set.');
  103.         }
  104.  
  105.         if(empty($this->_sources))
  106.         {
  107.             trigger_error('No sources set!'E_USER_NOTICE);
  108.         }
  109.  
  110.         //First step, compare string
  111.         $stringCompare $this->compareString();
  112.  
  113.         //If return is false we go to the next step
  114.         if($stringCompare === false)
  115.         {
  116.             
  117.         }
  118.         else
  119.         {
  120.             //Set the response and type
  121.             $this->_response = $stringCompare;
  122.             $this->_responseType = Mai::AN_RESPONSE_FULL_PATTERN;
  123.         }
  124.     }
  125.     
  126.     /**
  127.      * Compare the string version of the pattern.
  128.      * Fast and easy but success is limited.
  129.      * @return array|bool
  130.      */
  131.     private function compareString()
  132.     {
  133.         foreach($this->_sources as $name => $data)
  134.         {
  135.             foreach($data as $key => $value)
  136.             {
  137.                 if($this->_patternString === $value['pattern'])
  138.                 {
  139.                     //Return data
  140.                     return array($key => $value);
  141.                 }
  142.             }
  143.         }
  144.         
  145.         //Nothing found
  146.         return false;
  147.     }
  148.  
  149.     /**
  150.      * Set data to analyze.
  151.      * @param string $type Can be 'pattern-array' or 'pattern-string'.
  152.      * @param array $data 
  153.      */
  154.     public function set($typearray $data)
  155.     {
  156.         switch($type)
  157.         {
  158.             case 'pattern-array':
  159.                 $this->_patternArray = $patternArray;
  160.                 break;
  161.         
  162.             case 'pattern-string':
  163.                 $this->_patternString = $patternString;
  164.                 break;
  165.             
  166.             default:
  167.                 trigger_error('Unknown type to set.'E_USER_ERROR);
  168.                 break;
  169.         }
  170.     }
  171.     
  172.     /**
  173.      * Add a pattern source to the analyzer.
  174.      * @param string $name 
  175.      * @param array $data 
  176.      */
  177.     public function addSource($namearray $data)
  178.     {
  179.         if(empty($name|| !is_string($name))
  180.         {
  181.             throw new UnexpectedValueException('Name of source has not been set or is invalid!');
  182.         }
  183.  
  184.         $this->_sources[$name$data;
  185.     }
  186.  
  187.     /**
  188.      * Remove source.
  189.      * @param string $name 
  190.      */
  191.     public function removeSource($name)
  192.     {
  193.         if(array_key_exists($name$this->_sources))
  194.         {
  195.             unset($this->_sources[$name]);
  196.         }
  197.         else
  198.         {
  199.             trigger_error('Source '.$name.' does not excist.'E_USER_NOTICE);
  200.         }
  201.     }
  202. }

Documentation generated on Mon, 27 Jul 2009 19:55:11 +0200 by phpDocumentor 1.4.1