SPARQL VALUES BLOCK AND GRAPH - sparql

I am trying to replace the value of code_type when it is empty and belongs to ifcowl:IfcSite with "INF" via this attempt. I don't know if this is possible.
First, I get the code_type values I need and I use the "values" block to replace the values
However, some values are empty and among these empty values, I have to replace the empty value by "INF" but only for ifcowl = IfcSite
Here is my attempt and thank you in advance for your help :
select ?code_type ?gid ?VALUE1 ?TEST
PREFIX ifcowl: <http://www.buildingsmart-tech.org/ifcOWL/IFC2X3_TC1#>
PREFIX express: <https://w3id.org/express#>
PREFIX blabla: <https://www.blabla.fr/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX iso23386: <https://www.iso.org/standard/iso23386#>
{
?o a ?class .
GRAPH blabla:optimized {
OPTIONAL {?o blabla:SEQ_GUID ?gid}
OPTIONAL {?o blabla:code_type ?code_type}
OPTIONAL {?o blabla:VALUE1 ?VALUE1}
FILTER (?code_type IN
(""^^xsd:string, "ATE_PCO"^^xsd:string, "ATE_SCA"^^xsd:string, "ATE_TNT"^^xsd:string))
}
optional {
values (?code_type ?TEST) {
("" "ZONE" && ?class = ifcowl:IfcSite)
}
}
optional {
values (?code_type ?TYPE) {
("ATE_PCO" "PCO")
("ATE_SCA" "CABLE")
("ATE_TNT" "TNT")
}
values (?code_type ?NOMOS) {
("ATE_PCO" "ABC")
("ATE_SCA" "BDC")
("ATE_TNT" "EDG")
}
}
}

Related

How to get a simple inferencing example to work

I might have understood something wrong, so bear with me.
I created a repository called 'reasoning' of type OWL2-RL (tried some other types already too).
I created a rule as follows:
prefix sys: <http://www.ontotext.com/owlim/system#>
INSERT DATA {
<_:pets> sys:addRuleset
'''Prefices { ex : <http://www.example.com#> }
Axioms {}
Rules
{
Id: custom
a <ex:hasPet> b
------------------------------------
b <ex:hasOwner> a
}'''
}
I insert some triples as follows:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ex: <http://www.example.com#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
INSERT DATA {
<ex:hasPet> a <owl:ObjectProperty>;
<rdfs:domain> <ex:Human>;
<rdfs:range> <ex:Pet>.
<ex:someHuman> <ex:hasPet> <ex:somePet>.
}
Consequently I set my rule as default as follows :
PREFIX sys: <http://www.ontotext.com/owlim/system#>
INSERT DATA {
_:b sys:defaultRuleset "pets"
}
I expect the following query
PREFIX ex: <http://www.example.com#>
select * where {
<ex:somePet> ?p ?o .
} limit 100
to return the following inferred triple
<ex:somePet> <ex:hasOwner> <ex:someHuman>
but unfortunately no inferred triples are present in the repository.
Could you please help me? Having a working example with the different steps to take will help me get rules to work in my application.
Thanks in advance!
Kind regards
There are multiple issues, first IRIs, within the Prefixes section of the ruleset must be written without angle brackets, e.g. use ex : http://www.example.com# instead ...
also your data and sample query do not make use of the prefix ex but are absolute IRIs, e.g. instead of
INSERT DATA {
<ex:hasPet> a <owl:ObjectProperty>;
<rdfs:domain> <ex:Human>;
<rdfs:range> <ex:Pet>.
<ex:someHuman> <ex:hasPet> <ex:somePet>.
}
rewrite to
INSERT DATA {
ex:hasPet a owl:ObjectProperty;
rdfs:domain ex:Human;
rdfs:range ex:Pet.
ex:someHuman ex:hasPet ex:somePet.
}

didn't get my column values against my column attributes in sparql

Can anyone help me in sparql. I'm working on ontology based project. I've a structure in protege-owl like
City-hasWeatherConditions-(multiple value i.e temp,dewpoint)
I want to get values against each attribute.
Query:
PREFIX pro: <http://www.semanticweb.org/computer/ontologies/2019/0/fyp.owl#>
select ?indi ?val
where
{
{
select ?indi
where
{
pro:Lahore
<http://www.semanticweb.org/computer/ontologies/2019/0/fyp.owl#hasWeatherCondition>
?indi
}
}
{
select ?val
where
{
<http://www.semanticweb.org/computer/ontologies/2019/0/fyp.owl#DewPoint>
<http://www.semanticweb.org/computer/ontologies/2019/0/fyp.owl#hasDewPoint>
?val
}
}
}
Figure1: Note -- 5.21 is DewPoint value which is repeated in temp.
Figure2:

Exact match of variable string in SPARQL Wikidata Query Service

Exact match of variable string in SPARQL Wikidata Query Service at https://query.wikidata.org does not give the the results I expected.
I was expecting I could do:
SELECT * {
hint:Query hint:optimizer "None" .
{ SELECT DISTINCT (xsd:string(?author_name_) AS ?author_name) { wd:Q5565155 skos:altLabel ?author_name_ . } }
?work wdt:P2093 ?author_name .
}
But I get no returned results from the Wikidata Query Service:
However, if I use the "=" comparison, I can match the strings:
SELECT * {
hint:Query hint:optimizer "None" .
{ SELECT DISTINCT (xsd:string(?author_name_) AS ?author_name) { wd:Q5565155 skos:altLabel ?author_name_ . } }
?work wdt:P50 wd:Q5565155 .
?work wdt:P2093 ?author_name__ .
FILTER (?author_name = ?author_name__)
}
With the current data in Wikidata, I get five rows returned in this query.
Another way to get this data is by using a BIND:
SELECT * {
BIND("Knudsen GM" AS ?author_name)
?work wdt:P2093 ?author_name .
}
I suppose there might be something wrong with the casting as this does not return anything:
SELECT * {
BIND(xsd:string("Knudsen GM") AS ?author_name)
?work wdt:P2093 ?author_name .
}
Combinations with xsd:string changed to STR or no conversion at all in the original query do neither yield result rows.

Conditional subquery in SPIN function (SPARQL)

How do I change the query formula based on whether or not a variable is bound?
I am invoking the magic property like this:
WHERE {
VALUES (?subj) {
([my bound positional parameter value goes here...])
}
?subj :myMagicProperty ?result .
}
Inside the magic property, I do a union:
?result a :Rule .
{
?result :someProp ?subj .
}
UNION
{
FILTER NOT EXISTS {
?result :someProp ?anyValue .
}
}
In other words, get me all results where :someProp is this value or :someProp is not defined.
Here is the tricky part. If ?subj is unbound (i.e., I set it as UNDEF in the VALUES block), the above query goes wild and returns everything.
Instead, I want to check if ?subjis unbound. If ?subj is unbound, :myMagicProperty should only return the following results:
FILTER NOT EXISTS {
?result ?someProp ?anyValue .
}
I have experimented with using FILTER and the BOUND function, but I can't figure out how to get the correct behavior. How can I drop one of UNION clauses from my query when ?subj is not bound?
Updates
Revised the first query to add the VALUES block.
Added missing ?result a :Rule . statement.
Corrected ?someProp to :someProp.
First I'd like to confirm what your intent is. I'd like to do that by asking you to respond to the following query that you can run in TopBraid Composer.
SELECT *
WHERE { GRAPH <http://topbraid.org/examples/kennedys> {
VALUES (?property) {(kennedys:firstName) (kennedys:lastName) (UNDEF)}
{
FILTER(BOUND(?property) )
?s ?property ?result .
}
UNION
{
FILTER(!BOUND(?property))
BIND("not sure what you want to do in this case" AS ?result)
}
}
}
The difference in the code above to your code is that I am setting values of your ?someProp in the VALUES statement, whereas you are setting ?subj.
The UNIONed subgraphs are using BOUND and !BOUND as guards.
Before going further with help I'd like to hear from you with a clearer explanation of the query you are wanting to build. Then I can show you the magic property that will be needed.
It's this piece of your initial post I need to understand more:
Here is the tricky part. If ?subj is unbound (i.e., I set it as UNDEF in the VALUES block), the above query goes wild and returns everything.
Instead, I want to check if ?subj is unbound. If ?subj is unbound, myMagicProperty should only return the following results:
FILTER NOT EXISTS {
?result ?someProp ?anyValue .
}*
With ?someProp undefined, as well as ?result and ?anyValue, what were you expecting to come back? Also this subgraph of yours has no assertions that will populate the graph and therefore will return nothing.
Ralph
The trick is, I need to do the UNION using a variable different than the one passed in as an argument. This way, the UNION operation does not cause the unbound parameter to be bound. After the UNION, I can use a FILTER to control the results based on the input parameter.
SELECT ?result
WHERE {
?result a :Rule .
{
SELECT ?rule ?value ?anyValueMatch
WHERE {
{
?rule :someProp ?value .
BIND (false AS ?anyValueMatch) .
}
UNION
{
FILTER NOT EXISTS {
?rule :someProp ?any .
} .
BIND (true AS ?anyValueMatch) .
} .
}
} .
FILTER ((bound(?subj) && (?value = ?subj)) || (?anyValueMatch = true)) .
}
Another way to do this is with COALESCE:
SELECT ?result
WHERE {
?result a :Rule .
OPTIONAL {
?result :someProp ?value .
}
FILTER (COALESCE(?value = ?subj, !bound(?value)))
}
...this avoids the sub-select and simply filters to include only the ?result matches where '?value = ?subj', and if that clause fails the !bound() clause ensures matches that do not have a :someProp property are also included.

SPARQL DATASET definition

Can anyone explain to me what this statement "Dataset(QuadPattern,μ,GS,GS)" means?Especially, I am trying to figure out the model of DELETE DATA operation (DELETE DATA QuadData) , but I cant understand what Dataset(QuadPattern,{},GS,GS) means.
You seem to be referring to the SPARQL 1.1 Update spec:
Dataset(QuadPattern,μ,DS,GS) ...[an] auxiliary function constructs an RDF Dataset from a QuadPattern, given a solution mapping and an RDF Dataset.
Put simply it's function which takes a bunch of RDF in graphs which may include variables, e.g.:
GRAPH ?g { ?person a Person ; ex:tel ?tel }
{ ?g ex:source ?source }
and a set of solutions μ:
{ ?g => <http://example.com/graph1> , ?person => <http://example.com/alice> , ?tel => "0898 505050" , ?source => <http://192.com/> }
{ ?g => <http://example.com/graph2> , ?person => <http://example.com/bob> , ?tel => "117 117" , ?source => <http://192.com/> }
and binds those values, resulting in a dataset:
{
<http://example.com/graph1> ex:source <http://192.com/> .
<http://example.com/graph2> ex:source <http://192.com/> .
}
GRAPH <http://example.com/graph1> { <http://example.com/alice> a Person ; ex:tel "0898 505050" }
GRAPH <http://example.com/graph2> { <http://example.com/bob> a Person ; ex:tel "117 117" }