From 88beb7f6810003e7f1e286c97d300f30f480ba2f Mon Sep 17 00:00:00 2001 From: Thomas Francart Date: Mon, 4 Dec 2017 11:59:17 +0100 Subject: [PATCH] Added SPARQL query to query for super properties of a property --- model/sparql/GenericSparql.php | 42 +++++++++++++++++++++++++ tests/GenericSparqlTest.php | 12 +++++++ tests/test-vocab-data/myns-ontology.ttl | 21 +++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 tests/test-vocab-data/myns-ontology.ttl diff --git a/model/sparql/GenericSparql.php b/model/sparql/GenericSparql.php index 544229b6e..181daf3c9 100644 --- a/model/sparql/GenericSparql.php +++ b/model/sparql/GenericSparql.php @@ -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 = << 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; + } + } /** diff --git a/tests/GenericSparqlTest.php b/tests/GenericSparqlTest.php index 7afc2065e..15fb16b7a 100644 --- a/tests/GenericSparqlTest.php +++ b/tests/GenericSparqlTest.php @@ -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); + } } diff --git a/tests/test-vocab-data/myns-ontology.ttl b/tests/test-vocab-data/myns-ontology.ttl new file mode 100644 index 000000000..e82ccc9d1 --- /dev/null +++ b/tests/test-vocab-data/myns-ontology.ttl @@ -0,0 +1,21 @@ +@prefix prefix: . +@prefix skos: . +@prefix owl: . +@prefix rdfs: . +@prefix my: . +@prefix xsd: . + + 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 . + +