Search code examples
rubyrdfsesame

Context for RDF Graph can not be set


I am trying to give a context to my triples:

repo = Sesame_Repository.new("http://localhost:8080/openrdf-sesame/repositories/CONTEXT")
graph = RDF::Graph.new

sub = RDF::URI("https://force.it/base")
pre = RDF::URI("https://force.it/has")
obj = RDF::Literal("ABC Weapons")
context = RDF::URI("https://force.it")

graph << [sub, pre, obj, context]
graph.each_statement do |statement|
repo.public_insert_statement(statement)
end

But the context ist not set. Is this not possible anymore? or how can I set the context to my graph?

( I can save triples but when I am adding context it wether saves the context nor the triples)

EDIT: When using repository instead of graph the context stays empty

graph = RDF::Repository.new << RDF::Statement.new(sub, pre, obj, :context => RDF::URI("https://force.it"))

Solution

  • Thanks to gkellog (developer of ruby rdf gem) :" A graph is a simple container for triples. When it's creates, you can provide it with a single context. This is because a graph is really a projection of a named graph from a repository (dataset). You can use RDF::Repository instead of RDF::Graph in this example, as a repository does support contexts. The RDF concept is of a Quad, where the last e,event is an optiona graph name (uri or BNode). The notion of context can me earlier in RDF.rb. " The topic got much more clarified. – to the github-issue of ruby RDF

    SOLUTION FOR Sesame:

    # create repository from public Sesame_Repository (see lib/rdf/sesame_repo.rb)
    repo = Sesame_Repository.new("http://localhost:8080/openrdf-sesame/repositories/CONTEXT")
    #Save Context
    repo.set_context(RDF::URI("https://force.it/"))
    
    # --intialize graph for sesame --
    #create triple  
    sub = RDF::URI("https://force.it/base")
    pre = RDF::URI("https://force.it/has")
    obj = RDF::Literal("Machinegun")
     # save triple to the repo
    graph = RDF::Repository.new << RDF::Statement.new(sub, pre, obj)
    graph.each_statement do |statement|
       repo.public_insert_statement(statement)
        puts statement.inspect
    end