Ania David February 2016

how to create aggregation on a graph from CONSTRUCT

This is my query:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rs: <http://www.welovethesemanticweb.com/rs#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    construct {
  ?subject0 rs:similarityValue ?similairty0.
    ?subject1 rs:similarityValue ?similairty1
}
WHERE {

  {
  ?subject0 ?predicate0 ?object0.
  rs:Impromptu_No._1 ?predicate0 ?object0.
    ?predicate0 rs:hasSimilarityValue ?similairty0Helper.
    BIND(?similairty0Helper * (4/9) AS ?similairty0)
    FILTER (?subject0 != rs:Impromptu_No)  
  }
  union {
    ?subject1 ?predicate ?object.
    ?object ?predicate1 ?object1.
    ?predicate1 rs:hasSimilarityValue ?similairty1Helper.
    rs:Impromptu_No._1 ?predicateHelper ?objectHelper.
    ?objectHelper ?predicate1 ?object1
      BIND(?similairty1Helper * (1/9) AS ?similairty1)
    FILTER (?subject1 != rs:Impromptu_No._1)
  }
}

and the result is:

rs:5th_Symphony
      rs:similarityValue
            0.011111111111111112e0 .

rs:Polonaise_heroique
      rs:similarityValue
            0.011111111111111112e0 , 0.17777777777777778e0 , 0.26666666666666666e0 .

rs:Preludes
      rs:similarityValue
            0.011111111111111112e0 , 0.26666666666666666e0 , 0.17777777777777778e0 .

rs:Requiem_Sequentia
      rs:similarityValue
            0.011111111111111112e0 .

rs:Le_nozze_di_Figaro
      rs:similarityValue
            0.011111111111111112e0 .

rs:Symphony_No._29_in_A_major
      rs:similarityValue
            0.011111111111111112e0 .

rs:Piano_Concerto_No._24
      rs:similarityValue
            0.011111111111111112e0 .

rs:Impromptu_No._1
      rs:similarityValue
            0.26666666666666666e0 , 0.17777777777777778e0 .

rs:Sonata_Pathetique
      rs:similarityValue
            0.011111111111111112e0 .

rs:Dies_Irae
      rs:similarityValue
            0.011111111111111112e0 .

rs:Piano_Sonata_No._31
      rs:similarityValue
            0.011111111111111112e0 ,        

Answers


Joshua Taylor February 2016

First, it's probably better to make sure that you can select all the information that you're trying to retrieve. It looks like you're aiming for something like this:

prefix rs: <http://www.welovethesemanticweb.com/rs#>

select distinct ?s ?weight ?factor where {
  #-- ?x is the special value of interest.  This
  #-- is pulled out into a VALUES block just for
  #-- convenience; there's just one place to change
  #-- rs:Impromptu_No._1, now.
  values ?x { rs:Impromptu_No._1 }

  #-- find ?s which are "one step" away from
  #-- a common property/value with ?x, and
  #-- take 4/9 as ?weight.
  {
    ?s ?p ?o .
    ?x ?p ?o .
    bind(4/9 as ?weight)
  }
  union
  #-- find ?s which are are "two steps" away from
  #-- a common property/value with ?x, and take
  #-- 1/9 as ?weight
  {
    ?s ?a ?b . ?b ?p ?o .
    ?x ?c ?d . ?d ?p ?o .
    bind(1/9 as ?weight)
  }

  #-- get the similarity factor of the property
  #-- and make sure that ?s is different from ?x.
  ?p rs:hasSimilarityValue ?factor .
  filter(?s != ?x)
}
-----------------------------------------------------------------------------------------------------------------------
| s                                   | weight                     | factor                                           |
=======================================================================================================================
| rs:5th_Symphony                     | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Dies_Irae                        | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Le_nozze_di_Figaro               | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Concerto_No._24            | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata 

Post Status

Asked in February 2016
Viewed 3,693 times
Voted 13
Answered 1 times

Search




Leave an answer