json-ld 2.0.0.beta1 → 2.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41122ee834694585f5bccd788b5310ca40ece180
4
- data.tar.gz: 7c6ab81c44cdc21260b6dc0b2c9882c96d3dd0c0
3
+ metadata.gz: 5adc54c6221f35206e44c2d599acc5bdf40431c5
4
+ data.tar.gz: 484e28238ca6b1615943d1b3040fa08cf9b077c1
5
5
  SHA512:
6
- metadata.gz: 3fecd04504e2bc9b4a7c40cb867c95c8b0aec0afc560e928bf330642ac397571bad7bc6b61cdcd7e1deae115a859346aa27ef07e3aab079c19736e4dbc2a3b96
7
- data.tar.gz: 52bb0c863bd72dbd28fa02caa1b2eeb93c263b9310f09a6ca2e9745ad4df3dccb6c013d77255a6d4467ec2a7dcf275297605dd48549bfd7c514ed432507f79b7
6
+ metadata.gz: 9d268dcf9996b5cb83bf2a775c96c04a2cca8afd6901fdf7b40815b2a654edc3e032d528bc4bc59c712c11fa0d78c6aabc08ddd8010f42abc9c76f089ccf5aed
7
+ data.tar.gz: a283566902b6d1896f2685c06de158d35da2a94317afaf942ebb6ee4714eb9d7d4eb1f65e2538fc381a278e457a0d99b21640bc7ab9e3a2fc90bb69799c13516
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.beta1
1
+ 2.0.0.beta2
@@ -414,22 +414,11 @@ module JSON::LD
414
414
  # This removes any existing context to allow the given context to be cleanly applied.
415
415
  log_debug(".toRdf") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
416
416
 
417
- # Generate _nodeMap_
418
- graphs = {'@default' => {}}
419
- create_node_map(expanded_input, graphs)
420
- log_debug(".toRdf") {"node map: #{graphs.to_json(JSON_STATE) rescue 'malformed json'}"}
421
-
422
- # Start generating statements
423
- graphs.each do |graph_name, graph|
424
- context = as_resource(graph_name) unless graph_name == '@default'
425
- log_debug(".toRdf") {"graph_name: #{context ? context.to_ntriples : 'null'}"}
426
- # Drop results for graphs which are named with relative IRIs
427
- if graph_name.is_a?(RDF::URI) && !graph_name.absolute
428
- log_debug(".toRdf") {"drop relative graph_name: #{statement.to_ntriples}"}
429
- next
430
- end
431
- graph_to_rdf(graph) do |statement|
417
+ # Recurse through input
418
+ expanded_input.each do |node|
419
+ item_to_rdf(node) do |statement|
432
420
  next if statement.predicate.node? && !options[:produceGeneralizedRdf]
421
+
433
422
  # Drop results with relative IRIs
434
423
  relative = statement.to_a.any? do |r|
435
424
  case r
@@ -446,12 +435,7 @@ module JSON::LD
446
435
  next
447
436
  end
448
437
 
449
- statement.graph_name = context if context
450
- if block_given?
451
- yield statement
452
- else
453
- results << statement
454
- end
438
+ yield statement
455
439
  end
456
440
  end
457
441
  end
@@ -6,68 +6,14 @@ module JSON::LD
6
6
  include Utils
7
7
 
8
8
  ##
9
- #
10
- # @param [Hash{String => Hash}] active_graph
11
- # A hash of IRI to Node definitions
9
+ # @param [Hash{String => Object}] node
10
+ # @param [RDF::Resource] graph_name
12
11
  # @yield statement
13
12
  # @yieldparam [RDF::Statement] statement
14
- def graph_to_rdf(active_graph, &block)
15
- log_debug('graph_to_rdf') {"graph_to_rdf: #{active_graph.inspect}"}
16
-
17
- # For each id-node in active_graph
18
- active_graph.each do |id, node|
19
- # Initialize subject as the IRI or BNode representation of id
20
- subject = as_resource(id)
21
- log_debug("graph_to_rdf") {"subject: #{subject.to_ntriples rescue 'malformed rdf'} (id: #{id})"}
22
-
23
- # For each property-values in node
24
- node.each do |property, values|
25
- case property
26
- when '@type'
27
- # If property is @type, construct triple as an RDF Triple composed of id, rdf:type, and object from values where id and object are represented either as IRIs or Blank Nodes
28
- values.each do |value|
29
- object = as_resource(value)
30
- log_debug("graph_to_rdf") {"type: #{object.to_ntriples rescue 'malformed rdf'}"}
31
- yield RDF::Statement(subject, RDF.type, object)
32
- end
33
- when /^@/
34
- # Otherwise, if @type is any other keyword, skip to the next property-values pair
35
- else
36
- # Otherwise, property is an IRI or Blank Node identifier
37
- # Initialize predicate from property as an IRI or Blank node
38
- predicate = as_resource(property)
39
- log_debug("graph_to_rdf") {"predicate: #{predicate.to_ntriples rescue 'malformed rdf'}"}
40
-
41
- # For each item in values
42
- values.each do |item|
43
- if item.has_key?('@list')
44
- log_debug("graph_to_rdf") {"list: #{item.inspect}"}
45
- # If item is a list object, initialize list_results as an empty array, and object to the result of the List Conversion algorithm, passing the value associated with the @list key from item and list_results.
46
- object = parse_list(item['@list']) {|stmt| yield stmt}
47
-
48
- # Append a triple composed of subject, prediate, and object to results and add all triples from list_results to results.
49
- yield RDF::Statement(subject, predicate, object)
50
- else
51
- # Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
52
- object = parse_object(item)
53
- log_debug("graph_to_rdf") {"object: #{object.to_ntriples rescue 'malformed rdf'}"}
54
- # Append a triple composed of subject, prediate, and literal to results.
55
- yield RDF::Statement(subject, predicate, object)
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
62
-
63
- ##
64
- # Parse an item, either a value object or a node definition
65
- # @param [Hash] item
66
- # @return [RDF::Value]
67
- def parse_object(item)
68
- if item.has_key?('@value')
69
- # Otherwise, if element is a JSON object that contains the key @value
70
- # Initialize value to the value associated with the @value key in element. Initialize datatype to the value associated with the @type key in element, or null if element does not contain that key.
13
+ # @return RDF::Resource the subject of this item
14
+ def item_to_rdf(item, graph_name: nil, &block)
15
+ # Just return value object as Term
16
+ if value?(item)
71
17
  value, datatype = item.fetch('@value'), item.fetch('@type', nil)
72
18
 
73
19
  case value
@@ -75,7 +21,7 @@ module JSON::LD
75
21
  # If value is true or false, then set value its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to xsd:boolean.
76
22
  value = value.to_s
77
23
  datatype ||= RDF::XSD.boolean.to_s
78
- when Float, Fixnum
24
+ when Integer, Float, Fixnum
79
25
  # Otherwise, if value is a number, then set value to its canonical lexical form as defined in the section Data Round Tripping. If datatype is null, set it to either xsd:integer or xsd:double, depending on if the value contains a fractional and/or an exponential component.
80
26
  lit = RDF::Literal.new(value, canonicalize: true)
81
27
  value = lit.to_s
@@ -88,13 +34,77 @@ module JSON::LD
88
34
 
89
35
  # Initialize literal as an RDF literal using value and datatype. If element has the key @language and datatype is xsd:string, then add the value associated with the @language key as the language of the object.
90
36
  language = item.fetch('@language', nil)
91
- RDF::Literal.new(value, datatype: datatype, language: language)
92
- else
93
- # Otherwise, value must be a node definition containing only @id whos value is an IRI or Blank Node identifier
94
- raise "Expected node reference, got #{item.inspect}" unless item.keys == %w(@id)
95
- # Return value associated with @id as an IRI or Blank node
96
- as_resource(item['@id'])
37
+ return RDF::Literal.new(value, datatype: datatype, language: language)
97
38
  end
39
+
40
+ subject = item['@id'] ? as_resource(item['@id']) : node
41
+ log_debug("item_to_rdf") {"subject: #{subject.to_ntriples rescue 'malformed rdf'}"}
42
+ item.each do |property, values|
43
+ case property
44
+ when '@type'
45
+ # If property is @type, construct triple as an RDF Triple composed of id, rdf:type, and object from values where id and object are represented either as IRIs or Blank Nodes
46
+ values.each do |v|
47
+ object = as_resource(v)
48
+ log_debug("item_to_rdf") {"type: #{object.to_ntriples rescue 'malformed rdf'}"}
49
+ yield RDF::Statement(subject, RDF.type, object, graph_name: graph_name)
50
+ end
51
+ when '@graph'
52
+ values = [values].compact unless values.is_a?(Array)
53
+ values.each do |nd|
54
+ item_to_rdf(nd, graph_name: subject, &block)
55
+ end
56
+ when '@reverse'
57
+ raise "Huh?" unless values.is_a?(Hash)
58
+ values.each do |prop, vv|
59
+ predicate = as_resource(prop)
60
+ log_debug("item_to_rdf") {"@reverse predicate: #{predicate.to_ntriples rescue 'malformed rdf'}"}
61
+ # For each item in values
62
+ vv.each do |v|
63
+ if list?(v)
64
+ log_debug("item_to_rdf") {"list: #{v.inspect}"}
65
+ # If item is a list object, initialize list_results as an empty array, and object to the result of the List Conversion algorithm, passing the value associated with the @list key from item and list_results.
66
+ object = parse_list(v['@list'], graph_name: graph_name, &block)
67
+
68
+ # Append a triple composed of object, prediate, and object to results and add all triples from list_results to results.
69
+ yield RDF::Statement(object, predicate, subject, graph_name: graph_name)
70
+ else
71
+ # Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
72
+ object = item_to_rdf(v, graph_name: graph_name, &block)
73
+ log_debug("item_to_rdf") {"subject: #{object.to_ntriples rescue 'malformed rdf'}"}
74
+ # yield subject, prediate, and literal to results.
75
+ yield RDF::Statement(object, predicate, subject, graph_name: graph_name)
76
+ end
77
+ end
78
+ end
79
+ when /^@/
80
+ # Otherwise, if @type is any other keyword, skip to the next property-values pair
81
+ else
82
+ # Otherwise, property is an IRI or Blank Node identifier
83
+ # Initialize predicate from property as an IRI or Blank node
84
+ predicate = as_resource(property)
85
+ log_debug("item_to_rdf") {"predicate: #{predicate.to_ntriples rescue 'malformed rdf'}"}
86
+
87
+ # For each item in values
88
+ values.each do |v|
89
+ if list?(v)
90
+ log_debug("item_to_rdf") {"list: #{v.inspect}"}
91
+ # If item is a list object, initialize list_results as an empty array, and object to the result of the List Conversion algorithm, passing the value associated with the @list key from item and list_results.
92
+ object = parse_list(v['@list'], graph_name: graph_name, &block)
93
+
94
+ # Append a triple composed of subject, prediate, and object to results and add all triples from list_results to results.
95
+ yield RDF::Statement(subject, predicate, object, graph_name: graph_name)
96
+ else
97
+ # Otherwise, item is a value object or a node definition. Generate object as the result of the Object Converstion algorithm passing item.
98
+ object = item_to_rdf(v, graph_name: graph_name, &block)
99
+ log_debug("item_to_rdf") {"object: #{object.to_ntriples rescue 'malformed rdf'}"}
100
+ # yield subject, prediate, and literal to results.
101
+ yield RDF::Statement(subject, predicate, object, graph_name: graph_name)
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ subject
98
108
  end
99
109
 
100
110
  ##
@@ -106,7 +116,7 @@ module JSON::LD
106
116
  # @yieldparam [RDF::Resource] statement
107
117
  # @return [Array<RDF::Statement>]
108
118
  # Statements for each item in the list
109
- def parse_list(list)
119
+ def parse_list(list, graph_name: nil, &block)
110
120
  log_debug('parse_list') {"list: #{list.inspect}"}
111
121
 
112
122
  last = list.pop
@@ -114,16 +124,16 @@ module JSON::LD
114
124
 
115
125
  list.each do |list_item|
116
126
  # Set first to the result of the Object Converstion algorithm passing item.
117
- object = parse_object(list_item)
118
- yield RDF::Statement(first_bnode, RDF.first, object)
127
+ object = item_to_rdf(list_item, graph_name: graph_name, &block)
128
+ yield RDF::Statement(first_bnode, RDF.first, object, graph_name: graph_name)
119
129
  rest_bnode = node
120
- yield RDF::Statement(first_bnode, RDF.rest, rest_bnode)
130
+ yield RDF::Statement(first_bnode, RDF.rest, rest_bnode, graph_name: graph_name)
121
131
  first_bnode = rest_bnode
122
132
  end
123
133
  if last
124
- object = parse_object(last)
125
- yield RDF::Statement(first_bnode, RDF.first, object)
126
- yield RDF::Statement(first_bnode, RDF.rest, RDF.nil)
134
+ object = item_to_rdf(last, graph_name: graph_name, &block)
135
+ yield RDF::Statement(first_bnode, RDF.first, object, graph_name: graph_name)
136
+ yield RDF::Statement(first_bnode, RDF.rest, RDF.nil, graph_name: graph_name)
127
137
  end
128
138
  result
129
139
  end
@@ -130,6 +130,7 @@ module JSON::LD
130
130
  options[:base_uri] ||= options[:base] if options.has_key?(:base)
131
131
  options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
132
132
  super do
133
+ log_statistics.clear # FIXME: shouldn't be necessary
133
134
  @repo = RDF::Repository.new
134
135
 
135
136
  if block_given?
@@ -115,17 +115,19 @@ module Fixtures
115
115
  logger.info "repo: #{repo.dump(id == '#t0012' ? :nquads : :trig)}"
116
116
  JSON::LD::API.fromRdf(repo, options.merge(logger: logger))
117
117
  when "jld:ToRDFTest"
118
- JSON::LD::API.toRdf(input_loc, options.merge(logger: logger)).map do |statement|
119
- to_quad(statement)
118
+ repo = RDF::Repository.new
119
+ JSON::LD::API.toRdf(input_loc, options.merge(logger: logger)) do |statement|
120
+ repo << statement
120
121
  end
122
+ repo
121
123
  else
122
124
  fail("Unknown test type: #{testType}")
123
125
  end
124
126
  if evaluationTest?
125
127
  if testType == "jld:ToRDFTest"
126
- expected = expect
128
+ expected = RDF::Repository.new << RDF::NQuads::Reader.new(expect)
127
129
  rspec_example.instance_eval {
128
- expect(result.sort.join("")).to produce(expected, logger)
130
+ expect(result).to be_equivalent_graph(expected, logger)
129
131
  }
130
132
  else
131
133
  expected = JSON.load(expect)
@@ -163,9 +165,7 @@ module Fixtures
163
165
  logger.info "repo: #{repo.dump(id == '#t0012' ? :nquads : :trig)}"
164
166
  JSON::LD::API.fromRdf(repo, options.merge(logger: logger))
165
167
  when "jld:ToRDFTest"
166
- JSON::LD::API.toRdf(t.input_loc, options.merge(logger: logger)).map do |statement|
167
- t.to_quad(statement)
168
- end
168
+ JSON::LD::API.toRdf(t.input_loc, options.merge(logger: logger)) {}
169
169
  else
170
170
  success("Unknown test type: #{testType}")
171
171
  end
@@ -9,6 +9,9 @@ describe JSON::LD do
9
9
  describe m.name do
10
10
  m.entries.each do |t|
11
11
  specify "#{t.property('input')}: #{t.name}#{' (negative test)' unless t.positiveTest?}" do
12
+ skip "Native value fidelity" if %w(toRdf-0035-in.jsonld).include?(t.property('input'))
13
+ pending "Generalized RDF" if %w(toRdf-0118-in.jsonld).include?(t.property('input'))
14
+ pending "Blank nodes with reverse properties" if %w(toRdf-0119-in.jsonld).include?(t.property('input'))
12
15
  t.run self
13
16
  end
14
17
  end
@@ -9,7 +9,7 @@ describe JSON::LD::Writer do
9
9
  after(:each) {|example| puts logger.to_s if example.exception}
10
10
 
11
11
  it_behaves_like 'an RDF::Writer' do
12
- let(:writer) {JSON::LD::Writer.new(StringIO.new(""))}
12
+ let(:writer) {JSON::LD::Writer.new(StringIO.new, logger: logger)}
13
13
  end
14
14
 
15
15
  describe ".for" do
@@ -196,11 +196,14 @@ describe JSON::LD::Writer do
196
196
  describe m.name do
197
197
  m.entries.each do |t|
198
198
  next unless t.positiveTest? && !t.property('input').include?('0016')
199
- t.debug = ["test: #{t.inspect}", "source: #{t.input}"]
200
199
  specify "#{t.property('input')}: #{t.name}" do
200
+ logger.info "test: #{t.inspect}"
201
+ logger.info "source: #{t.input}"
202
+ t.logger = logger
201
203
  pending "Shared list BNode in different graphs" if t.property('input').include?("fromRdf-0021")
204
+ pending "graph comparison issue" if t.property('input').include?("fromRdf-0008")
202
205
  repo = RDF::Repository.load(t.input_loc, format: :nquads)
203
- jsonld = JSON::LD::Writer.buffer(debug: t.debug) do |writer|
206
+ jsonld = JSON::LD::Writer.buffer(logger: t.logger) do |writer|
204
207
  writer << repo
205
208
  end
206
209
 
@@ -221,7 +224,7 @@ describe JSON::LD::Writer do
221
224
  # Serialize ntstr to a string and compare against regexps
222
225
  def serialize(ntstr, options = {})
223
226
  g = ntstr.is_a?(String) ? parse(ntstr, options) : ntstr
224
- logger.info g.dump(:ttl)
227
+ #logger.info g.dump(:ttl)
225
228
  result = JSON::LD::Writer.buffer(options.merge(logger: logger)) do |writer|
226
229
  writer << g
227
230
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-ld
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta1
4
+ version: 2.0.0.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-22 00:00:00.000000000 Z
11
+ date: 2016-02-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdf
@@ -420,7 +420,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
420
420
  version: 1.3.1
421
421
  requirements: []
422
422
  rubyforge_project: json-ld
423
- rubygems_version: 2.5.1
423
+ rubygems_version: 2.4.8
424
424
  signing_key:
425
425
  specification_version: 4
426
426
  summary: JSON-LD reader/writer for Ruby.