From 02340e834fe8ac6da8f06f5de5be02cbe4e7c9d5 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Mon, 21 Jan 2019 13:56:34 -0800 Subject: [PATCH] Update Repository to remember statement options along with object, and not simply the object itself. This allows inferred statements to be added to the repo, and come back as having been inferred. --- lib/rdf/model/statement.rb | 3 +++ lib/rdf/repository.rb | 16 ++++++++-------- spec/repository_spec.rb | 7 +++++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/rdf/model/statement.rb b/lib/rdf/model/statement.rb index c9d2a129..8a51c31c 100644 --- a/lib/rdf/model/statement.rb +++ b/lib/rdf/model/statement.rb @@ -57,6 +57,9 @@ def self.from(statement, graph_name: nil, **options) # @return [RDF::Term] attr_accessor :object + # @return [Hash{Symbol => Object}] + attr_accessor :options + ## # @overload initialize(**options) # @param [Hash{Symbol => Object}] options diff --git a/lib/rdf/repository.rb b/lib/rdf/repository.rb index bd4f63b8..bf55133b 100644 --- a/lib/rdf/repository.rb +++ b/lib/rdf/repository.rb @@ -325,8 +325,8 @@ def each_statement(&block) @data.each do |g, ss| ss.each do |s, ps| ps.each do |p, os| - os.each do |o| - yield RDF::Statement.new(s, p, o, graph_name: g.equal?(DEFAULT_GRAPH) ? nil : g) + os.each do |o, object_options| + yield RDF::Statement.new(s, p, o, object_options.merge(graph_name: g.equal?(DEFAULT_GRAPH) ? nil : g)) end end end @@ -403,9 +403,9 @@ def query_pattern(pattern, **options, &block) [] end ps.each do |p, os| - os.each do |o| + os.each do |o, object_options| next unless object.nil? || object.eql?(o) - yield RDF::Statement.new(s, p, o, graph_name: c.equal?(DEFAULT_GRAPH) ? nil : c) + yield RDF::Statement.new(s, p, o, object_options.merge(graph_name: c.equal?(DEFAULT_GRAPH) ? nil : c)) end end end @@ -462,7 +462,7 @@ def has_statement_in?(data, statement) data.has_key?(g) && data[g].has_key?(s) && data[g][s].has_key?(p) && - data[g][s][p].include?(o) + data[g][s][p].has_key?(o) end ## @@ -476,9 +476,9 @@ def insert_to(data, statement) c ||= DEFAULT_GRAPH return data.put(c) do |subs| - subs = (subs || Hamster::Hash.new).put(s) do |preds| - preds = (preds || Hamster::Hash.new).put(p) do |objs| - (objs || Hamster::Set.new).add(o) + (subs || Hamster::Hash.new).put(s) do |preds| + (preds || Hamster::Hash.new).put(p) do |objs| + (objs || Hamster::Hash.new).put(o, statement.options) end end end diff --git a/spec/repository_spec.rb b/spec/repository_spec.rb index 85ef10ca..d2d53974 100644 --- a/spec/repository_spec.rb +++ b/spec/repository_spec.rb @@ -55,4 +55,11 @@ expect(solutions.size).to eq unnamed_statements.size end end + + it "remembers statement obtions" do + subject << existing_statement = RDF::Statement(:s, RDF.type, :o, inferred: true) + expect(subject).to have_statement(existing_statement) + expect(subject.statements.first).to eq existing_statement + expect(subject.statements.first).to be_inferred + end end