How to recursively expand blank nodes in SPARQL construct query?

前端 未结 3 1674
南方客
南方客 2021-02-20 00:10

There is probably an easy to answer to this, but I can\'t even figure out how to formulate the Google query to find it.

I\'m writing SPARQL construct queries against a d

3条回答
  •  無奈伤痛
    2021-02-20 00:46

    With regard to working with the ruby RDF.rb library, which allows SPARQL queries with significant convenience methods on RDF::Graph objects, the following should expand blank nodes.

    rdf_type = RDF::SCHEMA.Person # for example
    rdf.query([nil, RDF.type, rdf_type]).each_subject do |subject|
      g = RDF::Graph.new
      rdf.query([subject, nil, nil]) do |s,p,o|
        g << [s,p,o]
        g << rdf_expand_blank_nodes(o) if o.node?
      end
    end
    
    def rdf_expand_blank_nodes(object)
      g = RDF::Graph.new
      if object.node?
        rdf.query([object, nil, nil]) do |s,p,o|
          g << [s,p,o]
          g << rdf_expand_blank_nodes(o) if o.node?
        end
      end
      g
    end
    

提交回复
热议问题