Skip to content

Commit

Permalink
Issue 666 - squash commits (#8)
Browse files Browse the repository at this point in the history
* Added preferredVocabUri parameter to Model::guessVocabularyFromURI()

* Added preferredVocabUri parameter to Model::guessVocabularyFromURI()

* Comment just to have one additionnal commit

* Compared vocab on IDs rather than URIs

* Add the preferred vocabulary parameter when calling
guessVocabularyFromURI

* Comments
  • Loading branch information
tfrancart authored and osma committed Dec 11, 2017
1 parent 4b131ad commit 59bc3e0
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion model/Concept.php
Original file line number Diff line number Diff line change
@@ -250,7 +250,8 @@ public function getMappingProperties()
if (isset($ret[$prop])) {
// checking if the target vocabulary can be found at the skosmos endpoint
$exuri = $val->getUri();
$exvoc = $this->model->guessVocabularyFromURI($exuri);
// if multiple vocabularies are found, the following method will return in priority the current vocabulary of the concept
$exvoc = $this->model->guessVocabularyFromURI($exuri, $this->vocab->getId());
// if not querying the uri itself
if (!$exvoc) {
$response = null;
8 changes: 5 additions & 3 deletions model/ConceptMappingPropertyValue.php
Original file line number Diff line number Diff line change
@@ -47,7 +47,8 @@ private function queryLabel($lang)
$lang = $this->clang;
}

$exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri());
// if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
$exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());

if ($this->resource->label($lang) !== null) { // current language
return $this->resource->label($lang);
@@ -79,7 +80,7 @@ public function getUri()

public function getExVocab()
{
$exvocab = $this->model->guessVocabularyFromURI($this->getUri());
$exvocab = $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId());
return $exvocab;
}

@@ -90,7 +91,8 @@ public function getVocab()

public function getVocabName()
{
$exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri());
// if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
$exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());
if ($exvocab) {
return $exvocab->getTitle();
}
20 changes: 16 additions & 4 deletions model/Model.php
Original file line number Diff line number Diff line change
@@ -529,16 +529,27 @@ public function getVocabularyByGraph($graph, $endpoint = null)
*
* @param Vocabulary[] $vocabs vocabularies to search
* @param string $uri URI to look for
* @param $preferredVocabId string ID of the preferred vocabulary to return if more than one is found
* @return Vocabulary the vocabulary with the URI
*/

private function disambiguateVocabulary($vocabs, $uri)
private function disambiguateVocabulary($vocabs, $uri, $preferredVocabId = null)
{
// if there is only one candidate vocabulary, return it
if (sizeof($vocabs) == 1) {
return $vocabs[0];
}

// if there are multiple vocabularies and one is the preferred vocabulary, return it
if($preferredVocabId != null) {
foreach ($vocabs as $vocab) {
if($vocab->getId() == $preferredVocabId) {
return $vocab;
}
}
}

// no preferred vocabulary, or it was not found, search in which vocabulary the concept has a label
foreach ($vocabs as $vocab) {
if ($vocab->getConceptLabel($uri, null) !== null)
return $vocab;
@@ -553,9 +564,10 @@ private function disambiguateVocabulary($vocabs, $uri)
* vocabulary URI spaces.
*
* @param $uri string URI to search
* @param $preferredVocabId string ID of the preferred vocabulary to return if more than one is found
* @return Vocabulary vocabulary of this URI, or null if not found
*/
public function guessVocabularyFromURI($uri)
public function guessVocabularyFromURI($uri, $preferredVocabId = null)
{
if ($this->vocabsByUriSpace === null) { // initialize cache
$this->vocabsByUriSpace = array();
@@ -569,13 +581,13 @@ public function guessVocabularyFromURI($uri)
$namespace = substr($uri, 0, -strlen($res->localName()));
if (array_key_exists($namespace, $this->vocabsByUriSpace)) {
$vocabs = $this->vocabsByUriSpace[$namespace];
return $this->disambiguateVocabulary($vocabs, $uri);
return $this->disambiguateVocabulary($vocabs, $uri, $preferredVocabId);
}

// didn't work, try to match with each URI space separately
foreach ($this->vocabsByUriSpace as $urispace => $vocabs) {
if (strpos($uri, $urispace) === 0) {
return $this->disambiguateVocabulary($vocabs, $uri);
return $this->disambiguateVocabulary($vocabs, $uri, $preferredVocabId);
}
}

0 comments on commit 59bc3e0

Please sign in to comment.