SPARQL CONSTRUCT subClassOf - sparql

i want to create a construct query with triples of subjects, that has a certain subclass.
That works fine:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX AS: <http://www.w3.org/ns/activitystreams#>
CONSTRUCT {?s ?p ?o}
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create ;
?p ?o .
}
But now i want to ask for more than one type!
Something like
WHERE {
?s rdf:type/rdfs:subClassOf* AS:Create|AS:Announce ;
?p ?o .
}
any idea ?

You can use a VALUES clause for this:
VALUES ?cls {AS:Create AS:Announce} ?s rdf:type/rdfs:subClassOf* ?cls ;

Related

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

Get structure of RDF graph by SPARQL query

How can I get only triples which represent graph structure - class and properties hierarchy (i.e. without individuals, property values)?
It seems that I need rdf:type, owl:class and etc. triplets. So that is my variant:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
select ?s ?p ?o
where
{
{
graph <http://graph.org/gr>
{
?s rdf:type ?o.
?s ?p ?o.
}
FILTER
(?o IN (owl:Class, owl:DatatypeProperty, owl:AnnotationProperty, owl:ObjectProperty, owl:DataRange, owl:Ontology,
owl:DataRange,owl:DeprecatedClass,owl:DeprecatedProperty,owl:OntologyProperty,rdfs:Class,owl:Restriction,owl:InverseFunctionalProperty,
owl:FunctionalProperty,owl:AllDisjointClasses,rdf:Property, rdfs:Datatype )
)
}
UNION
{
graph <http://graph.org/gr>
{
?s ?p ?o.
}
FILTER
(?p IN (rdfs:subClassOf,rdfs:subPropertyOf,rdfs:domain,rdfs:range,rdfs:label,rdfs:comment,rdfs:member,
rdf:first,rdf:rest,owl:allValuesFrom,owl:someValuesFrom,owl:AnnotationProperty,owl:equivalentClass,
owl:equivalentProperty,owl:hasValue,owl:OntologyProperty,owl:SymmetricProperty,owl:TransitiveProperty,
owl:versionInfo,owl:priorVersion,owl:oneOf,owl:maxCardinality,owl:minCardinality,owl:inverseOf,
owl:incompatibleWith,owl:intersectionOf,owl:imports,owl:backwardCompatibleWith,owl:AllDifferent,
owl:differentFrom,owl:disjointWith,owl:distinctMembers,owl:complementOf,owl:cardinality,owl:unionOf,owl:onProperty))
}
}

Sparql - Conditional Output

I am very new to the semantic web and sparql. I have an internal ontology that uses SmartLogic in order to manage the data.
I am writing some simple queries to get started.
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> # Simple Knowledge Organization System - https://www.w3.org/2004/02/skos/
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <https://www.w3.org/TR/rdf-schema/>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix ap: <http://cv.ap.org/ns>
SELECT DISTINCT
?subjectPrefLabel ?p ?o ?oL
WHERE {
{
?subject skosxl:prefLabel ?subjectLabel .
?subjectLabel skosxl:literalForm ?subjectPrefLabel .
?subject ?p ?o .
OPTIONAL {?o skos:prefLabel ?oL}
}
FILTER regex(?subjectPrefLabel, "Trump", 'i')
} order by ?subjectPrefLabel
This query returns results that look like :
I am trying to merge the ?o and ?oL fields, so that it will replace the ?o field, if and only if there is a valid ?oL Field
I haven't been able to figure it out quite yet. If there is any suggestions please let me know.
A bit difficult without data for testing the query, but in SPARQL 1.1 you can use BIND(IF(condition,then,else) as ?result ):
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <https://www.w3.org/TR/rdf-schema/>
PREFIX ap: <http://cv.ap.org/ns>
SELECT DISTINCT ?subjectPrefLabel ?p ?o
WHERE
{ ?subject skosxl:prefLabel ?subjectLabel .
?subjectLabel
skosxl:literalForm ?subjectPrefLabel .
?subject ?p ?o_tmp
OPTIONAL
{ ?o_tmp skos:prefLabel ?oL }
BIND(if(bound(?oL), ?oL, ?o_tmp) AS ?o)
FILTER regex(?subjectPrefLabel, "Trump", "i")
}
ORDER BY ?subjectPrefLabel

Property Path equivalent to OPTION(TRANSITIVE) statement in SPARQL

I'm a naive user trying to replicate a query that results in the following type of string:
derives_from some (epithelial cell and (part_of some (uterine cervix and (part_of some (Homo sapiens and (has disease some adenocarcinoma)))))
I'm at a hackathon, we have no ontology/SPARQL expert, and we're just trying to get these related fields out of this ontology and into SOLR. We're desperate!
The webpage this is from
http://www.ontobee.org/ontology/CLO?iri=http://purl.obolibrary.org/obo/CLO_000368)
helpfully provides all the SPARQL queries that are used on the page. I think this is the relevant query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?ref ?refp ?label ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?ref ?refp ?o .
FILTER ( ?refp IN ( owl:equivalentClass, rdfs:subClassOf ) ) .
OPTIONAL { ?ref rdfs:label ?label } .
{
{
SELECT ?s ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o ?p ?s .
FILTER ( ?p IN ( rdf:first, rdf:rest, owl:intersectionOf, owl:unionOf, owl:someValuesFrom, owl:hasValue, owl:allValuesFrom, owl:complementOf, owl:inverseOf, owl:onClass, owl:onProperty ) )
}
}
OPTION ( TRANSITIVE, t_in( ?s ), t_out( ?o ), t_step( ?s ) as ?link ).
FILTER ( ?s= <http://purl.obolibrary.org/obo/CLO_0003684> )
}
}
ORDER BY ?label
However, I can't even run that to check, because my SPARQL endpoint doesn't support Virtuoso. http://sparql.bioontology.org
So, it spits back an error on OPTION(TRANSITIVE).
Can anyone show me the equivalent standard pathway language? There are varying pathway lengths between the target nodes.
Virtuoso's transitivity operator is more powerful than what standard SPARQL provides, so in the general case it will not always be possible to express the same thing in a single standard SPARQL query. However, I believe that in this case it is possible.
The following property path would be the equivalent (disclaimer, I haven't tested this query, but it should give you the general idea):
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ ?s .
The full query would become something like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT ?ref ?refp ?label ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?ref ?refp ?o .
FILTER ( ?refp IN ( owl:equivalentClass, rdfs:subClassOf ) ) .
OPTIONAL { ?ref rdfs:label ?label } .
{
{
SELECT ?s ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ ?s .
}
}
FILTER ( ?s= <http://purl.obolibrary.org/obo/CLO_0003684> )
}
}
ORDER BY ?label
Note, by the way, that the FILTER condition on your variable ?s is outside the subselect, which might make this query a bit of a performance hog. Since you are not using ?s anywhere else in the query, you can simplify this part of the query further, eliminating the FILTER, like so:
{
{
SELECT ?o FROM <http://purl.obolibrary.org/obo/merged/CLO> WHERE {
?o (rdf:first|rdf:rest|owl:intersectionOf|owl:unionOf|owl:someValuesFrom|owl:hasValue|owl:allValuesFrom|owl:complementOf|owl:inverseOf|owl:onClass|owl:onProperty)+ <http://purl.obolibrary.org/obo/CLO_0003684> .
}
}
}

Sesame repository not being updated using INSERT despite no error

I am trying to update a Sesame repository with data from dbpedia. I have the following query:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
INSERT{?s ?p ?o}
WHERE {
SERVICE <http://dbpedia.org/sparql>{
{:Rotavirus_vaccine ?p ?o.
}
UNION
{
?s ?p :Rotavirus_vaccine.
}
}
}
This query doesn't show any error doesn't update the repository. On the other hand, splitting the UNION into two separate queries and then updating the repository one by one works. Why do the queries work in isolation but not in union? The code of an individual query is:
INSERT{:Rotavirus_vaccine ?p ?o}
WHERE {
SERVICE <http://dbpedia.org/sparql>{
{:Rotavirus_vaccine ?p ?o.
}
}
}
?s ?p ?o must all be defined in a row for the template be meaningful. Any time a variable is not bound, no update is done.
In one branch of the UNION, ?s ?p are defined and in the other ?p ?o. So all 3 are not defined in the same row.
Either add a BIND or a FILTER e.g for the first part:
{:Rotavirus_vaccine ?p ?o. BIND(:Rotavirus_vaccine AS ?s) }
{?s ?p ?o. FILTER(?s = :Rotavirus_vaccine }
or use this
INSERT{:Rotavirus_vaccine ?p ?o.
?s ?p :Rotavirus_vaccine.
}
because exactly one of those is defined for each case.
I've been able to execute a functional query by using BIND for both clauses of the UNION. The code is:
INSERT{?s ?p ?o}
WHERE
{
SERVICE <dbpedia.org/sparql>
{
{:Rotavirus_vaccine ?p ?o. BIND(:Rotavirus_vaccine AS ?s)}
UNION {?s ?p :Rotavirus_vaccine. BIND(:Rotavirus_vaccine AS ?o) }
}
}