Skip to content

Commit

Permalink
Update RDF* mode:
Browse files Browse the repository at this point in the history
* Eliminate PG mode, `rdfstar` is not boolean valued.
* Change CLI from `--rdf-star` to `--rdfstar`.
* Update RDF* spec reference.

This follows the CG direction to not have modal operation, as annotations (in other serialization formats) more directly handle the PG use case.
  • Loading branch information
gkellogg committed Dec 12, 2020
1 parent 62025c4 commit 28077dd
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 63 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,10 @@ By default, the Turtle reader will reject a document containing a subject resour
end
# => RDF::ReaderError

Readers support a `rdfstar` option with either `:PG` (Property Graph) or `:SA` (Separate Assertions) modes. In `:PG` mode, statements that are used in the subject or object positions are also implicitly added to the graph:
Readers support a boolean valued `rdfstar` option; only one statement is asserted, although the reified statement is contained within the graph.

graph = RDF::Graph.new do |graph|
RDF::Turtle::Reader.new(ttl, rdfstar: :PG) {|reader| graph << reader}
end
graph.count #=> 2

When using the `:SA` mode, only one statement is asserted, although the reified statement is contained within the graph.

graph = RDF::Graph.new do |graph|
RDF::Turtle::Reader.new(ttl, rdfstar: :SA) {|reader| graph << reader}
RDF::Turtle::Reader.new(ttl, rdfstar: true) {|reader| graph << reader}
end
graph.count #=> 1

Expand All @@ -94,7 +87,7 @@ where the subject is the the triple ending with that annotation.
end
# => RDF::ReaderError

Note that this requires the `rdfstar` option to be set, but works the same regardless of `:PG` or `:SA` mode.
Note that this requires the `rdfstar` option to be se.

## Documentation
Full documentation available on [Rubydoc.info][Turtle doc]
Expand Down Expand Up @@ -196,7 +189,7 @@ A copy of the [Turtle EBNF][] and derived parser files are included in the repos
[Backports]: https://rubygems.org/gems/backports
[N-Triples]: https://www.w3.org/TR/rdf-testcases/#ntriples
[Turtle]: https://www.w3.org/TR/2012/WD-turtle-20120710/
[RDF*]: https://lists.w3.org/Archives/Public/public-rdf-star/
[RDF*]: https://w3c.github.io/rdf-star/rdf-star-cg-spec.html
[Turtle doc]: https://rubydoc.info/github/ruby-rdf/rdf-turtle/master/file/README.md
[Turtle EBNF]: https://dvcs.w3.org/hg/rdf/file/default/rdf-turtle/turtle.bnf
[Freebase Dumps]: https://developers.google.com/freebase/data
2 changes: 0 additions & 2 deletions lib/rdf/turtle/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,6 @@ def read_embTriple
end
@lexer.shift
statement = RDF::Statement(subject, predicate, object)
# Emit the statement if in Property Graph mode
add_statement(:embTriple, statement) if @options[:rdfstar] == :PG
statement
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rdf/turtle/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ def format_node(node, **options)
end

##
# Returns an embedded triples
# Returns an embedded triple.
#
# @param [RDF::Statement] statement
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_rdfstar(statement, **options)
def format_embTriple(statement, **options)
log_debug("rdfstar") {"#{statement.to_ntriples}"}
"<<%s %s %s>>" % statement.to_a.map { |value| format_term(value, **options) }
end
Expand Down
4 changes: 2 additions & 2 deletions script/parse
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ OPT_ARGS = [
["--output", "-o", GetoptLong::REQUIRED_ARGUMENT, "Save output to file"],
["--profile", GetoptLong::NO_ARGUMENT, "Show an execution profile"],
["--quiet", GetoptLong::NO_ARGUMENT, "Do not show parser output"],
["--rdfstar", GetoptLong::OPTIONAL_ARGUMENT, "Parse as RDF* (PG or SA)"],
["--rdfstar", GetoptLong::NO_ARGUMENT, "Parse as RDF*"],
["--stream", GetoptLong::NO_ARGUMENT, "Use streaming writer"],
["--uri", GetoptLong::REQUIRED_ARGUMENT, "Default base URI"],
["--validate", GetoptLong::NO_ARGUMENT, "Run parser in strict validation mode"],
Expand Down Expand Up @@ -152,7 +152,7 @@ opts.each do |opt, arg|
when '--input-format' then options[:input_format] = arg.to_sym
when '--output' then options[:output] = File.open(arg, "w")
when '--profile' then options[:profile] = true
when '--rdfstar' then parser_options[:rdfstar] = (arg || :PG).to_sym
when '--rdfstar' then parser_options[:rdfstar] = true
when '--quiet'
options[:quiet] = options[:quiet].to_i + 1
logger.level = Logger::FATAL
Expand Down
49 changes: 4 additions & 45 deletions spec/reader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,6 @@
),
%(
<<<http://example/s1> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .
<http://example/s1> <http://example/p1> <http://example/o1> .
)
],
"subject-iib": [
Expand All @@ -847,7 +846,6 @@
),
%(
<<<http://example/s1> <http://example/p1> _:o1>> <http://example/p> <http://example/o> .
<http://example/s1> <http://example/p1> _:o1 .
)
],
"subject-iil": [
Expand All @@ -857,7 +855,6 @@
),
%(
<<<http://example/s1> <http://example/p1> "o1">> <http://example/p> <http://example/o> .
<http://example/s1> <http://example/p1> "o1" .
)
],
"subject-iia": [
Expand All @@ -867,7 +864,6 @@
),
%(
<<<http://example/s1> <http://example/p1> _:anon>> <http://example/p> <http://example/o> .
<http://example/s1> <http://example/p1> _:anon .
)
],
"subject-bii": [
Expand All @@ -877,7 +873,6 @@
),
%(
<<_:s1 <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .
_:s1 <http://example/p1> <http://example/o1> .
)
],
"subject-bib": [
Expand All @@ -887,7 +882,6 @@
),
%(
<<_:s1 <http://example/p1> _:o1>> <http://example/p> <http://example/o> .
_:s1 <http://example/p1> _:o1 .
)
],
"subject-bil": [
Expand All @@ -897,7 +891,6 @@
),
%(
<<_:s1 <http://example/p1> "o1">> <http://example/p> <http://example/o> .
_:s1 <http://example/p1> "o1" .
)
],
"subject-bia": [
Expand All @@ -907,7 +900,6 @@
),
%(
<<_:s1 <http://example/p1> _:anon>> <http://example/p> <http://example/o> .
_:s1 <http://example/p1> _:anon .
)
],
"object-iii": [
Expand All @@ -917,7 +909,6 @@
),
%(
<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> <http://example/o1>>> .
<http://example/s1> <http://example/p1> <http://example/o1> .
)
],
"object-iib": [
Expand All @@ -927,7 +918,6 @@
),
%(
<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> _:o1>> .
<http://example/s1> <http://example/p1> _:o1 .
)
],
"object-iil": [
Expand All @@ -937,7 +927,6 @@
),
%(
<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> "o1">> .
<http://example/s1> <http://example/p1> "o1" .
)
],
"object-iia": [
Expand All @@ -947,7 +936,6 @@
),
%(
<http://example/s> <http://example/p> <<<http://example/s1> <http://example/p1> _:anon>> .
<http://example/s1> <http://example/p1> _:anon .
)
],
"recursive-subject": [
Expand All @@ -960,41 +948,12 @@
),
%(
<<<<<http://example/s2> <http://example/p2> <http://example/o2>>> <http://example/p1> <http://example/o1>>> <http://example/p> <http://example/o> .
<<<http://example/s2> <http://example/p2> <http://example/o2>>> <http://example/p1> <http://example/o1> .
<http://example/s2> <http://example/p2> <http://example/o2> .
)
],
}.each do |name, (ttl, nt)|
it name do
expect_graph = RDF::Graph.new {|g| g << RDF::NTriples::Reader.new(nt, rdfstar: :PG)}
expect(parse(ttl, rdfstar: :PG, validate: true)).to be_equivalent_graph(expect_graph, logger: @logger)
end
end

{
"subject-lii" => %(
@prefix ex: <http://example/> .
<<"s1" ex:p1 ex:o1>> ex:p ex:o .
),
"subject-ili" => %(
@prefix ex: <http://example/> .
<<ex:s1 "p1" ex:o1>> ex:p ex:o .
),
"subject-iiB" => %(
@prefix ex: <http://example/> .
<<ex:s1 ex:p1 [ex:p2 ex:o2]>> ex:p ex:o .
),
"subject-iiL" => %(
@prefix ex: <http://example/> .
<<ex:s1 ex:p1 ()>> ex:p ex:o .
),
"subject-iiL 2" => %(
@prefix ex: <http://example/> .
<<ex:s1 ex:p1 ("o1")>> ex:p ex:o .
),
}.each do |name, ttl|
it "raises error for #{name}" do
expect {parse(ttl, rdfstar: :SA)}.to raise_error(RDF::ReaderError)
expect_graph = RDF::Graph.new {|g| g << RDF::NTriples::Reader.new(nt, rdfstar: true)}
expect(parse(ttl, rdfstar: true, validate: true)).to be_equivalent_graph(expect_graph, logger: @logger)
end
end

Expand Down Expand Up @@ -1035,8 +994,8 @@
]
}.each do |name, (ttl, nt)|
it name do
expect_graph = RDF::Graph.new {|g| g << RDF::NTriples::Reader.new(nt, rdfstar: :PG)}
expect(parse(ttl, rdfstar: :SA, validate: true)).to be_equivalent_graph(expect_graph, logger: @logger)
expect_graph = RDF::Graph.new {|g| g << RDF::NTriples::Reader.new(nt, rdfstar: true)}
expect(parse(ttl, rdfstar: true, validate: true)).to be_equivalent_graph(expect_graph, logger: @logger)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/star_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
base_uri: t.base,
canonicalize: false,
validate: true,
rdfstar: :SA,
rdfstar: true,
logger: t.logger)

graph = RDF::Repository.new
Expand Down

0 comments on commit 28077dd

Please sign in to comment.