/** * Builds the SPARQL query */ private function _buildQueries() { $query = new Erfurt_Sparql_Query2(); $uri = new Erfurt_Sparql_Query2_IriRef($this->_uri); $predVar = new Erfurt_Sparql_Query2_Var('predicate'); $objVar = new Erfurt_Sparql_Query2_Var('object'); $query->addTriple($uri, $predVar, $objVar); $query->addFilter(new Erfurt_Sparql_Query2_UnaryExpressionNot(new Erfurt_Sparql_Query2_isBlank($objVar))); if (!empty($this->_ignoredPredicates)) { $or = new Erfurt_Sparql_Query2_ConditionalAndExpression(); $filter = new Erfurt_Sparql_Query2_Filter($or); foreach ($this->_ignoredPredicates as $ignored) { $or->addElement(new Erfurt_Sparql_Query2_UnaryExpressionNot(new Erfurt_Sparql_Query2_sameTerm($predVar, new Erfurt_Sparql_Query2_IriRef($ignored)))); } $query->getWhere()->addElement($filter); } $query->setDistinct(true)->addProjectionVar($predVar)->addProjectionVar($objVar)->getOrder()->add($predVar); $queries = array(); $closure = Erfurt_App::getInstance()->getStore()->getImportsClosure($this->_model->getModelUri(), true); $queryGraphs = array_merge(array($this->_graph), $closure); foreach ($queryGraphs as $currentGraph) { $query->setFroms(array($currentGraph)); $queries[$currentGraph] = clone $query; } return $queries; }
public function testGetSparql() { $q1 = new Erfurt_Sparql_Query2(Erfurt_Sparql_Query2::typeAsk); $this->assertEquals("ASK \nWHERE { \n} \n", $q1->getSparql()); $q2 = new Erfurt_Sparql_Query2(Erfurt_Sparql_Query2::typeDescribe); $this->assertEquals("DESCRIBE * \nWHERE { \n} \n", $q2->getSparql()); $q3 = new Erfurt_Sparql_Query2(Erfurt_Sparql_Query2::typeSelect); $this->assertEquals("SELECT * \nWHERE { \n} \n", $q3->getSparql()); $q4 = new Erfurt_Sparql_Query2(Erfurt_Sparql_Query2::typeConstruct); $constrPure = "CONSTRUCT \n{ \n} \nWHERE { \n} \n"; $this->assertEquals($constrPure, $q4->getSparql()); $q4->setBase(new Erfurt_Sparql_Query2_IriRef('http://base.com')); $this->assertEquals("BASE <http://base.com> \nCONSTRUCT \n{ \n} \nWHERE { \n} \n", $q4->getSparql()); $q4->removeBase(); $this->assertEquals($constrPure, $q4->getSparql()); $q3->setDistinct(); $this->assertEquals("SELECT DISTINCT * \nWHERE { \n} \n", $q3->getSparql()); $q3->setReduced(); $this->assertEquals("SELECT REDUCED * \nWHERE { \n} \n", $q3->getSparql()); }
protected function _buildSubCheckQuery($uri, $setup) { $subVar = new Erfurt_Sparql_Query2_Var('subResourceUri'); $searchVar = new Erfurt_Sparql_Query2_Var('resourceUri'); //$classVar = new Erfurt_Sparql_Query2_Var('classUri'); $query = new Erfurt_Sparql_Query2(); $query->addProjectionVar($subVar); $query->setDistinct(); $this->_owApp->logger->info("data: " . print_r($query, true)); $elements = array(); $in = array(); $out = array(); if (isset($setup->config->hierarchyRelations->in)) { if (count($setup->config->hierarchyRelations->in) > 1) { // init union var $unionSub = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); // parse config gile foreach ($setup->config->hierarchyRelations->in as $rel) { // sub stuff $groupPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); // add triplen $groupPattern->addTriple($subVar, new Erfurt_Sparql_Query2_IriRef($rel), $searchVar); // add triplet to union var $unionSub->addElement($groupPattern); } $in[] = $unionSub; } else { $rel = $setup->config->hierarchyRelations->in; // add optional sub relation // create optional graph to load sublacsses of selected class $queryOptional = new Erfurt_Sparql_Query2_GroupGraphPattern(); $queryOptional->addTriple($subVar, new Erfurt_Sparql_Query2_IriRef($rel[0]), $searchVar); $in[] = $queryOptional; } } if (isset($setup->config->hierarchyRelations->out)) { if (count($setup->config->hierarchyRelations->out) > 1) { // init union var $unionSub = new Erfurt_Sparql_Query2_GroupGraphPattern(); // parse config gile foreach ($setup->config->hierarchyRelations->out as $rel) { // sub stuff $optPattern = new Erfurt_Sparql_Query2_OptionalGraphPattern(); // add triple $optPattern->addTriple($searchVar, new Erfurt_Sparql_Query2_IriRef($rel), $subVar); // add triplet to union var $unionSub->addElement($optPattern); } $out[] = $unionSub; } else { $rel = $setup->config->hierarchyRelations->out; // add optional sub relation // create optional graph to load sublacsses of selected class $queryOptional = new Erfurt_Sparql_Query2_GroupGraphPattern(); $queryOptional->addTriple($searchVar, new Erfurt_Sparql_Query2_IriRef($rel[0]), $subVar); $out[] = $queryOptional; } } $inout = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); $inout->addElements($in); $inout->addElements($out); $elements[] = $inout; /*$elements[] = new Erfurt_Sparql_Query2_Triple( $searchVar, new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE), $classVar );*/ // add filter $elements[] = new Erfurt_Sparql_Query2_Filter(new Erfurt_Sparql_Query2_sameTerm($searchVar, new Erfurt_Sparql_Query2_IriRef($uri))); $query->addElements($elements); $query->setLimit(1); // log results $this->_owApp->logger->info('NavigationController CHECK SUB: ' . PHP_EOL . $query->__toString()); return $query; }
/** * Searches the local database for URIs. If mode is set to properties only, * only defined properties and URIs that are used at least once as a * property are returned. * * the classes array is used for class restrictions * * @param array $termsArray * @param string $modelUri * @param int $mode * @param int $limit * @param array $classes * * @return array */ private function _searchLocal(array $termsArray, $modelUri, $mode, $limit, $classes = array()) { if ($mode === self::SEARCH_MODE_PROPERTIES) { return $this->_searchLocalPropertiesOnly($termsArray, $modelUri, $limit); } $store = Erfurt_App::getInstance()->getStore(); // get a store specific text-search query2 object $searchPattern = $store->getSearchPattern(implode(" ", $termsArray), $modelUri); $query = new Erfurt_Sparql_Query2(); $query->addElements($searchPattern); $projVar = new Erfurt_Sparql_Query2_Var('resourceUri'); $query->addProjectionVar($projVar); // add class restriction patterns for each class foreach ($classes as $class) { $classPattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); $classPattern->addTriple($projVar, new Erfurt_Sparql_Query2_IriRef(EF_RDF_TYPE), new Erfurt_Sparql_Query2_IriRef($class)); $query->addElement($classPattern); } $query->setLimit(20); $query->setDistinct(true); $queryResult = $store->sparqlQuery($query, array('result_format' => 'extended')); $tempResult = array(); foreach ($queryResult['results']['bindings'] as $row) { $object = isset($row['o']) ? $row['o']['value'] : null; $weight = $this->_getWeight($termsArray, $row['resourceUri']['value'], $object); if (isset($tempResult[$row['resourceUri']['value']])) { if ($weight > $tempResult[$row['resourceUri']['value']]) { $tempResult[$row['resourceUri']['value']] = $weight; } } else { $tempResult[$row['resourceUri']['value']] = $weight; } } arsort($tempResult); require_once 'OntoWiki/Model/TitleHelper.php'; require_once 'OntoWiki/Utils.php'; if (null !== $modelUri) { $model = $store->getModel($modelUri, false); $titleHelper = new OntoWiki_Model_TitleHelper($model); } else { $titleHelper = new OntoWiki_Model_TitleHelper(); } $titleHelper->addResources(array_keys($tempResult)); $translate = $this->_owApp->translate; // create different source description strings if (count($classes) > 0) { $sourceString = $translate->_('Local Search') . ' (' . $translate->_('recommended') . ')'; } else { $sourceString = $translate->_('Local Search'); } $result = array(); foreach ($tempResult as $uri => $w) { $title = $titleHelper->getTitle($uri); if (null !== $title) { $result[$uri] = str_replace('|', 'Ι', $title) . '|' . $uri . '|' . $sourceString; } else { $result[$uri] = OntoWiki_Utils::compactUri($uri) . $uri . '|' . $sourceString; } } return $result; }
/** * Method that counts already existing distinct datasets for given uri * * @param $uri uri string * * @return int distinct existing datasets */ private function countUriPattern($uri) { $query = new Erfurt_Sparql_Query2(); $query->setDistinct(true); $unions = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); $subjectVar = new Erfurt_Sparql_Query2_Var('s'); $query->addProjectionVar($subjectVar); // create six temporary vars (not selected in query) $tempVars = array(); for ($i = 0; $i < 6; $i++) { $tempVars[] = new Erfurt_Sparql_Query2_Var('var' . $i); } $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); $singlePattern->addTriple($subjectVar, $tempVars[0], $tempVars[1]); $unions->addElement($singlePattern); $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); $singlePattern->addTriple($tempVars[2], $subjectVar, $tempVars[3]); $unions->addElement($singlePattern); $singlePattern = new Erfurt_Sparql_Query2_GroupGraphPattern(); $singlePattern->addTriple($tempVars[4], $tempVars[5], $subjectVar); $unions->addElement($singlePattern); $query->getWhere()->addElement($unions); $filter = new Erfurt_Sparql_Query2_ConditionalOrExpression(); $filter->addElement(new Erfurt_Sparql_Query2_Regex($subjectVar, new Erfurt_Sparql_Query2_RDFLiteral('^' . $uri), new Erfurt_Sparql_Query2_RDFLiteral('i'))); $query->addFilter($filter); $result = $this->_owApp->erfurt->getStore()->countWhereMatches($this->_model->getModelIri(), $query->getWhere(), 's', true); return $result; }
/** * Moves resource to new URI * renaming all occurences of the resource. * * @param string $oldUri The URI that identifies the resource. * @param string $newUri The URI to move resource to. * * @return void */ public function renameResource($oldUri, $newUri) { $query = new Erfurt_Sparql_Query2(); $query->setDistinct(true); $vars = array(); foreach (array('s', 'p', 'o') as $varName) { $vars[$varName] = new Erfurt_Sparql_Query2_Var($varName); $query->addProjectionVar($vars[$varName]); } $oldUriRef = new Erfurt_Sparql_Query2_IriRef($oldUri); $union = new Erfurt_Sparql_Query2_GroupOrUnionGraphPattern(); foreach ($vars as $var) { $group = new Erfurt_Sparql_Query2_GroupGraphPattern(); $group->addTriple($vars['s'], $vars['p'], $vars['o']); $group->addFilter(new Erfurt_Sparql_Query2_sameTerm($var, $oldUriRef)); $union->addElement($group); } $query->addElement($union); $result = $this->sparqlQuery($query, array('result_format' => 'extended')); $removed = array(); $added = array(); foreach ($result['results']['bindings'] as $s) { // result format from sparqlQuery // isn't the same as format for delete/addMultipleStatements if (array_key_exists('xml:lang', $s['o'])) { $s['o']['lang'] = $s['o']['xml:lang']; unset($s['o']['xml:lang']); } $removed[$s['s']['value']][$s['p']['value']][] = $s['o']; foreach (array('s', 'p', 'o') as $varName) { if ($s[$varName]['type'] === 'uri' && $s[$varName]['value'] === $oldUri) { $s[$varName]['value'] = $newUri; } } $added[$s['s']['value']][$s['p']['value']][] = $s['o']; } $this->deleteMultipleStatements($removed); $this->addMultipleStatements($added); }