Skip to content

Commit

Permalink
Use a FROM NAMED clause to query types per vocabs installed only
Browse files Browse the repository at this point in the history
  • Loading branch information
kinow committed Jun 5, 2022
1 parent 5d193c2 commit 5d375f4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
4 changes: 2 additions & 2 deletions model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public function getVocabularyList($categories = true, $shortname = false)
* Return all types (RDFS/OWL classes) present in the specified vocabulary or all vocabularies.
* @return array Array with URIs (string) as key and array of (label, superclassURI) as value
*/
public function getTypes($vocid = null, $lang = null)
public function getTypes(string $vocid = null, string $lang = null): array
{
$sparql = (isset($vocid)) ? $this->getVocabulary($vocid)->getSparql() : $this->getDefaultSparql();
$result = $sparql->queryTypes($lang);
$result = $sparql->queryTypes($lang, $vocid);

foreach ($result as $uri => $values) {
if (empty($values)) {
Expand Down
63 changes: 34 additions & 29 deletions model/sparql/GenericSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ protected function query($query) {
* @return string
*/
protected function generateFromClause($vocabs=null) {
$clause = '';
if (!$vocabs) {
return $this->graph !== '?graph' && $this->graph !== NULL ? "FROM <$this->graph>" : '';
}
$clause = '';
$graphs = $this->getVocabGraphs($vocabs);
foreach ($graphs as $graph) {
$clause .= "FROM NAMED <$graph> ";
Expand Down Expand Up @@ -540,36 +540,40 @@ public function queryConceptInfo($uris, $arrayClass = null, $vocabs = array(), $
/**
* Generates the sparql query for queryTypes
* @param string $lang
* @param string $vocid
* @return string sparql query
*/
private function generateQueryTypesQuery($lang) {
$fcl = $this->generateFromClause();
private function generateQueryTypesQuery($lang, string $vocid = null): string {
$vocabs = isset($vocid) ? [$vocid] : $this->model->getVocabularies();
$fcl = $this->generateFromClause($vocabs);
$query = <<<EOQ
SELECT DISTINCT ?type ?label ?superclass $fcl
WHERE {
{
{ BIND( skos:Concept as ?type ) }
UNION
{ BIND( skos:Collection as ?type ) }
UNION
{ BIND( isothes:ConceptGroup as ?type ) }
UNION
{ BIND( isothes:ThesaurusArray as ?type ) }
UNION
{ ?type rdfs:subClassOf/rdfs:subClassOf* skos:Concept . }
UNION
{ ?type rdfs:subClassOf/rdfs:subClassOf* skos:Collection . }
}
OPTIONAL {
?type rdfs:label ?label .
FILTER(langMatches(lang(?label), '$lang'))
}
OPTIONAL {
?type rdfs:subClassOf ?superclass .
}
FILTER EXISTS {
?s a ?type .
?s skos:prefLabel ?prefLabel .
GRAPH ?g {
{
{ BIND( skos:Concept as ?type ) }
UNION
{ BIND( skos:Collection as ?type ) }
UNION
{ BIND( isothes:ConceptGroup as ?type ) }
UNION
{ BIND( isothes:ThesaurusArray as ?type ) }
UNION
{ ?type rdfs:subClassOf/rdfs:subClassOf* skos:Concept . }
UNION
{ ?type rdfs:subClassOf/rdfs:subClassOf* skos:Collection . }
}
OPTIONAL {
?type rdfs:label ?label .
FILTER(langMatches(lang(?label), '$lang'))
}
OPTIONAL {
?type rdfs:subClassOf ?superclass .
}
FILTER EXISTS {
?s a ?type .
?s skos:prefLabel ?prefLabel .
}
}
}
EOQ;
Expand All @@ -581,7 +585,7 @@ private function generateQueryTypesQuery($lang) {
* @param EasyRdf\Sparql\Result $result
* @return array Array with URIs (string) as key and array of (label, superclassURI) as value
*/
private function transformQueryTypesResults($result) {
private function transformQueryTypesResults(\EasyRdf\Sparql\Result $result): array {
$ret = array();
foreach ($result as $row) {
$type = array();
Expand All @@ -601,10 +605,11 @@ private function transformQueryTypesResults($result) {
/**
* Retrieve information about types from the endpoint
* @param string $lang
* @param string $vocid
* @return array Array with URIs (string) as key and array of (label, superclassURI) as value
*/
public function queryTypes($lang) {
$query = $this->generateQueryTypesQuery($lang);
public function queryTypes($lang, string $vocid = null): array {
$query = $this->generateQueryTypesQuery($lang, $vocid);
$result = $this->query($query);
return $this->transformQueryTypesResults($result);
}
Expand Down
5 changes: 4 additions & 1 deletion tests/GenericSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,10 @@ public function testQueryTypes()
'http://www.skosmos.skos/test-meta/TestClass' => array(
'superclass' => 'http://www.w3.org/2004/02/skos/core#Concept',
'label' => 'Test class'
)
),
'http://www.w3.org/2004/02/skos/core#Collection' => array(),
'http://purl.org/iso25964/skos-thes#ThesaurusArray' => array(),
'http://purl.org/iso25964/skos-thes#ConceptGroup' => array()
);
$this->assertEquals($expected, $actual);
}
Expand Down
9 changes: 8 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,14 @@ public function testSearchWithNoParams() {
*/
public function testGetTypesWithoutParams() {
$result = $this->model->getTypes();
$this->assertEquals(array('http://www.w3.org/2004/02/skos/core#Concept', 'http://www.w3.org/2004/02/skos/core#Collection', 'http://purl.org/iso25964/skos-thes#ConceptGroup', 'http://purl.org/iso25964/skos-thes#ThesaurusArray', 'http://www.skosmos.skos/test-meta/TestClass'), array_keys($result));
$this->assertEquals(
array(
'http://www.w3.org/2004/02/skos/core#Concept',
'http://www.w3.org/2004/02/skos/core#Collection',
'http://purl.org/iso25964/skos-thes#ThesaurusArray',
'http://purl.org/iso25964/skos-thes#ConceptGroup',
'http://www.skosmos.skos/test-meta/TestClass'),
array_keys($result));
}

/**
Expand Down

0 comments on commit 5d375f4

Please sign in to comment.