Ignore:
Timestamp:
12/02/11 15:26:01 (13 years ago)
Author:
jkraaijeveld
Message:

Iteration of refactoring in the Connector code.

  • Abstracted load() and save() to a new superclass Connector. Every connector extends Connector.
  • Query arguments are now correctly created by Connector's createArguments() function, removing redundant if/if/if statements in every get() function.
  • Every sidefunction which previously used a seperate query to get data now uses the RAP findRegex method, should increase performance.
  • Question arguments have been changed slightly: 'uid' is now 'code' and 'answers' is now 'definedanswers' to avoid confusion.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/classes/QuestionConnector.php

    r158 r171  
    1212 * @author jkraaijeveld
    1313 */
    14 class QuestionConnector implements IConnector {
     14class QuestionConnector extends Connector {
    1515   
    16     protected $model;
    17     protected $fileName = 'data/questions/questions.rdf';
    1816   
    1917    /**
     
    2220    public function __construct()
    2321    {
     22                $this->fileName = 'data/questions/questions.rdf';
    2423        //Ensure the required folder for this connector exists
    2524        if (!is_dir('data/questions/'))
     
    2827   
    2928    /**
    30      * function load()
    31      * Loads the file into the standard MemModel.
    32      */
    33     public function load() {
    34         //Get the Memory Model from the ModelFactory
    35         $this->model = ModelFactory::getDefaultModel();
    36        
    37         //Ensure the required file exists before loading
    38         if(file_exists($this->fileName))
    39             $this->model->load($this->fileName);
    40     }
    41    
    42     /**
    43      * function save()
    44      * Saves the MemModel into the given file.
    45      */
    46     public function save() {
    47         $this->model->saveAs($this->fileName,'rdf');
    48     }
    49    
    50     /**
    5129     * Get the questions corresponding to the arguments.
    5230         * @param type $arguments : An array having one ore more of the following elements:
    53          * 'uid', 'title', 'type', 'description', 'category', 'has_answer'.
     31         * 'code', 'title', 'type', 'description', 'category', 'definedanswers'.
    5432     */
    5533    public function get($arguments)
    5634    {
    5735        $this->load();
    58        
    59         $keys = array_keys($arguments);
    60         //Set default values for arguments
    61         $uid = ""; $title = ""; $type = ""; $description = ""; $category = ""; $has_answer = "";
    62         //Set the arguments if they are supplied
    63         if(in_array("uid", $keys))
    64             $uid = 'predicates:question_code \''.$arguments["uid"].'\'';
    65         if(in_array("title", $keys))
    66             $title = 'predicates:title \''.$arguments["title"].'\'';
    67         if(in_array("type", $keys))
    68             $type = 'predicates:type \''.$arguments["type"].'\'';
    69         if(in_array("description", $keys))
    70             $description = "predicates:description \"".$arguments["description"]."\"";
    71         if(in_array("category", $keys))
    72                         $style = "predicates:category \"".$arguments["category"]."\"";
    73                 if(in_array("answers", $keys))
    74                 {
    75                         foreach($arguments["answers"] as $answer)
    76                         {
    77                                 $has_answer = $has_answer . 'predicates:has_answer \'' . $answer . '\' ';
    78                         }
    79                 }
    8036           
    8137        $querystring = '
     
    9147                predicates:description ?description ;
    9248                predicates:question_category ?category ;
    93                                 '. $uid . $title . $type . $description . $category . $has_answer . '
    94             }';
     49                                '. $this->createArguments($arguments) . '
     50        }';
     51
     52               
    9553        //Create the querystring
    9654        $results = $this->model->sparqlQuery($querystring);
     
    11573    private function getAnswers($uid)
    11674    {
    117         $querystring = '
    118             PREFIX  predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '>
    119             PREFIX  resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '>
    120             SELECT DISTINCT ?has_answer
    121             WHERE
    122             {
    123                  _question      predicates:resource_type        resources:question ;
    124                 predicates:has_answer ?has_answer ;
    125                 predicates:question_code \'' . $uid . '\'
    126             }';
     75                $result = $this->model->findRegex("[(".$uid.")]", "[(has_answer)]", null);
     76                $iterator = $result->getStatementIterator();
     77                $answers = array();
     78                while($iterator->hasNext())
     79                {
     80                        $element = $iterator->next();
     81                        $answers[] = $element->getLabelObject();
     82                }
     83                return $answers;
    12784
    128         $answers = $this->model->sparqlQuery($querystring);
    129         $returnArray = array();
    130         if(!empty($answers))
    131         {
    132             foreach($answers as $answer)
    133             {
    134                 $returnArray[] = $answer['?has_answer']->label;
    135             }
    136         }
    137         return $returnArray;
    138     }
    139 
     85        }
    14086   
    14187    /**
Note: See TracChangeset for help on using the changeset viewer.