2

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 , 0.26666666666666666e0 .

rs:Violin_Concerto_No._5_in_A_major
      rs:similarityValue
            0.011111111111111112e0 .

As you see, for each instance, there are many values, I want to aggregate them and make the SUM of them for each instance. I would do that with SELECT, but with CONSTRUCT, I didn't know how to apply the aggregation.

After reading, i found that we can't use the aggregation directly from CONSTRUCT, but i'd need to use SELECT and CONSTRUCT together, it seems i have to use something named "named graph" but i didn't know how to do that even i tried.

your help is highly appreciated.

Many thanks, best regards,

Update 1

One of the ways I've tried is:

construct {
  ?subject0 rs:similarityValue ?similairty0.
    ?subject1 rs:similarityValue ?similairty1
}
WHERE {
  GRAPH ?g  {?subject0 rs:similarityValue ?similairty0}.
  {
  ?subject0 ?predicate0 ?object0.
....

but i got empty results

6
  • Possible typoes again. similairty should be similarity (switched 'airty' and 'arity'). Commented Feb 8, 2016 at 16:41
  • That said, we can't really help much without seeing your data as well as your query. You have in your query some structure like ?s ?p ?o . ?p :hasSimilarityValue ?r, and while you certainly can have properties on your properties, I wonder if that's how your data is actually structured. That would be a little bit unusual. Commented Feb 8, 2016 at 16:45
  • @JoshuaTaylor i corrected the typo, but still no results, I'll upload for you again the data, is that good for you pleaes ? Commented Feb 8, 2016 at 16:50
  • 1
    That will certainly help, but we also don't know what the query is actually supposed to do, which makes it harder to figure out why it's not working. Some explanatory comments in your query would help. (# introduces a comment in SPARQL.) Commented Feb 8, 2016 at 16:52
  • @JoshuaTaylor i updated the question now and gave you the full query with the prefix and I correct the result, now you have everything i have :) Commented Feb 8, 2016 at 17:11

1 Answer 1

5

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_No._31              | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata_No._31              | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique               | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique               | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique               | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes                         | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes                         | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes                         | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Requiem_Sequentia                | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Sonata_Pathetique                | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Symphony_No._29_in_A_major       | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Violin_Concerto_No._5_in_A_major | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
-----------------------------------------------------------------------------------------------------------------------

Now, it seems like after this, you want to group by by the value of ?s and sum the weighted similarities:

select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
  values ?x { rs:Impromptu_No._1 }
  {
    ?s ?p ?o .
    ?x ?p ?o .
    bind(4/9 as ?weight)
  }
  union
  {
    ?s ?a ?b . ?b ?p ?o .
    ?x ?c ?d . ?d ?p ?o .
    bind(1/9 as ?weight)
  }

  ?p rs:hasSimilarityValue ?factor .
  filter(?s != ?x)
}
group by ?s
----------------------------------------------------------------
| s                                   | similarity             |
================================================================
| rs:5th_Symphony                     | 0.044444444444444446e0 |
| rs:Piano_Concerto_No._24            | 0.044444444444444446e0 |
| rs:Requiem_Sequentia                | 0.044444444444444446e0 |
| rs:Dies_Irae                        | 0.044444444444444446e0 |
| rs:Piano_Sonata_No._31              | 0.31111111111111117e0  |
| rs:Symphony_No._29_in_A_major       | 0.044444444444444446e0 |
| rs:Le_nozze_di_Figaro               | 0.044444444444444446e0 |
| rs:Violin_Concerto_No._5_in_A_major | 0.044444444444444446e0 |
| rs:Sonata_Pathetique                | 0.044444444444444446e0 |
| rs:Preludes                         | 0.48888888888888893e0  |
| rs:Polonaise_heroique               | 0.48888888888888893e0  |
----------------------------------------------------------------

Finally, since you've got the values that you're looking for, you can now construct the triples that you want:

construct {
  ?s rs:similarityValue ?similarity
}
where {{
  select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
    values ?x { rs:Impromptu_No._1 }
    {
      ?s ?p ?o .
      ?x ?p ?o .
      bind(4/9 as ?weight)
    }
    union
    {
      ?s ?a ?b . ?b ?p ?o .
      ?x ?c ?d . ?d ?p ?o .
      bind(1/9 as ?weight)
    }
    ?p rs:hasSimilarityValue ?factor .
    filter(?s != ?x)
  }
  group by ?s
}}
@prefix :      <http://www.semanticweb.org/rs#> .
@prefix rs:    <http://www.welovethesemanticweb.com/rs#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

rs:5th_Symphony  rs:similarityValue  0.044444444444444446e0 .

rs:Polonaise_heroique
        rs:similarityValue  0.48888888888888893e0 .

rs:Preludes  rs:similarityValue  0.48888888888888893e0 .

rs:Requiem_Sequentia  rs:similarityValue
                0.044444444444444446e0 .

rs:Le_nozze_di_Figaro
        rs:similarityValue  0.044444444444444446e0 .

rs:Symphony_No._29_in_A_major
        rs:similarityValue  0.044444444444444446e0 .

rs:Piano_Concerto_No._24
        rs:similarityValue  0.044444444444444446e0 .

rs:Sonata_Pathetique  rs:similarityValue
                0.044444444444444446e0 .

rs:Dies_Irae  rs:similarityValue  0.044444444444444446e0 .

rs:Piano_Sonata_No._31
        rs:similarityValue  0.31111111111111117e0 .

rs:Violin_Concerto_No._5_in_A_major
        rs:similarityValue  0.044444444444444446e0 .
Sign up to request clarification or add additional context in comments.

5 Comments

Let us know when we can buy your book.
Thanks for the great efforts and sorry for answering late, that's what I wanted though I though the final result could be a table select not a graph construct, anyway, I can handle that, but just one question, may I ask you please why you use the same ?p variable name in both union, do you imply they are the same? or it doesn't matter because each one in different scope ?
@AniaDavid I only used construct because that's what the example query in your question used. If you just want tags table, then the next to last query should be fine, right?
@AniaDavid I used the same p variable in each part of the union for the same reason that I also used the same s, o, factor, discount, and x variables; as I understand it, the point of the query is that there are two options or alternative cases for finding p and o (one away and two away). After finding them though, you want to do the same thing (get the similarity factor and multiply by the discount). Note that retrieving p's similarity factor happens outside the union.
please help me stackoverflow.com/questions/35553683/… I am stuck there, my solution is not good because its performance is a disaster, and i need the two aggregation in many places in many levels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.