why this variable is never have a value

2019-08-12 06:00发布

问题:

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.

回答1:

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.