Skip to content

Instantly share code, notes, and snippets.

@elrayle
Created March 25, 2019 02:05
Show Gist options
  • Save elrayle/7d747ee952efd3413903a9d21323206a to your computer and use it in GitHub Desktop.
Save elrayle/7d747ee952efd3413903a9d21323206a to your computer and use it in GitHub Desktop.
Spec to demonstrate ascii tree error parsing complex ldpath for ldpath.rb issue #4
require 'spec_helper'
require 'byebug'
describe 'ldpath_program' do
let(:program) { ldpath_program(ldpath) }
let(:prefixes) do
{
"madsrdf" => "http://www.loc.gov/mads/rdf/v1#",
"rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
"skos" => "http://www.w3.org/2004/02/skos/core#"
}
end
let(:subject_uri) { RDF::URI('http://id.loc.gov/authorities/names/n79021164') }
let(:rwo1_uri) { RDF::URI('http://id.loc.gov/rwo/agents/n79021164') }
let(:foa1_uri) { RDF::Node('_:bnode386authoritiesnamesn79021164') }
let(:foa2_uri) { RDF::Node('_:bnode390authoritiesnamesn79021164') }
let(:foa3_uri) { RDF::URI('http://id.loc.gov/authorities/childrensSubjects/sj96006382') }
let(:rdfs_label) { RDF::URI('http://www.w3.org/2000/01/rdf-schema#label') }
let(:graph) do
graph = RDF::Graph.new
graph << [subject_uri, RDF::Vocab::MADS.authoritativeLabel, 'Twain, Mark, 1835-1910']
graph << [subject_uri, RDF::Vocab::SKOS.prefLabel, 'Twain, Mark']
graph << [subject_uri, RDF::Vocab::MADS.identifiesRWO, rwo1_uri]
graph << [rwo1_uri, RDF::Vocab::MADS.fieldOfActivity, foa1_uri]
graph << [foa1_uri, rdfs_label, "(lcgft) Literature"]
graph << [rwo1_uri, RDF::Vocab::MADS.fieldOfActivity, foa2_uri]
graph << [foa2_uri, rdfs_label, "(lcgft) Humor"]
graph << [rwo1_uri, RDF::Vocab::MADS.fieldOfActivity, foa3_uri]
graph << [foa3_uri, RDF::Vocab::MADS.authoritativeLabel, "Wit and humor"]
graph
end
context 'test use of | in ldpath' do
let(:ldpath) { "madsrdf:authoritativeLabel | skos:prefLabel :: xsd:string" }
let(:expected_values) { ['Twain, Mark', 'Twain, Mark, 1835-1910'] }
it 'should return the authoritativeLable and prefLabel' do
expect(ldpath_evaluate(program, graph, subject_uri)).to match_array expected_values
end
end
context 'test first half of ldpath' do
let(:ldpath) { "madsrdf:identifiesRWO/madsrdf:fieldOfActivity/rdfs:label :: xsd:string" }
let(:expected_values) { ['(lcgft) Literature', '(lcgft) Humor'] }
it 'should return the Literature and Humor' do
expect(ldpath_evaluate(program, graph, subject_uri)).to match_array expected_values
end
end
context 'test second half of ldpath' do
let(:ldpath) { "madsrdf:identifiesRWO/madsrdf:fieldOfActivity/madsrdf:authoritativeLabel :: xsd:string" }
let(:expected_values) { ['Wit and humor'] }
it 'should return the Wit and humor' do
expect(ldpath_evaluate(program, graph, subject_uri)).to match_array expected_values
end
end
context 'test full path' do
let(:ldpath) { "madsrdf:identifiesRWO/madsrdf:fieldOfActivity/rdfs:label | madsrdf:identifiesRWO/madsrdf:fieldOfActivity/madsrdf:authoritativeLabel :: xsd:string" }
let(:expected_values) do
['Literature', 'Humor', 'Wit and Humor']
end
it 'should return all values for fieldOfActivity' do
expect(ldpath_evaluate(program, graph, subject_uri)).to eq expected_values
end
end
end
def ldpath_program(ldpath)
program_code = ""
prefixes.each { |key, url| program_code << "@prefix #{key} : <#{url}> \;\n" }
program_code << "property = #{ldpath} \;"
Ldpath::Program.parse program_code
end
def ldpath_evaluate(program, graph, subject_uri)
output = program.evaluate subject_uri, context: graph
output.nil? ? nil : output['property'].uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment