Skip to content

Commit acdf029

Browse files
committed
Added Context#find_definition to find a term definition.
1 parent ba4e891 commit acdf029

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

lib/json/ld/context.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ def from_vocabulary(graph)
564564
# Add term definitions for each class and property not in schema:, and
565565
# for those properties having an object range
566566
graph.each do |statement|
567+
next if statement.subject.node?
567568
(statements[statement.subject] ||= []) << statement
568569

569570
# Keep track of predicate ranges
@@ -635,6 +636,15 @@ def set_mapping(term, value)
635636
term_definitions[term]
636637
end
637638

639+
##
640+
# Find a term definition
641+
#
642+
# @param [Term, #to_s] term in unexpanded form
643+
# @return [Term]
644+
def find_definition(term)
645+
term.is_a?(TermDefinition) ? term : term_definitions[term.to_s]
646+
end
647+
638648
##
639649
# Retrieve container mapping, add it if `value` is provided
640650
#
@@ -643,7 +653,7 @@ def set_mapping(term, value)
643653
def container(term)
644654
return '@set' if term == '@graph'
645655
return term if KEYWORDS.include?(term)
646-
term = term_definitions[term.to_s] unless term.is_a?(TermDefinition)
656+
term = find_definition(term)
647657
term && term.container_mapping
648658
end
649659

@@ -652,7 +662,7 @@ def container(term)
652662
# @param [Term, #to_s] term in unexpanded form
653663
# @return [String]
654664
def language(term)
655-
term = term_definitions[term.to_s] unless term.is_a?(TermDefinition)
665+
term = find_definition(term)
656666
lang = term && term.language_mapping
657667
lang.nil? ? @default_language : lang
658668
end
@@ -662,7 +672,7 @@ def language(term)
662672
# @param [Term, #to_s] term in unexpanded form
663673
# @return [Boolean]
664674
def reverse?(term)
665-
term = term_definitions[term.to_s] unless term.is_a?(TermDefinition)
675+
term = find_definition(term)
666676
term && term.reverse_property
667677
end
668678

script/gen_context

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ options = {
2222
OPT_ARGS = [
2323
["--body", GetoptLong::NO_ARGUMENT, "Include the vocabulary definition in the body of the document"],
2424
["--hierarchical", GetoptLong::NO_ARGUMENT, "Create a hierachy using 'children' as reverse of rdfs:subClassOf"],
25+
["--initial", GetoptLong::REQUIRED_ARGUMENT,"Initial context to load, overrides default"],
2526
["--language", GetoptLong::REQUIRED_ARGUMENT,"Default language for vocabulary"],
2627
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT,"Output to the specified file path"],
2728
["--prefix", GetoptLong::REQUIRED_ARGUMENT,"space-separated prefix uri combination"],
@@ -45,12 +46,28 @@ def usage
4546
exit(1)
4647
end
4748

49+
base_context = ::JSON.parse %({
50+
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
51+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
52+
"rdfs:domain": {"@type": "@id"},
53+
"rdfs:range": {"@type": "@id"},
54+
"rdfs:subClassOf": {"@type": "@id"},
55+
"rdfs:subPropertyOf": {"@type": "@id"},
56+
"owl": "http://www.w3.org/2002/07/owl#",
57+
"owl:equivalentClass": {"@type": "@vocab"},
58+
"owl:equivalentProperty": {"@type": "@vocab"},
59+
"owl:oneOf": {"@container": "@list", "@type": "@vocab"},
60+
"owl:imports": {"@type": "@id"},
61+
"owl:versionInfo": {"@type": "xsd:string", "@language": null},
62+
"owl:inverseOf": {"@type": "@vocab"}
63+
})
4864

4965
opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
5066

5167
opts.each do |opt, arg|
5268
case opt
5369
when '--body' then options[:include_body] = true
70+
when '--initial' then base_context = ::JSON.parse(File.read(arg))
5471
when '--hierarchical' then options[:hier] = true
5572
when '--language' then options[:language] = arg
5673
when '--output' then options[:output] = File.open(arg, "w")
@@ -64,22 +81,6 @@ end
6481
# Load vocabulary
6582
graph = RDF::Graph.load(ARGV[0])
6683

67-
base_context = {
68-
"rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
69-
"rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
70-
"rdfs:domain" => {"@type" => "@id"},
71-
"rdfs:range" => {"@type" => "@id"},
72-
"rdfs:subClassOf" => {"@type" => "@id"},
73-
"rdfs:subPropertyOf" => {"@type" => "@id"},
74-
"owl" => "http://www.w3.org/2002/07/owl#",
75-
"owl:equivalentClass" => {"@type" => "@vocab"},
76-
"owl:equivalentProperty" => {"@type" => "@vocab"},
77-
"owl:oneOf" => {"@container" => "@list", "@type" => "@vocab"},
78-
"owl:imports" => {"@type" => "@id"},
79-
"owl:versionInfo" => {"@type" => "xsd:string", "@language" => nil},
80-
"owl:inverseOf" => {"@type" => "@vocab"},
81-
}
82-
8384
context = JSON::LD::Context.new(options).
8485
parse(base_context).
8586
from_vocabulary(graph)

0 commit comments

Comments
 (0)