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 6fec308
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ 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);
Expand Down
59 changes: 31 additions & 28 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 @@ -542,34 +542,37 @@ public function queryConceptInfo($uris, $arrayClass = null, $vocabs = array(), $
* @param string $lang
* @return string sparql query
*/
private function generateQueryTypesQuery($lang) {
$fcl = $this->generateFromClause();
private function generateQueryTypesQuery(string $lang): string {
$vocabs = $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 +584,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 @@ -603,7 +606,7 @@ private function transformQueryTypesResults($result) {
* @param string $lang
* @return array Array with URIs (string) as key and array of (label, superclassURI) as value
*/
public function queryTypes($lang) {
public function queryTypes(string $lang): array {
$query = $this->generateQueryTypesQuery($lang);
$result = $this->query($query);
return $this->transformQueryTypesResults($result);
Expand Down

0 comments on commit 6fec308

Please sign in to comment.