Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine default graph and optional graphs via UNION to return all the types available in the dataset #1327

Open
wants to merge 1 commit into
base: skosmos-2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me. You are already using $fcl i.e. a generated block of FROM clauses. There is no need for the GRAPH ?g { block.

{
{ 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()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a look at testconfig.ttl, and I think the function shuold return these new types now as they are types used in vocabularies there. These extra values are added now since the query is searching types in all graphs for all configured vocabs.

);
$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',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change in this array is the order of ThesaurusArray and ConceptGroup. Not sure why these two swapped places in this array.

'http://www.skosmos.skos/test-meta/TestClass'),
array_keys($result));
}

/**
Expand Down