coalesce not working in this example

2019-09-12 17:47发布

问题:

This is my minimum data:

    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX rs: <http://www.SemanticRecommender.com/rs#>
    PREFIX mo: <http://www.musicontology.com/mo#>
    PREFIX  : <http://www.musicsemanticontology.com/mso#>

    mo:5th_symphony_for_beethoven a mo:GermanSymphony .
    :symphonyFestival2016 a rs:TemporalContext ; rs:appliedOnItems mo:GermanSymphony ; rs:canBeRecommendedFrom "2016-07-01T00:00:00-00:00"^^xsd:dateTime ; rs:canBeRecommendedUntil "2016-09-01T00:00:00-00:00"^^xsd:dateTime .

This is my query:

   PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rs: <http://www.SemanticRecommender.com/rs#>
PREFIX mo: <http://www.musicontology.com/mo#>
PREFIX  : <http://www.musicsemanticontology.com/mso#>

   SELECT ?item  ?resultForMe  ?matchedWeight ?defaultMatchedTemporalContext
    WHERE
      {
    values ?item {mo:5th_symphony_for_beethoven}
          VALUES ( ?defaultMatchedTemporalContext ?defaultNotMatchedTemporalContext ?defaultNoTemporalContext ) {( 2 0.5 1 )}
            ?temporalContext
                      rdf:type           rs:TemporalContext ; rs:appliedOnItems  ?itemClass .
                       OPTIONAl {       ?item     rdf:type           ?itemClass
            OPTIONAL { ?temporalContext rs:canBeRecommendedFrom  ?fromLimit }
            OPTIONAL { ?temporalContext rs:canBeRecommendedUntil  ?untilLimit  }
            OPTIONAL { ?temporalContext rs:hasWeightIfContextMatched  ?matchedWeight }
            OPTIONAL { ?temporalContext rs:hasWeightIfContextDoesNotMatch  ?unmatchedWeight }
     BIND(if(( (?fromLimit <= now()) && (now() <= ?untilLimit ) )  , coalesce (?matchedWeight, ?defaultMatchedTemporalContext),2 ) as ?resultForMe)
    }
}

This is the results;

Look at the resultForMe, it doesn't have any values, why? this is my question though the defaultMatchedTemporalContext has a value.

I gave you the so minimum data required, you can upload the data on your SPARQL store if you want, it is just 4 lines of rdf

回答1:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rs: <http://www.SemanticRecommender.com/rs#>
PREFIX mo: <http://www.musicontology.com/mo#>
PREFIX  : <http://www.musicsemanticontology.com/mso#>

   SELECT ?item  ?resultForMe  ?matchedWeight ?defaultMatchedTemporalContext
    WHERE

{
    VALUES ?item {mo:5th_symphony_for_beethoven}
    VALUES ?defaultNoTemporalContext  { 1 }
    VALUES ?defaultMatchedTemporalContext { 2 }
    VALUES ?defaultNotMatchedTemporalContext { 0.5 }
    ?temporalContext
                      rdf:type           rs:TemporalContext ; rs:appliedOnItems  ?itemClass .
                       OPTIONAl {       ?item     rdf:type           ?itemClass.
 VALUES ?defaultMatchedTemporalContext { 2 }

            OPTIONAL { ?temporalContext rs:canBeRecommendedFrom  ?fromLimit }
            OPTIONAL { ?temporalContext rs:canBeRecommendedUntil  ?untilLimit  }
            OPTIONAL { ?temporalContext rs:hasWeightIfContextMatched  ?matchedWeight }
            OPTIONAL { ?temporalContext rs:hasWeightIfContextDoesNotMatch  ?unmatchedWeight }
     BIND(if(( (?fromLimit <= now()) && (now() <= ?untilLimit ) )  , coalesce (?matchedWeight, ?defaultMatchedTemporalContext) ,2 ) as ?resultForMe)
    }
}

The problem was that the values clause was in above clause than the bind, putting them in the same clause solves my problem