why this variable is never have a value - sparql

I am checking that if an instance has a value for a specific predicate, bound that value to a specific variable, otherwise, bound that variable to the value 1 of type integer. this is my code
select ?boosted where {
:r1 a ?x
optional
{
?item rs:boostedBy ?boostedOptional
bind (if(bound(?boostedOptional), ?boostedOptional, "1"^^xsd:integer) as ?boosted)
}
}
the value of ?boosted is always empty, look please
why please?
Note
I think you don't need data to test why my code is not working, because for me it sounds like a general mistake about using the bound. however, if you want data, i give you data.
note2
there is no rs:boostedBy predicate from the first place, so i was looking to have the default value always , which is 1 of type integer in this case.

The IF needs to be outside of the optional graph pattern:
SELECT ?boosted WHERE {
:r1 a ?x
OPTIONAL{ ?item rs:boostedBy ?boostedOptional . }
BIND (IF(bound(?boostedOptional), ?boostedOptional, "1"^^xsd:integer) as ?boosted)
}
Secondly, I don't see the relationship between the rs:boostedBy property and the {:r1 a ?x} triple pattern. I.e. are you trying to see if the subject has a boostedBy property? In that case :r1 and ?item should be the same, i.e. both should be :r1 or both should be ?item, if I'm understanding your intent here.

Related

PDDL planner does not recognize types

I wrote a pddl code in a domain file that describes kitchen environment (eggs, coffee, receptacles, surfaces etc.) thus types were defined in hierarchical manner.
(define (domain robochef)
(:requirements :adl :strips :fluents :typing)
(:types
locatable surface - object
cookable receptacle - locatable
food liquid - cookable
mug pan plate coffeeMachine - receptacle
egg - food
coffee water - liquid
table - surface
)
(:constants
ROBOT
)
The predicates of this domain may be dependent on some of the types:
(is-at ?y ?x) ;true iff an object ?y is in front of an object ?x
(is-visible ?x ?r - ROBOT) ;true iff the object visible by the robot
(is-held ?x ?r - ROBOT) ;true iff the robot holds ?x
(contains ?y - receptacle ?x) ;true iff ?x is contained in ?y
(on ?y - surface ?x - locatable) ;true iff ?x is on top of ?y
(is-cooked ?x - cookable) ;true iff ?x is cooked
(is-egg-cracked ?e - egg) ;true iff ?x is cracked
(is-coffeMachine-available ?cm - coffeeMachine) ;true iff the coffee machine is free use
)
Running solver.planning.domains planner on this code resulted in errors of the format "predicate [X] is declared to use unknown or empty type [Y]".
More in detail - the output was:
predicate IS-COFFEMACHINE-AVAILABLE is declared to use unknown or empty type COFFEEMACHINE
predicate IS-EGG-CRACKED is declared to use unknown or empty type EGG
predicate IS-COOKED is declared to use unknown or empty type COOKABLE
predicate ON is declared to use unknown or empty type SURFACE
predicate OCCUPIED is declared to use unknown or empty type RECEPTACLE
predicate CONTAINS is declared to use unknown or empty type RECEPTACLE
predicate IS-HELD is declared to use unknown or empty type ROBOT
predicate IS-VISIBLE is declared to use unknown or empty type ROBOT
Failed to parse the problem -- The types found in the problem file must be a subset of the types listed in the domain file
Domain types: set(['plate', 'coffee', 'coffeemachine', 'liquid', 'food', 'receptacle', 'object', 'locatable', 'surface', 'water', 'mug', 'table', 'cookable', 'egg', 'pan'])
Problem types: set(['default_object'])
predicate IS-COFFEMACHINE-AVAILABLE is declared to use unknown or empty type COFFEEMACHINE
predicate IS-EGG-CRACKED is declared to use unknown or empty type EGG
predicate IS-COOKED is declared to use unknown or empty type COOKABLE
predicate ON is declared to use unknown or empty type SURFACE
predicate OCCUPIED is declared to use unknown or empty type RECEPTACLE
predicate CONTAINS is declared to use unknown or empty type RECEPTACLE
predicate IS-HELD is declared to use unknown or empty type ROBOT
predicate IS-VISIBLE is declared to use unknown or empty type ROBOT
How come the types are explicitly written but not recognized?
You are missing the type ROBOT.
Instead, you have defined a constant named ROBOT with no type specification; it is therefore defaulted to object.
Try with:
(:types
locatable surface ROBOT - object
cookable receptacle - locatable
food liquid - cookable
mug pan plate coffeeMachine - receptacle
egg - food
coffee water - liquid
table - surface
)
(:constants
self - ROBOT
)
And make sure that if ROBOT is used elsewhere in your domain to refer to the constant (and not the type), you replace it with self.

SPARQL - Returning label when object is URI or string when object is Literal

I would like to get the labels (rdfs:label) of objects when the object is a URI. But I would also like to get the string values when the object is a literal string. The issue is, I do not know beforehand if the object is storing a literal or a URI, and in some cases I see a mix of both literals and URIs, like in the image attached.
Any suggestions on how I can return the strings, and if there's an object, return the rdfs:label?
Thanks for your help!
First, this problem shouldn't occur in an ideal world because a property is supposed to be either an object property or a datatype property.
However, when dealing with low quality data where this does occur, I suggest the following workaround:
SELECT ?x ?desc
{
?x dbp:keyPeople ?y.
{?y rdfs:label ?desc. FILTER(isIRI(?y))} UNION
{BIND(STR(?y) AS ?desc). FILTER(!isIRI(?y))}
}
Warning
This is not thoroughly tested. I could only verify that it works correctly on DBpedia Live, which uses Virtuoso 08.03.3319. On the default DBpedia endpoint https://dbpedia.org/sparql on Virtuoso version 07.20.3235 it seems to not work correctly. Also, you need to uncheck both "Strict checking of void variables" and
"Strict checking of variable names used in multiple clauses but not logically connected to each other".

Makes using a URI as predicate them to a property?

I have only the following Turtle statement:
x isAuthorOf y
If I have only this statement where isAuthorOf is used as a predicate, means this that I can conclude that isAuthorOf is also a Property without instanciation (isAuthorOf rdf:type rdfs:Property)?
Thanks in advance.
Yes, an IRI used ad a property implies it is a property. However, without declaration you won't know if it's a datatype or an object property.

How do I retrieve variables from an OPTIONAL statement in SPARQL?

If I give my SPARQL query a known item which could be a number of different types. Depending on which type I'm currently interested in I want to get a certain property from this type (the way to do this will be different for each type) but once I have found this property I want to perform the same operation.
currently I have the following pseudocode:
?object rdf:hasProperty "known Property Type"
OPTIONAL {
?object rdf:hasProperty "property type 1"
#do this thing and store thing of interest in ?variableOfInterest
}
OPTIONAL{
?object rdf:hasProperty "property type 2"
#do different thing and store thing of interest in ?variableOfInterest
}
?thingIAmActuallyInterestedIn rdf:has type ?variableOfInterest
#now do long query
My problem is that outside of the OPTIONAL statement ?variableOfInterest does not get passed out, instead ?thingIAmActuallyInterestedIn is just a list off all objects.
I could put the 'long query' in both of the optional blocks but that would be a huge amount of code replication.
Is there a way to output the ?variableOfInterest from the optional statement rather than it being a dummy variable?
Ensure that ?variableOfInterest is in the SELECT clause, or it's *
?variableOfInterest will be bound in result rows when the OPTIONAL matched, and unbound when the OPTIONAL did not match.
How that gets reflected to your code depends on the API of your SPARQL engine.

SPARQL extension function , where is added to SPARQL grammar?

I want to know when i creating sparql extension functions with apache jena arq, where is it added to the grammar sparql, for the property function and filter function.
The grammar does not change.
A new expression function has a URI and it's invoked as
BIND(my:function(?x,?y) AS ?newValue)
or in FILTER, in SELECT expressions etc.
Register with FunctionRegistry.get().put(....) or use <java:...> for auto-loading.
A property function is a property in a triple pattern:
?S my:propertyFunction ?O .
Register with PropertyFunctionRegistry.get().put(....)