Hi there, I am just starting with Sparql and RDF. We are using a Virtuoso endpoint, and now I am trying to investigate the data, and I want to "find" more data, I want to find how items are related, but not just on the first level.

I will give you an example of what I want:

select ?obj2 ?p2 ?obj ?p1 ?ing
where 
{

        ?ing rdfs:label "corn starch".
        ?obj ?p1 ?ing.
        ?obj2 ?p2 ?obj
} 
LIMIT 100

This will find me objects, two levels deep. I could easily extend this to find three levels, four levels, ... Then I read about TRANSITIVE queries in Vrituoso, which sounds extremely interesting, but I am not sure if it applies, because I want to find related items, levels deep, regardless of the type of the relation (?p).

E.g. theoretically, the following query could replace the previous:

select ?obj ?dist
where 
{
    {
      select ?ing ?obj
      where
      { 
        ?obj ?p1 ?ing
      }
    }
    OPTION ( TRANSITIVE, 
                    t_in (?obj), 
                    t_out (?ing), 
                    t_distinct, 
                    t_min (1),
                 t_min (1), 
                 t_max (4), 
                 t_step ('step_no') as ?dist )  .
    FILTER(?ing=<http://www4.wiwiss.fu-berlin.de/dailymed/resource/ingredient/corn_starch>)
} 
LIMIT 100

However, this query does not give me the same results as the first one. Is there a smart way to make this work? Or should I be looking at something like:

select ?ing ?p1 ?obj1 ?p2 ?obj2 ?p3 ?obj3
where 
{

        ?ing rdfs:label "corn starch".
        ?obj1 ?p1 ?ing.
        OPTIONAL { ?obj2 ?p2 ?obj1 }
        OPTIONAL { ?obj3 ?p3 ?obj2 }
} 
LIMIT 100

Which does not return any result, so I am still doing something wrong. But anyway that approach seems like it could work, I should have to UNION for relations which are defined in the other direction as well, but it feels like there should be an easier way.

So in short: what is the best way to find related items, indifferent of the specific type of the relation (and direction), and to preferable some levels deep?

Possible idea (not sure how to execute it): should I define an inference, a superclass of all possible relations (?p), so I could do the TRANSITIVE query on that? Is that possible and how should that work?

asked 01 Feb '13, 06:45

nathanvda's gravatar image

nathanvda
112
accept rate: 0%

edited 01 Feb '13, 06:58


Ok, I found my answer, after playing around with sparql the whole day, I was able to a transitive query in virtuoso to find all objects somehow "pointing" to my object.

select ?ing ?obj ?step
where 
{
    {
      select ?obj ?ing
      where
      { 
        ?obj ?p1 ?ing
      }
    }
    OPTION ( TRANSITIVE, t_in (?obj), t_out (?ing), t_distinct, t_no_cycles,
                 t_min (1), t_max (4), t_step ('step_no') as ?step
                )  .
    FILTER(?ing=<http://www4.wiwiss.fu-berlin.de/dailymed/resource/ingredient/corn_starch>)
} 
ORDER BY DESC (?step)
permanent link

answered 01 Feb '13, 11:34

nathanvda's gravatar image

nathanvda
112
accept rate: 0%

I can't accept my own answer? Why?

(06 Feb '13, 03:52) nathanvda nathanvda'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

Question tags:

×1,328
×204

question asked: 01 Feb '13, 06:45

question was seen: 701 times

last updated: 06 Feb '13, 03:52