# BSBM Explore Query 5: Find product that are similar to a given product.
#
# Use Case Motivation: The consumer has found a product that fulfills his requirements. He now wants to find products with similar features. 
#
#    touches a lot of data
#    uses more complex FILTER clauses
#    uses LIMIT
#
# A hand coded rewrite.  Two sub-groups are extracted. Each sub-group is
# identified by the presence of a join which does not have any explicitly
# shared variables but where there is a constraint imposed through a FILTER
# which runs with the 2nd statement pattern in the sub-group.
# 
# Note: For BSBM, it works out that these groups can be united with the
# parent group using a hash join on [?product]. It would be a pretty
# odd query if there were no such variable which could unite the groups.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>

SELECT DISTINCT ?product ?productLabel
WHERE {
        hint:Query hint:optimizer "None" .
        <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/dataFromProducer1/Product4> bsbm:productFeature ?prodFeature .
        ?product bsbm:productFeature ?prodFeature .
        FILTER (<http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/dataFromProducer1/Product4> != ?product)
        {
        <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/dataFromProducer1/Product4> bsbm:productPropertyNumeric1 ?origProperty1 .
        ?product bsbm:productPropertyNumeric1 ?simProperty1 .
        FILTER (?simProperty1 < (?origProperty1 + 120) && ?simProperty1 > (?origProperty1 - 120))
        }
        {
        <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/dataFromProducer1/Product4> bsbm:productPropertyNumeric2 ?origProperty2 .
        ?product bsbm:productPropertyNumeric2 ?simProperty2 .
        FILTER (?simProperty2 < (?origProperty2 + 170) && ?simProperty2 > (?origProperty2 - 170))
        }
        ?product rdfs:label ?productLabel .
}
ORDER BY ?productLabel
LIMIT 5
