Skip to content

Instantly share code, notes, and snippets.

@tatiana
Last active December 15, 2015 00:08
Show Gist options
  • Save tatiana/5170442 to your computer and use it in GitHub Desktop.
Save tatiana/5170442 to your computer and use it in GitHub Desktop.
Why adding transitivity breaks language filter in SPARQL Query?
@prefix tpedia: <http://tatipedia.org/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
tpedia:new_york a tpedia:Place ;
rdfs:label "Nova Iorque"@pt ;
rdfs:label "New York"@en .
tpedia:london a tpedia:Place ;
rdfs:label "Londres"@pt ;
rdfs:label "London"@en .
tpedia:name rdf:type owl:DatatypeProperty ;
rdfs:subPropertyOf rdfs:label .
tpedia:munich a tpedia:Place ;
tpedia:name "Munique"@pt ;
tpedia:name "Munich"@en .
@tatiana
Copy link
Author

tatiana commented Mar 15, 2013

I'd like to build a query to retrieve a list of instances' URIs and rdfs:labels provided:

  • class (tpedia:Place)
  • language filter (e.g. "pt" ou "en")

For this task I have loaded the TTL above into OpenLink Virtuoso OpenSource.

The query bellow:

SELECT DISTINCT ?subject ?label
WHERE {
    ?subject a <http://tatipedia.org/Place>;
             rdfs:label ?label .
    FILTER(langMatches(lang(?label), "pt")) .
}

Returns for this dataset:

+-----------------+------------------+
|     subject     |      label       |
+-----------------+------------------+
| tpedia:london   | "Londres"@pt     |
| tpedia:new_york | "Nova Iorque"@pt |
+-----------------+------------------+

Ignoring:

+---------------+--------------+
|    subject    |    label     |
+---------------+--------------+
| tpedia:munich | "Munique"@pt |
+---------------+--------------+

While my intention is:

+-----------------+------------------+
|     subject     |      label       |
+-----------------+------------------+
| tpedia:london   | "Londres"@pt     |
| tpedia:munich   | "Munique"@pt     |
| tpedia:new_york | "Nova Iorque"@pt |
+-----------------+------------------+

I tried to fix this problem using transitivity:

SELECT DISTINCT ?subject ?label
WHERE {
    ?subject a <http://tatipedia.org/Place>;
             rdfs:label ?label option (transitive, t_distinct, t_step('step_no') as ?n, t_min(0)) .
    FILTER (langMatches(lang(?label), "pt"))
}

But it returned:

+--------------------------------+-------------------------------+
|            subject             |             label             |
+--------------------------------+-------------------------------+
| http://tatipedia.org/new_york  | http://tatipedia.org/new_york |
| http://tatipedia.org/new_york  | "New York"@en                 |
| http://tatipedia.org/new_york  | "Nova Iorque"@pt              |
| http://tatipedia.org/london    | http://tatipedia.org/london   |
| http://tatipedia.org/london    | "London"@en                   |
| http://tatipedia.org/london    | "Londres"@pt                  |
| http://tatipedia.org/munich    | http://tatipedia.org/munich   |
+--------------------------------+-------------------------------+

Not even applying filters.

Any suggestions?

@tatiana
Copy link
Author

tatiana commented Mar 19, 2013

The issue was solved using inference.

First, we need to add the inference rule using the Virtuoso ISQL:

rdfs_rule_set ('http://tatipedia.org/property_ruleset', 'http://tatipedia.org');

After this, the query bellow will work as expected:

DEFINE input:inference <http://tpedia.org/property_ruleset>
SELECT DISTINCT ?subject ?label
WHERE {
    ?subject a <http://tatipedia.org/Place>;
             rdfs:label ?label  .
    FILTER (langMatches(lang(?label), "pt"))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment