rdf-n3 0.3.4.1 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/example.rb DELETED
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
4
-
5
- require 'rubygems'
6
- require 'rdf/n3'
7
-
8
- data = <<-EOF;
9
- @prefix dc: <http://purl.org/dc/elements/1.1/>.
10
- @prefix po: <http://purl.org/ontology/po/>.
11
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
12
- _:broadcast
13
- a po:Broadcast;
14
- po:schedule_date """2008-06-24T12:00:00Z""";
15
- po:broadcast_of _:version;
16
- po:broadcast_on <http://www.bbc.co.uk/programmes/service/6music>;
17
- .
18
- _:version
19
- a po:Version;
20
- .
21
- <http://www.bbc.co.uk/programmes/b0072l93>
22
- dc:title """Nemone""";
23
- a po:Brand;
24
- .
25
- <http://www.bbc.co.uk/programmes/b00c735d>
26
- a po:Episode;
27
- po:episode <http://www.bbc.co.uk/programmes/b0072l93>;
28
- po:version _:version;
29
- po:long_synopsis """Actor and comedian Rhys Darby chats to Nemone.""";
30
- dc:title """Nemone""";
31
- po:synopsis """Actor and comedian Rhys Darby chats to Nemone.""";
32
- .
33
- <http://www.bbc.co.uk/programmes/service/6music>
34
- a po:Service;
35
- dc:title """BBC 6 Music""";
36
- .
37
-
38
- #_:abcd a po:Episode.
39
- EOF
40
-
41
- RDF::N3::Reader.new(data, :base_uri => 'http://www.bbc.co.uk/programmes/on-now.n3') do |reader|
42
- reader.each_statement do |statement|
43
- statement.inspect!
44
- end
45
- end
@@ -1,34 +0,0 @@
1
- module RDF
2
- class Graph
3
- # Resource properties
4
- #
5
- # Properties arranged as a hash with the predicate Term as index to an array of resources or literals
6
- #
7
- # Example:
8
- # graph.load(':foo a :bar; rdfs:label "An example" .', "http://example.com/")
9
- # graph.resources(URI.new("http://example.com/subject")) =>
10
- # {
11
- # "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" => [<http://example.com/#bar>],
12
- # "http://example.com/#label" => ["An example"]
13
- # }
14
- def properties(subject, recalc = false)
15
- @properties ||= {}
16
- @properties.delete(subject.to_s) if recalc
17
- @properties[subject.to_s] ||= begin
18
- hash = Hash.new
19
- self.query(:subject => subject) do |statement|
20
- pred = statement.predicate.to_s
21
-
22
- hash[pred] ||= []
23
- hash[pred] << statement.object
24
- end
25
- hash
26
- end
27
- end
28
-
29
- # Get type(s) of subject, returns a list of symbols
30
- def type_of(subject)
31
- query(:subject => subject, :predicate => RDF.type).map {|st| st.object}
32
- end
33
- end
34
- end
@@ -1,134 +0,0 @@
1
- #
2
- # Baccus - Naur Form (BNF) vocabulary
3
- #
4
-
5
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
6
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
7
- @prefix bnf: <http://www.w3.org/2000/10/swap/grammar/bnf#>.
8
- @prefix : <http://www.w3.org/2000/10/swap/grammar/bnf-rules#>.
9
- @prefix rul: <http://www.w3.org/2000/10/swap/grammar/bnf-rules#>.
10
- @prefix n3: <http://www.w3.org/2000/10/swap/grammar/n3#>.
11
- @prefix list: <http://www.w3.org/2000/10/swap/list#>.
12
- @prefix doc: <http://www.w3.org/2000/10/swap/pim/doc#>.
13
- @prefix log: <http://www.w3.org/2000/10/swap/log#>.
14
- @prefix string: <http://www.w3.org/2000/10/swap/string#>.
15
- @keywords a, is, of.
16
-
17
-
18
- <> rdfs:comment
19
-
20
- """This set of rules process a BNF graph in its basic
21
- cfg:mustBeOneOf BNF form and create the branching tables to drive a
22
- predictive parser.
23
-
24
- See also cfg2bnf.n3 which expands the shothand ontology into the basic
25
- BNF terms.
26
- """.
27
-
28
- #_____________________________________
29
-
30
-
31
- # Enumerate options:
32
-
33
- { ?x bnf:mustBeOneSequence ?y} => { ?x optionTail ?y }.
34
-
35
- {?x optionTail [rdf:first ?y; rdf:rest ?z]} => {
36
- ?x bnf:branch [ bnf:sequence ?y];
37
- optionTail ?z.
38
- }.
39
-
40
- { ?x bnf:branch [bnf:sequence ?y] } => { ?y sequenceTail ?y }.
41
-
42
- sequenceTail a log:Chaff.
43
- optionTail a log:Chaff.
44
-
45
- { ?x sequenceTail [ rdf:rest ?z ] } => { ?x sequenceTail ?z }.
46
-
47
- # What productions can follow each other?
48
- # This is used for working out when to
49
-
50
- { ?x sequenceTail [ rdf:first ?y; rdf:rest [ rdf:first ?z ]] } =>
51
- { ?y bnf:canPrecede ?z }.
52
-
53
- { ?x bnf:branch [
54
- bnf:sequence [
55
- list:last ?y]].
56
- ?x bnf:canPrecede ?z} =>
57
- { ?y bnf:canPrecede ?z }.
58
-
59
- { ?x bnf:canPrecede ?y.
60
- ?y bnf:branch [ bnf:sequence () ].
61
- ?y bnf:canPrecede ?z.
62
- } => {
63
-
64
- ?x bnf:canPrecede ?z.
65
- }.
66
-
67
-
68
- bnf:eof bnf:canStartWith "@EOFDUMMY". # @@ kludge
69
-
70
- # Have to separate the next three rules or cwm seems to
71
- # get screwed up and assume there is no solution @@@
72
-
73
- { ?x bnf:branch [bnf:sequence [ rdf:first ?y ]].
74
- } => { ?x bnf:TEST ?y }.
75
-
76
- { ?x bnf:TEST ?y .
77
- ?y log:rawType log:Literal. } => { ?x bnf:canStartWithLiteral ?y }.
78
-
79
- { ?x bnf:canStartWithLiteral ?y .
80
- # (?y "(.).*") string:scrape ?c # Use whole string
81
- } => { ?y bnf:canStartWith ?y }.
82
-
83
- #______________________________________________________________
84
-
85
-
86
-
87
- # Rules for determining branching
88
-
89
- # A branch has a sequence, which is the given BNF production, and
90
- # one or more conditions, which are the strings on which to consider
91
- # that branch. N3 is a langauge in whch the look-ahead often is only
92
- # one character, and may allways be a constsnt string rather than a
93
- # regexp (check).
94
-
95
- # A branchTail is a sequnece which a branch could start with
96
- { ?x bnf:branch ?b.
97
- ?b bnf:sequence ?s.
98
- } => {
99
- ?b bnf:branchTail ?s.
100
- }.
101
-
102
- { ?b bnf:branchTail ?s.
103
- ?s rdf:first [ bnf:branch [ bnf:sequence () ]];
104
- rdf:rest ?t
105
- } => {
106
- ?b bnf:branchTail ?t.
107
- }.
108
-
109
-
110
- { ?x bnf:branch ?b.
111
- ?b bnf:branchTail ?s.
112
- ?s rdf:first [bnf:canStartWith ?y].
113
- } => {
114
- ?x bnf:canStartWith ?y.
115
- ?b bnf:condition ?y.
116
- }.
117
-
118
-
119
-
120
- { ?x bnf:branch ?b;
121
- bnf:canPrecede ?z.
122
- ?z log:rawType log:Literal.
123
- ?b bnf:sequence ().
124
- } => { ?b bnf:condition ?z}.
125
-
126
- { ?x bnf:branch ?b;
127
- bnf:canPrecede [bnf:canStartWith ?z].
128
- ?b bnf:sequence ().
129
- } => { ?b bnf:condition ?z}.
130
-
131
-
132
-
133
-
134
- #ends
Binary file
@@ -1,261 +0,0 @@
1
- # Notation3 in Notation3
2
- # Context Free Grammar without tokenization
3
- #
4
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
5
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
6
- @prefix cfg: <http://www.w3.org/2000/10/swap/grammar/bnf#>.
7
- @prefix rul: <http://www.w3.org/2000/10/swap/grammar/bnf-rules#>.
8
- @prefix : <http://www.w3.org/2000/10/swap/grammar/n3#>.
9
- @prefix n3: <http://www.w3.org/2000/10/swap/grammar/n3#>.
10
- @prefix list: <http://www.w3.org/2000/10/swap/list#>.
11
- @prefix string: <http://www.w3.org/2000/10/swap/string#>.
12
- @keywords a, is, of.
13
-
14
-
15
- # Issues:
16
- # - string token regexp not right FIXED
17
- # - tokenizing rules in general: whitespace are not defined in n3.n3
18
- # and it would be nice for the *entire* syntax description to be in RDF.
19
- # - encoding really needs specifying
20
- # - @keywords affects tokenizing
21
- # - comments (tokenizer deals with)
22
- # - We assume ASCII, in fact should use not notNameChars for i18n
23
-
24
- # tokenizing:
25
- # Absorb anything until end of regexp, then stil white space
26
- # period followed IMMEDIATELY by an opener or name char is taken as "!".
27
- # Except after a "." used instead of in those circumstances,
28
- # ws may be inserted between tokens.
29
- # WS MUST be inserted between tokens where ambiguity would arise.
30
- # (possible ending characters of one and beginning characters overlap)
31
- #
32
-
33
- #<> cfg:syntaxFor [ cfg:internetMediaType
34
- # <http://www.w3.org/2003/mediatypes/text/n3>].
35
-
36
-
37
- # __________________________________________________________________
38
- #
39
- # The N3 Full Grammar
40
-
41
-
42
- language a cfg:Language;
43
- cfg:document document;
44
- cfg:whiteSpace "@@@@@".
45
-
46
-
47
- document a rul:Used;
48
- cfg:mustBeOneSequence(
49
-
50
- (
51
- # [ cfg:zeroOrMore declaration ]
52
- # [ cfg:zeroOrMore universal ]
53
- # [ cfg:zeroOrMore existential ]
54
- statements_optional
55
- cfg:eof
56
- )
57
- ).
58
-
59
- statements_optional cfg:mustBeOneSequence (() ( statement "." statements_optional ) ).
60
-
61
- # Formula does NOT need period on last statement
62
-
63
- formulacontent cfg:mustBeOneSequence (
64
- ( statementlist )
65
- ).
66
-
67
-
68
- statementlist cfg:mustBeOneSequence (
69
- ( )
70
- ( statement statementtail )
71
- ).
72
-
73
- statementtail cfg:mustBeOneSequence (
74
- ( )
75
- ( "." statementlist )
76
- ).
77
-
78
-
79
- statement cfg:mustBeOneSequence (
80
- (declaration)
81
- (universal)
82
- (existential)
83
- (simpleStatement)
84
- ).
85
-
86
- universal cfg:mustBeOneSequence (
87
- (
88
- "@forAll"
89
- [ cfg:commaSeparatedListOf symbol ]
90
- )).
91
-
92
- existential cfg:mustBeOneSequence(
93
- ( "@forSome"
94
- [ cfg:commaSeparatedListOf symbol ]
95
- )).
96
-
97
-
98
- declaration cfg:mustBeOneSequence(
99
- ( "@base" explicituri )
100
- ( "@prefix" prefix explicituri )
101
- ( "@keywords" [ cfg:commaSeparatedListOf barename ] )
102
- ).
103
-
104
-
105
- simpleStatement cfg:mustBeOneSequence(( subject propertylist )).
106
-
107
- propertylist cfg:mustBeOneSequence (
108
- ( )
109
- ( predicate object objecttail propertylisttail )
110
- ).
111
-
112
- propertylisttail cfg:mustBeOneSequence (
113
- ( )
114
- ( ";" propertylist )
115
- ).
116
-
117
-
118
- objecttail cfg:mustBeOneSequence (
119
- ( )
120
- ( "," object objecttail )
121
- ).
122
-
123
-
124
- predicate cfg:mustBeOneSequence (
125
- ( expression )
126
- ( "@has" expression )
127
- ( "@is" expression "@of" )
128
- ( "@a" )
129
- ( "=" )
130
- ( "=>" )
131
- ( "<=" )
132
- ).
133
-
134
- subject cfg:mustBeOneSequence ((expression)).
135
-
136
- object cfg:mustBeOneSequence ((expression)).
137
-
138
- expression cfg:mustBeOneSequence(
139
- ( pathitem pathtail )
140
- ).
141
-
142
- pathtail cfg:mustBeOneSequence(
143
- ( )
144
- ( "!" expression )
145
- ( "^" expression )
146
- ).
147
-
148
-
149
- pathitem cfg:mustBeOneSequence (
150
- ( symbol )
151
- ( "{" formulacontent "}" )
152
- ( quickvariable )
153
- ( numericliteral )
154
- ( literal )
155
- ( "[" propertylist "]" )
156
- ( "(" pathlist ")" )
157
- ( boolean )
158
- # ( "@this" ) # Deprocated. Was allowed for this log:forAll x
159
- ).
160
-
161
-
162
- boolean cfg:mustBeOneSequence (
163
- ( "@true" )
164
- ( "@false" )
165
- ) .
166
-
167
- pathlist cfg:mustBeOneSequence (() (expression pathlist)).
168
-
169
- symbol cfg:mustBeOneSequence (
170
- (explicituri)
171
- (qname)
172
- ).
173
-
174
-
175
- numericliteral cfg:mustBeOneSequence (
176
- ( integer )
177
- ( rational )
178
- ( double )
179
- ( decimal )
180
- ) .
181
-
182
- rational cfg:mustBeOneSequence (( integer "/" unsignedint)).
183
-
184
-
185
- literal cfg:mustBeOneSequence(( string dtlang)).
186
-
187
- dtlang cfg:mustBeOneSequence( () ("@" langcode) ("^^" symbol)).
188
-
189
-
190
- #______________________________________________________________________
191
- #
192
- # TERMINALS
193
- #
194
- # "canStartWith" actually gives "a" for the whole class of alpha characters
195
- # and "0" for any of the digits 0-9. This is used to build the branching
196
- # tables.
197
- #
198
- integer cfg:matches """[-+]?[0-9]+""";
199
- cfg:canStartWith "0", "-", "+".
200
- unsignedint cfg:matches """[0-9]+""";
201
- cfg:canStartWith "0".
202
- double cfg:matches """[-+]?[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)""";
203
- cfg:canStartWith "0", "-", "+".
204
- decimal cfg:matches """[-+]?[0-9]+\\.[0-9]*""";
205
- cfg:canStartWith "0", "-", "+".
206
-
207
- #numericliteral cfg:matches """[-+]?[0-9]+(\\.[0-9]+)?(e[-+]?[0-9]+)?""";
208
- # cfg:canStartWith "0", "-", "+".
209
-
210
- explicituri cfg:matches "<[^>]*>";
211
- cfg:canStartWith "<".
212
-
213
- prefix cfg:matches "([A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*)?:";
214
- cfg:canStartWith "a", "_", ":". # @@ etc unicode
215
-
216
- qname cfg:matches "(([A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*)?:)?[A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*";
217
- cfg:canStartWith "a", "_", ":". # @@ etc unicode
218
-
219
- # ASCII version:
220
- #barename cfg:matches "[a-zA-Z_][a-zA-Z0-9_]*"; # subset of qname
221
- # cfg:canStartWith "a", "_". # @@ etc
222
-
223
- # This is the XML1.1
224
- barename cfg:matches "[A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*";
225
- cfg:canStartWith "a", "_". # @@ etc .
226
-
227
- # as far as I can tell, the regexp should be
228
- # barename cfg:matches "[A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*" .
229
- #
230
-
231
- quickvariable cfg:matches "\\?[A-Z_a-z\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u02ff\u0370-\u037d\u037f-\u1fff\u200c-\u200d\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff][\\-0-9A-Z_a-z\u00b7\u00c0-\u00d6\u00d8-\u00f6\u00f8-\u037d\u037f-\u1fff\u200c-\u200d\u203f-\u2040\u2070-\u218f\u2c00-\u2fef\u3001-\ud7ff\uf900-\ufdcf\ufdf0-\ufffd\U00010000-\U000effff]*"; # ? barename
232
- cfg:canStartWith "?". #
233
-
234
- # Maybe dtlang should just be part of string regexp?
235
- # Whitespace is not allowed
236
-
237
- # was: "[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)?";
238
- langcode cfg:matches "[a-z]+(-[a-z0-9]+)*"; # http://www.w3.org/TR/rdf-testcases/#language
239
- cfg:canStartWith "a".
240
-
241
-
242
- # raw regexp single quoted would be "([^"]|(\\"))*"
243
- # See:
244
- # $ PYTHONPATH=$SWAP python
245
- # >>> import tokenize
246
- # >>> import notation3
247
- # >>> print notation3.stringToN3(tokenize.Double3)
248
- # "[^\"\\\\]*(?:(?:\\\\.|\"(?!\"\"))[^\"\\\\]*)*\"\"\""
249
- # >>> print notation3.stringToN3(tokenize.Double)
250
- # "[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\""
251
- # After that we have to prefix with one or three opening \" which
252
- # the python regexp doesn't have.
253
- #
254
- # string3 cfg:matches "\"\"\"[^\"\\\\]*(?:(?:\\\\.|\"(?!\"\"))[^\"\\\\]*)*\"\"\"".
255
- # string1 cfg:matches "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"".
256
-
257
- string cfg:matches "(\"\"\"[^\"\\\\]*(?:(?:\\\\.|\"(?!\"\"))[^\"\\\\]*)*\"\"\")|(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")";
258
- cfg:canStartWith "\"".
259
-
260
-
261
- #ends