rdf-turtle 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/rdf/turtle/writer.rb +73 -65
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9093a424b624c733fd209a33dc7ac8ceb650bd2f
|
|
4
|
+
data.tar.gz: ab2bb587fdb532cbb6a7068df58d0c40c4fb2b98
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 05ba9c14422a1be8c51ca24f6e2c7e87fafae3ee24b04023a96f726cf112bb51b7fde1cc053659c61ee6cf87e04dff92d719065a44705dd84ca3fa65cf417c10
|
|
7
|
+
data.tar.gz: 5020e9eb963ea2433c173557750cfb8d62de8a218deee5e5ff406642e53d1e8b3ef8b7979199827c93f86241e974b198e8e439e2611ce7066cb9ed53f709d9aa
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
1.1.
|
|
1
|
+
1.1.2
|
data/lib/rdf/turtle/writer.rb
CHANGED
|
@@ -208,8 +208,6 @@ module RDF::Turtle
|
|
|
208
208
|
end
|
|
209
209
|
|
|
210
210
|
@uri_to_pname[uri] = pname
|
|
211
|
-
rescue Addressable::URI::InvalidURIError => e
|
|
212
|
-
raise RDF::WriterError, "Invalid URI #{resource.inspect}: #{e.message}"
|
|
213
211
|
end
|
|
214
212
|
|
|
215
213
|
# Take a hash from predicate uris to lists of values.
|
|
@@ -365,8 +363,7 @@ module RDF::Turtle
|
|
|
365
363
|
# @param [Statement] statement
|
|
366
364
|
def preprocess_statement(statement)
|
|
367
365
|
#debug("preprocess") {statement.inspect}
|
|
368
|
-
|
|
369
|
-
@references[statement.object] = references
|
|
366
|
+
bump_reference(statement.object)
|
|
370
367
|
@subjects[statement.subject] = true
|
|
371
368
|
|
|
372
369
|
# Pre-fetch pnames, to fill prefixes
|
|
@@ -374,14 +371,6 @@ module RDF::Turtle
|
|
|
374
371
|
get_pname(statement.predicate)
|
|
375
372
|
get_pname(statement.object)
|
|
376
373
|
get_pname(statement.object.datatype) if statement.object.literal? && statement.object.datatype
|
|
377
|
-
|
|
378
|
-
@references[statement.predicate] = ref_count(statement.predicate) + 1
|
|
379
|
-
end
|
|
380
|
-
|
|
381
|
-
# Return the number of times this node has been referenced in the object position
|
|
382
|
-
# @return [Integer]
|
|
383
|
-
def ref_count(node)
|
|
384
|
-
@references.fetch(node, 0)
|
|
385
374
|
end
|
|
386
375
|
|
|
387
376
|
# Returns indent string multiplied by the depth
|
|
@@ -414,8 +403,6 @@ module RDF::Turtle
|
|
|
414
403
|
end
|
|
415
404
|
end
|
|
416
405
|
|
|
417
|
-
protected
|
|
418
|
-
|
|
419
406
|
##
|
|
420
407
|
# Add debug event to debug array, if specified
|
|
421
408
|
#
|
|
@@ -455,10 +442,10 @@ module RDF::Turtle
|
|
|
455
442
|
position = :object
|
|
456
443
|
end
|
|
457
444
|
end
|
|
458
|
-
|
|
459
|
-
def
|
|
445
|
+
|
|
446
|
+
def collection(node, position)
|
|
460
447
|
return false if !is_valid_list?(node)
|
|
461
|
-
#debug("
|
|
448
|
+
#debug("collection") {"#{node.inspect}, #{position}"}
|
|
462
449
|
|
|
463
450
|
@output.write(position == :subject ? "(" : " (")
|
|
464
451
|
@depth += 2
|
|
@@ -466,55 +453,61 @@ module RDF::Turtle
|
|
|
466
453
|
@depth -= 2
|
|
467
454
|
@output.write(')')
|
|
468
455
|
end
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
456
|
+
|
|
457
|
+
# Can object be represented using a predicateObjectList?
|
|
458
|
+
def p_squared?(resource, position)
|
|
459
|
+
resource.is_a?(RDF::Node) &&
|
|
460
|
+
!@serialized.has_key?(resource) &&
|
|
461
|
+
ref_count(resource) <= 1
|
|
474
462
|
end
|
|
475
|
-
|
|
476
|
-
def p_squared(node, position)
|
|
477
|
-
return false unless p_squared?(node, position)
|
|
478
463
|
|
|
479
|
-
|
|
480
|
-
|
|
464
|
+
# Represent an object as a predicateObjectList
|
|
465
|
+
def p_squared(resource, position)
|
|
466
|
+
return false unless p_squared?(resource, position)
|
|
467
|
+
|
|
468
|
+
#debug("p_squared") {"#{resource.inspect}, #{position}"}
|
|
469
|
+
subject_done(resource)
|
|
481
470
|
@output.write(position == :subject ? '[' : ' [')
|
|
482
471
|
@depth += 2
|
|
483
|
-
|
|
472
|
+
predicateObjectList(resource)
|
|
484
473
|
@depth -= 2
|
|
485
474
|
@output.write(']')
|
|
486
475
|
|
|
487
476
|
true
|
|
488
477
|
end
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
478
|
+
|
|
479
|
+
# Default singular resource representation.
|
|
480
|
+
def p_default(resource, position)
|
|
481
|
+
#debug("p_default") {"#{resource.inspect}, #{position}"}
|
|
482
|
+
l = (position == :subject ? "" : " ") + format_value(resource)
|
|
493
483
|
@output.write(l)
|
|
494
484
|
end
|
|
495
|
-
|
|
496
|
-
|
|
485
|
+
|
|
486
|
+
# Represent a resource in subject, predicate or object position.
|
|
487
|
+
# Use either collection, blankNodePropertyList or singular resource notation.
|
|
488
|
+
def path(resource, position)
|
|
497
489
|
debug("path") do
|
|
498
|
-
"#{
|
|
490
|
+
"#{resource.inspect}, " +
|
|
499
491
|
"pos: #{position}, " +
|
|
500
|
-
"[]: #{is_valid_list?(
|
|
501
|
-
"p2?: #{p_squared?(
|
|
502
|
-
"rc: #{ref_count(
|
|
492
|
+
"[]: #{is_valid_list?(resource)}, " +
|
|
493
|
+
"p2?: #{p_squared?(resource, position)}, " +
|
|
494
|
+
"rc: #{ref_count(resource)}"
|
|
503
495
|
end
|
|
504
|
-
raise RDF::WriterError, "Cannot serialize
|
|
496
|
+
raise RDF::WriterError, "Cannot serialize resource '#{resource}'" unless collection(resource, position) || p_squared(resource, position) || p_default(resource, position)
|
|
505
497
|
end
|
|
506
498
|
|
|
507
|
-
def
|
|
508
|
-
debug("
|
|
509
|
-
if
|
|
499
|
+
def predicate(resource)
|
|
500
|
+
debug("predicate") {resource.inspect}
|
|
501
|
+
if resource == RDF.type
|
|
510
502
|
@output.write(" a")
|
|
511
503
|
else
|
|
512
|
-
path(
|
|
504
|
+
path(resource, :predicate)
|
|
513
505
|
end
|
|
514
506
|
end
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
507
|
+
|
|
508
|
+
# Render an objectList having a common subject and predicate
|
|
509
|
+
def objectList(objects)
|
|
510
|
+
debug("objectList") {objects.inspect}
|
|
518
511
|
return if objects.empty?
|
|
519
512
|
|
|
520
513
|
objects.each_with_index do |obj, i|
|
|
@@ -522,8 +515,9 @@ module RDF::Turtle
|
|
|
522
515
|
path(obj, :object)
|
|
523
516
|
end
|
|
524
517
|
end
|
|
525
|
-
|
|
526
|
-
|
|
518
|
+
|
|
519
|
+
# Render a predicateObjectList having a common subject.
|
|
520
|
+
def predicateObjectList(subject)
|
|
527
521
|
properties = {}
|
|
528
522
|
@graph.query(:subject => subject) do |st|
|
|
529
523
|
properties[st.predicate.to_s] ||= []
|
|
@@ -531,49 +525,50 @@ module RDF::Turtle
|
|
|
531
525
|
end
|
|
532
526
|
|
|
533
527
|
prop_list = sort_properties(properties) - [RDF.first.to_s, RDF.rest.to_s]
|
|
534
|
-
debug("
|
|
528
|
+
debug("predicateObjectList") {prop_list.inspect}
|
|
535
529
|
return if prop_list.empty?
|
|
536
530
|
|
|
537
531
|
prop_list.each_with_index do |prop, i|
|
|
538
532
|
begin
|
|
539
533
|
@output.write(";\n#{indent(2)}") if i > 0
|
|
540
534
|
prop[0, 2] == "_:"
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
rescue Addressable::URI::InvalidURIError => e
|
|
544
|
-
debug {"Predicate #{prop.inspect} is an invalid URI: #{e.message}"}
|
|
535
|
+
predicate(prop[0, 2] == "_:" ? RDF::Node.new(prop.split(':').last) : RDF::URI.intern(prop))
|
|
536
|
+
objectList(properties[prop])
|
|
545
537
|
end
|
|
546
538
|
end
|
|
547
539
|
end
|
|
548
|
-
|
|
549
|
-
|
|
540
|
+
|
|
541
|
+
# Can subject be represented as a blankNodePropertyList?
|
|
542
|
+
def blankNodePropertyList?(subject)
|
|
550
543
|
ref_count(subject) == 0 && subject.is_a?(RDF::Node) && !is_valid_list?(subject)
|
|
551
544
|
end
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
545
|
+
|
|
546
|
+
# Represent subject as a blankNodePropertyList?
|
|
547
|
+
def blankNodePropertyList(subject)
|
|
548
|
+
return false unless blankNodePropertyList?(subject)
|
|
555
549
|
|
|
556
|
-
debug("
|
|
550
|
+
debug("blankNodePropertyList") {subject.inspect}
|
|
557
551
|
@output.write("\n#{indent} [")
|
|
558
552
|
@depth += 1
|
|
559
|
-
|
|
553
|
+
predicateObjectList(subject)
|
|
560
554
|
@depth -= 1
|
|
561
555
|
@output.write("] .")
|
|
562
556
|
true
|
|
563
557
|
end
|
|
564
|
-
|
|
565
|
-
|
|
558
|
+
|
|
559
|
+
# Render triples having the same subject using an explicit subject
|
|
560
|
+
def triples(subject)
|
|
566
561
|
@output.write("\n#{indent}")
|
|
567
562
|
path(subject, :subject)
|
|
568
|
-
|
|
563
|
+
predicateObjectList(subject)
|
|
569
564
|
@output.write(" .")
|
|
570
565
|
true
|
|
571
566
|
end
|
|
572
567
|
|
|
573
568
|
def statement(subject)
|
|
574
|
-
debug("statement") {"#{subject.inspect},
|
|
569
|
+
debug("statement") {"#{subject.inspect}, bnodePL?: #{blankNodePropertyList?(subject)}"}
|
|
575
570
|
subject_done(subject)
|
|
576
|
-
|
|
571
|
+
blankNodePropertyList(subject) || triples(subject)
|
|
577
572
|
@output.puts
|
|
578
573
|
end
|
|
579
574
|
|
|
@@ -581,6 +576,19 @@ module RDF::Turtle
|
|
|
581
576
|
@serialized.include?(subject)
|
|
582
577
|
end
|
|
583
578
|
|
|
579
|
+
# Return the number of times this node has been referenced in the object position
|
|
580
|
+
# @return [Integer]
|
|
581
|
+
def ref_count(resource)
|
|
582
|
+
@references.fetch(resource, 0)
|
|
583
|
+
end
|
|
584
|
+
|
|
585
|
+
# Increase the reference count of this resource
|
|
586
|
+
# @param [RDF::Resource] resource
|
|
587
|
+
# @return [Integer] resulting reference count
|
|
588
|
+
def bump_reference(resource)
|
|
589
|
+
@references[resource] = ref_count(resource) + 1
|
|
590
|
+
end
|
|
591
|
+
|
|
584
592
|
# Mark a subject as done.
|
|
585
593
|
def subject_done(subject)
|
|
586
594
|
@serialized[subject] = true
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rdf-turtle
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gregg Kellogg
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2013-12-
|
|
11
|
+
date: 2013-12-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rdf
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - '>='
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: 1.1.1.1
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - '>='
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version:
|
|
26
|
+
version: 1.1.1.1
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: ebnf
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|