model = ModelFactory::getDefaultModel(); if(file_exists($this->fileName)) $this->model->load($this->fileName); } /** * fucntion save() * Saves the MemModel into the given file. */ public function save() { $this->model->saveAs($this->fileName,'rdf'); } /** * function get() * @param type $arguments : An array containing one or more of the following elements: * 'uid', 'title', 'datetime', 'applications', 'surveys', 'answersets' */ public function get($arguments) { $this->load(); $keys = array_keys($arguments); $uid = ""; $title = ""; $creator = ""; $datetime = ""; $applications = ""; $surveys = ""; $answersets = ""; if(in_array("uid", $keys)) $uid = 'predicates:uid \''.$arguments["uid"].'\' '; if(in_array("title", $keys)) $title = 'predicates:title \''.$arguments["title"].'\' '; if(in_array("creator", $keys)) $creator = 'predicates:creator \''.$arguments["creator"].'\' '; if(in_array("datetime", $keys)) $datetime = 'predicates:datetime \''.$arguments["datetime"].'\' '; if(in_array("applications", $keys)) { foreach($arguments["applications"] as $application) { $applications = $applications . 'predicates:has_application \'' . $application . '\' '; } } if(in_array("surveys", $keys)) { foreach($arguments["surveys"] as $survey) { $surveys = $surveys . 'predicates:has_survey \'' . $survey . '\' '; } } if(in_array("answersets", $keys)) { foreach($arguments["answersets"] as $answerset) { $answersets = $answersets . 'predicates:has_answerset \'' . $answerset . '\' '; } } //Build the query string $querystring = ' PREFIX predicates: <' . SURVEYTOOL_PREDICATES_NAMESPACE . '> PREFIX resources: <' . SURVEYTOOL_RESOURCES_NAMESPACE . '> SELECT DISTINCT ?uid, ?title, ?creator, ?datetime WHERE { _session predicates:resource_type resources:session ; predicates:uid ?uid ; predicates:title ?title ; predicates:creator ?creator ; predicates:datetime ?datetime ; ' . $uid . $title . $creator . $datetime . $applications . $surveys . $answersets . ' }'; //Query the model $results = $this->model->sparqlQuery($querystring); $sessions = array(); if(!empty($results)) { $this->db = new DatabaseInterface(); foreach($results as $result) { //Create a session object out of every result, get all required fields as well. $pipeline = $this->getPipeline($result['?uid']->label); $answersets = $this->getAnswerSets($result['?uid']->label); $creator = $this->db->get("User", array("uid" => $result['?creator']->label)); $datetime = new DateTime(); $datetime->setTimestamp(intval($result['?datetime']->label)); $sessions[] = new Session($result['?uid']->label, $result['?title']->label, $creator, $datetime, $pipeline, $answersets); } } return $sessions; } /** * function getPipeline() * param type $uid : The Session uid for which the pipeline should be retrieved. */ private function getPipeline($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_application)|(has_survey)]" ,null); $iterator = $result->getStatementIterator(); $pipeline = array(); while($iterator->hasNext()) { $element = $iterator->next(); if(strpos($element->getLabelPredicate(), "has_application") != false) { $applications = $this->db->get("application", array("uid" => $element->getLabelObject())); $pipeline[] = $applications[0]; } else { $surveys = $this->db->get("survey", array("uid" => $element->getLabelObject())); $pipeline[] = $surveys[0]; } } return $pipeline; } /** * function getAnswerSets() * @param type $uid : The Session uid for which the answerSets should be retrieved. */ private function getAnswerSets($uid) { $result = $this->model->findRegex("[(".$uid.")]", "[(has_answerset)]" ,null); $iterator = $result->getStatementIterator(); $answersets = array(); while($iterator->hasNext()) { $element = $iterator->next(); $resultsets = $this->db->get("answerset", array("uid" => $element->getLabelObject())); $answersets[] = $resultsets[0]; } return $answersets; } /** * function set() * @param type $rToolObjects : The ResearchToolObject to be saved. */ public function set($rToolObject) { $this->load(); $resourceSession = new Resource(SESSION . '/' . $rToolObject->uid); //Remove the old value stored with the given id $this->model->subtract($this->model->find($resourceSession, null, null)); //Set the new values $resourceSessionType = new Resource(SESSION); $predicateType = new Resource(RTYPE); $this->model->add(new Statement($resourceSession, $predicateType, $resourceSessionType)); $sessionId = new Literal($rToolObject->uid); $predicateId = new Resource(UID); $this->model->add(new Statement($resourceSession, $predicateId, $sessionId)); $sessionTitle = new Literal($rToolObject->title); $predicateTitle = new Resource(TITLE); $this->model->add(new Statement($resourceSession, $predicateTitle, $sessionTitle)); $sessionCreator = new Literal($rToolObject->creator->uid); $predicateCreator = new Resource(CREATOR); $this->model->add(new Statement($resourceSession, $predicateCreator, $sessionCreator)); $sessionTimestamp = new Literal($rToolObject->datetime->getTimestamp()); // $sessionTimestamp = new Literal($rToolObject->datetime); // Edit of above function, allows for creation of session, but still results in errors... $predicateTimestamp = new Resource(DATETIME); $this->model->add(new Statement($resourceSession, $predicateTimestamp, $sessionTimestamp)); //Create a sequence to store the different applications and surveys. if(isset($rToolObject->pipeline)) { foreach($rToolObject->pipeline as $element) { $sessionObject = new Literal($element->uid); $predicateObject = null; if(get_class($element) == "Application") $predicateObject = new Resource(HAS_APPLICATION); else if(get_class($element) == "Survey") $predicateObject = new Resource(HAS_SURVEY); if(isset($predicateObject)) $this->model->add(new Statement($resourceSession, $predicateObject, $sessionObject)); } } if(isset($rToolObject->answersets)) { foreach($rToolObject->answersets as $answerset) { $sessionAnswerSet = new Literal($answerset->uid); $predicateAnswerSet = new Resource(HAS_ANSWERSET); $this->model->add(new Statement($resourceSession, $predicateAnswerSet, $sessionAnswerSet)); } } $this->save(); } } ?>