Hello all,

This is my first post on the forums. I found plenty of answers to my questions in the forum however sometimes I have difficulties to find out exactly what I want. This is why I am asking you now.

i don't know if the title of this post can explain what I want however I will try to explain it here in more details.

So I have a variable ?place that matches some values from the named graph 1. Lets say it matched the graph1:New_York value. Then I want to pass this variable to the second named graph in the form of ?place_graph_2 owl:sameAs ?place and get the matches that I found there. However what I will get is the following example:

graph2:New_York owl:sameAs graph1:New_York

This is the answer that I want however then the query will not stop and it will practically match every owl:sameAs triple that is on named graph 2. So in addition I will get the triple graph2:London owl:sameAs graph2:London_2.

I don't want to receive answers that have nothing to do with the initial content of ?name variable which means that graph2:London and graph2:London_2 even if they are bound with owl:sameAs are irrelevant to me!

So to summarize and make the question even more general. Is there a way that a variable will maintain its content and only get bigger when there is a specific match?

Thank you for your time!

Update so here is an example query:

SELECT DISTINCT ?places 
FROM NAMED <1.ttl> 
FROM NAMED <2.ttl>
WHERE { 
    { GRAPH <1.ttl> 
         { dbpedia:New__York owl:sameAs ?places }
    }
    UNION 
    { GRAPH <2.ttl> 
         { ?new_places owl:sameAs ?places } 
    }
} #end query

With this query I will take as a result from the first graph that: dbpedia:New_York owl:sameAs dataset_1:New_York

Then on the second graph I will get as a result: dataset_2:New_York owl:sameAs dataset_1:New_York (this is because the ?places variable already had this value matched from the graph 1 and it also bound with a triple from graph 2), however, I will also get the triples: dataset_2:London owl:sameAs dataset_1:London and dataset_2:dog owl:sameAs dbpedia:dog, which is irelevant to me, because I wanted to match only values that where from before inside the ?places (only the dataset_1:New_York value) and not everyhing which binds with the ?new_places owl:sameAs ?place triple pattern.

asked 09 Nov '12, 07:06

Christos's gravatar image

Christos
15417
accept rate: 33%

edited 11 Nov '12, 12:16

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
11.3k412

1

@Christos, welcome.

http://answers.semanticweb.com/questions/14494/welcome-to-answerssemanticwebcom

It's a little unclear to me what you would like to try. It would probably help a lot if you could edit your question to post the query you are currently trying to use.

(09 Nov '12, 13:12) Signified ♦ Signified's gravatar image

@Christos, please do not provide additional information as an answer. Instead, edit your question, or post comments. I have taken care of converting your answer this time around.

(11 Nov '12, 12:15) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

Thank you! I appreciated it :)

(11 Nov '12, 12:24) Christos Christos's gravatar image

Hello. I am returning to this question. You write that the ?newplaces will bind values. However from the tests that I have done, the value that is captured in ?new_places is not going to be inside the ?places variable which is what I want, because I want to have a central variable that contains all the binds.

Seems like a slightly strange request, but okay. :)

SELECT DISTINCT ?places 
FROM NAMED <1.ttl> 
FROM NAMED <2.ttl>
WHERE { 
  { {
    GRAPH <1.ttl> 
    { dbpedia:New__York owl:sameAs ?places } }
  } UNION {
    GRAPH <1.ttl> 
    { dbpedia:New__York owl:sameAs ?tempplaces } }
    GRAPH <2.ttl> 
    { ?places owl:sameAs ?tempplaces } }
  }
}

There could be more elegant ways to do this using (e.g.) property paths if

  1. you had an engine supporting SPARQL 1.1; and
  2. you didn't need to specify the GRAPHS for the individual patterns (they can be kept in FROM NAMED).

which would be ...

SELECT DISTINCT ?places 
FROM NAMED <1.ttl> 
FROM NAMED <2.ttl>   
WHERE {
   dbpedia:New__York owl:sameAs|(owl:sameAs/^owl:sameAs) ?places .
}

I now roughly understand what you mean by "growing" the bindings for a variable that is already bound ... to perform a disjunction (OR) ... the only way to do that is within the scope of a UNION (or maybe a disjunctive FILTER or property path). Otherwise, all other features of SPARQL are conjunctive, meaning AND, meaning that they cannot extend previous sets of bindings for variables that have been evaluated somewhere already.

link

answered 20 Nov '12, 14:04

Signified's gravatar image

Signified ♦
23.1k623
accept rate: 38%

1

I see your point on your first query! I will try it out and inform you about the results whether they are the one expected or not. To your second query now, I learned a lot about the property paths and I am using them a lot! Thank you for your suggestions, i really apreciated!

(20 Nov '12, 14:13) Christos Christos's gravatar image

The problem is that you are using a UNION construction, which is precisely not what you want. UNION gives you the matches that occur in either the first OR the second part of your query, and what you want is only those results that match in both (so AND).

This is actually simpler. Just get rid of the UNION:

SELECT DISTINCT ?places 
FROM NAMED <1.ttl> 
FROM NAMED <2.ttl>
WHERE { 
    {GRAPH <1.ttl> 
         { dbpedia:New__York owl:sameAs ?places } }
    { GRAPH <2.ttl> 
         { ?new_places owl:sameAs ?places } 
    }
}
link

answered 11 Nov '12, 12:20

Jeen%20Broekstra's gravatar image

Jeen Broekstra ♦
11.3k412
accept rate: 37%

Thanks for your answer! It wasn't exactly the solution that I wanted but it guided me to think differently about my query structure.

One more sub-question which is related to this one.

I want the ?places variable to be like a static variable. Let's say it contains 5 values, if I use the variable inside a triple pattern and is not bound with any new value then it will lose all the previous values. I want even if I find no matchings the variable to retain its values in it.

(11 Nov '12, 16:36) Christos Christos's gravatar image
1

SPARQL has no notion of a "static" variable, and thinking about SPARQL variables as "growing" or "losing values" is IMHO not the right way to go - to be honest I don't quite understand what you are after. Perhaps you should ask this as a new question instead, and explain a in a bit more detail what your goal is, as there are probably other, better ways to achieve what you want.

(11 Nov '12, 23:00) Jeen Broekstra ♦ Jeen%20Broekstra's gravatar image

I want the ?places variable to be like a static variable. Let's say it contains 5 values, if I use the variable inside a triple pattern and is not bound with any new value then it will lose all the previous values. I want even if I find no matchings the variable to retain its values in it.

Just based on this comment for Jeen's answer, sounds like you're looking for OPTIONAL.

SELECT DISTINCT ?places 
FROM NAMED <1.ttl> 
FROM NAMED <2.ttl>
WHERE { 
   { GRAPH <1.ttl> 
      { dbpedia:New__York owl:sameAs ?places } }
   OPTIONAL {
      { GRAPH <2.ttl> 
        { ?new_places owl:sameAs ?places } 
      }
   }
}

This will keep all values bound for ?places outside of the OPTIONAL, but will also bind values for ?newplaces (only if available).

Please see the appropriate section of the SPARQL spec. for more details and some examples of OPTIONAL.

link

answered 12 Nov '12, 17:38

Signified's gravatar image

Signified ♦
23.1k623
accept rate: 38%

Thank you Signified for your post. I also came up the previous days with this solution and I think is the only way to mimic a static-non changeable variable.

Initially i putted the OPTIONAL clause inside the GRAPH and not outside of it, but I guess this is the same since I am getting the same results.

I will also ask one more question, however I will open a new question thread for it. My queries are starting slowly giving me the results I expect!

(13 Nov '12, 11:33) Christos Christos's gravatar image

Hello. I am returning to this question. You write that the ?newplaces will bind values. However from the tests that I have done, the value that is captured in ?new_places is not going to be inside the ?places variable which is what I want, because I want to have a central variable that contains all the binds.

Thats why I also asked the other question some days ago and you told me to use UNION. However in this case, if I use UNION I will also get other irrelevant owl:sameAs triples.

(20 Nov '12, 04:27) Christos Christos's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×1,159
×7
×7

Asked: 09 Nov '12, 07:06

Seen: 789 times

Last updated: 20 Nov '12, 14:13