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

Issue 669 2 #671

Merged
merged 9 commits into from
Jan 8, 2018
38 changes: 32 additions & 6 deletions model/Concept.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ public function getProperties()
$properties['skos:narrower'] = $membersArray;
}
}

foreach ($longUris as &$prop) {
// storing full URI without brackets in a separate variable
$longUri = $prop;
if (EasyRdf\RdfNamespace::shorten($prop) !== null) {
// shortening property labels if possible
$prop = $sprop = EasyRdf\RdfNamespace::shorten($prop);
Expand All @@ -329,10 +331,32 @@ public function getProperties()
}
// EasyRdf requires full URIs to be in angle brackets

if (!in_array($prop, $this->DELETED_PROPERTIES) || ($this->isGroup() === false && $prop === 'skos:member')) {
if (!in_array($prop, $this->DELETED_PROPERTIES) || ($this->isGroup() === false && $prop === 'skos:member')) {
// retrieve property label and super properties from the current vocabulary first
$propres = new EasyRdf\Resource($prop, $this->graph);
$proplabel = $propres->label($this->getEnvLang()) ? $propres->label($this->getEnvLang()) : $propres->label();
$superprop = $propres->get('rdfs:subPropertyOf') ? $propres->get('rdfs:subPropertyOf')->getURI() : null;

// if not found in current vocabulary, look up in the default graph to be able
// to read an ontology loaded in a separate graph
// note that this imply that the property has an rdf:type declared for the query to work
if(!$proplabel) {
$envLangLabels = $this->model->getDefaultSparql()->queryLabel($longUri, $this->getEnvLang());
$proplabel = ($envLangLabels)?$envLangLabels[$this->getEnvLang()]:$this->model->getDefaultSparql()->queryLabel($longUri, '')[''];
}

// look for superproperties in the current graph
$superprops = null;
foreach ($this->graph->allResources($prop, 'rdfs:subPropertyOf') as $subi) {
$superprops[] = $subi->getUri();
}

// also look up superprops in the default graph if not found in current vocabulary
if(!$superprops) {
$superprops = $this->model->getDefaultSparql()->querySuperProperties($longUri);
}

// we're reading only one super property, even if there are multiple ones
$superprop = ($superprops)?$superprops[0]:null;
if ($superprop) {
$superprop = EasyRdf\RdfNamespace::shorten($superprop) ? EasyRdf\RdfNamespace::shorten($superprop) : $superprop;
}
Expand All @@ -344,9 +368,11 @@ public function getProperties()
}

// searching for subproperties of literals too
foreach ($this->graph->allResources($prop, 'rdfs:subPropertyOf') as $subi) {
$suburi = EasyRdf\RdfNamespace::shorten($subi->getUri()) ? EasyRdf\RdfNamespace::shorten($subi->getUri()) : $subi->getUri();
$duplicates[$suburi] = $prop;
if($superprops) {
foreach ($superprops as $subi) {
$suburi = EasyRdf\RdfNamespace::shorten($subi) ? EasyRdf\RdfNamespace::shorten($subi) : $subi;
$duplicates[$suburi] = $prop;
}
}

// Iterating through every literal and adding these to the data object.
Expand Down
42 changes: 42 additions & 0 deletions model/sparql/GenericSparql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,48 @@ public function queryLabel($uri, $lang) {
return null;
}
}

/**
* Generates a SPARQL query to retrieve the super properties of a given property URI.
* Note this must be executed in the graph where this information is available.
* @param string $uri
* @return string sparql query string
*/
private function generateSubPropertyOfQuery($uri) {
$fcl = $this->generateFromClause();
$query = <<<EOQ
SELECT ?superProperty $fcl
WHERE {
<$uri> rdfs:subPropertyOf ?superProperty
}
EOQ;
return $query;
}

/**
* Query the super properties of a provided property URI.
* @param string $uri URI of a propertyes
* @return array array super properties, or null if none exist
*/
public function querySuperProperties($uri) {
$query = $this->generateSubPropertyOfQuery($uri);
$result = $this->query($query);
$ret = array();
foreach ($result as $row) {
if (isset($row->superProperty)) {
$ret[] = $row->superProperty->getUri();
}

}

if (sizeof($ret) > 0) {
// return result
return $ret;
} else {
// no result, return null
return null;
}
}


/**
Expand Down
12 changes: 12 additions & 0 deletions tests/GenericSparqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,16 @@ public function testQueryConceptsWithExtraFields()
$this->assertEquals(array('en' => 'Bass'), $actual[0]['prefLabels']);
$this->assertEquals(array(0 => array('uri' => 'http://www.skosmos.skos/test/ta1')), $actual[0]['skos:broader']);
}

/**
* @covers GenericSparql::querySuperProperties
*/
public function testQuerySuperProperties()
{
$this->sparql = new GenericSparql('http://localhost:3030/ds/sparql', '?graph', $this->model);
$actual = $this->sparql->querySuperProperties('http://example.com/myns#property');
$this->assertEquals(1, sizeof($actual));
$expected = array('http://example.com/myns#superProperty');
$this->assertEquals($actual, $expected);
}
}
3 changes: 2 additions & 1 deletion tests/test-vocab-data/date.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@prefix skosmos: <http://www.skosmos.skos/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

date:d1 a skos:Concept, meta:TestClass ;
dc:created "2000-01-03T12:46:39+03:00"^^xsd:dateTime ;
Expand All @@ -19,7 +20,7 @@ date:d2 a skos:Concept, meta:TestClass ;
date:ownDate "1986-21-00"^^xsd:date ; #invalid on purpose
skos:prefLabel "Broken date"@en .

date:ownDate
date:ownDate a owl:DatatypeProperty ;
rdfs:label "This is also a dateTime" .

<http://www.skosmos.skos/date/> a owl:Ontology ;
Expand Down
11 changes: 7 additions & 4 deletions tests/test-vocab-data/dup.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ dup:d5 a skos:Concept ;
dup:prop4 "Identical property value"@en;
skos:prefLabel "Not a subproperty"@en .

dup:prop1
# in order for getLabel query to work, a type has to be declared
# on the properties

dup:prop1 a owl:DatatypeProperty ;
rdfs:label "Very nice property"@en .

dup:prop2
dup:prop2 a owl:DatatypeProperty ;
rdfs:subPropertyOf dup:prop1 ;
rdfs:label "Nearly as nice subproperty"@en .

dup:prop3
dup:prop3 a owl:DatatypeProperty ;
rdfs:label "Nice-ish property"@en .

dup:prop4
dup:prop4 a owl:DatatypeProperty ;
rdfs:label "Not at all a subproperty"@en .
21 changes: 21 additions & 0 deletions tests/test-vocab-data/myns-ontology.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@prefix prefix: <http://www.skosmos.skos/prefix/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix my: <http://example.com/myns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/myns> a owl:Ontology .

my:superProperty a owl:DatatypeProperty ;
rdfs:domain skos:Concept ;
rdfs:label "my super property"@en ;
rdfs:range xsd:string .

my:property a owl:DatatypeProperty ;
rdfs:domain skos:Concept ;
rdfs:label "my property"@en ;
rdfs:subPropertyOf my:superProperty ;
rdfs:range xsd:string .


2 changes: 1 addition & 1 deletion tests/test-vocab-data/sub.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ sub:d1 a skos:Concept ;
skos:prefLabel "Has a hidden property"@en ;
sub:prop1 "Do not show this"@en .

sub:prop1
sub:prop1 a owl:DatatypeProperty ;
rdfs:subPropertyOf skos:hiddenLabel ;
rdfs:label "This subproperty should not be shown in the UI"@en .