Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
270 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/services/qa/linked_data/mapper/context_mapper_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Provide service for mapping predicates to object values. | ||
module Qa | ||
module LinkedData | ||
module Mapper | ||
class ContextMapperService | ||
class_attribute :graph_service | ||
self.graph_service = Qa::LinkedData::GraphService | ||
|
||
class << self | ||
# Extract predicates specified in the predicate_map from the graph and return as a value map for a single subject URI. | ||
# @param graph [RDF::Graph] the graph from which to extract result values | ||
# @param context_map [Qa::LinkedData::Config::ContextMap] defines properties to extract from the graph to provide additional context | ||
# @param subject_uri [RDF::URI] the subject within the graph for which the values are being extracted | ||
# @return [<Hash<Symbol><Array<Object>>] mapped context values and information with hash of map key = array of object values for predicates identified in predicate_map. | ||
# @example returned context map with one property defined | ||
# [{"group" => "group label, | ||
# "property" => "property label", | ||
# "values" => ["value 1","value 2"], | ||
# "selectable" => true, | ||
# "drillable" => false}] | ||
def map_context(graph:, context_map:, subject_uri:) | ||
context = [] | ||
context_map.properties.each do |property_map| | ||
values = property_map.values(graph, subject_uri) | ||
next if values.blank? | ||
context << construct_context(context_map, property_map, values) | ||
end | ||
context | ||
end | ||
|
||
private | ||
|
||
def construct_context(context_map, property_map, values) | ||
property_info = {} | ||
property_info["group"] = context_map.group_label(property_map.group_id) if property_map.group? # TODO: should be group label | ||
property_info["property"] = property_map.label | ||
property_info["values"] = values | ||
property_info["selectable"] = property_map.selectable? | ||
property_info["drillable"] = property_map.drillable? | ||
property_info | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
spec/services/linked_data/mapper/context_mapper_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Qa::LinkedData::Mapper::ContextMapperService do | ||
subject { described_class.map_context(graph: graph, context_map: context_map, subject_uri: subject_uri) } | ||
|
||
let(:graph) { instance_double(RDF::Graph) } | ||
let(:context_map) { instance_double(Qa::LinkedData::Config::ContextMap) } | ||
let(:subject_uri) { instance_double(RDF::URI) } | ||
|
||
let(:context_properties) { [birth_date_property_map, death_date_property_map, occupation_property_map] } | ||
|
||
let(:birth_date_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) } | ||
let(:death_date_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) } | ||
let(:occupation_property_map) { instance_double(Qa::LinkedData::Config::ContextPropertyMap) } | ||
|
||
let(:group_id) { 'dates' } | ||
|
||
let(:birth_date_values) { ['10/15/1943'] } | ||
let(:death_date_values) { ['12/17/2018'] } | ||
let(:occupation_values) { ['Actress', 'Director', 'Producer'] } | ||
|
||
before do | ||
allow(context_map).to receive(:properties).and_return(context_properties) | ||
allow(context_map).to receive(:group_label).with('dates').and_return('Dates') | ||
|
||
allow(birth_date_property_map).to receive(:label).and_return('Birth') | ||
allow(birth_date_property_map).to receive(:values).with(graph, subject_uri).and_return(birth_date_values) | ||
allow(birth_date_property_map).to receive(:group?).and_return(false) | ||
allow(birth_date_property_map).to receive(:selectable?).and_return(false) | ||
allow(birth_date_property_map).to receive(:drillable?).and_return(false) | ||
|
||
allow(death_date_property_map).to receive(:label).and_return('Death') | ||
allow(death_date_property_map).to receive(:values).with(graph, subject_uri).and_return(death_date_values) | ||
allow(death_date_property_map).to receive(:group?).and_return(false) | ||
allow(death_date_property_map).to receive(:selectable?).and_return(false) | ||
allow(death_date_property_map).to receive(:drillable?).and_return(false) | ||
|
||
allow(occupation_property_map).to receive(:label).and_return('Occupation') | ||
allow(occupation_property_map).to receive(:values).with(graph, subject_uri).and_return(occupation_values) | ||
allow(occupation_property_map).to receive(:group?).and_return(false) | ||
allow(occupation_property_map).to receive(:selectable?).and_return(false) | ||
allow(occupation_property_map).to receive(:drillable?).and_return(false) | ||
end | ||
|
||
describe '.map_context' do | ||
it 'sets the property labels from the property map' do | ||
find_property_to_test(subject, 'Birth') | ||
find_property_to_test(subject, 'Death') | ||
find_property_to_test(subject, 'Occupation') | ||
expect(subject.size).to be 3 | ||
end | ||
|
||
it 'sets the property values from the graph' do | ||
result = find_property_to_test(subject, 'Birth') | ||
expect(result['values']).to match_array birth_date_values | ||
result = find_property_to_test(subject, 'Death') | ||
expect(result['values']).to match_array death_date_values | ||
result = find_property_to_test(subject, 'Occupation') | ||
expect(result['values']).to match_array occupation_values | ||
end | ||
|
||
context 'when group? is false' do | ||
before { allow(birth_date_property_map).to receive(:group?).and_return(false) } | ||
it 'does not include group in results' do | ||
result = find_property_to_test(subject, 'Birth') | ||
expect(result.key?('group')).to be false | ||
end | ||
end | ||
|
||
context 'when group? is true' do | ||
before do | ||
allow(birth_date_property_map).to receive(:group?).and_return(true) | ||
allow(birth_date_property_map).to receive(:group_id).and_return('dates') | ||
allow(context_map).to receive(:group_label).with('dates').and_return('Dates') | ||
end | ||
|
||
it 'includes group in results' do | ||
result = find_property_to_test(subject, 'Birth') | ||
expect(result['group']).to eq 'Dates' | ||
end | ||
end | ||
|
||
context 'when drillable? is false' do | ||
before { allow(death_date_property_map).to receive(:drillable?).and_return(false) } | ||
it 'includes drillable set to false' do | ||
result = find_property_to_test(subject, 'Death') | ||
expect(result['drillable']).to be false | ||
end | ||
end | ||
|
||
context 'when drillable? is true' do | ||
before { allow(death_date_property_map).to receive(:drillable?).and_return(true) } | ||
it 'includes drillable set to true' do | ||
result = find_property_to_test(subject, 'Death') | ||
expect(result['drillable']).to be true | ||
end | ||
end | ||
|
||
context 'when selectable? is false' do | ||
before { allow(occupation_property_map).to receive(:selectable?).and_return(false) } | ||
it 'includes selectable set to false' do | ||
result = find_property_to_test(subject, 'Occupation') | ||
expect(result['selectable']).to be false | ||
end | ||
end | ||
|
||
context 'when selectable? is true' do | ||
before { allow(occupation_property_map).to receive(:selectable?).and_return(true) } | ||
it 'includes selectable set to true' do | ||
result = find_property_to_test(subject, 'Occupation') | ||
expect(result['selectable']).to be true | ||
end | ||
end | ||
end | ||
|
||
def find_property_to_test(results, label) | ||
results.each do |r| | ||
next unless r['property'] == label | ||
return r | ||
end | ||
raise "property (#{label}) to test not found" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters